-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.c
77 lines (53 loc) · 1.59 KB
/
time.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
#include <math.h>
#include <utmp.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/sysinfo.h>
#include <string.h>
#include <time.h>
static int get_time(int pos, char * headline) {
struct sysinfo info;
struct tm *realtime;
time_t realseconds;
sysinfo(&info);
time(&realseconds);
realtime = localtime(&realseconds);
pos = sprintf(headline, " %02d:%02d:%02d up",
realtime->tm_hour, realtime->tm_min, realtime->tm_sec);
struct tm *runtime;
runtime = gmtime( &info.uptime );
pos += sprintf(headline + pos, " %02d:%02d, ",realtime->tm_hour, realtime->tm_min);
return(pos);
}
static void get_user(int pos, char* headline){
FILE * ptrtime;
char buffer[100];
struct utmp *utmpstruct;
int numuser = 0;
setutent();
while ((utmpstruct = getutent())) {
if ((utmpstruct->ut_type == USER_PROCESS) &&
(utmpstruct->ut_name[0] != '\0'))
numuser++;
}
endutent();
pos += sprintf(headline + pos, "%2d user%s, ", numuser, numuser == 1 ? "" : "s");
double av[3];
ptrtime = fopen("/proc/loadavg","r");
fgets(buffer,100,ptrtime);
sscanf(buffer,"%lf %lf %lf",&av[0], &av[1], &av[2]);
fclose(ptrtime);
pos += sprintf(headline + pos, " load average: %.2f, %.2f, %.2f",
av[0], av[1], av[2]);
}
char *sprint_uptime() {
int pos = 0;
char * headline1 = malloc(sizeof(char)*128);
pos = get_time(pos, headline1);
get_user(pos,headline1);
return headline1;
}