FENIX_coreutils/logname.c

24 lines
644 B
C
Raw Normal View History

#define _POSIX_C_SOURCE 200809L
2020-12-11 07:20:00 +00:00
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
int main(int argc, char * argv[]) {
2022-10-09 16:35:42 +00:00
char * logname = getlogin(); /* get current user's name */
2020-12-11 07:20:00 +00:00
2022-10-09 16:35:42 +00:00
/* If we didn't get their name, find out why. */
2020-12-11 07:20:00 +00:00
if(logname == NULL) {
switch(errno) {
case EMFILE: fprintf(stderr, "%s: all file descriptors open\n", argv[0]);
break;
case ENFILE: fprintf(stderr, "%s: too many files open", argv[0]); break;
default: fprintf(stderr, "%s: other/unknown error", argv[0]);
}
return 1;
}
else {
2022-10-09 16:35:42 +00:00
printf("%s\n", logname); /* Print their name */
2020-12-11 07:20:00 +00:00
return 0;
}
}