home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 4 / CDPD_IV.bin / networking / tcpip / amitcp-support / wustl-ftpdaemon / support / getusershell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-29  |  3.1 KB  |  139 lines

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