forked from KuNgia09/MemDump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mylib.c
59 lines (54 loc) · 954 Bytes
/
mylib.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
#include "mylib.h"
static ssize_t buffer_read(int fd, char *ptr)
{
if (read_cnt <= 0)
{
again:
if ((read_cnt = read(fd, read_buf, sizeof(read_buf))) < 0)
{
if (errno == EINTR)
goto again;
return(-1);
} else if (read_cnt == 0)
return(0);
read_ptr = read_buf;
}
read_cnt--;
*ptr = *read_ptr++;
return(1);
}
ssize_t readline(int fd, void *vptr, ssize_t maxlen)
{
ssize_t n, rc;
char c, *ptr;
ptr = vptr;
for (n = 1; n < maxlen; n++)
{
if ((rc = buffer_read(fd, &c)) == 1)
{
*ptr++ = c;
if (c == '\n')
break;
}
else if (rc == 0)
{
*ptr = 0;
return(n - 1);
} else
return(-1);
}
*ptr = 0;
return(n);
}
unsigned long hex2dec(const char* str)
{
return strtol(str, NULL, 16);
}
unsigned long bit2dec(const char* str)
{
return strtol(str, NULL, 2);
}
unsigned long oct2dec(const char* str)
{
return strtol(str, NULL, 8);
}