home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / unix / unixlib_1 / !UnixLib37_netlib_netlib_c_proto < prev    next >
Encoding:
Text File  |  1996-07-28  |  4.3 KB  |  237 lines

  1. /****************************************************************************
  2.  *
  3.  * $Source$
  4.  * $Date$
  5.  * $Revision$
  6.  * $State$
  7.  * $Author$
  8.  *
  9.  * $Log$
  10.  ***************************************************************************/
  11.  
  12. static const char rcs_id[] = "$Id$";
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17.  
  18. #include <netdb.h>
  19. #include <netinet/in.h>
  20. #include <sys/socket.h>
  21.  
  22. #include <socketlib.h>
  23.  
  24. /*
  25.  * File handle for the protocols file
  26.  */
  27. static FILE *protofile = NULL;
  28.  
  29. /*
  30.  * Keep the file open between calls to these routines?
  31.  */
  32. static int keepopen = 0;
  33.  
  34. /*
  35.  * Local routines
  36.  */
  37. static int __setprotoent (int allowrewind);
  38. static struct protoent *__getprotoent (void);
  39.  
  40. /*
  41.  * Open and rewind the protocols file
  42.  */
  43. int
  44. setprotoent (int stayopen)
  45. {
  46.   /* Record whether the file should be kept open */
  47.   keepopen = stayopen;
  48.  
  49.   return __setprotoent (1);
  50. }
  51.  
  52. /*
  53.  * Do the real work of opening/rewinding the protocols file
  54.  */
  55. static int
  56. __setprotoent (int allowrewind)
  57. {
  58.   /* Open or rewind the file as necessary */
  59.   if (protofile)
  60.     {
  61.       if (allowrewind)
  62.     rewind (protofile);
  63.     }
  64.   else
  65.     {
  66.       protofile = fopen ("InetDBase:Protocols", "r");
  67.     }
  68.  
  69.   return (protofile == NULL) ? -1 : 0;
  70. }
  71.  
  72. /*
  73.  * Fetch the next entry from the protocols file
  74.  */
  75. struct protoent *
  76. getprotoent ()
  77. {
  78.   struct protoent *proto;
  79.  
  80.   /* Open the file if necessary */
  81.   if (protofile == NULL)
  82.     if (__setprotoent (0) == -1)
  83.       return NULL;
  84.  
  85.   /* Do the actual read */
  86.   proto = __getprotoent ();
  87.  
  88.   /* Close the file unless the user has prohibited it */
  89.   if (!keepopen)
  90.     endprotoent ();
  91.  
  92.   return proto;
  93. }
  94.  
  95. /*
  96.  * Do the real work of getting an entry from the file
  97.  */
  98. struct protoent *
  99. __getprotoent ()
  100. {
  101.   static struct protoent proto =
  102.   {
  103.     NULL, NULL, 0
  104.   };
  105.  
  106.   char **item;
  107.   char *line;
  108.   char *element;
  109.   int aliases;
  110.  
  111.   /* Free up any memory in use */
  112.   if (proto.p_name)
  113.     {
  114.       for (item = proto.p_aliases; *item; item++)
  115.     free (*item);
  116.       free (proto.p_name);
  117.       free (proto.p_aliases);
  118.  
  119.       proto.p_name = NULL;
  120.     }
  121.  
  122.   /* Read a line from the file */
  123.   if ((line = __socketlib_readline (protofile)) == NULL)
  124.     return NULL;
  125.  
  126.   /* Extract the offical protocol name from the line */
  127.   element = strtok (line, " \t");
  128.   proto.p_name = strdup (element);
  129.  
  130.   /* Extract the port number from the line */
  131.   element = strtok (NULL, " \t");
  132.   proto.p_proto = atoi (element);
  133.  
  134.   /* Initialise the alias list */
  135.   proto.p_aliases = malloc (sizeof (char *));
  136.   proto.p_aliases[0] = NULL;
  137.   aliases = 1;
  138.  
  139.   /* Extract the aliases */
  140.   while ((element = strtok (NULL, " \t")) != NULL)
  141.     {
  142.       aliases += 1;
  143.       proto.p_aliases = realloc (proto.p_aliases, aliases * sizeof (char *));
  144.       proto.p_aliases[aliases - 2] = strdup (element);
  145.       proto.p_aliases[aliases - 1] = NULL;
  146.     }
  147.  
  148.   return &proto;
  149. }
  150.  
  151. /*
  152.  * Close the protocols file
  153.  */
  154. int
  155. endprotoent ()
  156. {
  157.   int status = 0;
  158.  
  159.   /* If its open, close it */
  160.   if (protofile)
  161.     {
  162.       status = fclose (protofile);
  163.       protofile = 0;
  164.     }
  165.  
  166.   return status;
  167. }
  168.  
  169. /*
  170.  * Search the protocols file for a given protocol name
  171.  */
  172. struct protoent *
  173. getprotobyname (const char *name)
  174. {
  175.   struct protoent *proto;
  176.   char **alias;
  177.  
  178.   /* Open/rewind the file */
  179.   if (__setprotoent (1) == -1)
  180.     return NULL;
  181.  
  182.   /* Look through the file for a match */
  183.   while ((proto = __getprotoent ()) != NULL)
  184.     {
  185.  
  186.       /* Does the offical name match? */
  187.       if (strcmp (proto->p_name, name) == 0)
  188.     break;
  189.  
  190.       /* Do any of the aliases match? */
  191.       for (alias = proto->p_aliases; *alias; alias++)
  192.     {
  193.       if (strcmp (*alias, name) == 0)
  194.         break;
  195.     }
  196.  
  197.       /* Did any of the aliases match? */
  198.       if (*alias)
  199.     break;
  200.     }
  201.  
  202.   /* Close the file unless the user has prohibited it */
  203.   if (!keepopen)
  204.     endprotoent ();
  205.  
  206.   return proto;
  207. }
  208.  
  209. /*
  210.  * Search the protocols file for a given number
  211.  */
  212. struct protoent *
  213. getprotobynumber (int protonum)
  214. {
  215.   struct protoent *proto;
  216.  
  217.   /* Open/rewind the file */
  218.   if (__setprotoent (1) == -1)
  219.     return NULL;
  220.  
  221.   /* Look through the file for a match */
  222.   while ((proto = __getprotoent ()) != NULL)
  223.     {
  224.  
  225.       /* If the protocol number matches, we've found it */
  226.       if (proto->p_proto == protonum)
  227.     break;
  228.  
  229.     }
  230.  
  231.   /* Close the file unless the user has prohibited it */
  232.   if (!keepopen)
  233.     endprotoent ();
  234.  
  235.   return proto;
  236. }
  237.