-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstrannex.c
98 lines (89 loc) · 2.02 KB
/
strannex.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <unistd.h>
#include "strannex.h"
char *_strannex(char *dst, char *src, char *dstend)
{
if (!dst || !src || !dstend || dst>dstend) return NULL;
// skip to the end of the string
while (*dst) {
if (++dst>dstend) return NULL;
}
// Append, breaking if past end
while (*src) {
if (dst>=dstend) break;
*dst++ = *src++;
}
// add terminator
*dst = 0;
// return where it last touched
return dst;
}
char *_strannex_uint(char *dst, unsigned long value, char *dstend)
{
char *p, *src, buf[10];
src = buf;
p=utoa_radix_nz(value,src,10,-10);
*p = 0;
if (!dst || !src || !dstend || dst>dstend) return NULL;
// skip to the end of the string
while (*dst) {
if (++dst>dstend) return NULL;
}
// Append, breaking if past end
while (*src) {
if (dst>=dstend) break;
*dst++ = *src++;
}
// add terminator
*dst = 0;
// return where it last touched
return dst;
}
char *utoa_radix_nz(unsigned long value, char *out, int radix, int ndigits)
{
if (!out || radix>36) return NULL;
char *p=out;
if (value) {
while (value) {
char d = value % radix;
value /= radix;
d += (d>9) ? 'A'-10 : '0';
*p++ = d;
if (ndigits<0) ndigits++;
if (ndigits>0) ndigits--;
if (!ndigits) break;
}
} else {
if (ndigits<1) ndigits=1;
}
while (ndigits-->0) *p++='0';
char *end=p;
p--;
while (out<p) {
char tmp=*out;
*out++=*p;
*p--=tmp;
}
return end;
}
void write_int(int fd, int value)
{
char *p, str[11];
unsigned size;
p=str;
if (value < 0) {
*p++ = '-';
value *= -1;
}
p=utoa_radix_nz((unsigned long)value,p,10,-10);
size = p - str;
write(fd,str,size);
}
void write_uint(int fd, unsigned long value)
{
char *p, str[10];
unsigned size;
p=utoa_radix_nz(value,str,10,-10);
size = p - str;
write(fd,str,size);
}