* 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.
#include <sys/file.h>
#include <sys/stat.h>
#include <stdio.h>
+#include <stdio_ext.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
* /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.
void
endusershell()
{
-
- if (shells != NULL)
- free(shells);
+
+ free(shells);
shells = NULL;
- if (strings != NULL)
- free(strings);
+ free(strings);
strings = NULL;
curshell = NULL;
}
{
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')