home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / lib / dial / d_utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-28  |  4.7 KB  |  255 lines

  1. # include  "util.h"
  2. # include  "d_returns.h"
  3. # include  <pwd.h>
  4. # include  <stdio.h>
  5. # include  "d_proto.h"
  6. # include  "d_structs.h"
  7.  
  8. /*  4 Sept 81   D. Krafft (Cornell) & D. Crocker
  9.  *                              getuser log calls didn't name routine
  10.  *                              also converted to using getpwuid()
  11.  */
  12.  
  13.  
  14. /*
  15.  *     routine which reads a line from the password file and pulls out the
  16.  *     user's name
  17.  */
  18.  
  19. d_getuser(uid, name, path)
  20.   int  uid;
  21.   char  *name, *path;
  22.     {
  23.     extern struct passwd *getpwuid ();
  24.     register struct passwd *pwdptr;
  25.  
  26. /*  if we did this once, just used the stuff we saved  */
  27.  
  28.     if ((pwdptr = getpwuid (uid)) == 0)
  29.       {
  30. #ifdef D_LOG
  31.       d_log("d_getuser", "couldn't find user in password file.\n");
  32. #endif D_LOG
  33.       return(D_FATAL);
  34.       }
  35.  
  36.     if (name)
  37.       (void) strcpy(name, pwdptr -> pw_name);
  38.  
  39.  
  40.     if (path)
  41.     {
  42.     if (strlen(pwdptr -> pw_dir) > 64)
  43.       {
  44. #ifdef D_LOG
  45.       d_log("d_getuser", "user's default path too long  '%s'\n",
  46.             pwdptr -> pw_dir);
  47. #endif D_LOG
  48.       return(D_FATAL);
  49.       }
  50.     (void) strcpy(path, pwdptr -> pw_dir);
  51.     }
  52.  
  53.     return(D_OK);
  54.     }
  55.  
  56. /*
  57.  *     D_CANON
  58.  *
  59.  *     routine which translates a C style string specification into
  60.  *     internal format.  it understands 'escape' characters equivalents
  61.  *     for certain control characters: newline ('\n'), carriage return ('\r'),
  62.  *     tab ('\t'), form feed ('\f'), and delete ('\x').  in addition, any
  63.  *     character can be specified with a backslash followed by up to 3 octal
  64.  *     digits.
  65.  *
  66.  *     in -- pointer to canonical format string
  67.  *
  68.  *     out -- pointer to buffer for internal format equivalent.
  69.  *
  70.  *     the number of characters in the (null-terminated) output string is
  71.  *     returned.  a return value of -1 indicates an error.
  72.  */
  73.  
  74. d_canon(in, out)
  75.   register char  *in, *out;
  76.     {
  77.     int  value, count, place;
  78.     char  c;
  79.  
  80.     count = 0;
  81.  
  82. /*  check for obvious bad format  */
  83.  
  84.     if (*in++ != '"')
  85.       return(-1);
  86.  
  87. /*  loop through all the characters.  lots of special cases  */
  88.  
  89.     while (1)
  90.       {
  91.       c = *in++;
  92.  
  93.       switch (c)
  94.         {
  95.         default:
  96.  
  97.             *out++ = c;
  98.             break;
  99.  
  100.         case  '\0':
  101.  
  102.             return(-1);
  103.  
  104.         case  '"':
  105.  
  106.             *out = '\0';
  107.             return(count);
  108.  
  109.         case  '\\':
  110.  
  111. /*  handle the escapes  */
  112.  
  113.             c = *in++;
  114.  
  115.             switch (c)
  116.               {
  117.               case  '"':
  118.  
  119.                   *out++ = '"';
  120.                   break;
  121.  
  122.               case  '\0':
  123.  
  124.                   return(-1);
  125.  
  126.               case  '\\':
  127.  
  128.                   *out++ = '\\';
  129.                   break;
  130.  
  131.               case  'r':
  132.  
  133.                   *out++ = '\15';
  134.                   break;
  135.  
  136.               case  'n':
  137.  
  138.                   *out++ = '\12';
  139.                   break;
  140.  
  141.               case  't':
  142.  
  143.                   *out++ = '\11';
  144.                   break;
  145.  
  146.               case  'x':
  147.  
  148.                   *out++ = DELAY_CH;
  149.                   break;
  150.  
  151.           case  'b':
  152.  
  153.           *out++ = '\10';
  154.           break;
  155.  
  156.           case  '#':
  157.  
  158.           *out++ = BREAK_CH;
  159.           break;
  160.  
  161. /*  allow the specification of characters by up to 3 octal digits after a  */
  162. /*  backslash                                                              */
  163.  
  164.               default:
  165.  
  166.                   if (!d_isodigit(c))
  167.                     {
  168.                     *out++ = c;
  169.                     break;
  170.                     }
  171.  
  172.                   value = c & 07;
  173.  
  174.                   for (place = 0; place <= 1; place++)
  175.                     {
  176.                     c = *in++;
  177.  
  178.                     if (d_isodigit(c))
  179.                       value = (value << 3) | (c & 07);
  180.                     else
  181.                       {
  182.                       in--;
  183.                       break;
  184.                       }
  185.                     }
  186.  
  187.                   *out++ = value;
  188.  
  189.               }
  190.         }
  191.  
  192.       count++;
  193.       }
  194.     }
  195.  
  196. /*
  197.  *     D_ISODIGIT
  198.  *
  199.  *     this routine returns a non-zero value if the argument is an octal
  200.  *     digit, ie. between 0 and 7.
  201.  *
  202.  *     c -- the character to be tested
  203.  */
  204.  
  205. d_isodigit(c)
  206.   char  c;
  207.     {
  208.  
  209.     if ((c >= '0') && (c <= '7'))
  210.       return(1);
  211.     else
  212.       return(0);
  213.     }
  214.  
  215. /*
  216.  *     D_TRYFORK
  217.  *
  218.  *     the purpose of this routine is to fork, retrying 10 times if necessary
  219.  *     -1 is returned if failure after all retries
  220.  */
  221.  
  222. d_tryfork()
  223.     {
  224.     register int  try, pid;
  225.  
  226.     for (try = 0; try <= 9; try++)
  227.       {
  228.       pid = fork();
  229.  
  230.       if (pid != -1)
  231.         return(pid);
  232.       }
  233.  
  234.     return(-1);
  235.     }
  236.  
  237.  
  238. /*
  239.  *     D_MINIMUM
  240.  *
  241.  *     this routine returns the minimum of its two arguments
  242.  *
  243.  *     a, b -- the minimum of these values is returned
  244.  */
  245.  
  246. d_minimum(a, b)
  247.   register int  a, b;
  248.     {
  249.  
  250.     if (a < b)
  251.       return(a);
  252.     else
  253.       return(b);
  254.     }
  255.