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 */
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));
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
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
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)
--- /dev/null
+#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;
+}
--- /dev/null
+#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;
+}
-
#include <errno.h>
#include <sys/stat.h>
#include <dirent.h>
#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)
{
{
closedir(fp);
errno = noerr;
- return name;
+ return 0;
}
}
closedir(fp);
errno = noerr;
- return 0;
+ return -1;
}