1/* 2 * $Id: getusershell.c,v 1.4 2003-02-17 01:51:08 srittau Exp $ 3 * 4 * Copyright (c) 1985 Regents of the University of California. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms are permitted 8 * provided that: (1) source distributions retain this entire copyright 9 * notice and comment, and (2) distributions including binaries display 10 * the following acknowledgement: ``This product includes software 11 * developed by the University of California, Berkeley and its contributors'' 12 * in the documentation or other materials provided with the distribution 13 * and in all advertising materials mentioning features or use of this 14 * software. Neither the name of the University nor the names of its 15 * contributors may be used to endorse or promote products derived 16 * from this software without specific prior written permission. 17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 20 */ 21 22#ifdef HAVE_CONFIG_H 23#include "config.h" 24#endif /* HAVE_CONFIG_H */ 25 26#if defined(LIBC_SCCS) && !defined(lint) 27static char sccsid[] = "@(#)getusershell.c 5.6 (Berkeley) 6/1/90"; 28#endif /* LIBC_SCCS and not lint */ 29 30#if defined(ultrix) || defined(_IBMR2) || defined(NEED_GETUSERSHELL) 31 32#include <sys/param.h> 33#include <sys/file.h> 34#include <sys/stat.h> 35#include <ctype.h> 36#include <stdio.h> 37 38#define SHELLS "/etc/shells" 39 40/* 41 * Do not add local shells here. They should be added in /etc/shells 42 */ 43static char *okshells[] = { 44 "/bin/sh", "/bin/csh", 45#ifdef _IBMR2 46 "/bin/ksh", 47#endif /* _IBMR2 */ 48 0 49}; 50 51static char **shells, *strings; 52static char **curshell = NULL; 53extern char **initshells(); 54 55/* 56 * Get a list of shells from SHELLS, if it exists. 57 */ 58char * 59getusershell() 60{ 61 char *ret; 62 63 if (curshell == NULL) 64 curshell = initshells(); 65 ret = *curshell; 66 if (ret != NULL) 67 curshell++; 68 return (ret); 69} 70 71endusershell() 72{ 73 74 if (shells != NULL) 75 free((char *)shells); 76 shells = NULL; 77 if (strings != NULL) 78 free(strings); 79 strings = NULL; 80 curshell = NULL; 81} 82 83setusershell() 84{ 85 86 curshell = initshells(); 87} 88 89static char ** 90initshells() 91{ 92 register char **sp, *cp; 93 register FILE *fp; 94 struct stat statb; 95 extern char *malloc(), *calloc(); 96 97 if (shells != NULL) 98 free((char *)shells); 99 shells = NULL; 100 if (strings != NULL) 101 free(strings); 102 strings = NULL; 103 if ((fp = fopen(SHELLS, "r")) == (FILE *)0) 104 return(okshells); 105 if (fstat(fileno(fp), &statb) == -1) { 106 (void)fclose(fp); 107 return(okshells); 108 } 109 if ((strings = malloc((unsigned)statb.st_size)) == NULL) { 110 (void)fclose(fp); 111 return(okshells); 112 } 113 shells = (char **)calloc((unsigned)statb.st_size / 3, sizeof (char *)); 114 if (shells == NULL) { 115 (void)fclose(fp); 116 free(strings); 117 strings = NULL; 118 return(okshells); 119 } 120 sp = shells; 121 cp = strings; 122 while (fgets(cp, MAXPATHLEN + 1, fp) != NULL) { 123 while (*cp != '#' && *cp != '/' && *cp != '\0') 124 cp++; 125 if (*cp == '#' || *cp == '\0') 126 continue; 127 *sp++ = cp; 128 while (!isspace(*cp) && *cp != '#' && *cp != '\0') 129 cp++; 130 *cp++ = '\0'; 131 } 132 *sp = (char *)0; 133 (void)fclose(fp); 134 return (shells); 135} 136 137# endif /* ultrix */ 138