aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKlaatu <[email protected]>2015-05-17 15:33:21 +1200
committerKlaatu <[email protected]>2015-05-17 15:33:21 +1200
commitb0de699679e8f1e39af847ed172d1ba605b4370c (patch)
tree01dac00471d61f727394e508c613b29cff0ceae5 /lib
bulk upload of source
Diffstat (limited to 'lib')
-rw-r--r--lib/do_wtmp.c11
-rw-r--r--lib/err.c39
-rw-r--r--lib/err_b.c5
-rw-r--r--lib/errmsg_argv0.c1
-rw-r--r--lib/errmsg_put.c20
-rw-r--r--lib/errmsg_puts.c5
-rw-r--r--lib/error_string.c16
-rw-r--r--lib/fu.c16
-rw-r--r--lib/nano_sleep.c6
-rw-r--r--lib/open_tmpfd.c18
-rw-r--r--lib/pathexec_run.c43
-rw-r--r--lib/read_header.c18
-rw-r--r--lib/read_ulongs.c8
-rw-r--r--lib/scan_sec.c23
-rw-r--r--lib/scan_ulongs.c17
-rw-r--r--lib/skip_comments.c11
-rw-r--r--lib/splitmem.c18
-rw-r--r--lib/strpbrk.c10
-rw-r--r--lib/strstr.c16
-rw-r--r--lib/utmp_io.c29
-rw-r--r--lib/x_atoi.c12
21 files changed, 342 insertions, 0 deletions
diff --git a/lib/do_wtmp.c b/lib/do_wtmp.c
new file mode 100644
index 0000000..1799895
--- /dev/null
+++ b/lib/do_wtmp.c
@@ -0,0 +1,11 @@
+#include <unistd.h>
+#include <fcntl.h>
+#include <utmp.h>
+
+void do_wtmp(struct utmp *utmp) /*EXTRACT_INCL*/ {
+ int fd;
+ if ((fd=open(_PATH_WTMP, O_WRONLY | O_APPEND)) >= 0) {
+ write(fd, utmp, sizeof(struct utmp));
+ close(fd);
+ }
+}
diff --git a/lib/err.c b/lib/err.c
new file mode 100644
index 0000000..fd4218e
--- /dev/null
+++ b/lib/err.c
@@ -0,0 +1,39 @@
+#include <stdarg.h>
+#include "../ninitfeatures.h"
+#include "../djb/buffer.h"
+
+#if 0
+void err(int fd, const char *m, ...) /*EXTRACT_INCL*/
+#endif
+
+#ifdef ERRMSG_BUFFER
+#define P(S) buffer_puts(X,S)
+#define E() err_b(buffer *X, const char *m, ...)
+
+#else
+#define P(S) errmsg_puts(X,S)
+#define E() err(int X, const char *m, ...)
+#endif
+
+extern const char *errmsg_argv0;
+
+void E() {
+ const char *s=m;
+ va_list a;
+ va_start(a,m);
+
+ if (errmsg_argv0) {
+ P(errmsg_argv0);
+ P(": ");
+ }
+
+ while (s) {
+ P(s);
+ s=va_arg(a,const char*);
+ }
+ P("\n");
+#ifndef ERRMSG_BUFFER
+ P(0);
+#endif
+ va_end(a);
+}
diff --git a/lib/err_b.c b/lib/err_b.c
new file mode 100644
index 0000000..2de7017
--- /dev/null
+++ b/lib/err_b.c
@@ -0,0 +1,5 @@
+#define ERRMSG_BUFFER
+#include "err.c"
+#if 0
+void err_b(buffer *b, const char *m, ...) /*EXTRACT_INCL*/
+#endif
diff --git a/lib/errmsg_argv0.c b/lib/errmsg_argv0.c
new file mode 100644
index 0000000..ef63ea9
--- /dev/null
+++ b/lib/errmsg_argv0.c
@@ -0,0 +1 @@
+const char *errmsg_argv0=0;
diff --git a/lib/errmsg_put.c b/lib/errmsg_put.c
new file mode 100644
index 0000000..9afe0ce
--- /dev/null
+++ b/lib/errmsg_put.c
@@ -0,0 +1,20 @@
+#include <sys/uio.h>
+#include "../ninitfeatures.h"
+
+#ifndef ERRMSG_PUTS_LEN
+#define ERRMSG_PUTS_LEN 15
+#endif
+
+void errmsg_put(int fd, const char *buf, unsigned int len) /*EXTRACT_INCL*/ {
+ static struct iovec errmsg_iov[ERRMSG_PUTS_LEN];
+ static int k;
+ if (buf==0 || k==ERRMSG_PUTS_LEN) {
+ if (fd>=0) writev(fd,errmsg_iov,k);
+ k = 0;
+ }
+ if (buf && len) {
+ errmsg_iov[k].iov_base = (char *)buf;
+ errmsg_iov[k].iov_len = len;
+ k++;
+ }
+}
diff --git a/lib/errmsg_puts.c b/lib/errmsg_puts.c
new file mode 100644
index 0000000..41c3b88
--- /dev/null
+++ b/lib/errmsg_puts.c
@@ -0,0 +1,5 @@
+#include "../ninitfeatures.h"
+void errmsg_puts(int fd, const char *buf) /*EXTRACT_INCL*/ {
+ if (buf==0) { errmsg_put(fd, buf, 0); return; }
+ if (buf[0]) errmsg_put(fd, buf, str_len(buf));
+}
diff --git a/lib/error_string.c b/lib/error_string.c
new file mode 100644
index 0000000..d7fd210
--- /dev/null
+++ b/lib/error_string.c
@@ -0,0 +1,16 @@
+#include "../ninitfeatures.h"
+#if 0
+struct error_table { int n; char *s; }; /*EXTRACT_UNMOD*/
+#endif
+
+char *error_string(struct error_table *table, int n) /*EXTRACT_INCL*/ {
+ static char y[28];
+ char *x=y;
+ for (; table->s; table++)
+ if (table->n == n) return table->s;
+
+ x += str_copy(x,"error=");
+ x += fmt_ulong(x,n);
+ *x = 0;
+ return y;
+}
diff --git a/lib/fu.c b/lib/fu.c
new file mode 100644
index 0000000..074007d
--- /dev/null
+++ b/lib/fu.c
@@ -0,0 +1,16 @@
+#include "../int_defs.h"
+
+static char fu_buffer[7*12];
+/* format up to 6 numbers */
+
+char *fu(uint32t u) /*EXTRACT_INCL*/ {
+ static char k=7;
+ char *p = fu_buffer + 12*k;
+ *--p = 0;
+ do {
+ *--p = '0' + (u%10);
+ u /= 10;
+ } while (u);
+ if (--k == 1) k=7;
+ return p;
+}
diff --git a/lib/nano_sleep.c b/lib/nano_sleep.c
new file mode 100644
index 0000000..6da74a0
--- /dev/null
+++ b/lib/nano_sleep.c
@@ -0,0 +1,6 @@
+#include "../int_defs.h"
+#include <time.h>
+void nano_sleep(uint32t sec, uint32t nsec) /*EXTRACT_INCL*/ {
+ struct timespec ts = { sec, nsec };
+ nanosleep(&ts, 0);
+}
diff --git a/lib/open_tmpfd.c b/lib/open_tmpfd.c
new file mode 100644
index 0000000..e94bf32
--- /dev/null
+++ b/lib/open_tmpfd.c
@@ -0,0 +1,18 @@
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include "../ninitfeatures.h"
+
+int open_tmpfd(char *target, char *tmp, int mode) /*EXTRACT_INCL*/ {
+ unsigned long len;
+ char *x;
+ int fd;
+ for (len=0 ;; len++) {
+ x = tmp + str_copy(tmp, target);
+ if (len) x += fmt_ulong(x,len);
+ x[0] = '~'; x[1] = 0;
+ fd = open(tmp, O_WRONLY|O_CREAT|O_EXCL, mode);
+ if (fd >=0 || errno != EEXIST) break;
+ }
+ return fd;
+}
diff --git a/lib/pathexec_run.c b/lib/pathexec_run.c
new file mode 100644
index 0000000..2e369e1
--- /dev/null
+++ b/lib/pathexec_run.c
@@ -0,0 +1,43 @@
+/* again DJB; see http://cr.yp.to */
+#include <unistd.h>
+#include <errno.h>
+#include <alloca.h>
+#include <stdlib.h>
+
+#include "../ninitfeatures.h"
+
+void pathexec_run(char *file,char **argv,char **envp) /*EXTRACT_INCL*/ {
+ char *path,*tmp;
+ int savederrno=0;
+ unsigned int len, next, file_len;
+
+ if (file[str_chr(file,'/')]) { execve(file,argv,envp); return; }
+ file_len = str_len(file) + 1;
+
+ path = env_get("PATH");
+ if (!path) path = "/bin:/usr/bin";
+ tmp = alloca(4 + str_len(path) + file_len);
+ if (!tmp) { errno=ENOMEM; return; }
+
+ for (;;) {
+ next = str_chr(path,':');
+ len = next;
+
+ if (next==0) { tmp[0] = '.'; len = 1; }
+ else { byte_copy(tmp,next,path); }
+ tmp[len] = '/';
+ byte_copy(tmp+len+1, file_len, file);
+
+ execve(tmp,argv,envp);
+ if (errno != ENOENT) {
+ savederrno = errno;
+ if ((errno != EACCES) && (errno != ENOEXEC) && (errno != ENOTDIR)) return;
+ }
+
+ if (path[next]==0) {
+ if (savederrno) errno = savederrno;
+ return;
+ }
+ path += (next+1);
+ }
+}
diff --git a/lib/read_header.c b/lib/read_header.c
new file mode 100644
index 0000000..4a78b23
--- /dev/null
+++ b/lib/read_header.c
@@ -0,0 +1,18 @@
+#include <unistd.h>
+#include <fcntl.h>
+
+#define MEM_BUF 160
+
+char *read_header(const char *name) /*EXTRACT_INCL*/ {
+ static char buf[MEM_BUF+1];
+ int fd, k;
+
+ fd =open(name, O_RDONLY);
+ if (fd < 0) return 0;
+ k =read(fd, buf, MEM_BUF);
+ close (fd);
+ if (k < 0) return 0;
+
+ buf[k] = 0;
+ return buf;
+}
diff --git a/lib/read_ulongs.c b/lib/read_ulongs.c
new file mode 100644
index 0000000..90720ff
--- /dev/null
+++ b/lib/read_ulongs.c
@@ -0,0 +1,8 @@
+#include "../ninitfeatures.h"
+
+int read_ulongs(char *name, unsigned long *u, int len) /*EXTRACT_INCL*/ {
+ char *x = read_header(name);
+ int dummy;
+ if (x==0) return 0;
+ return scan_ulongs(x, u, len, scan_ulong, ':', &dummy);
+}
diff --git a/lib/scan_sec.c b/lib/scan_sec.c
new file mode 100644
index 0000000..6377399
--- /dev/null
+++ b/lib/scan_sec.c
@@ -0,0 +1,23 @@
+#include "../ninitfeatures.h"
+
+unsigned int scan_sec(const char *src, unsigned long *ul) /*EXTRACT_INCL*/ {
+ unsigned long tmp, u=0;
+ char ch, *s = (char *)src;
+ for (; *s; ) {
+ s += scan_ulong(s, &tmp);
+ ch = *s;
+ if (ch > 96) ch -= 32; /* upper case */
+
+ switch (ch) {
+ case 'W': tmp *= 10080; s++; break;
+ case 'D': tmp *= 1440; s++; break;
+ case 'H': tmp *= 60; s++; break;
+ case 0: break;
+ default: u += tmp; goto ready;
+ }
+ u += tmp;
+ }
+ ready:
+ *ul = u * 60;
+ return s-src;
+}
diff --git a/lib/scan_ulongs.c b/lib/scan_ulongs.c
new file mode 100644
index 0000000..db5631f
--- /dev/null
+++ b/lib/scan_ulongs.c
@@ -0,0 +1,17 @@
+#include "../ninitfeatures.h"
+
+unsigned int scan_ulongs(char *src, unsigned long *u, int len, unsigned int (*op)(), char sep, int *read_len) /*EXTRACT_INCL*/ {
+ int j, k;
+ char *p=src;
+
+ for (k=0; k<len;) {
+ j = op(p, u+k); if (j==0) break;
+ ++k;
+ p += j;
+ if (*p != sep) break;
+ ++p;
+ }
+
+ *read_len = (p-src);
+ return k;
+}
diff --git a/lib/skip_comments.c b/lib/skip_comments.c
new file mode 100644
index 0000000..fd0bdde
--- /dev/null
+++ b/lib/skip_comments.c
@@ -0,0 +1,11 @@
+void skip_comments(char **s) /*EXTRACT_INCL*/ {
+ char **d, *p;
+ for (d=s; (p=*s); s++) {
+ if (p[0] == '#') {
+ if (p[1] == '#') ++p;
+ else continue;
+ }
+ *d++ = p;
+ }
+ *d=0;
+}
diff --git a/lib/splitmem.c b/lib/splitmem.c
new file mode 100644
index 0000000..f5d13aa
--- /dev/null
+++ b/lib/splitmem.c
@@ -0,0 +1,18 @@
+unsigned int splitmem(char **v, char *s, char c) /*EXTRACT_INCL*/ {
+ if (v) {
+ char **w=v;
+ *w++=s;
+ for (;;) {
+ while (*s && *s!=c) s++;
+ if (*s==0) break;
+ *s=0;
+ *w++ = ++s;
+ }
+ *w=0;
+ return (w-v);
+ } else {
+ unsigned int n=1;
+ for (; *s; s++) if (*s==c) n++;
+ return n;
+ }
+}
diff --git a/lib/strpbrk.c b/lib/strpbrk.c
new file mode 100644
index 0000000..9f39fc3
--- /dev/null
+++ b/lib/strpbrk.c
@@ -0,0 +1,10 @@
+#include "../ninitfeatures.h"
+
+char *strpbrk(const char *s, const char *accept) /*EXTRACT_INCL*/ {
+ register int i,l=str_len(accept);
+ for (; *s; s++)
+ for (i=0; i<l; i++)
+ if (*s == accept[i])
+ return (char*)s;
+ return 0;
+}
diff --git a/lib/strstr.c b/lib/strstr.c
new file mode 100644
index 0000000..39e30e5
--- /dev/null
+++ b/lib/strstr.c
@@ -0,0 +1,16 @@
+#include "../ninitfeatures.h"
+
+char *strstr(const char *haystack, const char *needle) /*EXTRACT_INCL*/ {
+ unsigned int nl=str_len(needle);
+ unsigned int hl=str_len(haystack);
+ int i;
+ if (!nl) goto found;
+
+ for (i=hl-nl+1; i>0; --i) {
+ if (*haystack==*needle && !byte_diff(haystack,nl,needle))
+found:
+ return (char*)haystack;
+ ++haystack;
+ }
+ return 0;
+}
diff --git a/lib/utmp_io.c b/lib/utmp_io.c
new file mode 100644
index 0000000..1d8c423
--- /dev/null
+++ b/lib/utmp_io.c
@@ -0,0 +1,29 @@
+#include <unistd.h>
+#include <fcntl.h>
+#include <utmp.h>
+
+#define UTMP_SIZE (sizeof(struct utmp))
+
+/* type: F_RDLCK or F_WRLCK */
+struct utmp *utmp_io(int fd, struct utmp *ut, int type) /*EXTRACT_INCL*/ {
+ int ret;
+ struct flock fl;
+ int (*op)() = (type==F_WRLCK) ? (int(*)())write : (int(*)())read;
+
+ fl.l_whence = SEEK_CUR;
+ fl.l_start = 0;
+ fl.l_len = UTMP_SIZE;
+ fl.l_pid = 0;
+ fl.l_type = type;
+
+ if (fcntl(fd, F_SETLKW, &fl)) return 0;
+ ret = op(fd, ut, UTMP_SIZE);
+
+ fl.l_start = -UTMP_SIZE;
+ fl.l_type = F_UNLCK;
+
+ fcntl(fd, F_SETLK, &fl);
+
+ if (ret != UTMP_SIZE) return 0;
+ return ut;
+}
diff --git a/lib/x_atoi.c b/lib/x_atoi.c
new file mode 100644
index 0000000..2f6a5fb
--- /dev/null
+++ b/lib/x_atoi.c
@@ -0,0 +1,12 @@
+
+int x_atoi(const char *src) /*EXTRACT_INCL*/ {
+ register const char *s;
+ register long int dest=0;
+ register unsigned char c;
+
+ s=src;
+ if (*s=='-' /* || *s=='+' */) ++s;
+
+ while ((c=*s-'0')<10) { ++s; dest=dest*10 + c; }
+ return (*src=='-') ? -dest : dest;
+}