2 Copyright (c) 2001-2006, Gerrit Pape
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 /* Busyboxed by Denis Vlasenko <vda.linux@googlemail.com> */
29 /* Collected into one file from runit's many tiny files */
30 /* TODO: review, eliminate unneeded stuff, move good stuff to libbb */
35 #include "runit_lib.h"
37 unsigned byte_chr(char *s,unsigned n,int c)
55 return fcntl(fd, F_SETFD, FD_CLOEXEC);
59 static /* as it isn't used anywhere else */
60 void tai_pack(char *s, const struct tai *t)
65 s[7] = x & 255; x >>= 8;
66 s[6] = x & 255; x >>= 8;
67 s[5] = x & 255; x >>= 8;
68 s[4] = x & 255; x >>= 8;
69 s[3] = x & 255; x >>= 8;
70 s[2] = x & 255; x >>= 8;
71 s[1] = x & 255; x >>= 8;
75 void tai_unpack(const char *s,struct tai *t)
79 x = (unsigned char) s[0];
80 x <<= 8; x += (unsigned char) s[1];
81 x <<= 8; x += (unsigned char) s[2];
82 x <<= 8; x += (unsigned char) s[3];
83 x <<= 8; x += (unsigned char) s[4];
84 x <<= 8; x += (unsigned char) s[5];
85 x <<= 8; x += (unsigned char) s[6];
86 x <<= 8; x += (unsigned char) s[7];
91 void taia_add(struct taia *t,const struct taia *u,const struct taia *v)
93 t->sec.x = u->sec.x + v->sec.x;
94 t->nano = u->nano + v->nano;
95 t->atto = u->atto + v->atto;
96 if (t->atto > 999999999UL) {
97 t->atto -= 1000000000UL;
100 if (t->nano > 999999999UL) {
101 t->nano -= 1000000000UL;
106 int taia_less(const struct taia *t, const struct taia *u)
108 if (t->sec.x < u->sec.x) return 1;
109 if (t->sec.x > u->sec.x) return 0;
110 if (t->nano < u->nano) return 1;
111 if (t->nano > u->nano) return 0;
112 return t->atto < u->atto;
115 void taia_now(struct taia *t)
118 gettimeofday(&now, NULL);
119 tai_unix(&t->sec, now.tv_sec);
120 t->nano = 1000 * now.tv_usec + 500;
125 void taia_pack(char *s, const struct taia *t)
129 tai_pack(s, &t->sec);
133 s[7] = x & 255; x >>= 8;
134 s[6] = x & 255; x >>= 8;
135 s[5] = x & 255; x >>= 8;
138 s[3] = x & 255; x >>= 8;
139 s[2] = x & 255; x >>= 8;
140 s[1] = x & 255; x >>= 8;
145 void taia_sub(struct taia *t, const struct taia *u, const struct taia *v)
147 unsigned long unano = u->nano;
148 unsigned long uatto = u->atto;
150 t->sec.x = u->sec.x - v->sec.x;
151 t->nano = unano - v->nano;
152 t->atto = uatto - v->atto;
153 if (t->atto > uatto) {
154 t->atto += 1000000000UL;
157 if (t->nano > unano) {
158 t->nano += 1000000000UL;
163 /* XXX: breaks tai encapsulation */
164 void taia_uint(struct taia *t, unsigned s)
172 uint64_t taia2millisec(const struct taia *t)
174 return (t->sec.x * 1000) + (t->nano / 1000000);
177 void iopause(iopause_fd *x, unsigned len, struct taia *deadline, struct taia *stamp)
182 if (taia_less(deadline, stamp))
188 taia_sub(&t, deadline, &t);
189 millisecs = m = taia2millisec(&t);
190 if (m > 1000) millisecs = 1000;
194 for (i = 0; i < len; ++i)
197 poll(x, len, millisecs);
198 /* XXX: some kernels apparently need x[0] even if len is 0 */
199 /* XXX: how to handle EAGAIN? are kernels really this dumb? */
200 /* XXX: how to handle EINVAL? when exactly can this happen? */
206 return flock(fd,LOCK_EX);
209 int lock_exnb(int fd)
211 return flock(fd,LOCK_EX | LOCK_NB);
214 int open_append(const char *fn)
216 return open(fn, O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
219 int open_read(const char *fn)
221 return open(fn, O_RDONLY|O_NDELAY);
224 int open_trunc(const char *fn)
226 return open(fn,O_WRONLY | O_NDELAY | O_TRUNC | O_CREAT,0644);
229 int open_write(const char *fn)
231 return open(fn, O_WRONLY|O_NDELAY);
234 unsigned pmatch(const char *p, const char *s, unsigned len)
241 if (!(c = *p)) return 1;
249 if ((c = *p++) != *s) return 0;
259 if (*s != '?') return 0;
267 if (*s != c) return 0;