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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
|
/*
Copyright (c) 2001-2006, Gerrit Pape
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Busyboxed by Denis Vlasenko <vda.linux@googlemail.com> */
/* Collected into one file from runit's many tiny files */
/* TODO: review, eliminate unneeded stuff, move good stuff to libbb */
#include <sys/poll.h>
#include <sys/file.h>
#include "libbb.h"
#include "runit_lib.h"
/*** buffer.c ***/
void buffer_init(buffer *s,int (*op)(int fd,char *buf,unsigned len),int fd,char *buf,unsigned len)
{
s->x = buf;
s->fd = fd;
s->op = op;
s->p = 0;
s->n = len;
}
/*** buffer_get.c ***/
static int oneread(int (*op)(int fd,char *buf,unsigned len),int fd,char *buf,unsigned len)
{
int r;
for (;;) {
r = op(fd,buf,len);
if (r == -1 && errno == EINTR)
continue;
return r;
}
}
static int getthis(buffer *s,char *buf,unsigned len)
{
if (len > s->p) len = s->p;
s->p -= len;
memcpy(buf,s->x + s->n,len);
s->n += len;
return len;
}
int buffer_feed(buffer *s)
{
int r;
if (s->p)
return s->p;
r = oneread(s->op,s->fd,s->x,s->n);
if (r <= 0)
return r;
s->p = r;
s->n -= r;
if (s->n > 0)
memmove(s->x + s->n,s->x,r);
return r;
}
int buffer_bget(buffer *s,char *buf,unsigned len)
{
int r;
if (s->p > 0)
return getthis(s,buf,len);
if (s->n <= len)
return oneread(s->op,s->fd,buf,s->n);
r = buffer_feed(s);
if (r <= 0)
return r;
return getthis(s,buf,len);
}
int buffer_get(buffer *s,char *buf,unsigned len)
{
int r;
if (s->p > 0)
return getthis(s,buf,len);
if (s->n <= len)
return oneread(s->op,s->fd,buf,len);
r = buffer_feed(s);
if (r <= 0)
return r;
return getthis(s,buf,len);
}
char *buffer_peek(buffer *s)
{
return s->x + s->n;
}
void buffer_seek(buffer *s,unsigned len)
{
s->n += len;
s->p -= len;
}
/*** buffer_put.c ***/
static int allwrite(int (*op)(int fd,char *buf,unsigned len),int fd,const char *buf,unsigned len)
{
int w;
while (len) {
w = op(fd,(char*)buf,len);
if (w == -1) {
if (errno == EINTR)
continue;
return -1; /* note that some data may have been written */
}
/* if (w == 0) ; luser's fault */
buf += w;
len -= w;
}
return 0;
}
int buffer_flush(buffer *s)
{
int p;
p = s->p;
if (!p) return 0;
s->p = 0;
return allwrite(s->op,s->fd,s->x,p);
}
int buffer_putalign(buffer *s,const char *buf,unsigned len)
{
unsigned n;
while (len > (n = s->n - s->p)) {
memcpy(s->x + s->p,buf,n);
s->p += n;
buf += n;
len -= n;
if (buffer_flush(s) == -1) return -1;
}
/* now len <= s->n - s->p */
memcpy(s->x + s->p,buf,len);
s->p += len;
return 0;
}
int buffer_put(buffer *s,const char *buf,unsigned len)
{
unsigned n;
n = s->n;
if (len > n - s->p) {
if (buffer_flush(s) == -1) return -1;
/* now s->p == 0 */
if (n < BUFFER_OUTSIZE) n = BUFFER_OUTSIZE;
while (len > s->n) {
if (n > len) n = len;
if (allwrite(s->op,s->fd,buf,n) == -1) return -1;
buf += n;
len -= n;
}
}
/* now len <= s->n - s->p */
memcpy(s->x + s->p,buf,len);
s->p += len;
return 0;
}
int buffer_putflush(buffer *s,const char *buf,unsigned len)
{
if (buffer_flush(s) == -1)
return -1;
return allwrite(s->op,s->fd,buf,len);
}
int buffer_putsalign(buffer *s,const char *buf)
{
return buffer_putalign(s,buf,strlen(buf));
}
int buffer_puts(buffer *s,const char *buf)
{
return buffer_put(s,buf,strlen(buf));
}
int buffer_putsflush(buffer *s,const char *buf)
{
return buffer_putflush(s,buf,strlen(buf));
}
/*** buffer_read.c ***/
int buffer_unixread(int fd,char *buf,unsigned len)
{
return read(fd,buf,len);
}
/*** buffer_write.c ***/
int buffer_unixwrite(int fd,char *buf,unsigned len)
{
return write(fd,buf,len);
}
/*** byte_chr.c ***/
unsigned byte_chr(char *s,unsigned n,int c)
{
char ch;
char *t;
ch = c;
t = s;
for (;;) {
if (!n) break;
if (*t == ch) break;
++t;
--n;
}
return t - s;
}
/*** coe.c ***/
int coe(int fd)
{
return fcntl(fd,F_SETFD,FD_CLOEXEC);
}
/*** fd_copy.c ***/
int fd_copy(int to,int from)
{
if (to == from)
return 0;
if (fcntl(from,F_GETFL,0) == -1)
return -1;
close(to);
if (fcntl(from,F_DUPFD,to) == -1)
return -1;
return 0;
}
/*** fd_move.c ***/
int fd_move(int to,int from)
{
if (to == from)
return 0;
if (fd_copy(to,from) == -1)
return -1;
close(from);
return 0;
}
/*** fifo.c ***/
int fifo_make(const char *fn,int mode)
{
return mkfifo(fn,mode);
}
/*** fmt_ptime.c ***/
void fmt_ptime30nul(char *s, struct taia *ta) {
struct tm *t;
unsigned long u;
if (ta->sec.x < 4611686018427387914ULL)
return; /* impossible? */
u = ta->sec.x -4611686018427387914ULL;
t = gmtime((time_t*)&u);
if (!t)
return; /* huh? */
//fmt_ulong(s, 1900 + t->tm_year);
//s[4] = '-'; fmt_uint0(&s[5], t->tm_mon+1, 2);
//s[7] = '-'; fmt_uint0(&s[8], t->tm_mday, 2);
//s[10] = '_'; fmt_uint0(&s[11], t->tm_hour, 2);
//s[13] = ':'; fmt_uint0(&s[14], t->tm_min, 2);
//s[16] = ':'; fmt_uint0(&s[17], t->tm_sec, 2);
//s[19] = '.'; fmt_uint0(&s[20], ta->nano, 9);
sprintf(s, "%04u-%02u-%02u_%02u:%02u:%02u.%09u",
(unsigned)(1900 + t->tm_year),
(unsigned)(t->tm_mon+1),
(unsigned)(t->tm_mday),
(unsigned)(t->tm_hour),
(unsigned)(t->tm_min),
(unsigned)(t->tm_sec),
(unsigned)(ta->nano)
);
/* 4+1 + 2+1 + 2+1 + 2+1 + 2+1 + 2+1 + 9 = */
/* 5 + 3 + 3 + 3 + 3 + 3 + 9 = */
/* 20 (up to '.' inclusive) + 9 (not including '\0') */
return;
}
unsigned fmt_taia25(char *s, struct taia *t) {
static char pack[TAIA_PACK];
taia_pack(pack, t);
*s++ = '@';
bin2hex(s, pack, 12);
return 25;
}
#ifdef UNUSED
/*** fmt_uint.c ***/
unsigned fmt_uint(char *s,unsigned u)
{
return fmt_ulong(s,u);
}
/*** fmt_uint0.c ***/
unsigned fmt_uint0(char *s,unsigned u,unsigned n)
{
unsigned len;
len = fmt_uint(FMT_LEN, u);
while (len < n) {
if (s)
*s++ = '0';
++len;
}
if (s)
fmt_uint(s, u);
return len;
}
#endif
#ifdef UNUSED
/*** fmt_ulong.c ***/
unsigned fmt_ulong(char *s,unsigned long u)
{
unsigned len; unsigned long q;
len = 1; q = u;
while (q > 9) { ++len; q /= 10; }
if (s) {
s += len;
do { *--s = '0' + (u % 10); u /= 10; } while (u); /* handles u == 0 */
}
return len;
}
#endif
#ifdef UNUSED
/*** tai_now.c ***/
void tai_now(struct tai *t)
{
tai_unix(t, time(NULL));
}
#endif
/*** tai_pack.c ***/
static /* as it isn't used anywhere else */
void tai_pack(char *s,const struct tai *t)
{
uint64_t x;
x = t->x;
s[7] = x & 255; x >>= 8;
s[6] = x & 255; x >>= 8;
s[5] = x & 255; x >>= 8;
s[4] = x & 255; x >>= 8;
s[3] = x & 255; x >>= 8;
s[2] = x & 255; x >>= 8;
s[1] = x & 255; x >>= 8;
s[0] = x;
}
#ifdef UNUSED
/*** tai_sub.c ***/
void tai_sub(struct tai *t, const struct tai *u, const struct tai *v)
{
t->x = u->x - v->x;
}
#endif
/*** tai_unpack.c ***/
void tai_unpack(const char *s,struct tai *t)
{
uint64_t x;
x = (unsigned char) s[0];
x <<= 8; x += (unsigned char) s[1];
x <<= 8; x += (unsigned char) s[2];
x <<= 8; x += (unsigned char) s[3];
x <<= 8; x += (unsigned char) s[4];
x <<= 8; x += (unsigned char) s[5];
x <<= 8; x += (unsigned char) s[6];
x <<= 8; x += (unsigned char) s[7];
t->x = x;
}
/*** taia_add.c ***/
/* XXX: breaks tai encapsulation */
void taia_add(struct taia *t,const struct taia *u,const struct taia *v)
{
t->sec.x = u->sec.x + v->sec.x;
t->nano = u->nano + v->nano;
t->atto = u->atto + v->atto;
if (t->atto > 999999999UL) {
t->atto -= 1000000000UL;
++t->nano;
}
if (t->nano > 999999999UL) {
t->nano -= 1000000000UL;
++t->sec.x;
}
}
#ifdef UNUSED
/*** taia_frac.c ***/
double taia_frac(const struct taia *t)
{
return (t->atto * 0.000000001 + t->nano) * 0.000000001;
}
/*** taia_approx.c ***/
double taia_approx(const struct taia *t)
{
return t->sec->x + taia_frac(t);
}
#endif
static
uint64_t taia2millisec(const struct taia *t)
{
return (t->sec.x * 1000) + (t->nano / 1000000);
}
/*** taia_less.c ***/
/* XXX: breaks tai encapsulation */
int taia_less(const struct taia *t,const struct taia *u)
{
if (t->sec.x < u->sec.x) return 1;
if (t->sec.x > u->sec.x) return 0;
if (t->nano < u->nano) return 1;
if (t->nano > u->nano) return 0;
return t->atto < u->atto;
}
/*** taia_now.c ***/
void taia_now(struct taia *t)
{
struct timeval now;
gettimeofday(&now, NULL);
tai_unix(&t->sec, now.tv_sec);
t->nano = 1000 * now.tv_usec + 500;
t->atto = 0;
}
/*** taia_pack.c ***/
void taia_pack(char *s, const struct taia *t)
{
unsigned long x;
tai_pack(s, &t->sec);
s += 8;
x = t->atto;
s[7] = x & 255; x >>= 8;
s[6] = x & 255; x >>= 8;
s[5] = x & 255; x >>= 8;
s[4] = x;
x = t->nano;
s[3] = x & 255; x >>= 8;
s[2] = x & 255; x >>= 8;
s[1] = x & 255; x >>= 8;
s[0] = x;
}
/*** taia_sub.c ***/
/* XXX: breaks tai encapsulation */
void taia_sub(struct taia *t,const struct taia *u,const struct taia *v)
{
unsigned long unano = u->nano;
unsigned long uatto = u->atto;
t->sec.x = u->sec.x - v->sec.x;
t->nano = unano - v->nano;
t->atto = uatto - v->atto;
if (t->atto > uatto) {
t->atto += 1000000000UL;
--t->nano;
}
if (t->nano > unano) {
t->nano += 1000000000UL;
--t->sec.x;
}
}
/*** taia_uint.c ***/
/* XXX: breaks tai encapsulation */
void taia_uint(struct taia *t,unsigned s)
{
t->sec.x = s;
t->nano = 0;
t->atto = 0;
}
/*** stralloc_cat.c ***/
#if 0
int stralloc_cat(stralloc *sato,const stralloc *safrom)
{
return stralloc_catb(sato,safrom->s,safrom->len);
}
/*** stralloc_catb.c ***/
int stralloc_catb(stralloc *sa,const char *s,unsigned n)
{
if (!sa->s) return stralloc_copyb(sa,s,n);
if (!stralloc_readyplus(sa,n + 1)) return 0;
memcpy(sa->s + sa->len,s,n);
sa->len += n;
sa->s[sa->len] = 'Z'; /* ``offensive programming'' */
return 1;
}
/*** stralloc_cats.c ***/
int stralloc_cats(stralloc *sa,const char *s)
{
return stralloc_catb(sa,s,strlen(s));
}
/*** stralloc_eady.c ***/
GEN_ALLOC_ready(stralloc,char,s,len,a,i,n,x,30,stralloc_ready)
GEN_ALLOC_readyplus(stralloc,char,s,len,a,i,n,x,30,stralloc_readyplus)
/*** stralloc_opyb.c ***/
int stralloc_copyb(stralloc *sa,const char *s,unsigned n)
{
if (!stralloc_ready(sa,n + 1)) return 0;
memcpy(sa->s,s,n);
sa->len = n;
sa->s[n] = 'Z'; /* ``offensive programming'' */
return 1;
}
/*** stralloc_opys.c ***/
int stralloc_copys(stralloc *sa,const char *s)
{
return stralloc_copyb(sa,s,strlen(s));
}
/*** stralloc_pend.c ***/
GEN_ALLOC_append(stralloc,char,s,len,a,i,n,x,30,stralloc_readyplus,stralloc_append)
#endif /* stralloc */
/*** iopause.c ***/
void iopause(iopause_fd *x,unsigned len,struct taia *deadline,struct taia *stamp)
{
int millisecs;
int i;
if (taia_less(deadline,stamp))
millisecs = 0;
else {
uint64_t m;
struct taia t;
t = *stamp;
taia_sub(&t, deadline, &t);
millisecs = m = taia2millisec(&t);
if (m > 1000) millisecs = 1000;
millisecs += 20;
}
for (i = 0; i < len; ++i)
x[i].revents = 0;
poll(x, len, millisecs);
/* XXX: some kernels apparently need x[0] even if len is 0 */
/* XXX: how to handle EAGAIN? are kernels really this dumb? */
/* XXX: how to handle EINVAL? when exactly can this happen? */
}
/*** lock_ex.c ***/
int lock_ex(int fd)
{
return flock(fd,LOCK_EX);
}
/*** lock_exnb.c ***/
int lock_exnb(int fd)
{
return flock(fd,LOCK_EX | LOCK_NB);
}
/*** open_append.c ***/
int open_append(const char *fn)
{
return open(fn, O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
}
/*** open_read.c ***/
int open_read(const char *fn)
{
return open(fn, O_RDONLY|O_NDELAY);
}
/*** open_trunc.c ***/
int open_trunc(const char *fn)
{
return open(fn,O_WRONLY | O_NDELAY | O_TRUNC | O_CREAT,0644);
}
/*** open_write.c ***/
int open_write(const char *fn)
{
return open(fn, O_WRONLY|O_NDELAY);
}
/*** openreadclose.c ***/
#if 0
int openreadclose(const char *fn,stralloc *sa,unsigned bufsize)
{
int fd;
fd = open_read(fn);
if (fd == -1) {
if (errno == ENOENT) return 0;
return -1;
}
if (readclose(fd,sa,bufsize) == -1) return -1;
return 1;
}
#endif
/*** pathexec_env.c ***/
#if 0
static stralloc plus;
static stralloc tmp;
int pathexec_env(const char *s,const char *t)
{
if (!s) return 1;
if (!stralloc_copys(&tmp,s)) return 0;
if (t) {
if (!stralloc_cats(&tmp,"=")) return 0;
if (!stralloc_cats(&tmp,t)) return 0;
}
if (!stralloc_0(&tmp)) return 0;
return stralloc_cat(&plus,&tmp);
}
void pathexec(char **argv)
{
char **e;
unsigned elen;
unsigned i;
unsigned j;
unsigned split;
unsigned t;
if (!stralloc_cats(&plus,"")) return;
elen = 0;
for (i = 0;environ[i];++i)
++elen;
for (i = 0;i < plus.len;++i)
if (!plus.s[i])
++elen;
e = malloc((elen + 1) * sizeof(char *));
if (!e) return;
elen = 0;
for (i = 0;environ[i];++i)
e[elen++] = environ[i];
j = 0;
for (i = 0;i < plus.len;++i)
if (!plus.s[i]) {
split = str_chr(plus.s + j,'=');
for (t = 0;t < elen;++t)
if (memcmp(plus.s + j,e[t],split) == 0)
if (e[t][split] == '=') {
--elen;
e[t] = e[elen];
break;
}
if (plus.s[j + split])
e[elen++] = plus.s + j;
j = i + 1;
}
e[elen] = 0;
pathexec_run(*argv,argv,e);
free(e);
}
#endif
/*** pathexec_run.c ***/
#if 0
static stralloc tmp;
void pathexec_run(const char *file,char *const *argv,char *const *envp)
{
const char *path;
unsigned split;
int savederrno;
if (file[str_chr(file,'/')]) {
execve(file,argv,envp);
return;
}
path = getenv("PATH");
if (!path) path = "/bin:/usr/bin";
savederrno = 0;
for (;;) {
split = str_chr(path,':');
if (!stralloc_copyb(&tmp,path,split)) return;
if (!split)
if (!stralloc_cats(&tmp,".")) return;
if (!stralloc_cats(&tmp,"/")) return;
if (!stralloc_cats(&tmp,file)) return;
if (!stralloc_0(&tmp)) return;
execve(tmp.s,argv,envp);
if (errno != ENOENT) {
savederrno = errno;
if ((errno != EACCES) && (errno != EPERM) && (errno != EISDIR)) return;
}
if (!path[split]) {
if (savederrno) errno = savederrno;
return;
}
path += split;
path += 1;
}
}
#endif
/*** pmatch.c ***/
unsigned pmatch(const char *p, const char *s, unsigned len) {
for (;;) {
char c = *p++;
if (!c) return !len;
switch (c) {
case '*':
if (!(c = *p)) return 1;
for (;;) {
if (!len) return 0;
if (*s == c) break;
++s; --len;
}
continue;
case '+':
if ((c = *p++) != *s) return 0;
for (;;) {
if (!len) return 1;
if (*s != c) break;
++s; --len;
}
continue;
/*
case '?':
if (*p == '?') {
if (*s != '?') return 0;
++p;
}
++s; --len;
continue;
*/
default:
if (!len) return 0;
if (*s != c) return 0;
++s; --len;
continue;
}
}
return 0;
}
/*** prot.c ***/
int prot_gid(int gid)
{
gid_t x = gid;
if (setgroups(1,&x) == -1) return -1;
return setgid(gid); /* _should_ be redundant, but on some systems it isn't */
}
int prot_uid(int uid)
{
return setuid(uid);
}
/*** readclose.c ***/
#if 0
int readclose_append(int fd,stralloc *sa,unsigned bufsize)
{
int r;
for (;;) {
if (!stralloc_readyplus(sa,bufsize)) { close(fd); return -1; }
r = read(fd,sa->s + sa->len,bufsize);
if (r == -1) if (errno == EINTR) continue;
if (r <= 0) { close(fd); return r; }
sa->len += r;
}
}
int readclose(int fd,stralloc *sa,unsigned bufsize)
{
if (!stralloc_copys(sa,"")) { close(fd); return -1; }
return readclose_append(fd,sa,bufsize);
}
#endif
/*** scan_ulong.c ***/
unsigned scan_ulong(const char *s,unsigned long *u)
{
unsigned pos = 0;
unsigned long result = 0;
unsigned long c;
while ((c = (unsigned long) (unsigned char) (s[pos] - '0')) < 10) {
result = result * 10 + c;
++pos;
}
*u = result;
return pos;
}
#ifdef UNUSED
/*** seek_set.c ***/
int seek_set(int fd,seek_pos pos)
{
if (lseek(fd,(off_t) pos,SEEK_SET) == -1) return -1; return 0;
}
#endif
/*** sig.c ***/
//int sig_alarm = SIGALRM;
//int sig_child = SIGCHLD;
//int sig_cont = SIGCONT;
//int sig_hangup = SIGHUP;
//int sig_int = SIGINT;
//int sig_pipe = SIGPIPE;
//int sig_term = SIGTERM;
//void (*sig_defaulthandler)(int) = SIG_DFL;
//void (*sig_ignorehandler)(int) = SIG_IGN;
/*** sig_block.c ***/
void sig_block(int sig)
{
sigset_t ss;
sigemptyset(&ss);
sigaddset(&ss,sig);
sigprocmask(SIG_BLOCK,&ss,(sigset_t *) 0);
}
void sig_unblock(int sig)
{
sigset_t ss;
sigemptyset(&ss);
sigaddset(&ss,sig);
sigprocmask(SIG_UNBLOCK,&ss,(sigset_t *) 0);
}
void sig_blocknone(void)
{
sigset_t ss;
sigemptyset(&ss);
sigprocmask(SIG_SETMASK,&ss,(sigset_t *) 0);
}
/*** sig_catch.c ***/
void sig_catch(int sig,void (*f)(int))
{
struct sigaction sa;
sa.sa_handler = f;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(sig,&sa,(struct sigaction *) 0);
}
/*** sig_pause.c ***/
void sig_pause(void)
{
sigset_t ss;
sigemptyset(&ss);
sigsuspend(&ss);
}
/*** str_chr.c ***/
unsigned str_chr(const char *s,int c)
{
char ch;
const char *t;
ch = c;
t = s;
for (;;) {
if (!*t) break;
if (*t == ch) break;
++t;
}
return t - s;
}
/*** wait_nohang.c ***/
int wait_nohang(int *wstat)
{
return waitpid(-1, wstat, WNOHANG);
}
/*** wait_pid.c ***/
int wait_pid(int *wstat, int pid)
{
int r;
do
r = waitpid(pid, wstat, 0);
while ((r == -1) && (errno == EINTR));
return r;
}
|