home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
GEMini Atari
/
GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso
/
zip
/
mint
/
mntlib16.lzh
/
MNTLIB16
/
GETLOGIN.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-03
|
1KB
|
48 lines
/* getlogin: return the user's login name.
* Written by Eric R. Smith and placed in the public domain.
*/
#include <pwd.h>
#include <string.h>
#include <stdlib.h>
#include <memory.h>
#include <unistd.h>
#ifdef __GNUC__
#define alloca __builtin_alloca
#endif
static char *logname = NULL;
char *getlogin()
{
struct passwd *temp;
char *tmplogname;
if(logname != NULL)
return logname;
tmplogname = (char *)alloca((size_t)80);
*tmplogname = '\0';
/* first try the /etc/passwd file */
temp = getpwuid(getuid());
if (temp) {
strncpy(tmplogname, temp->pw_name, (size_t)79);
tmplogname[79] = '\0';
}
/* if that didn't work, try the environment */
if (!*tmplogname && getenv("USER")) {
strncpy(tmplogname, getenv("USER"), (size_t)79);
tmplogname[79] = '\0';
}
/* finally, give up */
if (!*tmplogname)
strcpy(tmplogname, "user");
if((logname = (char *)malloc((size_t)(strlen(tmplogname)+1))) == NULL)
return (char *)NULL;
return strcpy(logname, tmplogname);
}