linux windows c system functions Introduction

windows

System function command in the windows may not be case-sensitive! 
Function: issuing a DOS command 

#include <stdlib.h>

int system(char *command);

  Implementation of successful return 0, the unsuccessful due to different values ​​for different operating return, you can check the manual to see

#include<stdio.h>
#include<stdlib.h>
int main()
{
    printf("About to spawn and run a DOS command\n");
    system("dir");
    return 0;
}

Automatic shutdown Code:

#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;
} 

Timing shutdown:

#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;
}

Delete Files:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    system("del d:\123.txt");
    return 0;
}

Linux

Source 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.

  If the parent process is to capture SIGCHLD signal, when executing system functions, should be blocked for the parent process to deliver SIGCHLD signal. Otherwise, when the end of the child process system is created, the system of the caller may mistakenly believe that one of its own the child process is over. so, the caller will call one of the wait function to terminate the state of the subprocess, thus preventing the system function to get the termination status of the child process, and as a return value.

  system () function call / bin / sh to execute the specified command parameter, / bin / sh is generally a soft link, pointing to a concrete shell, such as bash, -c option is to tell the shell to read commands from the command string ;

  1. During execution of the command, SIGCHLD is blocked, SIGCHLD not receive the signal sent by the kernel
  2. During the command execution, SIGINT and SIGQUIT are ignored, meaning that no action after the process receives two signals.

return value

  The value returned is -1 on error (e.g. fork(2) failed), and the return status of the command otherwise. This latter return status is in the format specified in wait(2). Thus, the exit code of the command will be WEXITSTATUS(status). In case /bin/sh could not be executed, the exit status will be that of a command that does exit(127).If the value of command is NULL, system() returns nonzero if the shell is available, and zero if not.

  system implementation process

  1. fork a child process
  2. Call exec function in the child process to execute the command
  3. Call in the parent process wait to wait for the end of child process
  4. For fork failure, system () function returns -1

note:

  1. If exec is executed successfully, that command completes successfully, exit or return value returned by the command is returned. (Note, the successful implementation of command does not mean that the successful implementation of such command: "rm debuglog.txt", regardless of the file does not exist save the command runs successfully)
  2. If the exec fails, i.e. no successful command execution, such as the signal is interrupted, or the command command does not exist, System () function returns 127.
  3. If the command is NULL, the system () function returns a non-zero value, typically 1.
  4. command command returns 0, system returns 0
  5. 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
  6. Do not use the system when programming has SUID / SGID permissions (), system () will inherit the environment variables, environment variables may result in system security. system function has been included in the standard c library can be called directly
system("mkdir $HOME/.SmartPlatform/"); 
system("mkdir $HOME/.SmartPlatform/Files/"); 
system("cp mainnew.cpp $HOME/.SmartPlatform/Files/"); 

 

 

Published 57 original articles · won praise 538 · Views 4.86 million +

Guess you like

Origin blog.csdn.net/whatday/article/details/104052755