From: Alan Cox Date: Thu, 22 Oct 2015 10:58:03 +0000 (+0100) Subject: libc: add a few more missing bits X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=b6e46fb325539bc4e65f5dfa804b474406557567;p=FUZIX.git libc: add a few more missing bits --- diff --git a/Library/include/stdio.h b/Library/include/stdio.h index 6ac79386..d5c6cf78 100644 --- a/Library/include/stdio.h +++ b/Library/include/stdio.h @@ -147,4 +147,7 @@ extern void rewind __P((FILE *fp)); extern FILE *popen __P((const char *, const char *)); extern int pclose __P((FILE *)); +extern char *cuserid __P((char *__buf)); +#define L_cuserid 9 + #endif /* __STDIO_H */ diff --git a/Library/include/unistd.h b/Library/include/unistd.h index 0348debd..41057750 100644 --- a/Library/include/unistd.h +++ b/Library/include/unistd.h @@ -38,7 +38,10 @@ extern int execve __P((const char *pathname, char * const argv[], char * const e extern int execvp __P((const char *pathname, char *const argv[])); extern int execvpe __P((const char *pathname, char *const argv[], char * const envp[])); -extern char *ttyname __P((int)); +extern char *ttyname __P((int __fd)); +extern int ttyname_r __P((int __fd, char *__buf, size_t __size)); +extern char *getlogin __P((void)); +extern int getlogin_r __P((char * __buf, size_t __size)); extern int system __P((const char *)); extern int pause __P((void)); extern pid_t fork __P((void)); diff --git a/Library/libs/Makefile b/Library/libs/Makefile index 3c280f96..08abd09d 100644 --- a/Library/libs/Makefile +++ b/Library/libs/Makefile @@ -22,12 +22,13 @@ OBJ_ASM = $(SRC_ASM:.s=.rel) SRC_C = __argv.c abort.c asctime.c assert.c atexit.c atoi_small.c SRC_C += bcmp.c bcopy.c bsearch.c bzero.c calloc.c cfree.c clock.c closedir.c SRC_C += clock_gettime.c clock_getres.c clock_settime.c -SRC_C += creat.c crypt.c ctime.c difftime.c err.c errno.c error.c +SRC_C += creat.c crypt.c ctime.c cuserid.c difftime.c err.c errno.c error.c SRC_C += execl.c execv.c execvp.c exit.c SRC_C += fclose.c fflush.c fgetc.c fgetgrent.c fgetpwent.c SRC_C += fgetpos.c fgets.c fopen.c fork.c fprintf.c fputc.c fputs.c fread.c SRC_C += free.c fsetpos.c ftell.c fwrite.c getcwd.c -SRC_C += getenv.c __getgrent.c getgrgid.c getgrnam.c getloadavg.c getopt.c +SRC_C += getenv.c __getgrent.c getgrgid.c getgrnam.c getloadavg.c getlogin.c +SRC_C += getopt.c SRC_C += getpw.c __getpwent.c getpwnam.c getpwuid.c gets.c gettimeofday.c SRC_C += gmtime.c gmtime_r.c grent.c index.c isatty.c killpg.c SRC_C += libintl.c diff --git a/Library/libs/Makefile.6809 b/Library/libs/Makefile.6809 index 4880e9a3..5fc1115d 100644 --- a/Library/libs/Makefile.6809 +++ b/Library/libs/Makefile.6809 @@ -14,12 +14,13 @@ OBJ_ASM = $(SRC_ASM:.s=.o) SRC_C = __argv.c abort.c asctime.c assert.c atexit.c SRC_C += bcmp.c bcopy.c bsearch.c bzero.c calloc.c cfree.c clock.c closedir.c SRC_C += clock_gettime.c clock_getres.c clock_settime.c -SRC_C += creat.c crypt.c ctime.c difftime.c err.c errno.c error.c +SRC_C += creat.c crypt.c ctime.c cuserid.c difftime.c err.c errno.c error.c SRC_C += execl.c execv.c execvp.c exit.c SRC_C += fclose.c fflush.c fgetc.c fgetgrent.c fgetpwent.c SRC_C += fgetpos.c fgets.c fopen.c fork.c fprintf.c fputc.c fputs.c fread.c SRC_C += free.c fsetpos.c ftell.c fwrite.c getcwd.c -SRC_C += getenv.c __getgrent.c getgrgid.c getgrnam.c getloadavg.c getopt.c +SRC_C += getenv.c __getgrent.c getgrgid.c getgrnam.c getloadavg.c getlogin.c +SRC_C += getopt.c SRC_C += getpw.c __getpwent.c getpwnam.c getpwuid.c gets.c gettimeofday.c SRC_C += gmtime.c gmtime_r.c grent.c index.c isatty.c killpg.c SRC_C += libintl.c diff --git a/Library/libs/TODO b/Library/libs/TODO index a7b3c3b0..fce09f5a 100644 --- a/Library/libs/TODO +++ b/Library/libs/TODO @@ -48,7 +48,7 @@ sys/shm.h signal.h sigaction bits need defining plus SA_NOCLDSTOP sys/statfs need to add fakery for it -stdio _NFILE, L_ctermid, L_cuserid, P_tmpdir, L_tmpnam +stdio _NFILE, L_ctermid, P_tmpdir, L_tmpnam stropts.h (streams) diff --git a/Library/libs/cuserid.c b/Library/libs/cuserid.c new file mode 100644 index 00000000..e82ea9dc --- /dev/null +++ b/Library/libs/cuserid.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include + +/* Legacy username query: Do not use this function in new (or old ;-) ) code. */ + + +static char cbuf[L_cuserid]; + +char *cuserid(char *buf) +{ + /* FIXME: need a getpwuid_r to do this threadsafe */ + struct passwd *p = getpwuid(getuid()); + if (p == NULL) + return NULL; + if (buf == NULL) + buf = cbuf; + strlcpy(buf, p->pw_name, L_cuserid); + return buf; +} diff --git a/Library/libs/getlogin.c b/Library/libs/getlogin.c new file mode 100644 index 00000000..741f784c --- /dev/null +++ b/Library/libs/getlogin.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +#include + +static char tmp[L_cuserid]; + +char *getlogin(void) +{ + if (getlogin_r(tmp, sizeof(tmp))) + return NULL; + return tmp; +} + +int getlogin_r(char *buf, size_t len) +{ + char tty[UT_LINESIZE + 6]; + struct utmp ut, *utp; + int err; + int fd = open("/dev/tty", O_RDONLY|O_NOCTTY); + + if (fd == -1) + return -1; + + if (len > UT_NAMESIZE + 1) + len = UT_NAMESIZE + 1; + + err = ttyname_r(fd, buf, sizeof(tty)); + close(fd); + if (err) + return -1; + /* Skip the /dev/ */ + strncpy(ut.ut_line, tty + 5, sizeof(ut.ut_line)); + ut.ut_type = USER_PROCESS; + setutent(); + /* FIXME: move to getutline_r once we have it */ + utp = getutline(&ut); + endutent(); + if (utp == NULL) { + errno = ENOENT; + return -1; + } + strlcpy(buf, utp->ut_user, len); + return 0; +} diff --git a/Library/libs/ttyname.c b/Library/libs/ttyname.c index 674aee30..bcd086b8 100644 --- a/Library/libs/ttyname.c +++ b/Library/libs/ttyname.c @@ -1,4 +1,3 @@ - #include #include #include @@ -6,28 +5,35 @@ #include static char dev[] = "/dev"; +static char name[16]; /* should be MAXNAMLEN but that's overkill */ char *ttyname(int fd) +{ + if (ttyname_r(fd, name, sizeof(name))) + return NULL; + return name; +} + +int ttyname_r(int fd, char *buf, size_t len) { struct stat st, dst; DIR *fp; struct dirent *d; - static char name[16]; /* should be MAXNAMLEN but that's overkill */ int noerr = errno; if (fstat(fd, &st) < 0) - return 0; + return -1; if (!isatty(fd)) { errno = ENOTTY; - return 0; + return -1; } fp = opendir(dev); if (fp == 0) - return 0; - strcpy(name, dev); - strcat(name, "/"); + return -1; + strlcpy(buf, dev, len); + strlcat(buf, "/", len); while ((d = readdir(fp)) != 0) { @@ -39,10 +45,10 @@ char *ttyname(int fd) { closedir(fp); errno = noerr; - return name; + return 0; } } closedir(fp); errno = noerr; - return 0; + return -1; }