* cygcheck.cc (pretty_id): Correct column calculations. Abort if id is acting

funny.
This commit is contained in:
Christopher Faylor 2004-10-14 17:35:46 +00:00
parent 2457f34144
commit 902edd4512
2 changed files with 36 additions and 31 deletions

View File

@ -1,3 +1,9 @@
2004-10-14 Christopher Faylor <cgf@timesys.com>
Bas van Gompel <cygwin-patch.buzz@bavag.tmfweb.nl>
* cygcheck.cc (pretty_id): Correct column calculations. Abort if id is
acting funny.
2004-10-10 Bas van Gompel <cygwin-patch.buzz@bavag.tmfweb.nl> 2004-10-10 Bas van Gompel <cygwin-patch.buzz@bavag.tmfweb.nl>
* cygcheck.cc (add_path): Don't leak memory when path is already in * cygcheck.cc (add_path): Don't leak memory when path is already in

View File

@ -20,6 +20,8 @@
#include "cygwin/include/sys/cygwin.h" #include "cygwin/include/sys/cygwin.h"
#include "cygwin/include/mntent.h" #include "cygwin/include/mntent.h"
#define alloca __builtin_alloca
int verbose = 0; int verbose = 0;
int registry = 0; int registry = 0;
int sysinfo = 0; int sysinfo = 0;
@ -791,7 +793,6 @@ pretty_id (const char *s, char *cygwin, size_t cyglen)
FILE *f = popen (id, "rt"); FILE *f = popen (id, "rt");
char buf[16384]; char buf[16384];
static char empty[] = "";
buf[0] = '\0'; buf[0] = '\0';
fgets (buf, sizeof (buf), f); fgets (buf, sizeof (buf), f);
pclose (f); pclose (f);
@ -799,20 +800,33 @@ pretty_id (const char *s, char *cygwin, size_t cyglen)
if (uid) if (uid)
uid += strlen ("uid="); uid += strlen ("uid=");
else else
uid = empty; {
fprintf (stderr, "garbled output from `id' command - no uid= found\n");
exit (1);
}
char *gid = strtok (NULL, ")"); char *gid = strtok (NULL, ")");
if (gid) if (gid)
gid += strlen ("gid=") + 1; gid += strlen ("gid=") + 1;
else else
gid = empty; {
char **ng; fprintf (stderr, "garbled output from `id' command - no gid= found\n");
size_t sz = 0; exit (1);
for (ng = groups; (*ng = strtok (NULL, ",")); ng++) }
char **ng = groups - 1;
size_t len_uid = strlen (uid);
size_t len_gid = strlen (gid);
*++ng = groups[0] = (char *) alloca (len_uid += sizeof ("UID: )"));
*++ng = groups[1] = (char *) alloca (len_uid += sizeof ("GID: )"));
sprintf (groups[0], "UID: %s)", uid);
sprintf (groups[1], "GID: %s)", gid);
size_t sz = max (len_uid, len_gid);
while ((*++ng = strtok (NULL, ",")))
{ {
char *p = strchr (*ng, '\n'); char *p = strchr (*ng, '\n');
if (p) if (p)
*p = '\0'; *p = '\0';
if (ng == groups) if (ng == groups + 2)
*ng += strlen (" groups="); *ng += strlen (" groups=");
size_t len = strlen (*ng); size_t len = strlen (*ng);
if (sz < len) if (sz < len)
@ -820,32 +834,17 @@ pretty_id (const char *s, char *cygwin, size_t cyglen)
} }
printf ("\nOutput from %s (%s)\n", id, s); printf ("\nOutput from %s (%s)\n", id, s);
size_t szmaybe = strlen ("UID: ") + strlen (uid);
if (sz < szmaybe)
sz = szmaybe;
sz += 1;
int n = 80 / (int) sz; int n = 80 / (int) sz;
sz = -sz; sz = -(sz + 1);
ng[0] += strlen ("groups="); int i = n - 2;
printf ("UID: %.*s) GID: %s)\n", sz + (sizeof ("UID: ") - 1), uid, gid);
int i = 0;
for (char **g = groups; g < ng; g++) for (char **g = groups; g < ng; g++)
{ if ((g != ng - 1) && (++i < n))
if (i < n) printf ("%*s ", sz, *g);
i++; else
else {
{ puts (*g);
i = 0; i = 0;
puts (""); }
}
if (++i <= n && g != (ng - 1))
printf ("%*s ", sz, *g);
else
{
printf ("%s\n", *g);
i = 0;
}
}
} }
static void static void