home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / library / getusershell.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  4KB  |  146 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, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)getusershell.c      5.7 (Berkeley) 2/23/91";
  36. #endif /* LIBC_SCCS and not lint */
  37.  
  38. #define _KERNEL
  39. #include "ixemul.h"
  40. #include <memory.h>
  41. #include <ctype.h>
  42. #include <stdio.h>
  43. #include <sys/stat.h>
  44.  
  45. #define SHELLS "etc:shells"
  46.  
  47. /*
  48.  * Do not add local shells here.  They should be added in /etc/shells
  49.  */
  50. static char *okshells[] =
  51.     { "/bin/sh", "/bin/csh", 0 };
  52.  
  53. static char **shells = NULL, *strings = NULL;
  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.     char *ret;
  63.  
  64.     if (curshell == NULL)
  65.     curshell = initshells();
  66.  
  67.     ret = *curshell;
  68.     if (ret != NULL)
  69.     curshell++;
  70.  
  71.     return (ret);
  72. }
  73.  
  74. void
  75. endusershell(void) {
  76.     if (shells != NULL)
  77.     syscall(SYS_free,(char *)shells);
  78.  
  79.     shells = NULL;
  80.     if (strings != NULL)
  81.     syscall(SYS_free,strings);
  82.  
  83.     strings = NULL;
  84.     curshell = NULL;
  85. }
  86.  
  87. void
  88. setusershell(void) {
  89.     curshell = initshells();
  90. }
  91.  
  92. static char **
  93. initshells(void) {
  94.     register char **sp, *cp;
  95.     register FILE *fp;
  96.     struct stat statb;
  97.  
  98.     if (shells != NULL)
  99.     syscall(SYS_free,(char *)shells);
  100.  
  101.     shells = NULL;
  102.     if (strings != NULL)
  103.     syscall(SYS_free,strings);
  104.  
  105.     strings = NULL;
  106.     if ((fp = (FILE *)syscall(SYS_fopen,SHELLS, "r")) == (FILE *)0)
  107.     return(okshells);
  108.  
  109.     if (syscall(SYS_fstat,fileno(fp), &statb) == -1) {
  110.     (void)syscall(SYS_fclose,fp);
  111.     return(okshells);
  112.     }
  113.  
  114.     if ((strings = (char *)syscall(SYS_malloc,(unsigned)statb.st_size + 1)) == NULL) {
  115.     (void)syscall(SYS_fclose,fp);
  116.     return(okshells);
  117.     }
  118.  
  119.     shells = (char **)syscall(SYS_calloc,(unsigned)statb.st_size / 3, sizeof (char *));
  120.     if (shells == NULL) {
  121.     (void)syscall(SYS_fclose,fp);
  122.     syscall(SYS_free,strings);
  123.     strings = NULL;
  124.     return(okshells);
  125.     }
  126.  
  127.     sp = shells;
  128.     cp = strings;
  129.     while (syscall(SYS_fgets,cp, MAXPATHLEN + 1, fp) != NULL) {
  130.     while (isspace(*cp) && *cp != '\0')
  131.         cp++;
  132.  
  133.     if (*cp == '#' || *cp == '\0')
  134.         continue;
  135.  
  136.     *sp++ = cp;
  137.     while (!isspace(*cp) && *cp != '#' && *cp != '\0')
  138.         cp++;
  139.  
  140.     *cp++ = '\0';
  141.     }
  142.     *sp = (char *)0;
  143.     (void)syscall(SYS_fclose,fp);
  144.     return (shells);
  145. }
  146.