home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib25.zoo / getlogin.c < prev    next >
C/C++ Source or Header  |  1992-09-05  |  1KB  |  43 lines

  1. /* getlogin: return the user's login name.
  2.  * Written by Eric R. Smith and placed in the public domain.
  3.  */
  4.  
  5. #include <pwd.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <memory.h>
  9. #include <unistd.h>
  10.  
  11. static char *logname = NULL;
  12.  
  13. char *getlogin()
  14. {
  15.         struct passwd *temp;
  16.     char tmplogname[80];
  17.  
  18.     if(logname != NULL)
  19.         return logname;
  20.  
  21.     *tmplogname = '\0';
  22.  
  23. /* first try the /etc/passwd file */
  24.         temp = getpwuid(getuid());
  25.         if (temp) {
  26.           strncpy(tmplogname, temp->pw_name, (size_t)79);
  27.       tmplogname[79] = '\0';
  28.     }
  29.  
  30. /* if that didn't work, try the environment */
  31.         if (!*tmplogname && getenv("USER")) {
  32.                 strncpy(tmplogname, getenv("USER"), (size_t)79);
  33.         tmplogname[79] = '\0';
  34.         }
  35.  
  36. /* finally, give up */
  37.         if (!*tmplogname)
  38.                 strcpy(tmplogname, "user");
  39.     if((logname = (char *)malloc((size_t)(strlen(tmplogname)+1))) == NULL)
  40.         return (char *)NULL;
  41.         return strcpy(logname, tmplogname);
  42. }
  43.