-
Notifications
You must be signed in to change notification settings - Fork 4
/
misc.c
72 lines (61 loc) · 1.28 KB
/
misc.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
/*
* (c) 2005 bl0rg.net
*
* Misc functions (mostly unix workarounds)
*/
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include "misc.h"
int unix_write(int fd, unsigned char *buf, size_t size) {
int i, len = 0;
while ((i = write(fd, buf + len, size - len))) {
if (i < 0) {
if ((errno == EINTR) || (errno == EAGAIN))
continue;
else {
perror("write");
return -1;
}
} else {
len += i;
if (len == size)
break;
}
}
if (i == 0)
return 0;
else
return len;
}
int unix_read(int fd, unsigned char *buf, size_t size) {
int i, len = 0;
while ((i = read(fd, buf + len, size - len))) {
if (i < 0) {
if ((errno == EINTR) || (errno == EAGAIN))
continue;
else {
perror("read");
return -1;
}
} else {
len += i;
if (len == size)
break;
}
}
if (i == 0)
return 0;
else
return len;
}
void format_time(unsigned long time, char *str, unsigned int len) {
unsigned long ms = time % 1000;
time /= 1000;
unsigned long secs = time % 60;
time /= 60;
unsigned long minutes = time % 60;
time /= 60;
unsigned long hours = time;
snprintf(str, len, "%.2lu:%.2lu:%.2lu+%.3lu", hours, minutes, secs, ms);
}