-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.c
216 lines (172 loc) · 4.08 KB
/
files.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "files.h"
char* get_uptime(char* char_buf)
{
FILE *fp;
char *uptime = char_buf;
char ch;
int int_uptime;
int buf;
fp = fopen("/proc/uptime", "r");
if (fp == NULL)
{
fprintf(stderr, "ERROR: Couldn't get uptime\n");
strcpy(uptime, "Unknown");
}
else
{
strcpy(uptime, "");
while ((ch = fgetc(fp)) != ' ')
sprintf(uptime, "%s%c", uptime, ch);
// convert to int
int_uptime = atoi(uptime);
buf = int_uptime / 3600 / 24;
if (buf == 1)
sprintf(uptime, "%d Day, ", buf);
else if (buf > 1)
sprintf(uptime, "%d Days, ", buf);
else
strcpy(uptime, "");
buf = int_uptime / 3600 % 24;
if (buf == 1)
sprintf(uptime, "%s%d Hour, ", uptime, buf);
else
sprintf(uptime, "%s%d Hours, ", uptime, buf);
buf = int_uptime / 60 % 60;
if (buf == 1)
sprintf(uptime, "%s%d Min", uptime, buf);
else
sprintf(uptime, "%s%d Mins", uptime, buf);
fclose(fp);
}
return char_buf;
}
char* get_mem(char* buf)
{
FILE *fp;
char *mem = buf;
char mem_available[20] = "";
char mem_total[20] = "";
int int_mem_available;
int int_mem_total;
char ch;
fp = fopen("/proc/meminfo", "r");
if (fp == NULL)
{
fprintf(stderr, "ERROR: Couldn't get memory\n");
strcpy(mem, "Unknown");
}
else
{
strcpy(mem, "");
while (fgetc(fp) != ':') ;
while ((ch = fgetc(fp)) != 'k')
sprintf(mem_total, "%s%c", mem_total, ch);
int_mem_total = atoi(mem_total);
while (fgetc(fp) != ':') ;
while (fgetc(fp) != ':') ;
while ((ch = fgetc(fp)) != 'k')
sprintf(mem_available, "%s%c", mem_available, ch);
int_mem_available = atoi(mem_available);
int_mem_available = int_mem_total - int_mem_available;
sprintf(mem, "%d / %d MiB", int_mem_available/1024, int_mem_total/1024);
fclose(fp);
}
return buf;
}
void get_cpu(char* buf)
{
FILE *fp;
char *cpu = buf;
char ch;
fp = fopen("/proc/cpuinfo", "r");
if (fp == NULL)
{
fprintf(stderr, "ERROR: Couldn't get cpu\n");
strcpy(cpu, "Unknown");
}
else
{
strcpy(cpu, "");
for (int i = 0; i < 5; i++)
while (fgetc(fp) != ':') ;
while ((ch = fgetc(fp)) != '\n')
sprintf(cpu, "%s%c", cpu, ch);
fclose(fp);
}
}
void get_device(char* buf)
{
FILE *fp;
char *device = buf;
char ch;
fp = fopen("/sys/devices/virtual/dmi/id/product_version", "r");
if (fp == NULL)
{
fprintf(stderr, "ERROR: Couldn't get device info\n");
strcpy(device, "Unknown");
}
else
{
strcpy(device, "");
while ((ch = fgetc(fp)) != '\n')
sprintf(device, "%s%c", device, ch);
fclose(fp);
}
}
void get_kernel(char* buf)
{
FILE *fp;
char *kernel = buf;
char ch;
fp = fopen("/proc/version", "r");
if (fp == NULL)
{
fprintf(stderr, "ERROR: Couldn't get kernel\n");
strcpy(kernel, "Unknown");
}
else
{
strcpy(kernel, "");
while (fgetc(fp) != ' ') ;
while (fgetc(fp) != ' ') ;
while ((ch = fgetc(fp)) != ' ')
sprintf(kernel, "%s%c", kernel, ch);
fclose(fp);
}
}
void get_distro(char* buf)
{
FILE *fp;
char *distro = buf;
char ch;
fp = fopen("/etc/os-release", "r");
if (fp == NULL)
{
fprintf(stderr, "Couldn't get distro\n");
strcpy(distro, "Unknown");
}
else
{
strcpy(distro, "");
while (fgetc(fp) != '"') ;
while ((ch = fgetc(fp)) != '"')
sprintf(distro, "%s%c", distro, ch);
fclose(fp);
}
}
int get_image_path(char* path)
{
while (1)
{
if (!access(path, R_OK))
return 0;
else if (path[0] != 0x0)
{
fprintf(stderr, "Couldn't find image: '%s'\n", path);
exit(2);
}
char home[10];
strcpy(home, getenv("HOME"));
sprintf(path, "%s/.logo.png", home);
}
}