Repository navigation
Expand file tree
/
Copy pathTask 48 (2nd sol).c
More file actions
90 lines (71 loc) · 1.58 KB
/
Copy pathTask 48 (2nd sol).c
File metadata and controls
90 lines (71 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// 2nd solution
#include <stdlib.h>
#include <unistd.h>
#include <err.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
int execChild(char *args[]){
int pid = fork();
if(pid == -1){
err(1, "could not fork");
}
if(pid == 0){
if(execvp(args[0], args) == -1){
err(2, "could not exec");
}
}
int status;
wait(&status);
if(WIFEXITED(status))
return WEXITSTATUS(status);
return 1;
}
void flog(time_t start, time_t end, int status){
int fd = open("log.txt", O_RDWR | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP);
if(fd == -1){
err(4, "could not open file log.txt");
}
char logline[30];
ssize_t len = 0;
sprintf(logline, "%ld %ld %d\n", start, end, status);
len = strlen(logline);
if(write(fd, logline, len) != len){
err(5, "could not write in log.txt");
}
close(fd);
}
bool execute(int lim, char *args[]){
time_t start = time(NULL);
int status = execChild(args);
time_t end = time(NULL);
flog(start, end, status);
return (((end - start) < lim) && status !=0);
}
int main(int argc, char **argv){
if(argc < 3){
errx(3, "Invalid number of arguments. Usage: %s <number> <cmd> [args]", argv[0]);
}
char c = argv[1][0];
if(c > '9' || c < '0' || argv[1][1] != '\0')
errx(99, "Invalid digit argument");
int sec = (argv[1][0] - '0');
char *args[12];
int i = 2, cnt = 0;
for(; i < argc; ++i){
args[cnt++] = argv[i];
}
args[cnt] = NULL;
int condcnt = 0;
while(condcnt < 2) {
if(execute(sec, args) != 0)
condcnt++;
else
condcnt = 0;
}
exit(0);
}