1
0
src/Wip/id/id.c
2023-12-31 10:46:23 -07:00

67 lines
1.2 KiB
C

#include <limits.h> /* LOGIN_NAME_MAX */
#include <stdbool.h>
#include <stdio.h> /* fprintf(3) */
#include <stdlib.h> /* stderr, stdout */
#include <sysexits.h>
#include <unistd.h> /* getuid(2), geteuid(2), getopt(3) */
#include "usefulmacros.h"
int main(int argc, char *argv[]){
int c;
char mode;
bool n;
char name[LOGIN_NAME_MAX];
bool r;
uid_t euid;
uid_t uid;
NOARGVZERO(argv);
mode = 0;
n = false;
r = false;
while((c = getopt(argc, argv, "Gghnru")) != -1)
switch(c){
case 'G': case 'g': case 'u':
if(mode == 0){
mode = c;
break;
}
/* FALLTHROUGH */
case 'h': default: usage:
fprintf(stderr, "Usage: %s [-G]|[-g]|[-u] (-rn) (user)\n", argv[0]);
return EX_USAGE;
case 'n':
n = true;
break;
case 'r':
r = true;
break;
}
switch(mode){
case 0:
goto usage;
case 'G':
case 'g':
return EX_SOFTWARE;
case 'u':
euid = geteuid();
uid = getuid();
if(!n){
if(r || euid != uid)
fprintf(stdout, "%u\n", uid);
if(!r)
fprintf(stdout, "%u\n", euid);
}else
{}
/* Both busybox and GNU coreutils (according to straces) parse passwd(5)
* for this. TODO. */
break;
default:
return EX_SOFTWARE;
}
return EX_OK;
}