Created
February 1, 2017 05:13
-
-
Save SergiyOsadchyy/9ae2634f93943d5da58f959fa883ae10 to your computer and use it in GitHub Desktop.
Linux_Lab_3
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
// Linux_Lab_3 | |
// | |
// Created by Sergiy on 25.01.17. | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/wait.h> | |
#include <string.h> | |
int main(int argc, const char * argv[]) | |
{ | |
const char *textFile; | |
pid_t pid = fork(); | |
int ret; | |
const char *emailBegin; | |
const char *emailAddress; | |
const char *emailEnd; | |
char *emailCommand; | |
size_t length; | |
if (argc > 1) | |
{ | |
textFile = argv[1]; | |
} | |
else | |
{ | |
textFile = "/Users/Sergiy/Desktop/congrats.c"; | |
} | |
if (pid == 0) | |
{ | |
printf("This is a child process\n"); | |
printf("Calling execl....\n"); | |
ret = execl("/usr/bin/head", "head", textFile, NULL); | |
printf("Failed execl... ret = %d\n", ret); | |
} | |
else | |
{ | |
sleep(3); | |
printf("\nThis is parent process\n\n"); | |
popen("/usr/bin/who", "w"); | |
sleep(1); | |
printf("\n--------\n\n"); | |
popen("/usr/bin/mail", "w"); | |
sleep(1); | |
printf("\n--------\n\n"); | |
emailBegin = "/usr/bin/mail -s \"Congratulations\" "; | |
emailEnd = " < /Users/Sergiy/Desktop/congrats.c"; | |
for (int i = 2; i < argc; i++) | |
{ | |
emailAddress = argv[i]; | |
length = strlen(emailBegin) + strlen(emailAddress) + strlen(emailEnd); | |
emailCommand = malloc(length); | |
strcat(emailCommand, emailBegin); | |
strcat(emailCommand, emailAddress); | |
strcat(emailCommand, emailEnd); | |
popen(emailCommand, "w"); | |
} | |
waitpid(pid, 0, 0); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment