home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3339 / dialup.c next >
Encoding:
C/C++ Source or Header  |  1991-05-17  |  2.6 KB  |  152 lines

  1. /*
  2.  * Copyright 1989, 1990, 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.  
  12. #include <stdio.h>
  13. #ifndef    BSD
  14. #include <string.h>
  15. #else
  16. #include <strings.h>
  17. #define    strchr    index
  18. #define    strrchr    rindex
  19. #endif
  20. #include "dialup.h"
  21.  
  22. #ifndef    lint
  23. static    char    sccsid[] = "@(#)dialup.c    3.3    19:44:20    12/10/90";
  24. #endif
  25.  
  26. static    FILE    *dialpwd;
  27.  
  28. void
  29. setduent ()
  30. {
  31.     if (dialpwd)
  32.         rewind (dialpwd);
  33.     else
  34.         dialpwd = fopen (DIALPWD, "r");
  35. }
  36.  
  37. void
  38. endduent ()
  39. {
  40.     if (dialpwd)
  41.         fclose (dialpwd);
  42.  
  43.     dialpwd = (FILE *) 0;
  44. }
  45.  
  46. struct dialup *
  47. fgetduent (fp)
  48. FILE    *fp;
  49. {
  50.     static    struct    dialup    dialup;    /* static structure to point to */
  51.     static    char    shell[64];    /* some space for a login shell */
  52.     static    char    passwd[16];    /* some space for dialup password */
  53.     char    buf[BUFSIZ];
  54.     char    *cp;
  55.  
  56.     if (! fp)
  57.         return 0;
  58.  
  59.     if (! fp || feof (fp))
  60.         return ((struct dialup *) 0);
  61.  
  62.     while (fgets (buf, BUFSIZ, fp) == buf && buf[0] == '#')
  63.         ;
  64.  
  65.     if (feof (fp))
  66.         return ((struct dialup *) 0);
  67.  
  68.     cp = strchr (buf, ':');
  69.     if (cp - buf > sizeof shell)    /* something is fishy ... */
  70.         return ((struct dialup *) 0);
  71.  
  72.     (void) strncpy (shell, buf, cp - buf);
  73.     shell[cp - buf] = '\0';
  74.  
  75.     if (strlen (cp + 1) > sizeof passwd) /* something is REALLY fishy */
  76.         return ((struct dialup *) 0);
  77.  
  78.     (void) strcpy (passwd, cp + 1);
  79.     passwd[strlen (passwd) - 1] = '\0';
  80.     if (cp = strchr (passwd, ':'))
  81.         *cp = '\0';
  82.  
  83.     dialup.du_shell = shell;
  84.     dialup.du_passwd = passwd;
  85.  
  86.     return (&dialup);
  87. }
  88.  
  89. struct dialup *
  90. getduent ()
  91. {
  92.     if (! dialpwd)
  93.         setduent ();
  94.  
  95.     return fgetduent (dialpwd);
  96. }
  97.  
  98. struct    dialup    *getdushell (shell)
  99. char    *shell;
  100. {
  101.     struct    dialup    *dialup;
  102.  
  103.     while (dialup = getduent ()) {
  104.         if (strcmp (shell, dialup->du_shell) == 0)
  105.             return (dialup);
  106.  
  107.         if (strcmp (dialup->du_shell, "*") == 0)
  108.             return (dialup);
  109.     }
  110.     return ((struct dialup *) 0);
  111. }
  112.  
  113. int    isadialup (tty)
  114. char    *tty;
  115. {
  116.     FILE    *fp;
  117.     char    buf[BUFSIZ];
  118.     int    dialup = 0;
  119.  
  120.     if (! (fp = fopen (DIALUPS, "r")))
  121.         return (0);
  122.  
  123.     while (fgets (buf, BUFSIZ, fp) == buf) {
  124.         if (buf[0] == '#')
  125.             continue;
  126.  
  127.         buf[strlen (buf) - 1] = '\0';
  128.  
  129.         if (strcmp (buf, tty) == 0) {
  130.             dialup = 1;
  131.             break;
  132.         }
  133.     }
  134.     fclose (fp);
  135.  
  136.     return (dialup);
  137. }
  138.  
  139. int
  140. putduent (dial, fp)
  141. struct    dialup    *dial;
  142. FILE    *fp;
  143. {
  144.     if (! fp || ! dial)
  145.         return -1;
  146.  
  147.     if (fprintf (fp, "%s:%s\n", dial->du_shell, dial->du_passwd) == EOF)
  148.         return -1;
  149.  
  150.     return 0;
  151. }
  152.