forked from Samsung/CAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
184 lines (160 loc) · 4.23 KB
/
utils.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include "utils.h"
#define RD_BUFSIZE 128*1024
#ifdef EINTR
# define IS_EINTR(x) ((x) == EINTR)
#else
# define IS_EINTR(x) 0
#endif
/* Taken from https://github.com/nbsdx/SimpleJSON.git: public license */
const char* json_escape(const char* s) {
if (!s) return 0;
size_t len = strlen(s);
char* output = 0;
char* __output = 0;
unsigned i;
for(i = 0; i < len; ++i ) {
if (((unsigned char)s[i]>=0x80)||(s[i]<=0x1F)) {
lazy_alloc_output();
}
switch( s[i] ) {
case '\"': { lazy_alloc_output(); *output++ = '\\'; *output++ = '\"'; break; }
case '\\': { lazy_alloc_output(); *output++ = '\\'; *output++ = '\\'; break; }
case '\b': { lazy_alloc_output(); *output++ = '\\'; *output++ = 'b'; break; }
case '\f': { lazy_alloc_output(); *output++ = '\\'; *output++ = 'f'; break; }
case '\n': { lazy_alloc_output(); *output++ = '\\'; *output++ = 'n'; break; }
case '\r': { lazy_alloc_output(); *output++ = '\\'; *output++ = 'r'; break; }
case '\t': { lazy_alloc_output(); *output++ = '\\'; *output++ = 't'; break; }
default :
{
if (output) {
if (((unsigned char)s[i]<0x80)&&(s[i]>0x1F)) { // Ignore non-ASCII characters
*output++ = s[i];
}
}
break;
}
}
}
if (output) {
*output++=0;
}
if (output)
return __output;
else
return 0;
}
/* Taken from GNU CoreUtils 6.9 cat: GPL v.2 */
ssize_t safe_rd (int fd, void const *buf, size_t count)
{
for (;;)
{
ssize_t result = read (fd, (void*)buf, count);
if (0 <= result)
return result;
else if (IS_EINTR (errno)) {
continue;
}
else
return result;
}
}
ssize_t count_file_lines(const char* path) {
int fdr = open(path,O_RDONLY);
/* Count number of lines */
void* pgbuff = malloc(RD_BUFSIZE);
assert(pgbuff!=0);
size_t total_nread = 0;
ssize_t nlcount = 0;
while(1) {
ssize_t n_read = safe_rd (fdr, pgbuff, RD_BUFSIZE);
if (n_read<0) {
fprintf(stderr,"ERROR[read]: %lu, %d\n",total_nread,errno);
close(fdr);
return -1;
}
if (n_read == 0) {
break;
}
/* n_read was read in */
total_nread+=n_read;
size_t noff = 0;
void* ppnewl = pgbuff;
void* pnewl = memchr(pgbuff,'\n',RD_BUFSIZE);
while(pnewl) {
noff+=pnewl-ppnewl+1;
nlcount++;
ppnewl = pnewl+1;
pnewl = memchr(pnewl+1,'\n',RD_BUFSIZE-noff);
}
}
nlcount++;
free(pgbuff);
close(fdr);
return nlcount;
}
#define COMP_MAX 50
#define ispathsep(ch) ((ch) == '/' || (ch) == '\\')
#define iseos(ch) ((ch) == '\0')
#define ispathend(ch) (ispathsep(ch) || iseos(ch))
/* Taken from https://gist.github.com/starwing/2761647 */
char *normpath(const char *in) {
char* out = malloc(strlen(in)+1);
char *pos[COMP_MAX], **top = pos, *head = out;
int isabs = ispathsep(*in);
if (isabs) *out++ = '/';
*top++ = out;
while (!iseos(*in)) {
while (ispathsep(*in)) ++in;
if (iseos(*in))
break;
if (memcmp(in, ".", 1) == 0 && ispathend(in[1])) {
++in;
continue;
}
if (memcmp(in, "..", 2) == 0 && ispathend(in[2])) {
in += 2;
if (top != pos + 1)
out = *--top;
else if (isabs)
out = top[-1];
else {
strcpy(out, "../");
out += 3;
}
continue;
}
if (top - pos >= COMP_MAX)
return NULL; /* path too complicated */
*top++ = out;
while (!ispathend(*in))
*out++ = *in++;
if (ispathsep(*in))
*out++ = '/';
}
*out = '\0';
if (*head == '\0')
strcpy(head, "./");
return head;
}
const char* path_join(const char* start, const char* end) {
if (end[0]=='/') {
return 0;
}
size_t ssz = strlen(start);
size_t esz = strlen(end);
char* s = malloc(ssz+esz+2);
memcpy(s,start,ssz);
s[ssz] = '/';
memcpy(s+ssz+1,end,esz);
s[ssz+1+esz] = 0;
char* ns = normpath(s);
free(s);
return ns;
}