125429Smckusick /* 225429Smckusick * Copyright (c) 1985 Regents of the University of California. 335109Sbostic * All rights reserved. 435109Sbostic * 542625Sbostic * %sccs.include.redist.c% 625429Smckusick */ 725429Smckusick 826564Sdonn #if defined(LIBC_SCCS) && !defined(lint) 9*53280Sbostic static char sccsid[] = "@(#)getusershell.c 5.8 (Berkeley) 04/29/92"; 1035109Sbostic #endif /* LIBC_SCCS and not lint */ 1125429Smckusick 1225429Smckusick #include <sys/param.h> 1325429Smckusick #include <sys/file.h> 1425429Smckusick #include <sys/stat.h> 15*53280Sbostic #include <stdio.h> 1625429Smckusick #include <ctype.h> 1746597Sdonn #include <stdlib.h> 1846597Sdonn #include <unistd.h> 19*53280Sbostic #include <paths.h> 2025429Smckusick 2125429Smckusick /* 22*53280Sbostic * Local shells should NOT be added here. They should be added in 23*53280Sbostic * /etc/shells. 2425429Smckusick */ 2525429Smckusick 26*53280Sbostic static char *okshells[] = { _PATH_BSHELL, _PATH_CSHELL, NULL }; 27*53280Sbostic static char **curshell, **shells, *strings; 28*53280Sbostic static char **initshells __P((void)); 2925429Smckusick 3025429Smckusick /* 31*53280Sbostic * Get a list of shells from _PATH_SHELLS, if it exists. 3225429Smckusick */ 3325429Smckusick char * 3425429Smckusick getusershell() 3525429Smckusick { 3625429Smckusick char *ret; 3725429Smckusick 3829662Sbloom if (curshell == NULL) 3929662Sbloom curshell = initshells(); 4029662Sbloom ret = *curshell; 4129662Sbloom if (ret != NULL) 4229662Sbloom curshell++; 4325429Smckusick return (ret); 4425429Smckusick } 4525429Smckusick 4646597Sdonn void 4725429Smckusick endusershell() 4825429Smckusick { 4925429Smckusick 5025429Smckusick if (shells != NULL) 51*53280Sbostic free(shells); 5225429Smckusick shells = NULL; 5325429Smckusick if (strings != NULL) 5425429Smckusick free(strings); 5525429Smckusick strings = NULL; 5629662Sbloom curshell = NULL; 5725429Smckusick } 5825429Smckusick 5946597Sdonn void 6025429Smckusick setusershell() 6125429Smckusick { 6225429Smckusick 6329662Sbloom curshell = initshells(); 6425429Smckusick } 6525429Smckusick 6625429Smckusick static char ** 6725429Smckusick initshells() 6825429Smckusick { 6925429Smckusick register char **sp, *cp; 7025429Smckusick register FILE *fp; 7125429Smckusick struct stat statb; 7225429Smckusick 7325429Smckusick if (shells != NULL) 74*53280Sbostic free(shells); 7525429Smckusick shells = NULL; 7625429Smckusick if (strings != NULL) 7725429Smckusick free(strings); 7825429Smckusick strings = NULL; 79*53280Sbostic if ((fp = fopen(_PATH_SHELLS, "r")) == NULL) 80*53280Sbostic return (okshells); 8125429Smckusick if (fstat(fileno(fp), &statb) == -1) { 8225429Smckusick (void)fclose(fp); 83*53280Sbostic return (okshells); 8425429Smckusick } 85*53280Sbostic if ((strings = malloc((u_int)statb.st_size)) == NULL) { 8625429Smckusick (void)fclose(fp); 87*53280Sbostic return (okshells); 8825429Smckusick } 89*53280Sbostic shells = calloc((unsigned)statb.st_size / 3, sizeof (char *)); 9025429Smckusick if (shells == NULL) { 9125429Smckusick (void)fclose(fp); 9225429Smckusick free(strings); 9325429Smckusick strings = NULL; 94*53280Sbostic return (okshells); 9525429Smckusick } 9625429Smckusick sp = shells; 9725429Smckusick cp = strings; 9825429Smckusick while (fgets(cp, MAXPATHLEN + 1, fp) != NULL) { 9929662Sbloom while (*cp != '#' && *cp != '/' && *cp != '\0') 10025429Smckusick cp++; 10129662Sbloom if (*cp == '#' || *cp == '\0') 10225429Smckusick continue; 10325429Smckusick *sp++ = cp; 10425429Smckusick while (!isspace(*cp) && *cp != '#' && *cp != '\0') 10525429Smckusick cp++; 10625429Smckusick *cp++ = '\0'; 10725429Smckusick } 108*53280Sbostic *sp = NULL; 10925429Smckusick (void)fclose(fp); 11025429Smckusick return (shells); 11125429Smckusick } 112