OSDN Git Service

Move getusername/getgroupname to lib. (Return name or string representation
authorRob Landley <rob@landley.net>
Sat, 13 Aug 2016 20:19:29 +0000 (15:19 -0500)
committerRob Landley <rob@landley.net>
Sat, 13 Aug 2016 20:19:29 +0000 (15:19 -0500)
of number, but never NULL. Both returned in static buffer good through
next call.)

lib/lib.c
lib/lib.h
toys/posix/ls.c

index 718ea3a..8f8b4a3 100644 (file)
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -1184,3 +1184,26 @@ int regexec0(regex_t *preg, char *string, long len, int nmatch,
     len -= ll;
   }
 }
+
+// Return user name or string representation of number, returned buffer
+// lasts until next call.
+char *getusername(uid_t uid)
+{
+  struct passwd *pw = bufgetpwuid(uid);
+  static char unum[12];
+
+  sprintf(unum, "%u", (unsigned)uid);
+  return pw ? pw->pw_name : unum;
+}
+
+// Return group name or string representation of number, returned buffer
+// lasts until next call.
+char *getgroupname(gid_t gid)
+{
+  struct group *gr = bufgetgrgid(gid);
+  static char gnum[12];
+
+  sprintf(gnum, "%u", (unsigned)gid);
+  return gr ? gr->gr_name : gnum;
+}
+
index 520a5ed..67d5303 100644 (file)
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -223,6 +223,8 @@ int readlinkat0(int dirfd, char *path, char *buf, int len);
 int readlink0(char *path, char *buf, int len);
 int regexec0(regex_t *preg, char *string, long len, int nmatch,
   regmatch_t pmatch[], int eflags);
+char *getusername(uid_t uid);
+char *getgroupname(gid_t gid);
 
 #define HR_SPACE 1 // Space between number and units
 #define HR_B     2 // Use "B" for single byte units
index 3ad28da..d48fb83 100644 (file)
@@ -61,7 +61,7 @@ GLOBALS(
 
   unsigned screen_width;
   int nl_title;
-  char uid_buf[12], gid_buf[12], *escmore;
+  char *escmore;
 )
 
 // Callback from crunch_str to represent unprintable chars
@@ -121,22 +121,6 @@ static char endtype(struct stat *st)
   return 0;
 }
 
-static char *getusername(uid_t uid)
-{
-  struct passwd *pw = bufgetpwuid(uid);
-
-  sprintf(TT.uid_buf, "%u", (unsigned)uid);
-  return pw ? pw->pw_name : TT.uid_buf;
-}
-
-static char *getgroupname(gid_t gid)
-{
-  struct group *gr = bufgetgrgid(gid);
-
-  sprintf(TT.gid_buf, "%u", (unsigned)gid);
-  return gr ? gr->gr_name : TT.gid_buf;
-}
-
 static int numlen(long long ll)
 {
   return snprintf(0, 0, "%llu", ll);