C / C ++ in the system () function Detailed

Reprinted from: http: //hi.baidu.com/519378007/item/5c49bb05e23728dbdce5b0e7

Detailed functions under the windows system () 

windows operating system system () function Detailed (mainly used in the C language) 

   Function name: system

  Function: issuing a DOS command

  Usage: int system (char * command);

system function has been included in the standard c library can be called directly

Example of program:


  
  
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. int main(void)
  4. {
  5.    printf( "About to spawn command.com and run a DOS command\n");
  6.   system( "dir");
  7.    return 0;
  8. }

Another example: system ( "pause") can achieve the freeze screen to facilitate an execution result of the observation program; system ( "CLS") clear the screen can be realized operation. The color function call can change the foreground and background console, the parameters described below .

For example, system ( "color 0A"); wherein the latter is the background color code color 0, A is the foreground color code. Each color code is as follows:

= 0 Black 1 Blue 2 = green = blue lake 4 = 3 = 5 = Red Yellow Purple = 6 = 7 = 8 Gray 9 = White light blue light green A = B = C = light pink, light green D = E = F = a pale yellow purple bright white

system ( "mkdir F: \ hello \ world") may be in F: create a folder footwall hello, hello, create a file in the following folder world.

 

(Note: in the Microsoft Visual C ++  6.0 support system) , for example

Read the following example, I believe you will learn more system applications in C program design.

Example One:   C language calling DOS commands to achieve regular shutdown :


  
  
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. int print()
  5. {
  6.    printf( " ╪╪╪╪╪╪╧╧╧╧╧╧╧╧╪╪╪╪╪╪\n");
  7.    printf ( "╔═══╧╧ C language shutdown procedures ╧╧═══╗ \ the n-" );
  8.    the printf ( . "║ ※ realization timing. 1 in 10 minutes and turn off the computer ║ \ n-" );
  9.    printf ( . "║ ║ immediately shut down the computer ※ 2 \ the n-" );
  10.    printf ( . "※ 3 log off the computer ║ ║ \ the n-" );
  11.    printf ( "0 ║ ※ exit the system ║ \ the n-." );
  12.    printf( "╚═══════════════════╝\n");
  13.    return 0;
  14. }
  15. void main ()
  16. {
  17.   system( "title C language program shutdown" ); // set up the cmd window title
  18.   system( "Lines MODE CON cols = 48 = 25" ); // Window Width Height
  19.   system( "color 0B");
  20.   system( "date /T");
  21.   system( "TIME /T");
  22.    char cmd[ 20]= "shutdown -s -t ";
  23.    char t[ 5]= "0";
  24.   print();
  25.    int c;
  26.    scanf( "%d",&c);
  27.   getchar();
  28.    switch(c)
  29.   {
  30.        Case 1 : printf ( "You want to automatically shut down the computer after the number of seconds (0 ~ 600) \ the n-?" ); scanf ( "% S" , t); System ( strcat (cmd, t)); BREAK ;
  31.        case 2:system( "shutdown -p"); break;
  32.        case 3:system( "shutdown -l"); break;
  33.        case 0: break;
  34.        default: printf( "Error!\n");
  35.   }
  36.   system( "pause");
  37.    exit( 0);
  38. }

 

Example Two:

C language deleting files, such as location of the file is d: \ 123.txt

A system () function to execute the command windows.


  
  
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. int main(void)
  4. {
  5.   system( "del d:\123.txt");
  6.    return 0;
  7. }

 

 

system function can call some DOS commands, such as
system ( "cls"); // clear the screen, use the cls on equal DOS command
below lists the common DOS commands, you can use the system function call:


