Reindent and restyle
[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   }
36   fputs("\n", stderr);
37 }
38
39
40 ssize_t fileRead(char *buf, size_t bufsize, char *file)
41 {
42   int fd;
43   int readbytes;
44
45   fd = open(file, 0);
46   if (fd < 0) {
47     return -1;
48   }
49
50   readbytes = read(fd, buf, bufsize - 1);
51   close(fd);
52
53   if (readbytes > 0) {
54     buf[readbytes] = '\0';
55   } else {
56     buf[0] = '\0';
57   }
58
59   return readbytes;
60 }