-
Notifications
You must be signed in to change notification settings - Fork 0
/
clk.c
139 lines (128 loc) · 2.28 KB
/
clk.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//libraries
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <signal.h>
#include <dirent.h>
#include <sys/dir.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <locale.h>
#include<sys/utsname.h>
#include "declarations.h"
void clk()
{
int n=0,t=0;
char clkt[100],clkn[100];
if(!strncmp(cmd,"clock ",6))
{
char*token=strtok(cmd," , ");
while(token!=NULL)
{
if(!strncmp(token,"-t",2))
{
t=1;
strcpy(clkt,&token[2]);
}
else if(!strncmp(token,"-n",2))
{
n=1;
strcpy(clkn,&token[2]);
}
token=strtok(NULL," , ");
}
if(!n && !t)
printf("invalid arguments given\n");
else if(t && !n)
printf("duration not specified\n");
else if(n && !t)
printf("interval not specified\n");
else
{
//check if int
int i;
for(i=0;i<strlen(clkt);i++)
if(clkt[i]>47 && clkt[i]<58)
t=1;
else
{
t=0;
break;
}
for(i=0;i<strlen(clkn);i++)
if(clkn[i]>47 && clkn[i]<58)
n=1;
else
{
n=0;
break;
}
if(n && t)
{
//ascii to int function
n=atoi(clkn);
t=atoi(clkt);
if(t==0)
{
printf("ERROR: t cannot be 0\n");
}
else
{
int loopcount=(n/t)+1;
while(loopcount!=0)
{
//open files
FILE*f=fopen("/sys/class/rtc/rtc0/date","r");
FILE*g=fopen("/sys/class/rtc/rtc0/time","r");
//check for errors
if(f==NULL || g==NULL)
{
printf("cannot open file\n");
}
else
{
char line[200];
//get date
if(fgets(line,sizeof line,f)!=NULL)
{
char j[100];
strcpy(j,line);
j[strlen(j)-1]='\0';//remove \n
}
//get time
if(fgets(line,sizeof line,g)!=NULL)
{
char j[100];
sprintf(j,"%s %s",j,line);
printf("%s",j);
}
else
perror("ERROR");
//closing files
fclose(f);
fclose(g);
//sleep
if(n<t)
sleep(n);
else
{
sleep(t);
n=n-t;
}
loopcount--;
}
}
}
}
}
}
else
printf("%s: command not found\n",cmd);
}