-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.c
221 lines (179 loc) · 3.38 KB
/
util.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <stddef.h>
#include <sys/time.h>
#include <signal.h>
#include "util.h"
void *umalloc(size_t l)
{
void *p = malloc(l);
if(!p){
perror("malloc()");
abort();
}
memset(p, 0, l);
return p;
}
void *urealloc(void *p, size_t l)
{
void *r = realloc(p, l);
if(!r){
perror("realloc()");
abort();
}
return r;
}
char *ustrdup(const char *s)
{
char *r = umalloc(strlen(s) + 1);
strcpy(r, s);
return r;
}
long mstime()
{
struct timeval t;
gettimeofday(&t, NULL);
return t.tv_sec * 1000 + t.tv_usec / 1000;
}
char *fline(const char *path, char **pbuf, int *plen)
{
FILE *f;
char *ret;
int c;
int len;
int i;
f = fopen(path, "r");
if(!f)
return NULL;
len = 64;
ret = umalloc(len + 1);
memset(ret, 0, len);
i = 0;
while((c = fgetc(f)) != EOF){
ret[i++] = c;
if(i == len){
len += 64;
ret = urealloc(ret, len + 1);
memset(ret + i + 1, 0, 64);
}
}
if(ferror(f)){
/* process exited while we were reading */
free(ret);
ret = NULL;
i = 0;
}
fclose(f);
*pbuf = ret;
if(plen)
*plen = i;
return ret;
}
int str_to_sig(const char *s)
{
#define SIG(i) { #i, SIG##i }
struct
{
const char *nam;
int sig;
} sigs[] = {
SIG(HUP), SIG(INT), SIG(QUIT), SIG(ILL),
SIG(ABRT), SIG(FPE), SIG(KILL), SIG(USR1),
SIG(SEGV), SIG(USR2), SIG(PIPE), SIG(ALRM),
SIG(TERM), SIG(CHLD), SIG(CONT), SIG(STOP),
SIG(TSTP), SIG(TTIN), SIG(TTOU),
#ifdef __linux__
SIG(PWR), SIG(SYS), SIG(POLL), SIG(WINCH),
SIG(PROF), SIG(TRAP), SIG(BUS), SIG(STKFLT),
SIG(URG), SIG(XCPU), SIG(XFSZ), SIG(VTALRM),
#endif
};
unsigned int i;
for(i = 0; i < sizeof(sigs)/sizeof(sigs[0]); i++)
if(!strcmp(sigs[i].nam, s))
return sigs[i].sig;
return -1;
}
int longest_passwd_line(const char *fname)
{
FILE *f = fopen(fname, "r");
int max = 8; /* default */
char buf[128];
if(!f)
return max;
while(fgets(buf, sizeof buf, f)){
char *colon = strchr(buf, ':');
if(colon){
int len = (int)(colon - buf);
if(len > max)
max = len;
}
}
fclose(f);
return max;
}
const char *format_kbytes(long unsigned val)
{
static char buf[64];
char prefix;
if(val / (1024 * 1024) > 10){
prefix = 'G';
val = val / (1024 * 1024);
}else if(val / 1024 > 10){
prefix = 'M';
val /= 1024;
}else{
prefix = 'K';
}
snprintf(buf, sizeof buf, "%lu%c", val, prefix);
return buf;
}
const char *format_seconds(unsigned long timeval)
{
#define BUF_PRINTF(fmt, ...) i += snprintf(&buf[i], sizeof(buf) - i, fmt, __VA_ARGS__)
static char buf[128];
unsigned long rest;
unsigned long days, hours, minutes, seconds;
size_t i = 0;
days = timeval / 86400;
rest = timeval % 86400;
hours = rest / 3600;
rest = rest % 3600;
minutes = rest / 60;
rest = rest % 60;
seconds = (unsigned int)rest;
if(days)
BUF_PRINTF("%ld+", days);
if(hours)
BUF_PRINTF("%02ld:", hours);
BUF_PRINTF("%02ld:%02ld", minutes, seconds);
return buf;
#undef BUF_PRINTF
}
void argv_free(size_t argc, char **argv)
{
for(size_t i = 0; i < argc; i++)
free(argv[i]);
free(argv);
}
static void lc(char *p)
{
for(; *p; p++)
*p = tolower(*p);
}
const char *ustrcasestr(const char *a, const char *b)
{
char *da = ustrdup(a), *db = ustrdup(b);
lc(da);
lc(db);
const char *r = strstr(da, db);
if(r){
size_t diff = r - da;
r = a + diff;
}
free(da), free(db);
return r;
}