X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=tests%2Ftest-userspec.c;h=dcb1b3ef3892ee3872ada326f0a42de6ca69b7cd;hb=b12df021ba5b5f12648d84f91d021777c347e9be;hp=3e101f3c40143a717bf488e33a75543cd0b5ca6f;hpb=c96d2778819f17724b099eebf105f960f74d1c42;p=gnulib.git diff --git a/tests/test-userspec.c b/tests/test-userspec.c index 3e101f3c4..dcb1b3ef3 100644 --- a/tests/test-userspec.c +++ b/tests/test-userspec.c @@ -1,5 +1,5 @@ /* Test userspec.c - Copyright (C) 2009-2010 Free Software Foundation, Inc. + Copyright (C) 2009-2011 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -35,12 +35,12 @@ struct test { - char *in; + const char *in; uid_t uid; gid_t gid; - char *user_name; - char *group_name; - char *result; + const char *user_name; + const char *group_name; + const char *result; }; static struct test T[] = @@ -101,36 +101,41 @@ main (void) { unsigned int i; int fail = 0; - uid_t uid; /* Find a UID that has both a user name and login group name, but skip UID 0. */ - for (uid = 1200; 0 < uid; uid--) - { - struct group *gr; - struct passwd *pw = getpwuid (uid); - unsigned int j; - size_t len; - if (!pw || !pw->pw_name || !(gr = getgrgid (pw->pw_gid)) || !gr->gr_name) - continue; - j = ARRAY_CARDINALITY (T) - 2; - assert (T[j].in == NULL); - assert (T[j+1].in == NULL); - len = strlen (pw->pw_name); - - /* Store "username:" in T[j].in. */ - T[j].in = xmalloc (len + 1 + 1); - memcpy (T[j].in, pw->pw_name, len); - T[j].in[len] = ':'; - T[j].in[len+1] = '\0'; - - T[j].uid = uid; - T[j].gid = gr->gr_gid; - T[j].user_name = xstrdup (pw->pw_name); - T[j].group_name = xstrdup (gr->gr_name); - T[j].result = NULL; - break; - } + { + uid_t uid; + for (uid = 1200; 0 < uid; uid--) + { + struct group *gr; + struct passwd *pw = getpwuid (uid); + unsigned int j; + size_t len; + if (!pw || !pw->pw_name || !(gr = getgrgid (pw->pw_gid)) || !gr->gr_name) + continue; + j = ARRAY_CARDINALITY (T) - 2; + assert (T[j].in == NULL); + assert (T[j+1].in == NULL); + len = strlen (pw->pw_name); + + /* Store "username:" in T[j].in. */ + { + char *t = xmalloc (len + 1 + 1); + memcpy (t, pw->pw_name, len); + t[len] = ':'; + t[len+1] = '\0'; + T[j].in = t; + } + + T[j].uid = uid; + T[j].gid = gr->gr_gid; + T[j].user_name = xstrdup (pw->pw_name); + T[j].group_name = xstrdup (gr->gr_name); + T[j].result = NULL; + break; + } + } for (i = 0; T[i].in; i++) { @@ -139,7 +144,7 @@ main (void) char *user_name; char *group_name; char const *diag = parse_user_spec (T[i].in, &uid, &gid, - &user_name, &group_name); + &user_name, &group_name); free (user_name); free (group_name); if (!same_diag (diag, T[i].result)) @@ -156,8 +161,8 @@ main (void) if (uid != T[i].uid || gid != T[i].gid) { printf ("%s mismatch (-: expected uid,gid; +:actual)\n" - "-%3lu,%3lu\n+%3lu,%3lu\n", - T[i].in, + "-%3lu,%3lu\n+%3lu,%3lu\n", + T[i].in, (unsigned long int) T[i].uid, (unsigned long int) T[i].gid, (unsigned long int) uid, @@ -170,8 +175,8 @@ main (void) { printf ("%s diagnostic mismatch (-: expected uid,gid; +:actual)\n" - "-%s\n+%s\n", - T[i].in, T[i].result, diag); + "-%s\n+%s\n", + T[i].in, T[i].result, diag); fail = 1; } }