Created
February 6, 2022 06:25
-
-
Save Park-Developer/3c2949579474525b7b695d86bcfb6357 to your computer and use it in GitHub Desktop.
Linux : system()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <errno.h> | |
#include <unistd.h> | |
#include <sys/wait.h> | |
int system(const char *cmd){ | |
pid_t pid; | |
int status; | |
if((pid=fork())<0){ | |
status=-1; | |
}else if(pid==0){ | |
execl("/bin/sh","sh","-c",cmd,(char*)0); | |
_exit(127); | |
}else{ | |
while(waitpid(pid,&status,0)<0) | |
if(errno != EINTR){ | |
status=-1; | |
break; | |
} | |
} | |
return status; | |
} | |
int main(int argc, char **argv, char **envp){ | |
while(*envp) | |
printf("%s\n",*envp++); | |
system("who"); | |
system("nocommand"); | |
system("cal"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment