-
-
Save ignatenkobrain/2abb9b6cb38c46e3031a to your computer and use it in GitHub Desktop.
This file contains 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
/* Copyright (C) 2014 Igor Gnatenko <i.gnatenko.brain@gmail.com> | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
/* Напишите на C программу myexec, которая в качестве аргумента будет принимать | |
* команду и запускать её. В случае завершения запущенной команды myexec должна | |
* распечатать exit_code и перезапустить пользовательскую команду. Пример: | |
* | |
* $ myexec sleep 42 | |
* sleep exited with code 0 | |
* | |
* Reference: http://company.yandex.ru/job/vacancies/sysadm_platform.xml | |
* gcc -Wall -Wextra ya.c -o ya | |
*/ | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/wait.h> | |
int | |
main (int argc, char *argv[]) | |
{ | |
pid_t pid; | |
int status; | |
if (argc < 2) { | |
printf ("Fuck you!\n"); | |
return 1; | |
} | |
for (;;) { | |
pid = fork (); | |
if (pid == -1) { | |
perror ("fork"); | |
exit (1); | |
} | |
if (pid == 0) { | |
execvp (argv[1], &argv[1]); | |
} else { | |
wait (&status); | |
if (WIFEXITED (status)) | |
{ | |
printf ("%s exited with return code %d\n", | |
argv[1], WEXITSTATUS (status)); | |
if (WEXITSTATUS (status) != 0) | |
} | |
else if (WIFSIGNALED(status)) | |
{ | |
printf ("%s terminated by signal number %d\n", | |
argv[1], WTERMSIG (status)); | |
} | |
/* we want to give time to user for read output of program */ | |
sleep (1); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment