libc: add a few more missing bits
authorAlan Cox <alan@etchedpixels.co.uk>
Thu, 22 Oct 2015 10:58:03 +0000 (11:58 +0100)
committerAlan Cox <alan@etchedpixels.co.uk>
Thu, 22 Oct 2015 10:58:03 +0000 (11:58 +0100)
Library/include/stdio.h
Library/include/unistd.h
Library/libs/Makefile
Library/libs/Makefile.6809
Library/libs/TODO
Library/libs/cuserid.c [new file with mode: 0644]
Library/libs/getlogin.c [new file with mode: 0644]
Library/libs/ttyname.c

index 6ac7938..d5c6cf7 100644 (file)
@@ -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 */
index 0348deb..4105775 100644 (file)
@@ -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));
index 3c280f9..08abd09 100644 (file)
@@ -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
index 4880e9a..5fc1115 100644 (file)
@@ -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
index a7b3c3b..fce09f5 100644 (file)
@@ -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 (file)
index 0000000..e82ea9d
--- /dev/null
@@ -0,0 +1,21 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <pwd.h>
+
+/* 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 (file)
index 0000000..741f784
--- /dev/null
@@ -0,0 +1,47 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <fcntl.h>
+#include <utmp.h>
+#include <errno.h>
+
+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;
+}
index 674aee3..bcd086b 100644 (file)
@@ -1,4 +1,3 @@
-
 #include <errno.h>
 #include <sys/stat.h>
 #include <dirent.h>
@@ -6,28 +5,35 @@
 #include <string.h>
 
 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;
 }