home *** CD-ROM | disk | FTP | other *** search
/ BUG 14 / BUGCD1998_05.ISO / internet / tcp4u / tcp4u330.exe / tcp4u.330 / src / util.c < prev   
C/C++ Source or Header  |  1998-03-05  |  4KB  |  146 lines

  1. /*
  2.  * Tcp4u v 3.30         Last Revision 27/02/1998  3.30.02
  3.  *
  4.  *===========================================================================
  5.  *
  6.  * Project: Tcp4u,      Library for tcp protocol
  7.  * File:    util.c
  8.  * Purpose: A few utilities functions
  9.  *
  10.  *===========================================================================
  11.  *
  12.  * This software is Copyright (c) 1996-1998 by Philippe Jounin
  13.  *
  14.  * This library is free software; you can redistribute it and/or
  15.  * modify it under the terms of the GNU Library General Public
  16.  * License as published by the Free Software Foundation; either
  17.  * version 2 of the License, or (at your option) any later version.
  18.  * 
  19.  * This library is distributed in the hope that it will be useful,
  20.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  22.  * Library General Public License for more details.
  23.  * 
  24.  * You should have received a copy of the GNU Library General Public
  25.  * License along with this library; if not, write to the
  26.  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  27.  * Boston, MA  02111-1307, USA.
  28.  *
  29.  *
  30.  *  If you make modifications to this software that you feel
  31.  *  increases it usefulness for the rest of the community, please
  32.  *  email the changes, enhancements, bug fixes as well as any and
  33.  *  all ideas to me. This software is going to be maintained and
  34.  *  enhanced as deemed necessary by the community.
  35.  *
  36.  *
  37.  *             Philippe Jounin (ph.jounin@computer.org)
  38.  */
  39.  
  40.  
  41. #include "build.h"
  42.  
  43. /* this definition will be enough for our use */
  44. #undef  toupper
  45. #define toupper(x)  ( (x) & 0xDF )
  46.  
  47.  
  48.  
  49. /* ------------------------------------------------------------------------- */
  50. /* Conversion d'un nom de machine en adresse IP                              */
  51. /* Le nom est soit une "dotted address" soit un alias                        */ 
  52. /* return : INADDR_NONE or IP address                                         */ 
  53. /* ------------------------------------------------------------------------- */
  54. struct in_addr Tcp4uGetIPAddr (LPCSTR szHost)
  55. {
  56. struct hostent far *  lpHostEnt;
  57. struct in_addr        sin_addr;
  58.  
  59.   Tcp4uLog (LOG4U_INTERN, "Tcp4uGetIPAddr host %s", szHost);
  60.  
  61.   sin_addr.s_addr = inet_addr (szHost); /* doted address */
  62.   if (sin_addr.s_addr==INADDR_NONE)     /* si pas une doted address  */
  63.   {                      /* regarder le fichier hosts */
  64.       Tcp4uLog (LOG4U_CALL, "gethostbyname host %s", szHost);
  65.       lpHostEnt = gethostbyname (szHost);
  66.       if (lpHostEnt!=NULL)
  67.          memcpy (& sin_addr.s_addr, lpHostEnt->h_addr, lpHostEnt->h_length);
  68.       else       
  69.       {
  70.         Tcp4uLog (LOG4U_ERROR, "gethostbyname host %s", szHost);
  71.       }
  72.  
  73.     }
  74. return sin_addr;
  75. } /* Tcp4uGetIPAddr */
  76.  
  77.  
  78.  
  79. /* -------------------------------------- */
  80. /* a self-made atoi                       */
  81. /* -------------------------------------- */
  82.  
  83. /* Note: ne pas utiliser la fonction isdigit: qui donne des */
  84. /* resultats errones en Windows                             */
  85. #define IsDigit(x)   ( (x)>='0' &&  (x)<='9')
  86.  
  87.  
  88. int Tcp4uAtoi (LPCSTR p)
  89. {
  90. int n;
  91.   Tcp4uLog (LOG4U_INTERN, "Tcp4uAtoi");
  92.  
  93.    for (n=0 ;  IsDigit (*p) ; p++)
  94.      {
  95.        n *= 10;
  96.        n += (*p - '0');
  97.      }
  98. return n;   
  99. } /* tcp4u_atoi */
  100.  
  101.  
  102. long Tcp4uAtol (LPCSTR p)
  103. {
  104. long n;
  105.   Tcp4uLog (LOG4U_INTERN, "Tcp4uAtol");
  106.  
  107.    for (n=0 ;  IsDigit (*p) ; p++)
  108.      {
  109.        n *= 10;
  110.        n += (*p - '0');
  111.      }
  112. return n;   
  113. } /* Tcp4uAtol */
  114.  
  115.  
  116. /* a unix function which is not available under Windows */
  117. /* should have same proto than strncmp                  */
  118. /* adapted from the GNU strncasecmp                     */
  119. int Tcp4uStrncasecmp (LPCSTR s1, LPCSTR s2, size_t n)
  120. {
  121. char c1, c2;
  122.    for ( ; n!=0 ; --n)
  123.    {
  124.       c1 = *s1++;
  125.       c2 = *s2++;
  126.       if (toupper (c1)!=toupper(c2))    return  toupper (c1) - toupper(c2);
  127.       if (c1==0)                        return 0;
  128.    }
  129. return  0;
  130. } /* Strncasecmp */
  131.  
  132. /* ------------------------------------------------------------------------- */
  133. /* a case-insentive strstr                                                   */
  134. /* ------------------------------------------------------------------------- */
  135. LPSTR Tcp4uStrIStr (LPCSTR s1, LPCSTR s2)
  136. {
  137. size_t nLength = Strlen(s2);
  138.     for (;;)
  139.     {
  140.        while (*s1!=0  &&  toupper(*s1)!=toupper(*s2) )  s1++;
  141.        if (*s1==0)   return NULL;
  142.        if (Tcp4uStrncasecmp (s1, s2, nLength)==0)  return  (LPSTR) s1;
  143.        s1++;
  144.     }
  145. } /* Tcp4uStrIStr  */
  146.