ASSOC Displays or modifies file extension name association.
AT commands and programs that run on your computer.
ATTRIB Displays or changes file attributes.
BREAK Sets or clears extended CTRL + C checking.
CACLS Displays or modifies access control lists (ACLs).
CALL Calls one batch program from another.
CD displays the name of the current directory or change it.
CHCP Displays or sets the active code page.
CHDIR Displays the name of the current directory or change it.
CHKDSK Checks a disk and displays a status report.
CHKNTFS display or modify the start time of the disk check.
CLS clears the screen.
CMD Windows command interpreter to open another window.
COLOR Sets the default console foreground and background colors.
COMP to compare two files or sets of content.
COMPACT Displays or alters the compression of files on NTFS partitions.
CONVERT Converts FAT volumes to NTFS. You can not convert the current drive.
COPY Copies one file to another location.
DATE Displays or sets the date.
DEL delete at least one file.
DIR to display a directory of files and subdirectories.
DISKCOMP compare the contents of two floppy disks.
DISKCOPY contents of a floppy disk will be copied to another floppy disk.
DOSKEY Edits command lines, recalls Windows commands, and creates macros.
ECHO message display or command echoing on or off.
ENDLOCAL Ends localization of environment changes in a batch file.
ERASE delete at least one file.
EXIT Quits the CMD.EXE program (command interpreter).
FC Compares two files or sets, and displays the differences.
FIND search for text strings in files.
FINDSTR search string in the file.
FOR run for each file in a set of documents specified command
FORMAT Formats a disk for use with Windows.
FTYPE display or modify file extension associations for the file type.
GOTO Directs the Windows command interpreter to a batch program to a labeled line.
GRAFTABL enable Windows to display an extended character set in graphics mode.
HELP Windows Help provides information command.
IF Performs conditional processing in batch programs.
LABEL create, change, or delete the volume label of a disk.
MD Creates a directory.
MKDIR Creates a directory.
MODE device configuration system.
MORE Displays output one screen.
MOVE files from one directory to another directory.
PATH Displays or sets a search path for executable files.
PAUSE suspend the processing of a batch file and displays a message.
A reduction of the value POPD PUSHD save the current directory.
PRINT print text files.
PROMPT Change the Windows command prompt.
PUSHD save the current directory, then change it.
RD delete the directory.
RECOVER Recovers readable information from a defective disk.
REM recorded a batch file or CONFIG.SYS in the comments.
REN to rename the file.
RENAME rename the file.
REPLACE Replaces files.
RMDIR delete the directory.
SET Displays, sets, or removes Windows environment variables.
SETLOCAL start the batch file localization of environment changes.
SHIFT batch file to replace the position of replaceable parameters.
SORT Sorts input.
START Starts a separate window to run a specified program or command.
SUBST path with a drive letter associated.
TIME display or set the system time.
TITLE Set the window title for a CMD.EXE session.
TREE displays the directory structure of a drive or path in graphics mode.
TYPE displays the contents of a text file.
VER Displays the Windows version.
VERIFY tell Windows whether to verify that your files are written correctly to a disk.
VOL Displays a disk volume label and serial number.
XCOPY Copies files and directory trees.

Command upper or lower case.

 

Under Linux system () function Detailed Description

(Execute shell commands)

related functions

  fork,execve,waitpid,popen

Header Files

  #include<stdlib.h>

Defined Functions

  int system(const char * string);

Function Description

    system () calls the fork () generates a child process, the child process invokes / bin / sh-c string string string to execute the command represented by the parameter, the command is executed immediately after the return process of the original call. SIGCHLD signal will be put on hold during the call system (), SIGINT and SIGQUIT signal will be ignored.

return value

  If fork () fails to return -1: An error occurred

  If exec () fails, Shell could not be executed, the return value is equivalent to the execution Shell exit (127)

  If successful termination status of the child is returned Shell

  If the system () 127 returns a call / bin / sh fail when other failure reason -1. If the parameter string is a null pointer (NULL), a nonzero value> is returned. If the system () call is successful then the return value will eventually be returned after the execution of shell commands, but this return value is also possible for the system () call / bin / sh returned 127 failed, so it is best to check again to confirm the successful execution errno .

Annotated

  Do not use the system when programming has SUID / SGID permissions (), system () will inherit the environment variables, environment variables may result in system security. example

 


  
  
  1.   #include<stdlib.h>
  2.   void main ()
  3.  {
  4.   system(“ls -al /etc/passwd /etc/shadow”);
  5.  }

 

Results of the:

 


  
  
  1. -rw-r--r-- 1 root root 705 Sep 3 13 : 52 /etc/passwd
  2. -r--------- 1 root root 572 Sep 2 15 : 34 /etc/shado

 

Example 2:


  
  
  1. char tmp[];
  2. sprintf(tmp, "/bin/mount -t vfat %s /mnt/usb",dev);
  3. system(tmp);

Wherein dev is / dev / sda1. Distinction System and the exec

1, system () and exec () can execute commands outside of the process, system is opened up a new process in the original process, but the exec is covered by the original process with a new process (command)  

2, system () and exec () are capable of generating a return value, system return value does not affect the original process, but exec affect the return value of the original process

Reprinted from: http: //hi.baidu.com/519378007/item/5c49bb05e23728dbdce5b0e7

Guess you like

Origin blog.csdn.net/wu694128/article/details/89710135