summaryrefslogtreecommitdiff
path: root/statuses/tools.c
blob: 7a4a677fc270a8ee4aebfcf2a28e65ef8353a29c (plain)
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
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include "tools.h"

char powerToChar(int power)
{
	switch(power)
	{
		case 0:
			return 'b';
		case 1:
			return 'k';
		case 2:
			return 'M';
		case 3:
			return 'G';
		case 4:
			return 'T';
		case 5:
			return 'P';
		case 6:
			return 'E';
	}

	return '?';
}


void statusError(char *where, char *what, char *extra)
{
	fprintf(stderr, "%s: %s", where, what);
	if (extra)
		fprintf(stderr, " -- %s", extra);
	fputs("\n", stderr);
}


ssize_t fileRead(char *buf, size_t bufsize, char *file)
{
	int fd;
	int readbytes;

	fd = open(file, 0);
	if (fd < 0)
		return -1;

	readbytes = read(fd, buf, bufsize - 1);
	close(fd);

	if (readbytes > 0)
		buf[readbytes] = '\0';
	else
		buf[0] = '\0';

	return readbytes;
}