-
Notifications
You must be signed in to change notification settings - Fork 2
/
print_minute.c
60 lines (53 loc) · 1.43 KB
/
print_minute.c
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
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
static const int minute = 18;
static const unsigned tenSeconds = 10;
static const char * sourceFile = "print_minute.c";
static const char * tempSourceFile = "temp";
static const char * command = "make -s build";
void read_file_line_by_line(int newMinute) {
FILE * fp;
FILE * writeFp;
char * line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen(sourceFile, "r");
writeFp = fopen(tempSourceFile, "w");
if (fp == NULL)
exit(2);
int index = 0;
while ((read = getline(&line, &len, fp)) != -1) {
if (index == 7) {
char buf[5];
sprintf(buf, "%d", newMinute);
fputs("static const int minute = ", writeFp);
fputs(buf, writeFp);
fputs(";\n", writeFp);
} else {
fputs(line, writeFp);
}
index++;
}
if (line)
free(line);
fclose(fp);
fclose(writeFp);
rename(tempSourceFile, sourceFile);
}
int main(int argc, char * argv[]) {
time_t foo = time(NULL);
struct tm *tm_struct = localtime(&foo);
int newMinute = tm_struct->tm_min;
if (minute != newMinute) {
read_file_line_by_line(newMinute);
system(command);
} else {
printf("%d\nSleeping for 10 seconds.\n", minute);
sleep(tenSeconds);
}
return execv(argv[0], argv);
}