home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2286 / dialup.c < prev    next >
C/C++ Source or Header  |  1990-12-28  |  2KB  |  122 lines

  1. /*
  2.  * Copyright 1989, 1990, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Use, duplication, and disclosure prohibited without
  6.  * the express written permission of the author.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #ifndef    BSD
  11. #include <string.h>
  12. #else
  13. #include <strings.h>
  14. #define    strchr    index
  15. #define    strrchr    rindex
  16. #endif
  17. #include "dialup.h"
  18.  
  19. #ifndef    lint
  20. static    char    _sccsid[] = "@(#)dialup.c    2.2    19:23:38    7/29/90";
  21. #endif
  22.  
  23. static    FILE    *dialpwd;
  24.  
  25. void    setduent ()
  26. {
  27.     if (dialpwd)
  28.         rewind (dialpwd);
  29.     else
  30.         dialpwd = fopen (DIALPWD, "r");
  31. }
  32.  
  33. void    endduent ()
  34. {
  35.     if (dialpwd)
  36.         fclose (dialpwd);
  37.  
  38.     dialpwd = (FILE *) 0;
  39. }
  40.  
  41. struct    dialup    *getduent ()
  42. {
  43.     static    struct    dialup    dialup;    /* static structure to point to */
  44.     static    char    shell[64];    /* some space for a login shell */
  45.     static    char    passwd[16];    /* some space for dialup password */
  46.     char    buf[BUFSIZ];
  47.     char    *cp;
  48.  
  49.     if (! dialpwd)
  50.         setduent ();
  51.  
  52.     if (! dialpwd || feof (dialpwd))
  53.         return ((struct dialup *) 0);
  54.  
  55.     while (fgets (buf, BUFSIZ, dialpwd) == buf && buf[0] == '#')
  56.         ;
  57.  
  58.     if (feof (dialpwd))
  59.         return ((struct dialup *) 0);
  60.  
  61.     cp = strchr (buf, ':');
  62.     if (cp - buf > sizeof shell)    /* something is fishy ... */
  63.         return ((struct dialup *) 0);
  64.  
  65.     (void) strncpy (shell, buf, cp - buf);
  66.     shell[cp - buf] = '\0';
  67.  
  68.     if (strlen (cp + 1) > sizeof passwd) /* something is REALLY fishy */
  69.         return ((struct dialup *) 0);
  70.  
  71.     (void) strcpy (passwd, cp + 1);
  72.     passwd[strlen (passwd) - 1] = '\0';
  73.     if (cp = strchr (passwd, ':'))
  74.         *cp = '\0';
  75.  
  76.     dialup.du_shell = shell;
  77.     dialup.du_passwd = passwd;
  78.  
  79.     return (&dialup);
  80. }
  81.  
  82. struct    dialup    *getdushell (shell)
  83. char    *shell;
  84. {
  85.     struct    dialup    *dialup;
  86.  
  87.     while (dialup = getduent ()) {
  88.         if (strcmp (shell, dialup->du_shell) == 0)
  89.             return (dialup);
  90.  
  91.         if (strcmp (dialup->du_shell, "*") == 0)
  92.             return (dialup);
  93.     }
  94.     return ((struct dialup *) 0);
  95. }
  96.  
  97. int    isadialup (tty)
  98. char    *tty;
  99. {
  100.     FILE    *fp;
  101.     char    buf[BUFSIZ];
  102.     int    dialup = 0;
  103.  
  104.     if (! (fp = fopen (DIALUPS, "r")))
  105.         return (0);
  106.  
  107.     while (fgets (buf, BUFSIZ, fp) == buf) {
  108.         if (buf[0] == '#')
  109.             continue;
  110.  
  111.         buf[strlen (buf) - 1] = '\0';
  112.  
  113.         if (strcmp (buf, tty) == 0) {
  114.             dialup = 1;
  115.             break;
  116.         }
  117.     }
  118.     fclose (fp);
  119.  
  120.     return (dialup);
  121. }
  122.