diff options
Diffstat (limited to 'src/legacy')
-rw-r--r-- | src/legacy/cuserid.c | 14 | ||||
-rw-r--r-- | src/legacy/daemon.c | 33 | ||||
-rw-r--r-- | src/legacy/err.c | 60 | ||||
-rw-r--r-- | src/legacy/ftw.c | 12 | ||||
-rw-r--r-- | src/legacy/futimes.c | 13 | ||||
-rw-r--r-- | src/legacy/getdtablesize.c | 9 | ||||
-rw-r--r-- | src/legacy/getpagesize.c | 7 | ||||
-rw-r--r-- | src/legacy/getpass.c | 39 | ||||
-rw-r--r-- | src/legacy/getusershell.c | 33 | ||||
-rw-r--r-- | src/legacy/isastream.c | 7 | ||||
-rw-r--r-- | src/legacy/lutimes.c | 13 | ||||
-rw-r--r-- | src/legacy/ulimit.c | 19 | ||||
-rw-r--r-- | src/legacy/utmpx.c | 43 |
13 files changed, 302 insertions, 0 deletions
diff --git a/src/legacy/cuserid.c b/src/legacy/cuserid.c new file mode 100644 index 00000000..4e78798d --- /dev/null +++ b/src/legacy/cuserid.c @@ -0,0 +1,14 @@ +#define _GNU_SOURCE +#include <pwd.h> +#include <stdio.h> +#include <unistd.h> + +char *cuserid(char *buf) +{ + struct passwd pw, *ppw; + long pwb[256]; + if (getpwuid_r(geteuid(), &pw, (void *)pwb, sizeof pwb, &ppw)) + return 0; + snprintf(buf, L_cuserid, "%s", pw.pw_name); + return buf; +} diff --git a/src/legacy/daemon.c b/src/legacy/daemon.c new file mode 100644 index 00000000..1568b1dc --- /dev/null +++ b/src/legacy/daemon.c @@ -0,0 +1,33 @@ +#define _GNU_SOURCE +#include <fcntl.h> +#include <unistd.h> + +int daemon(int nochdir, int noclose) +{ + if (!nochdir && chdir("/")) + return -1; + if (!noclose) { + int fd, failed = 0; + if ((fd = open("/dev/null", O_RDWR)) < 0) return -1; + if (dup2(fd, 0) < 0 || dup2(fd, 1) < 0 || dup2(fd, 2) < 0) + failed++; + if (fd > 2) close(fd); + if (failed) return -1; + } + + switch(fork()) { + case 0: break; + case -1: return -1; + default: _exit(0); + } + + if (setsid() < 0) return -1; + + switch(fork()) { + case 0: break; + case -1: return -1; + default: _exit(0); + } + + return 0; +} diff --git a/src/legacy/err.c b/src/legacy/err.c new file mode 100644 index 00000000..0f748538 --- /dev/null +++ b/src/legacy/err.c @@ -0,0 +1,60 @@ +#include <err.h> +#include <stdio.h> +#include <stdarg.h> +#include <stdlib.h> + +void vwarn(const char *fmt, va_list ap) +{ + if (fmt) vfprintf(stderr, fmt, ap); + perror(""); +} + +void vwarnx(const char *fmt, va_list ap) +{ + if (fmt) vfprintf(stderr, fmt, ap); + putc('\n', stderr); +} + +_Noreturn void verr(int status, const char *fmt, va_list ap) +{ + vwarn(fmt, ap); + exit(status); +} + +_Noreturn void verrx(int status, const char *fmt, va_list ap) +{ + vwarnx(fmt, ap); + exit(status); +} + +void warn(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vwarn(fmt, ap); + va_end(ap); +} + +void warnx(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vwarnx(fmt, ap); + va_end(ap); +} + +_Noreturn void err(int status, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + verr(status, fmt, ap); + va_end(ap); +} + +_Noreturn void errx(int status, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + verrx(status, fmt, ap); + va_end(ap); +} diff --git a/src/legacy/ftw.c b/src/legacy/ftw.c new file mode 100644 index 00000000..0429aba4 --- /dev/null +++ b/src/legacy/ftw.c @@ -0,0 +1,12 @@ +#include <ftw.h> +#include "libc.h" + +int ftw(const char *path, int (*fn)(const char *, const struct stat *, int), int fd_limit) +{ + /* The following cast assumes that calling a function with one + * argument more than it needs behaves as expected. This is + * actually undefined, but works on all real-world machines. */ + return nftw(path, (int (*)())fn, fd_limit, FTW_PHYS); +} + +LFS64(ftw); diff --git a/src/legacy/futimes.c b/src/legacy/futimes.c new file mode 100644 index 00000000..f8fd1cde --- /dev/null +++ b/src/legacy/futimes.c @@ -0,0 +1,13 @@ +#include <sys/stat.h> +#include <sys/time.h> +#include <fcntl.h> + +int futimes(int fd, const struct timeval tv[2]) +{ + struct timespec times[2]; + times[0].tv_sec = tv[0].tv_sec; + times[0].tv_nsec = tv[0].tv_usec * 1000; + times[1].tv_sec = tv[1].tv_sec; + times[1].tv_nsec = tv[1].tv_usec * 1000; + return futimens(fd, times); +} diff --git a/src/legacy/getdtablesize.c b/src/legacy/getdtablesize.c new file mode 100644 index 00000000..623a6af3 --- /dev/null +++ b/src/legacy/getdtablesize.c @@ -0,0 +1,9 @@ +#include <limits.h> +#include <sys/resource.h> + +int getdtablesize(void) +{ + struct rlimit rl; + getrlimit(RLIMIT_NOFILE, &rl); + return rl.rlim_max < INT_MAX ? rl.rlim_max : INT_MAX; +} diff --git a/src/legacy/getpagesize.c b/src/legacy/getpagesize.c new file mode 100644 index 00000000..5ede652b --- /dev/null +++ b/src/legacy/getpagesize.c @@ -0,0 +1,7 @@ +#include <unistd.h> +#include <limits.h> + +int getpagesize(void) +{ + return PAGE_SIZE; +} diff --git a/src/legacy/getpass.c b/src/legacy/getpass.c new file mode 100644 index 00000000..d439a2a5 --- /dev/null +++ b/src/legacy/getpass.c @@ -0,0 +1,39 @@ +#include <stdio.h> +#include <string.h> +#include <termios.h> +#include <unistd.h> +#include <fcntl.h> + +char *getpass(const char *prompt) +{ + int fd; + struct termios s, t; + ssize_t l; + static char password[128]; + + if ((fd = open("/dev/tty", O_RDONLY|O_NOCTTY)) < 0) fd = 0; + + tcgetattr(fd, &t); + s = t; + t.c_lflag &= ~(ECHO|ISIG); + t.c_lflag |= ICANON; + t.c_iflag &= ~(INLCR|IGNCR); + t.c_iflag |= ICRNL; + tcsetattr(fd, TCSAFLUSH, &t); + tcdrain(fd); + + fputs(prompt, stderr); + fflush(stderr); + + l = read(fd, password, sizeof password); + if (l >= 0) { + if (l > 0 && password[l-1] == '\n') l--; + password[l] = 0; + } + + tcsetattr(fd, TCSAFLUSH, &s); + + if (fd > 2) close(fd); + + return password; +} diff --git a/src/legacy/getusershell.c b/src/legacy/getusershell.c new file mode 100644 index 00000000..683158c8 --- /dev/null +++ b/src/legacy/getusershell.c @@ -0,0 +1,33 @@ +#define _GNU_SOURCE +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +static const char defshells[] = "/bin/sh\n/bin/csh\n"; + +static char *line; +static size_t linesize; +static FILE *f; + +void endusershell(void) +{ + if (f) fclose(f); + f = 0; +} + +void setusershell(void) +{ + if (!f) f = fopen("/etc/shells", "rb"); + if (!f) f = fmemopen((void *)defshells, sizeof defshells - 1, "rb"); +} + +char *getusershell(void) +{ + ssize_t l; + if (!f) setusershell(); + if (!f) return 0; + l = getline(&line, &linesize, f); + if (l <= 0) return 0; + if (line[l-1]=='\n') line[l-1]=0; + return line; +} diff --git a/src/legacy/isastream.c b/src/legacy/isastream.c new file mode 100644 index 00000000..4dafdb08 --- /dev/null +++ b/src/legacy/isastream.c @@ -0,0 +1,7 @@ +#include <stropts.h> +#include <fcntl.h> + +int isastream(int fd) +{ + return fcntl(fd, F_GETFD) < 0 ? -1 : 0; +} diff --git a/src/legacy/lutimes.c b/src/legacy/lutimes.c new file mode 100644 index 00000000..13dfe4ef --- /dev/null +++ b/src/legacy/lutimes.c @@ -0,0 +1,13 @@ +#include <sys/stat.h> +#include <sys/time.h> +#include <fcntl.h> + +int lutimes(const char *filename, const struct timeval tv[2]) +{ + struct timespec times[2]; + times[0].tv_sec = tv[0].tv_sec; + times[0].tv_nsec = tv[0].tv_usec * 1000; + times[1].tv_sec = tv[1].tv_sec; + times[1].tv_nsec = tv[1].tv_usec * 1000; + return utimensat(AT_FDCWD, filename, times, AT_SYMLINK_NOFOLLOW); +} diff --git a/src/legacy/ulimit.c b/src/legacy/ulimit.c new file mode 100644 index 00000000..1f59e8e6 --- /dev/null +++ b/src/legacy/ulimit.c @@ -0,0 +1,19 @@ +#include <sys/resource.h> +#include <ulimit.h> +#include <stdarg.h> + +long ulimit(int cmd, ...) +{ + struct rlimit rl; + getrlimit(RLIMIT_FSIZE, &rl); + if (cmd == UL_SETFSIZE) { + long val; + va_list ap; + va_start(ap, cmd); + val = va_arg(ap, long); + va_end(ap); + rl.rlim_cur = 512ULL * val; + if (setrlimit(RLIMIT_FSIZE, &rl)) return -1; + } + return rl.rlim_cur / 512; +} diff --git a/src/legacy/utmpx.c b/src/legacy/utmpx.c new file mode 100644 index 00000000..c483e4ed --- /dev/null +++ b/src/legacy/utmpx.c @@ -0,0 +1,43 @@ +#include <utmpx.h> +#include <stddef.h> +#include "libc.h" + +void endutxent(void) +{ +} + +void setutxent(void) +{ +} + +struct utmpx *getutxent(void) +{ + return NULL; +} + +struct utmpx *getutxid(const struct utmpx *ut) +{ + return NULL; +} + +struct utmpx *getutxline(const struct utmpx *ut) +{ + return NULL; +} + +struct utmpx *pututxline(const struct utmpx *ut) +{ + return NULL; +} + +void updwtmpx(const char *f, const struct utmpx *u) +{ +} + +weak_alias(endutxent, endutent); +weak_alias(setutxent, setutent); +weak_alias(getutxent, getutent); +weak_alias(getutxid, getutid); +weak_alias(getutxline, getutline); +weak_alias(pututxline, pututline); +weak_alias(updwtmpx, updwtmp); |