getlogin.c 805 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* $Id$ */
  2. #define UTMPFILE "/etc/utmp"
  3. #ifdef __USG
  4. struct utmp {
  5. char ut_name[8];
  6. char ut_id[4];
  7. char ut_line[12];
  8. short ut_pid;
  9. short ut_type;
  10. struct exit_status {
  11. short e_termination;
  12. short e_exit;
  13. } ut_exit;
  14. long ut_time;
  15. };
  16. #else
  17. struct utmp {
  18. char ut_line[8];
  19. char ut_name[8];
  20. #ifdef __BSD4_2
  21. char ut_host[16];
  22. #endif
  23. long ut_time;
  24. };
  25. #endif
  26. char *
  27. getlogin()
  28. {
  29. struct utmp ut;
  30. static char name[sizeof(ut.ut_name) + 1];
  31. int slotno = ttyslot();
  32. int fd;
  33. register char *p, *q;
  34. if (! slotno || !(fd = open(UTMPFILE, 0))) return 0;
  35. lseek(fd, (long) slotno * sizeof(ut), 0);
  36. if (read(fd, (char *) &ut, sizeof(ut)) < sizeof(ut)) return 0;
  37. close(fd);
  38. ut.ut_name[sizeof(ut.ut_name)] = ' ';
  39. p = ut.ut_name;
  40. q = name;
  41. while (*p != ' ') *q++ = *p++;
  42. *q = '\0';
  43. return name;
  44. }