home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume38 / shadow / part12 / login.c < prev    next >
C/C++ Source or Header  |  1993-08-14  |  3KB  |  153 lines

  1. /*
  2.  * Copyright 1989, 1990, 1991, 1992, 1993, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  *
  11.  * This software is provided on an AS-IS basis and the author makes
  12.  * no warrantee of any kind.
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <signal.h>
  17. #include <ctype.h>
  18. #ifndef    BSD
  19. #include <string.h>
  20. #include <memory.h>
  21. #else
  22. #include <strings.h>
  23. #define    strchr    index
  24. #define    strrchr    rindex
  25. #endif
  26. #include "config.h"
  27.  
  28. #ifndef    lint
  29. static    char    sccsid[] = "@(#)login.c    3.4    12:21:53    01 May 1993";
  30. #endif
  31.  
  32. void    setenv ();
  33.  
  34. /*
  35.  * login - prompt the user for their login name
  36.  *
  37.  * login() displays the standard login prompt.  If the option
  38.  * ISSUE_FILE_ENAB is set, the file /etc/issue is displayed
  39.  * before the prompt.
  40.  */
  41.  
  42. void
  43. login (name, prompt)
  44. char    *name;
  45. char    *prompt;
  46. {
  47.     char    buf[BUFSIZ];
  48.     char    *envp[32];
  49.     int    envc;
  50.     char    *cp;
  51.     int    i;
  52.     FILE    *fp;
  53.     SIGTYPE    (*sigquit)();
  54. #ifdef    SIGTSTP
  55.     SIGTYPE    (*sigtstp)();
  56. #endif
  57.     extern    void    exit();
  58.  
  59.     /*
  60.      * There is a small chance that a QUIT character will be part of
  61.      * some random noise during a prompt.  Deal with this by exiting
  62.      * instead of core dumping.  If SIGTSTP is defined, do the same
  63.      * thing for that signal.
  64.      */
  65.  
  66.     sigquit = signal (SIGQUIT, exit);
  67. #ifdef    SIGTSTP
  68.     sigtstp = signal (SIGTSTP, exit);
  69. #endif
  70.  
  71.     /*
  72.      * See if the user has configured the /etc/issue file to
  73.      * be displayed and display it before the prompt.
  74.      */
  75.  
  76.     if (prompt) {
  77.         if (getdef_bool ("ISSUE_FILE_ENAB")) {
  78.             if (fp = fopen ("/etc/issue", "r")) {
  79.                 while ((i = getc (fp)) != EOF)
  80.                     putc (i, stdout);
  81.  
  82.                 fflush (stdout);
  83.                 fclose (fp);
  84.             }
  85.         }
  86.         fputs (prompt, stdout);
  87.     }
  88.  
  89.     /* 
  90.      * Read the user's response.  The trailing newline will be
  91.      * removed.
  92.      */
  93.  
  94. #ifndef    BSD
  95.     (void) memset (buf, '\0', sizeof buf);
  96. #else
  97.     bzero (buf, sizeof buf);
  98. #endif
  99.     if (fgets (buf, BUFSIZ, stdin) != buf)
  100.         exit (1);
  101.  
  102.     buf[strlen (buf) - 1] = '\0';    /* remove \n [ must be there ] */
  103.  
  104.     /*
  105.      * Skip leading whitespace.  This makes "  username" work right.
  106.      * Then copy the rest (up to the end or the first "non-graphic"
  107.      * character into the username.
  108.      */
  109.  
  110.     for (cp = buf;*cp == ' ' || *cp == '\t';cp++)
  111.         ;
  112.  
  113.     for (i = 0;i < BUFSIZ - 1 && isgraph (*cp);name[i++] = *cp++)
  114.         ;
  115.  
  116.     if (*cp)
  117.         cp++;
  118.  
  119.     name[i] = '\0';
  120.  
  121.     /*
  122.      * This is a disaster, at best.  The user may have entered extra
  123.      * environmental variables at the prompt.  There are several ways
  124.      * to do this, and I just take the easy way out.
  125.      */
  126.  
  127.     if (*cp != '\0') {        /* process new variables */
  128.         static    int    count;
  129.         char    var[BUFSIZ];
  130.  
  131.         for (envc = 0;envc < 32;envc++) {
  132.             envp[envc] = strtok (envc == 0 ? cp:(char *) 0, " \t,");
  133.             if (envp[envc] == (char *) 0)
  134.                 break;
  135.  
  136.             if (! strchr (envp[envc], '=')) {
  137.                 sprintf (var, "L%d=%s", count++, envp[envc]);
  138.                 envp[envc] = strdup (var);
  139.             }
  140.         }
  141.         setenv (envc, envp);
  142.     }
  143.  
  144.     /*
  145.      * Set the SIGQUIT handler back to its original value
  146.      */
  147.  
  148.     (void) signal (SIGQUIT, sigquit);
  149. #ifdef    SIGTSTP
  150.     (void) signal (SIGTSTP, sigtstp);
  151. #endif
  152. }
  153.