System () functions in Windows
System () function in Windows (mainly used in C Language) function name: System
Function: Use INT system (char * command) to issue a DOS command. The system function has been included in the standard C library. You can directly call the program example: # include <stdlib. h> # include <stdio. h> int main (void) {printf ("about to spawn command.com and run a DOS command \ n"); System ("dir"); Return 0;}, for example: system ("pause") can be used to freeze the screen and observe the execution results of the program. System ("CLS") can be used to clear the screen. You can call the color function to change the foreground and background of the console. The specific parameters are described below. For example, the system ("color 0a") is used. The color 0 is the background color code, and the is the foreground color code. The color code is as follows: 0 = Black 1 = Blue 2 = green 3 = lake blue 4 = Red 5 = purple 6 = yellow 7 = white 8 = gray 9 = light blue A = light green B = light green c = light red D = lavender E = pale yellow F = bright white (note: microsoft Visual C ++ 6.0 supports system.) after taking the following example, I believe you will learn more about the Application of System in C programming. Example 1: Use the C language to call the doscommand for Timed Shutdown: # include <stdio. h> # include <string. h> # include <stdlib. h> int print () {printf "); printf ("╔ ═ ╧ \ n"); printf ("※ ※1. implement Timed Shutdown of the computer within 10 minutes \ n "); printf (" done ※2. disable the computer immediately. log out of the computer supervisor \ n "); printf (" supervisor ※0. log out of the system running \ n "); printf (" too many attempts have been made before too many attempts have been made \ n "); return 0;} void main () {system ("Title C language Shutdown program"); // set the CMD window title system ("Mode con Cols = 48 line S = 25 "); // window width and height system (" color 0b "); System (" Date/t "); System (" Time/t "); char cmd [20] = "shutdown-s-t"; char T [5] = "0"; print (); int C; scanf ("% d ", & C); getchar (); Switch (c) {Case 1: printf ("how many seconds do you Want to automatically shut down the computer? (0 ~ (600) \ n "); scanf (" % s ", T); System (strcat (CMD, t); break; Case 2: system ("shutdown-P"); break; Case 3: System ("shutdown-L"); break; Case 0: break; default: printf ("error! \ N ");} system (" pause "); exit (0);} Example 2: delete a file using C language. For example, the file is located at D: \ 123.txt and used system () function to execute Windows commands. # Include <stdlib. h> # include <stdio. h> int main (void) {system ("del D: \ 123.txt"); Return 0;} fork, execve, waitpid, and popen header file # include <stdlib. h> define the INT system (const char * string) function. The function indicates that system () calls fork () to generate sub-processes, the sub-process calls/bin/sh-C string to execute the command represented by the string parameter. After the command is executed, the original called process is returned. The sigchld signal is temporarily shelved during system () calls, while the SIGINT and sigquit signals are ignored. Returned value: If fork () fails, return-1: an error occurs. If exec () fails, shell cannot be executed. The returned value is equivalent to shell executing exit (127) if the execution is successful, the termination status of the sub-shell is returned. If system () fails to be called/bin/sh, 127 is returned. For other causes of failure,-1 is returned. If the string parameter is a null pointer, a non-zero value is returned. If system () is successfully called, the return value after the shell command is executed is returned. However, the returned value may also be 127 returned when system () fails to call/bin/sh, therefore, it is best to check errno again to confirm the execution is successful. Note: Do not use system () when writing programs with SUID/SGID permissions. System () inherits environment variables, which may cause system security problems. Example # I nclude <stdlib. h> main () {system ("LS-Al/etc/passwd/etc/shadow");} execution result: -RW-r -- 1 Root 705 Sep 3 13: 52/etc/passwd-r --------- 1 Root 572 Sep 2 15: 34/etc/Shado Example 2: char TMP []; sprintf (TMP, "/bin/Mount-T vfat % S/mnt/USB", Dev); System (TMP ); where Dev is/dev/sda1. difference between system and exec 1. Both system () and exec () can execute commands outside the process, system opens up a new process on the original process, but exec overwrites the original process 2, system (), and exec () with the new process (command, the system return value does not affect the original process, but the exec return value affects the original process.