1
0

add more features

This commit is contained in:
dtb
2022-10-03 02:00:38 -04:00
parent 63236f9e36
commit 5c9cda563c
5 changed files with 113 additions and 10 deletions

66
id/id.c Normal file
View File

@@ -0,0 +1,66 @@
#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;
}