Путь: Toys/POSIX, команды версии: Ver.4 Ver.9 Комментарии в файле id.c : Команд: 5 id
id_z
groups
logname
whoami
Исходный текст в файле id.c #define FOR_id #define FORCE_FLAGS #include "toys.h" GLOBALS( int is_groups; ) static void showone(char *prefix, char *s, unsigned u, int done) { if (FLAG(n)) printf("%s%s", prefix, s); else printf("%s%u", prefix, u); if (done) { xputc('\n'); xexit(); } } static void showid(char *prefix, unsigned u, char *s) { printf("%s%u(%s)", prefix, u, s); } static void do_id(char *username) { struct passwd *pw; struct group *grp; uid_t uid = getuid(), euid = geteuid(); gid_t gid = getgid(), egid = getegid(); gid_t *groups = (gid_t *)toybuf; int i = sizeof(toybuf)/sizeof(gid_t), ngroups; // check if a username is given if (username) { pw = getpwnam(username); if (!pw) { uid = atolx_range(username, 0, INT_MAX); if ((pw = getpwuid(uid))) username = pw->pw_name; } if (!pw) error_exit("no such user '%s'", username); uid = euid = pw->pw_uid; gid = egid = pw->pw_gid; if (TT.is_groups) printf("%s : ", pw->pw_name); } pw = xgetpwuid(FLAG(r) ? uid : euid); if (FLAG(u)) showone("", pw->pw_name, pw->pw_uid, 1); grp = xgetgrgid(FLAG(r) ? gid : egid); if (FLAG(g)) showone("", grp->gr_name, grp->gr_gid, 1); ngroups = username ? getgrouplist(username, gid, groups, &i) : getgroups(i, groups); if (ngroups<0) perror_exit("getgroups"); if (FLAG(G)) { showone("", grp->gr_name, grp->gr_gid, 0); for (i = 0; i<ngroups; i++) { if (groups[i] != egid) { if ((grp=getgrgid(groups[i]))) showone(" ",grp->gr_name,grp->gr_gid,0); else printf(" %u", groups[i]); } } xputc('\n'); return; } if (!FLAG(Z)) { showid("uid=", pw->pw_uid, pw->pw_name); showid(" gid=", grp->gr_gid, grp->gr_name); if (!FLAG(r)) { if (uid != euid) { pw = xgetpwuid(euid); showid(" euid=", pw->pw_uid, pw->pw_name); } if (gid != egid) { grp = xgetgrgid(egid); showid(" egid=", grp->gr_gid, grp->gr_name); } } showid(" groups=", gid, grp->gr_name); for (i = 0; i<ngroups; i++) { if (groups[i] != egid) { if ((grp=getgrgid(groups[i]))) showid(",", grp->gr_gid, grp->gr_name); else printf(",%u", groups[i]); } } } if (!CFG_TOYBOX_LSM_NONE) { if (lsm_enabled()) { char *context = lsm_context(); printf("%s%s", FLAG(Z) ? "" : " context=", context); if (CFG_TOYBOX_FREE) free(context); } else if (FLAG(Z)) error_exit("%s disabled", lsm_name()); } xputc('\n'); } void id_main(void) { if (toys.optc) while(*toys.optargs) do_id(*toys.optargs++); else do_id(NULL); } void groups_main(void) { TT.is_groups = 1; toys.optflags = FLAG_G|FLAG_n; id_main(); } void logname_main(void) { toys.optflags = FLAG_u|FLAG_n; id_main(); } |