7a4a677fc270a8ee4aebfcf2a28e65ef8353a29c
[sysstatus.git] / statuses / tools.c
1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 #include "tools.h"
5
6 char powerToChar(int power)
7 {
8         switch(power)
9         {
10                 case 0:
11                         return 'b';
12                 case 1:
13                         return 'k';
14                 case 2:
15                         return 'M';
16                 case 3:
17                         return 'G';
18                 case 4:
19                         return 'T';
20                 case 5:
21                         return 'P';
22                 case 6:
23                         return 'E';
24         }
25
26         return '?';
27 }
28
29
30 void statusError(char *where, char *what, char *extra)
31 {
32         fprintf(stderr, "%s: %s", where, what);
33         if (extra)
34                 fprintf(stderr, " -- %s", extra);
35         fputs("\n", stderr);
36 }
37
38
39 ssize_t fileRead(char *buf, size_t bufsize, char *file)
40 {
41         int fd;
42         int readbytes;
43
44         fd = open(file, 0);
45         if (fd < 0)
46                 return -1;
47
48         readbytes = read(fd, buf, bufsize - 1);
49         close(fd);
50
51         if (readbytes > 0)
52                 buf[readbytes] = '\0';
53         else
54                 buf[0] = '\0';
55
56         return readbytes;
57 }