#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[] = { (char *) _PATH_BSHELL, (char *) _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.
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;
- int flen;
+ 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;
- flen = statb.st_size;
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')