summaryrefslogtreecommitdiff
path: root/runit/runit_lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'runit/runit_lib.c')
-rw-r--r--runit/runit_lib.c147
1 files changed, 8 insertions, 139 deletions
diff --git a/runit/runit_lib.c b/runit/runit_lib.c
index 295b45f09..4b7950ca3 100644
--- a/runit/runit_lib.c
+++ b/runit/runit_lib.c
@@ -34,8 +34,6 @@ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34#include "libbb.h" 34#include "libbb.h"
35#include "runit_lib.h" 35#include "runit_lib.h"
36 36
37/*** byte_chr.c ***/
38
39unsigned byte_chr(char *s,unsigned n,int c) 37unsigned byte_chr(char *s,unsigned n,int c)
40{ 38{
41 char ch; 39 char ch;
@@ -52,62 +50,14 @@ unsigned byte_chr(char *s,unsigned n,int c)
52 return t - s; 50 return t - s;
53} 51}
54 52
55
56/*** coe.c ***/
57
58int coe(int fd) 53int coe(int fd)
59{ 54{
60 return fcntl(fd,F_SETFD,FD_CLOEXEC); 55 return fcntl(fd, F_SETFD, FD_CLOEXEC);
61}
62
63
64/*** fmt_ptime.c ***/
65
66void fmt_ptime30nul(char *s, struct taia *ta) {
67 struct tm *t;
68 unsigned long u;
69
70 if (ta->sec.x < 4611686018427387914ULL)
71 return; /* impossible? */
72 u = ta->sec.x -4611686018427387914ULL;
73 t = gmtime((time_t*)&u);
74 if (!t)
75 return; /* huh? */
76 //fmt_ulong(s, 1900 + t->tm_year);
77 //s[4] = '-'; fmt_uint0(&s[5], t->tm_mon+1, 2);
78 //s[7] = '-'; fmt_uint0(&s[8], t->tm_mday, 2);
79 //s[10] = '_'; fmt_uint0(&s[11], t->tm_hour, 2);
80 //s[13] = ':'; fmt_uint0(&s[14], t->tm_min, 2);
81 //s[16] = ':'; fmt_uint0(&s[17], t->tm_sec, 2);
82 //s[19] = '.'; fmt_uint0(&s[20], ta->nano, 9);
83 sprintf(s, "%04u-%02u-%02u_%02u:%02u:%02u.%09u",
84 (unsigned)(1900 + t->tm_year),
85 (unsigned)(t->tm_mon+1),
86 (unsigned)(t->tm_mday),
87 (unsigned)(t->tm_hour),
88 (unsigned)(t->tm_min),
89 (unsigned)(t->tm_sec),
90 (unsigned)(ta->nano)
91 );
92 /* 4+1 + 2+1 + 2+1 + 2+1 + 2+1 + 2+1 + 9 = */
93 /* 5 + 3 + 3 + 3 + 3 + 3 + 9 = */
94 /* 20 (up to '.' inclusive) + 9 (not including '\0') */
95} 56}
96 57
97unsigned fmt_taia25(char *s, struct taia *t) { 58#ifdef UNUSED
98 static char pack[TAIA_PACK];
99
100 taia_pack(pack, t);
101 *s++ = '@';
102 bin2hex(s, pack, 12);
103 return 25;
104}
105
106
107/*** tai_pack.c ***/
108
109static /* as it isn't used anywhere else */ 59static /* as it isn't used anywhere else */
110void tai_pack(char *s,const struct tai *t) 60void tai_pack(char *s, const struct tai *t)
111{ 61{
112 uint64_t x; 62 uint64_t x;
113 63
@@ -122,19 +72,6 @@ void tai_pack(char *s,const struct tai *t)
122 s[0] = x; 72 s[0] = x;
123} 73}
124 74
125
126#ifdef UNUSED
127/*** tai_sub.c ***/
128
129void tai_sub(struct tai *t, const struct tai *u, const struct tai *v)
130{
131 t->x = u->x - v->x;
132}
133#endif
134
135
136/*** tai_unpack.c ***/
137
138void tai_unpack(const char *s,struct tai *t) 75void tai_unpack(const char *s,struct tai *t)
139{ 76{
140 uint64_t x; 77 uint64_t x;
@@ -151,8 +88,6 @@ void tai_unpack(const char *s,struct tai *t)
151} 88}
152 89
153 90
154/*** taia_add.c ***/
155
156void taia_add(struct taia *t,const struct taia *u,const struct taia *v) 91void taia_add(struct taia *t,const struct taia *u,const struct taia *v)
157{ 92{
158 t->sec.x = u->sec.x + v->sec.x; 93 t->sec.x = u->sec.x + v->sec.x;
@@ -168,9 +103,6 @@ void taia_add(struct taia *t,const struct taia *u,const struct taia *v)
168 } 103 }
169} 104}
170 105
171
172/*** taia_less.c ***/
173
174int taia_less(const struct taia *t, const struct taia *u) 106int taia_less(const struct taia *t, const struct taia *u)
175{ 107{
176 if (t->sec.x < u->sec.x) return 1; 108 if (t->sec.x < u->sec.x) return 1;
@@ -180,9 +112,6 @@ int taia_less(const struct taia *t, const struct taia *u)
180 return t->atto < u->atto; 112 return t->atto < u->atto;
181} 113}
182 114
183
184/*** taia_now.c ***/
185
186void taia_now(struct taia *t) 115void taia_now(struct taia *t)
187{ 116{
188 struct timeval now; 117 struct timeval now;
@@ -192,9 +121,7 @@ void taia_now(struct taia *t)
192 t->atto = 0; 121 t->atto = 0;
193} 122}
194 123
195 124/* UNUSED
196/*** taia_pack.c ***/
197
198void taia_pack(char *s, const struct taia *t) 125void taia_pack(char *s, const struct taia *t)
199{ 126{
200 unsigned long x; 127 unsigned long x;
@@ -213,9 +140,7 @@ void taia_pack(char *s, const struct taia *t)
213 s[1] = x & 255; x >>= 8; 140 s[1] = x & 255; x >>= 8;
214 s[0] = x; 141 s[0] = x;
215} 142}
216 143*/
217
218/*** taia_sub.c ***/
219 144
220void taia_sub(struct taia *t, const struct taia *u, const struct taia *v) 145void taia_sub(struct taia *t, const struct taia *u, const struct taia *v)
221{ 146{
@@ -235,11 +160,7 @@ void taia_sub(struct taia *t, const struct taia *u, const struct taia *v)
235 } 160 }
236} 161}
237 162
238
239/*** taia_uint.c ***/
240
241/* XXX: breaks tai encapsulation */ 163/* XXX: breaks tai encapsulation */
242
243void taia_uint(struct taia *t, unsigned s) 164void taia_uint(struct taia *t, unsigned s)
244{ 165{
245 t->sec.x = s; 166 t->sec.x = s;
@@ -247,16 +168,12 @@ void taia_uint(struct taia *t, unsigned s)
247 t->atto = 0; 168 t->atto = 0;
248} 169}
249 170
250
251/*** iopause.c ***/
252
253static 171static
254uint64_t taia2millisec(const struct taia *t) 172uint64_t taia2millisec(const struct taia *t)
255{ 173{
256 return (t->sec.x * 1000) + (t->nano / 1000000); 174 return (t->sec.x * 1000) + (t->nano / 1000000);
257} 175}
258 176
259
260void iopause(iopause_fd *x, unsigned len, struct taia *deadline, struct taia *stamp) 177void iopause(iopause_fd *x, unsigned len, struct taia *deadline, struct taia *stamp)
261{ 178{
262 int millisecs; 179 int millisecs;
@@ -282,59 +199,40 @@ void iopause(iopause_fd *x, unsigned len, struct taia *deadline, struct taia *st
282 /* XXX: how to handle EAGAIN? are kernels really this dumb? */ 199 /* XXX: how to handle EAGAIN? are kernels really this dumb? */
283 /* XXX: how to handle EINVAL? when exactly can this happen? */ 200 /* XXX: how to handle EINVAL? when exactly can this happen? */
284} 201}
285 202#endif
286
287/*** lock_ex.c ***/
288 203
289int lock_ex(int fd) 204int lock_ex(int fd)
290{ 205{
291 return flock(fd,LOCK_EX); 206 return flock(fd,LOCK_EX);
292} 207}
293 208
294
295/*** lock_exnb.c ***/
296
297int lock_exnb(int fd) 209int lock_exnb(int fd)
298{ 210{
299 return flock(fd,LOCK_EX | LOCK_NB); 211 return flock(fd,LOCK_EX | LOCK_NB);
300} 212}
301 213
302
303/*** open_append.c ***/
304
305int open_append(const char *fn) 214int open_append(const char *fn)
306{ 215{
307 return open(fn, O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600); 216 return open(fn, O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
308} 217}
309 218
310
311/*** open_read.c ***/
312
313int open_read(const char *fn) 219int open_read(const char *fn)
314{ 220{
315 return open(fn, O_RDONLY|O_NDELAY); 221 return open(fn, O_RDONLY|O_NDELAY);
316} 222}
317 223
318
319/*** open_trunc.c ***/
320
321int open_trunc(const char *fn) 224int open_trunc(const char *fn)
322{ 225{
323 return open(fn,O_WRONLY | O_NDELAY | O_TRUNC | O_CREAT,0644); 226 return open(fn,O_WRONLY | O_NDELAY | O_TRUNC | O_CREAT,0644);
324} 227}
325 228
326
327/*** open_write.c ***/
328
329int open_write(const char *fn) 229int open_write(const char *fn)
330{ 230{
331 return open(fn, O_WRONLY|O_NDELAY); 231 return open(fn, O_WRONLY|O_NDELAY);
332} 232}
333 233
334 234unsigned pmatch(const char *p, const char *s, unsigned len)
335/*** pmatch.c ***/ 235{
336
337unsigned pmatch(const char *p, const char *s, unsigned len) {
338 for (;;) { 236 for (;;) {
339 char c = *p++; 237 char c = *p++;
340 if (!c) return !len; 238 if (!c) return !len;
@@ -373,32 +271,3 @@ unsigned pmatch(const char *p, const char *s, unsigned len) {
373 } 271 }
374 return 0; 272 return 0;
375} 273}
376
377
378#ifdef UNUSED
379/*** seek_set.c ***/
380
381int seek_set(int fd,seek_pos pos)
382{
383 if (lseek(fd,(off_t) pos,SEEK_SET) == -1) return -1; return 0;
384}
385
386
387/*** str_chr.c ***/
388
389// strchrnul?
390unsigned str_chr(const char *s,int c)
391{
392 char ch;
393 const char *t;
394
395 ch = c;
396 t = s;
397 for (;;) {
398 if (!*t) break;
399 if (*t == ch) break;
400 ++t;
401 }
402 return t - s;
403}
404#endif