S/390: Add hwcap value for transactional execution.
[platform/upstream/glibc.git] / misc / getusershell.c
index 6782c3e..2e8d97e 100644 (file)
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *     This product includes software developed by the University of
- *     California, Berkeley and its contributors.
  * 4. Neither the name of the University nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.
@@ -39,6 +35,7 @@ static char sccsid[] = "@(#)getusershell.c    8.1 (Berkeley) 6/4/93";
 #include <sys/file.h>
 #include <sys/stat.h>
 #include <stdio.h>
+#include <stdio_ext.h>
 #include <ctype.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -49,9 +46,17 @@ static char sccsid[] = "@(#)getusershell.c   8.1 (Berkeley) 6/4/93";
  * /etc/shells.
  */
 
-static char *okshells[] = { _PATH_BSHELL, _PATH_CSHELL, NULL };
+/* NB: we do not initialize okshells here.  The initialization needs
+   relocations.  These interfaces are used so rarely that this is not
+   justified.  Instead explicitly initialize the array when it is
+   used.  */
+#if 0
+static const char *const okshells[] = { _PATH_BSHELL, _PATH_CSHELL, NULL };
+#else
+static const char *okshells[3];
+#endif
 static char **curshell, **shells, *strings;
-static char **initshells __P((void));
+static char **initshells (void) __THROW;
 
 /*
  * Get a list of shells from _PATH_SHELLS, if it exists.
@@ -72,12 +77,10 @@ getusershell()
 void
 endusershell()
 {
-       
-       if (shells != NULL)
-               free(shells);
+
+       free(shells);
        shells = NULL;
-       if (strings != NULL)
-               free(strings);
+       free(strings);
        strings = NULL;
        curshell = NULL;
 }
@@ -94,37 +97,40 @@ initshells()
 {
        register char **sp, *cp;
        register FILE *fp;
-       struct stat statb;
+       struct stat64 statb;
+       size_t flen;
 
-       if (shells != NULL)
-               free(shells);
+       free(shells);
        shells = NULL;
-       if (strings != NULL)
-               free(strings);
+       free(strings);
        strings = NULL;
-       if ((fp = fopen(_PATH_SHELLS, "r")) == NULL)
-               return (okshells);
-       if (fstat(fileno(fp), &statb) == -1) {
+       if ((fp = fopen(_PATH_SHELLS, "rce")) == NULL)
+               goto init_okshells_noclose;
+       if (fstat64(fileno(fp), &statb) == -1) {
+       init_okshells:
                (void)fclose(fp);
-               return (okshells);
+       init_okshells_noclose:
+               okshells[0] = _PATH_BSHELL;
+               okshells[1] = _PATH_CSHELL;
+               return (char **) okshells;
        }
-       if ((strings = malloc((u_int)statb.st_size + 1)) == NULL) {
-               (void)fclose(fp);
-               return (okshells);
-       }
-       shells = calloc((unsigned)statb.st_size / 3, sizeof (char *));
+       if (statb.st_size > ~(size_t)0 / sizeof (char *) * 3)
+               goto init_okshells;
+       flen = statb.st_size + 3;
+       if ((strings = malloc(flen)) == NULL)
+               goto init_okshells;
+       shells = malloc(statb.st_size / 3 * sizeof (char *));
        if (shells == NULL) {
-               (void)fclose(fp);
                free(strings);
                strings = NULL;
-               return (okshells);
+               goto init_okshells;
        }
        sp = shells;
        cp = strings;
-       while (fgets(cp, statb.st_size - (cp - strings), fp) != NULL) {
+       while (fgets_unlocked(cp, flen - (cp - strings), fp) != NULL) {
                while (*cp != '#' && *cp != '/' && *cp != '\0')
                        cp++;
-               if (*cp == '#' || *cp == '\0')
+               if (*cp == '#' || *cp == '\0' || cp[1] == '\0')
                        continue;
                *sp++ = cp;
                while (!isspace(*cp) && *cp != '#' && *cp != '\0')