namei: users and groups printing Added file owner and group printing switch -u. Signed-off-by: Sami Kerola <kerolasa@xxxxxx> -- Sami Kerola http://www.iki.fi/kerolasa/
diff -u util-linux-ng-2.14.1-rc2/misc-utils/namei.1 util-linux-ng-kerola/misc-utils/namei.1 --- util-linux-ng-2.14.1-rc2/misc-utils/namei.1 2008-05-29 01:01:02.000000000 +0200 +++ util-linux-ng-kerola/misc-utils/namei.1 2008-10-20 16:30:17.000000000 +0200 @@ -6,7 +6,7 @@ namei - follow a pathname until a terminal point is found .SH SYNOPSIS .B namei -.I [-mx] +.I [-mux] .I pathname .I "[ pathname ... ]" .SH DESCRIPTION @@ -43,12 +43,15 @@ the maximum number of symbolic links this system can have has been exceeded. .SH OPTIONS .TP 8 -.B -x -Show mount point directories with a 'D', rather than a 'd'. -.TP 8 .B -m Show the mode bits of each file type in the style of ls(1), for example 'rwxr-xr-x'. +.TP 8 +.B -u +Show owner and group of each file. +.TP 8 +.B -x +Show mount point directories with a 'D', rather than a 'd'. .SH AUTHOR Roger Southwick (rogers@xxxxxxxxxxxxxxxxxx) .SH BUGS diff -u util-linux-ng-2.14.1-rc2/misc-utils/namei.c util-linux-ng-kerola/misc-utils/namei.c --- util-linux-ng-2.14.1-rc2/misc-utils/namei.c 2008-07-29 12:48:38.000000000 +0200 +++ util-linux-ng-kerola/misc-utils/namei.c 2008-10-21 08:11:55.000000000 +0200 @@ -50,6 +50,9 @@ 2007-09-10 Li Zefan <lizf@xxxxxxxxxxxxxx> - added to identify FIFO +2008-10-21 Sami Kerola <kerolasa@xxxxxx> +- added owner and group printing + -------------------------------------------------------------*/ #include <stdio.h> @@ -61,12 +64,15 @@ #include <sys/types.h> #include <sys/stat.h> #include <sys/param.h> +#include <pwd.h> +#include <grp.h> #include "nls.h" #define ERR strerror(errno),errno int symcount; int mflag = 0; +int uflag = 0; int xflag = 0; #ifndef MAXSYMLINKS @@ -80,6 +86,7 @@ static char *pperm(unsigned short); static void namei(char *, int, mode_t *); static void usage(void); +static void uprint(struct stat *); int main(int argc, char **argv) { @@ -98,12 +105,16 @@ if(argc < 2) usage(); - while((c = getopt(argc, argv, "mx")) != -1){ + while((c = getopt(argc, argv, "mux")) != -1){ switch(c){ case 'm': mflag = !mflag; break; + case 'u': + uflag = !uflag; + break; + case 'x': xflag = !xflag; break; @@ -145,7 +156,7 @@ static void usage(void) { - (void)fprintf(stderr,_("usage: namei [-mx] pathname [pathname ...]\n")); + (void)fprintf(stderr,_("usage: namei [-mux] pathname [pathname ...]\n")); exit(1); } @@ -183,9 +194,15 @@ lastdev = stb.st_dev; if(mflag) - (void)printf(" d%s /\n", pperm(stb.st_mode)); + (void)printf(" d%s", pperm(stb.st_mode)); else - (void)printf(" d /\n"); + (void)printf(" d"); + + if(uflag) { + uprint(&stb); + } + + printf(" /\n"); } for(; file && *file;){ @@ -259,15 +276,21 @@ if(xflag && lastdev != stb.st_dev && lastdev != NODEV){ /* Across mnt point */ if(mflag) - (void)printf(" D%s %s\n", pperm(stb.st_mode), buf); + (void)printf(" D%s", pperm(stb.st_mode)); else - (void)printf(" D %s\n", buf); + (void)printf(" D"); + if(uflag) + uprint(&stb); + printf(" %s\n", buf); } else { if(mflag) - (void)printf(" d%s %s\n", pperm(stb.st_mode), buf); + (void)printf(" d%s", pperm(stb.st_mode), buf); else - (void)printf(" d %s\n", buf); + (void)printf(" d", buf); + if(uflag) + uprint(&stb); + printf(" %s\n", buf); } lastdev = stb.st_dev; @@ -286,9 +309,12 @@ } if(mflag) - (void)printf(" l%s %s -> %s", pperm(stb.st_mode), buf, sym); + (void)printf(" l%s", pperm(stb.st_mode)); else - (void)printf(" l %s -> %s", buf, sym); + (void)printf(" l"); + if(uflag) + uprint(&stb); + (void)printf(" %s -> %s", buf, sym); if(symcount > 0 && symcount++ > MAXSYMLINKS){ (void)printf(_(" *** EXCEEDED UNIX LIMIT OF SYMLINKS ***\n")); @@ -302,37 +328,52 @@ case S_IFCHR: if(mflag) - (void)printf(" c%s %s\n", pperm(stb.st_mode), buf); + (void)printf(" c%s", pperm(stb.st_mode)); else - (void)printf(" c %s\n", buf); + (void)printf(" c"); + if(uflag) + uprint(&stb); + (void)printf(" %s\n", buf); break; case S_IFBLK: if(mflag) - (void)printf(" b%s %s\n", pperm(stb.st_mode), buf); + (void)printf(" b%s", pperm(stb.st_mode)); else - (void)printf(" b %s\n", buf); + (void)printf(" b"); + if(uflag) + uprint(&stb); + (void)printf(" %s\n", buf); break; case S_IFSOCK: if(mflag) - (void)printf(" s%s %s\n", pperm(stb.st_mode), buf); + (void)printf(" s%s", pperm(stb.st_mode)); else - (void)printf(" s %s\n", buf); + (void)printf(" s"); + if(uflag) + uprint(&stb); + (void)printf(" %s\n", buf); break; case S_IFIFO: if (mflag) - printf(" p%s %s\n", pperm(stb.st_mode), buf); + printf(" p%s", pperm(stb.st_mode)); else - printf(" p %s\n", buf); + printf(" p"); + if(uflag) + uprint(&stb); + (void)printf(" %s\n", buf); break; case S_IFREG: if(mflag) - (void)printf(" -%s %s\n", pperm(stb.st_mode), buf); + (void)printf(" -%s", pperm(stb.st_mode)); else - (void)printf(" - %s\n", buf); + (void)printf(" -"); + if(uflag) + uprint(&stb); + (void)printf(" %s\n", buf); break; default: @@ -390,3 +431,22 @@ return &buf[0]; } +static void uprint(struct stat *stb) { + struct passwd *username; + struct group *groupname; + + username = getpwuid(stb->st_uid); + groupname = getgrgid(stb->st_gid); + + if(username == NULL) { + printf(" %8d", (int)stb->st_uid); + } else { + printf(" %8s", username->pw_name); + } + printf(":"); + if(groupname == NULL) { + printf("%-8d", (int)stb->st_gid); + } else { + printf("%-8s", groupname->gr_name); + } +} Signed-off-by: Sami Kerola <kerolasa@xxxxxx>