-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.c
61 lines (48 loc) · 1.34 KB
/
clock.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
61
void exec_clock(char ** cmd_input,int * cmd_count)
{
if(* cmd_count<5)
{
perror("least arguments passed");
}
else
{
pid_t pid;
int background=0,i;// initally no back ground
int lapse= atoi(cmd_input[2]);
int duration=atoi(cmd_input[4]);
int status;
struct timeval start, end;
gettimeofday(&start, NULL);
double delta;
time_t now = time( NULL);
struct tm now_tm = *localtime( &now);
struct tm *t;
char text[100]; // for time display
if(strcmp(cmd_input[*cmd_count-1], "&") == 0)
{ /* if & is included process running in background*/
cmd_input[*cmd_count-1] = NULL;
background = 0; // since this dosnt care aout back ground
}
pid=fork();
if(pid == 0 && background != 1) // execute normally
{
for( i=0;delta<duration;i=lapse)
{
now_tm.tm_sec+=i;
mktime( &now_tm);
t=&now_tm;
strftime(text, sizeof(text)-1, "%d %m %Y %H:%M:%S",t);
printf("%s\n",text );
gettimeofday(&end, NULL);
delta = ((end.tv_sec-start.tv_sec) * 1000000u +end.tv_usec - start.tv_usec) / 1.e6;
sleep(2);
}
exit(1);
}
else if (!background)
{
waitpid(pid, &status, WUNTRACED);
exit(1);
}
}
}