windows
在windows下的system函數中命令可以不區別大小寫!
功 能: 發出一個DOS命令
#include <stdlib.h> int system(char *command);
執行成功返回0,執行不成功由於不同的操作返回的值不同,可以查手冊看
#include<stdio.h> #include<stdlib.h> int main() { printf("About to spawn and run a DOS command\n"); system("dir"); return 0; }
調用color函數可以改變控制台的前景色和背景,具體參數在下面說明。
用 system(“color 0A”); 其中color后面的0是背景色代號,A是前景色代號。各顏色代碼如下:
0=黑色 1=藍色 2=綠色 3=湖藍色 4=紅色 5=紫色 6=黃色 7=白色 8=灰色 9=淡藍色 A=淡綠色 B=淡淺綠色 C=淡紅色 D=淡紫色 E=淡黃色 F=亮白色
自動關機代碼:
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char order[10]; system("color 0C");//設置默認控制台前景個背景色 system("date /T");//該函數可以返回當前系統日期 system("TIME /T");//該函數可以返回當前系統時間 flag: printf("輸入\"我是豬\",否則電腦兩分鍾關機!!!\n"); system("shutdown -s -t 120"); scanf("%s",order); if(strcmp(order,"我是豬")==0) { printf("恭喜你成功的定位自己的身份!!!關機動作取消\n"); system("shutdown -a"); system("pause"); } else goto flag; return 0; }
定時關機:
#include<stdio.h> #include<stdlib.h> //可以輸入system用以鍵入DOS管理窗口界面下的cmd中的命令 #include<string.h> void print() { printf("****************關機程序****************\n"); printf("****1.實現在十分鍾內的定時關閉計算機****\n"); printf("****2.立即關閉計算機********************\n"); printf("****3.注銷計算機************************\n"); printf("****4.取消自動關機**********************\n"); printf("****5.退出系統**************************\n"); } int main() { system("title C語言關機程序");//設置cmd窗口寬度 system("color 2C");//設置默認控制台前景個背景色 system("date /T"); system("TIME /T"); char cmd[20] = "shutdown -s -t "; char t[5]; print(); flag: printf("請輸入您的選擇1-5:"); int c; scanf("%d", &c); if(c>5||c==0) { printf("您輸入的不合法,請重新輸入.\n"); fflush(stdin); goto flag; } getchar(); switch(c) { case 1: printf("您想在多少秒后自動關閉計算機?(0~600)\n"); scanf("%s", t); system(strcat(cmd, t)); break; case 2: system("shutdown -p"); break; case 3: system("shutdown -l"); break; case 4: system("shutdown -a"); case 5: return 0; default: printf("Error!\n"); } system("pause"); return 0; }
刪除文件:
#include<stdio.h> #include<stdlib.h> int main() { system("del d:\123.txt"); return 0; }
Linux
system源碼
#include <sys/wait.h> #include <erron.h> #include <signal.h> #include <unistd.h> int system(const char* cmdstring) { pid_t pid; int status; struct sigaction ignore,saveintr,savequit; sigset_t chldmask,savemask; if(cmdstring==NULL) return 1; ignore.sa_handler=SIG_IGN; if(sigaction(SIGINT,&ignore,&saveintr)<0) return -1; if(sigaction(SIGQUIT,&ignore,&savequit)<0) return -1; sigemptyset(&chldmask); sigaddset(&chldmask,SIGCHLD); if(sigpromask(SIG_BOLCK,&chllmask,&savemask) return -1; if((pid=fork())<0) status=-1; else if(pid==0) { sigaction(SIGINT,&saveintr,NULL); sigaction(SIGQUIT,&savequit,NULL); sigpromask(SIG_SETMASK,&savemask,NULL); execl("/bin/sh","sh","-c",cmdstring,(char*)0); _exit(127); } else { while(waitpid(pid,&status,0)<0) { if(errno!=EINTR) { status=-1; break; } } if(sigaction(SIGINT,&saveintr,NULL)<0) return -1; if(sigaction(SIGQUIT,&savequit,NULL)<0) return -1; if(sigpromask(SIG_SETMASK,&savemask,NULL)<0) return -1; } return status; }
man:system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.
如果父進程正在捕捉SIGCHLD信號,那么正在執行system函數的時候,應當阻塞對父進程遞送SIGCHLD信號.否則,當system創建的子進程結束的時候,system的調用者可能錯誤的認為,它自己的一個子進程結束了.於是,調用者將會調用一種wait函數以獲得子進程的終止狀態,這樣就阻止了system函數獲得子進程的終止狀態,並將其作為返回值。
- 在該command執行期間,SIGCHLD是被阻塞的,收不到內核發送的SIGCHLD信號
- 在該command執行期間,SIGINT和SIGQUIT是被忽略的,意思是進程收到這兩個信號后沒有任何動作。
返回值
- fork一個子進程
- 在子進程中調用exec函數去執行command
- 在父進程中調用wait去等待子進程結束
- 對於fork失敗,system()函數返回-1
注意:
- 如果exec執行成功,也即command順利執行完畢,則返回command通過exit或return返回的值。(注意,command順利執行不代表執行成功,比如command:"rm debuglog.txt",不管文件存不存在該command都順利執行了)
- 如果exec執行失敗,也即command沒有順利執行,比如被信號中斷,或者command命令根本不存在,system()函數返回127.
- 如果command為NULL,則system()函數返回非0值,一般為1.
- command命令返回0時,system返回0
- 如果system()調用成功則最后會返回執行shell命令后的返回值,但是此返回值也有可能為system()調用/bin/sh失敗所返回的127,因此最好能再檢查errno來確認執行成功
- 在編寫具有SUID/SGID權限的程序時請勿使用system(),system()會繼承環境變量,通過環境變量可能會造成系統安全的問題。system函數已經被收錄在標准c庫中,可以直接調用
system("mkdir $HOME/.SmartPlatform/"); system("mkdir $HOME/.SmartPlatform/Files/"); system("cp mainnew.cpp $HOME/.SmartPlatform/Files/");