home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / ircd4652.zip / ircd-df-4.6.5-os2 / src / support.c < prev    next >
C/C++ Source or Header  |  1997-12-29  |  10KB  |  521 lines

  1. /************************************************************************
  2.  *   IRC - Internet Relay Chat, common/support.c
  3.  *   Copyright (C) 1990, 1991 Armin Gruner
  4.  *
  5.  *   This program is free software; you can redistribute it and/or modify
  6.  *   it under the terms of the GNU General Public License as published by
  7.  *   the Free Software Foundation; either version 1, or (at your option)
  8.  *   any later version.
  9.  *
  10.  *   This program is distributed in the hope that it will be useful,
  11.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *   GNU General Public License for more details.
  14.  *
  15.  *   You should have received a copy of the GNU General Public License
  16.  *   along with this program; if not, write to the Free Software
  17.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #ifndef lint
  21. static  char sccsid[] = "@(#)support.c    2.21 4/13/94 1990, 1991 Armin Gruner;\
  22. 1992, 1993 Darren Reed";
  23. #endif
  24.  
  25. #include "config.h"
  26. #ifdef DYNIXPTX
  27. #include <sys/timers.h>
  28. #include <stddef.h>
  29. #endif
  30. #include "struct.h"
  31. #include "common.h"
  32. #include "sys.h"
  33. #ifdef _WIN32
  34. #include <io.h>
  35. #else
  36.  
  37. extern    int errno; /* ...seems that errno.h doesn't define this everywhere */
  38. #endif
  39. #ifndef    CLIENT_COMPILE
  40. extern    void    outofmemory();
  41. #endif
  42.  
  43. #ifdef NEED_STRTOKEN
  44. /*
  45. **     strtoken.c --      walk through a string of tokens, using a set
  46. **            of separators
  47. **            argv 9/90
  48. **
  49. **    $Id: support.c,v 1.2 1997/12/29 07:17:44 wd Exp $
  50. */
  51.  
  52. char *strtoken(save, str, fs)
  53. char **save;
  54. char *str, *fs;
  55. {
  56.     char *pos = *save;    /* keep last position across calls */
  57.     Reg1 char *tmp;
  58.  
  59.     if (str)
  60.     pos = str;        /* new string scan */
  61.  
  62.     while (pos && *pos && index(fs, *pos) != NULL)
  63.     pos++;              /* skip leading separators */
  64.  
  65.     if (!pos || !*pos)
  66.     return (pos = *save = NULL);     /* string contains only sep's */
  67.  
  68.     tmp = pos;             /* now, keep position of the token */
  69.  
  70.     while (*pos && index(fs, *pos) == NULL)
  71.     pos++;             /* skip content of the token */
  72.  
  73.     if (*pos)
  74.     *pos++ = '\0';        /* remove first sep after the token */
  75.     else
  76.     pos = NULL;        /* end of string */
  77.  
  78.     *save = pos;
  79.     return(tmp);
  80. }
  81. #endif /* NEED_STRTOKEN */
  82.  
  83. #ifdef    NEED_STRTOK
  84. /*
  85. ** NOT encouraged to use!
  86. */
  87.  
  88. char *strtok(str, fs)
  89. char *str, *fs;
  90. {
  91.     static char *pos;
  92.  
  93.     return strtoken(&pos, str, fs);
  94. }
  95.  
  96. #endif /* NEED_STRTOK */
  97.  
  98. #ifdef NEED_STRERROR
  99. /*
  100. **    strerror - return an appropriate system error string to a given errno
  101. **
  102. **           argv 11/90
  103. **    $Id: support.c,v 1.2 1997/12/29 07:17:44 wd Exp $
  104. */
  105.  
  106. char *strerror(err_no)
  107. int err_no;
  108. {
  109.     extern    char    *sys_errlist[];     /* Sigh... hopefully on all systems */
  110.     extern    int    sys_nerr;
  111.  
  112.     static    char    buff[40];
  113.     char    *errp;
  114.  
  115.     errp = (err_no > sys_nerr ? (char *)NULL : sys_errlist[err_no]);
  116.  
  117.     if (errp == (char *)NULL)
  118.         {
  119.         errp = buff;
  120. #ifndef _WIN32
  121.         (void) sprintf(errp, "Unknown Error %d", err_no);
  122. #else
  123.         switch (err_no)
  124.             {
  125.             case WSAECONNRESET:
  126.                 sprintf(errp, "Connection reset by peer");
  127.                 break;
  128.             default:
  129.                 sprintf(errp, "Unknown Error %d", err_no);
  130.                 break;
  131.             }
  132. #endif
  133.         }
  134.     return errp;
  135. }
  136.  
  137. #endif /* NEED_STRERROR */
  138.  
  139. /*
  140. **    inetntoa  --    changed name to remove collision possibility and
  141. **            so behaviour is gaurunteed to take a pointer arg.
  142. **            -avalon 23/11/92
  143. **    inet_ntoa --    returned the dotted notation of a given
  144. **            internet number (some ULTRIX don't have this)
  145. **            argv 11/90).
  146. **    inet_ntoa --    its broken on some Ultrix/Dynix too. -avalon
  147. **    $Id: support.c,v 1.2 1997/12/29 07:17:44 wd Exp $
  148. */
  149.  
  150. char    *inetntoa(in)
  151. char    *in;
  152. {
  153.     static    char    buf[16];
  154.     Reg1    u_char    *s = (u_char *)in;
  155.     Reg2    int    a,b,c,d;
  156.  
  157.     a = (int)*s++;
  158.     b = (int)*s++;
  159.     c = (int)*s++;
  160.     d = (int)*s++;
  161.     (void) sprintf(buf, "%d.%d.%d.%d", a,b,c,d );
  162.  
  163.     return buf;
  164. }
  165.  
  166. #ifdef NEED_INET_NETOF
  167. /*
  168. **    inet_netof --    return the net portion of an internet number
  169. **            argv 11/90
  170. **    $Id: support.c,v 1.2 1997/12/29 07:17:44 wd Exp $
  171. **
  172. */
  173.  
  174. int inet_netof(in)
  175. struct in_addr in;
  176. {
  177.     int addr = in.s_net;
  178.  
  179.     if (addr & 0x80 == 0)
  180.     return ((int) in.s_net);
  181.  
  182.     if (addr & 0x40 == 0)
  183.     return ((int) in.s_net * 256 + in.s_host);
  184.  
  185.     return ((int) in.s_net * 256 + in.s_host * 256 + in.s_lh);
  186. }
  187. #endif /* NEED_INET_NETOF */
  188.  
  189.  
  190. #if defined(DEBUGMODE) && !defined(CLIENT_COMPILE)
  191. void    dumpcore(msg, p1, p2, p3, p4, p5, p6, p7, p8, p9)
  192. char    *msg, *p1, *p2, *p3, *p4, *p5, *p6, *p7, *p8, *p9;
  193. {
  194.     static    time_t    lastd = 0;
  195.     static    int    dumps = 0;
  196.     char    corename[12];
  197.     time_t    now;
  198.     int    p;
  199.  
  200.     now = time(NULL);
  201.  
  202.     if (!lastd)
  203.         lastd = now;
  204.     else if (now - lastd < 60 && dumps > 2)
  205.         (void)s_die();
  206.     if (now - lastd > 60)
  207.         {
  208.         lastd = now;
  209.         dumps = 1;
  210.         }
  211.     else
  212.         dumps++;
  213. #ifndef _WIN32
  214.     p = getpid();
  215.     if (fork()>0) {
  216.         kill(p, 3);
  217.         kill(p, 9);
  218.     }
  219.     write_pidfile();
  220.     (void)sprintf(corename, "core.%d", p);
  221.     (void)rename("core", corename);
  222.     Debug((DEBUG_FATAL, "Dumped core : core.%d", p));
  223.     sendto_ops("Dumped core : core.%d", p);
  224. #endif
  225.     Debug((DEBUG_FATAL, msg, p1, p2, p3, p4, p5, p6, p7, p8, p9));
  226.     sendto_ops(msg, p1, p2, p3, p4, p5, p6, p7, p8, p9);
  227.         (void)s_die();
  228. }
  229.  
  230. static    char    *marray[20000];
  231. static    int    mindex = 0;
  232.  
  233. #define    SZ_EX    (sizeof(char *) + sizeof(size_t) + 4)
  234. #define    SZ_CHST    (sizeof(char *) + sizeof(size_t))
  235. #define    SZ_CH    (sizeof(char *))
  236. #define    SZ_ST    (sizeof(size_t))
  237.  
  238. char    *MyMalloc(x)
  239. size_t    x;
  240. {
  241.     register int    i;
  242.     register char    **s;
  243.     char    *ret;
  244.  
  245. #ifndef _WIN32
  246.     ret = (char *)malloc(x + (size_t)SZ_EX);
  247. #else
  248.     ret = (char *)GlobalAlloc(GPTR, x + (size_t)SZ_EX);
  249. #endif
  250.  
  251.     if (!ret)
  252.         {
  253. # ifndef    CLIENT_COMPILE
  254.         outofmemory();
  255. # else
  256.         perror("malloc");
  257.         exit(-1);
  258. # endif
  259.         }
  260.     bzero(ret, (int)x + SZ_EX);
  261.     bcopy((char *)&ret, ret, SZ_CH);
  262.     bcopy((char *)&x, ret + SZ_CH, SZ_ST);
  263.     bcopy("VAVA", ret + SZ_CHST + (int)x, 4);
  264.     Debug((DEBUG_MALLOC, "MyMalloc(%ld) = %#x", x, ret+8));
  265.     for(i = 0, s = marray; *s && i < mindex; i++, s++)
  266.         ;
  267.      if (i < 20000)
  268.         {
  269.         *s = ret;
  270.         if (i == mindex)
  271.             mindex++;
  272.         }
  273.     return ret + SZ_CHST;
  274.     }
  275.  
  276. char    *MyRealloc(x, y)
  277. char    *x;
  278. size_t    y;
  279.     {
  280.     register int    l;
  281.     register char    **s;
  282.     char    *ret, *cp;
  283.     size_t    i;
  284.     int    k;
  285.  
  286.     x -= SZ_CHST;
  287.     bcopy(x, (char *)&cp, SZ_CH);
  288.     bcopy(x + SZ_CH, (char *)&i, SZ_ST);
  289.     bcopy(x + (int)i + SZ_CHST, (char *)&k, 4);
  290.     if (bcmp((char *)&k, "VAVA", 4) || (x != cp))
  291.         dumpcore("MyRealloc %#x %d %d %#x %#x", x, y, i, cp, k);
  292. #ifndef _WIN32
  293.     ret = (char *)realloc(x, y + (size_t)SZ_EX);
  294. #else
  295.     ret = (char *)GlobalReAlloc(x, y + (size_t)SZ_EX, GMEM_MOVEABLE|GMEM_ZEROINIT);
  296. #endif
  297.  
  298.     if (!ret)
  299.         {
  300. # ifndef    CLIENT_COMPILE
  301.         outofmemory();
  302. # else
  303.         perror("realloc");
  304.         exit(-1);
  305. # endif
  306.         }
  307.     bcopy((char *)&ret, ret, SZ_CH);
  308.     bcopy((char *)&y, ret + SZ_CH, SZ_ST);
  309.     bcopy("VAVA", ret + SZ_CHST + (int)y, 4);
  310.     Debug((DEBUG_NOTICE, "MyRealloc(%#x,%ld) = %#x", x, y, ret + SZ_CHST));
  311.     for(l = 0, s = marray; *s != x && l < mindex; l++, s++)
  312.         ;
  313.      if (l < mindex)
  314.         *s = NULL;
  315.     else if (l == mindex)
  316.         Debug((DEBUG_MALLOC, "%#x !found", x));
  317.     for(l = 0, s = marray; *s && l < mindex; l++,s++)
  318.         ;
  319.      if (l < 20000)
  320.         {
  321.         *s = ret;
  322.         if (l == mindex)
  323.             mindex++;
  324.         }
  325.     return ret + SZ_CHST;
  326.     }
  327.  
  328. void    MyFree(x)
  329. char    *x;
  330. {
  331.     size_t    i;
  332.     char    *j;
  333.     u_char    k[4];
  334.     register int    l;
  335.     register char    **s;
  336.  
  337.     if (!x)
  338.         return;
  339.     x -= SZ_CHST;
  340.  
  341.     bcopy(x, (char *)&j, SZ_CH);
  342.     bcopy(x + SZ_CH, (char *)&i, SZ_ST);
  343.     bcopy(x + SZ_CHST + (int)i, (char *)k, 4);
  344.  
  345.     if (bcmp((char *)k, "VAVA", 4) || (j != x))
  346.         dumpcore("MyFree %#x %ld %#x %#x", x, i, j,
  347.              (k[3]<<24) | (k[2]<<16) | (k[1]<<8) | k[0]);
  348.  
  349. #undef    free
  350. #ifndef _WIN32
  351.     (void)free(x);
  352. #else
  353.     (void)GlobalFree(x);
  354. #endif
  355. #define    free(x)    MyFree(x)
  356.     Debug((DEBUG_MALLOC, "MyFree(%#x)",x + SZ_CHST));
  357.  
  358.     for (l = 0, s = marray; *s != x && l < mindex; l++, s++)
  359.         ;
  360.     if (l < mindex)
  361.         *s = NULL;
  362.     else if (l == mindex)
  363.         Debug((DEBUG_MALLOC, "%#x !found", x));
  364. }
  365.  
  366. #else
  367. char    *MyMalloc(x)
  368. size_t    x;
  369. {
  370. #ifndef _WIN32
  371.     char *ret = (char *)malloc(x);
  372. #else
  373.     char *ret = (char *)GlobalAlloc(GPTR, x);
  374. #endif
  375.  
  376.     if (!ret)
  377.         {
  378. # ifndef    CLIENT_COMPILE
  379.         outofmemory();
  380. # else
  381.         perror("malloc");
  382.         exit(-1);
  383. # endif
  384.         }
  385.     return    ret;
  386. }
  387.  
  388. char    *MyRealloc(x, y)
  389. char    *x;
  390. size_t    y;
  391.     {
  392. #ifndef _WIN32
  393.     char *ret = (char *)realloc(x, y);
  394. #else
  395.     char *ret = (char *)GlobalReAlloc(x, y, GMEM_MOVEABLE|GMEM_ZEROINIT);
  396. #endif
  397.  
  398.     if (!ret)
  399.         {
  400. # ifndef CLIENT_COMPILE
  401.         outofmemory();
  402. # else
  403.         perror("realloc");
  404.         exit(-1);
  405. # endif
  406.         }
  407.     return ret;
  408.     }
  409. #endif
  410.  
  411.  
  412. /*
  413. ** read a string terminated by \r or \n in from a fd
  414. **
  415. ** Created: Sat Dec 12 06:29:58 EST 1992 by avalon
  416. ** Returns:
  417. **    0 - EOF
  418. **    -1 - error on read
  419. **     >0 - number of bytes returned (<=num)
  420. ** After opening a fd, it is necessary to init dgets() by calling it as
  421. **    dgets(x,y,0);
  422. ** to mark the buffer as being empty.
  423. */
  424. int    dgets(fd, buf, num)
  425. int    fd, num;
  426. char    *buf;
  427. {
  428.     static    char    dgbuf[8192];
  429.     static    char    *head = dgbuf, *tail = dgbuf;
  430.     register char    *s, *t;
  431.     register int    n, nr;
  432.  
  433.     /*
  434.     ** Sanity checks.
  435.     */
  436.     if (head == tail)
  437.         *head = '\0';
  438.     if (!num)
  439.         {
  440.         head = tail = dgbuf;
  441.         *head = '\0';
  442.         return 0;
  443.         }
  444.     if (num > sizeof(dgbuf) - 1)
  445.         num = sizeof(dgbuf) - 1;
  446. dgetsagain:
  447.     if (head > dgbuf)
  448.         {
  449.         for (nr = tail - head, s = head, t = dgbuf; nr > 0; nr--)
  450.             *t++ = *s++;
  451.         tail = t;
  452.         head = dgbuf;
  453.         }
  454.     /*
  455.     ** check input buffer for EOL and if present return string.
  456.     */
  457.     if (head < tail &&
  458.         ((s = index(head, '\n')) || (s = index(head, '\r'))) && s < tail)
  459.         {
  460.         n = MIN(s - head + 1, num);    /* at least 1 byte */
  461. dgetsreturnbuf:
  462.         bcopy(head, buf, n);
  463.         head += n;
  464.         if (head == tail)
  465.             head = tail = dgbuf;
  466.         return n;
  467.         }
  468.  
  469.     if (tail - head >= num)        /* dgets buf is big enough */
  470.         {
  471.         n = num;
  472.         goto dgetsreturnbuf;
  473.         }
  474.  
  475.     n = sizeof(dgbuf) - (tail - dgbuf) - 1;
  476.     nr = read(fd, tail, n);
  477.     if (nr == -1)
  478.         {
  479.         head = tail = dgbuf;
  480.         return -1;
  481.         }
  482.     if (!nr)
  483.         {
  484.         if (head < tail)
  485.             {
  486.             n = MIN(tail - head, num);
  487.             goto dgetsreturnbuf;
  488.             }
  489.         head = tail = dgbuf;
  490.         return 0;
  491.         }
  492.     tail += nr;
  493.     *tail = '\0';
  494.     for (t = head; (s = index(t, '\n')); )
  495.         {
  496.         if ((s > head) && (s > dgbuf))
  497.             {
  498.             t = s-1;
  499.             for (nr = 0; *t == '\\'; nr++)
  500.                 t--;
  501.             if (nr & 1)
  502.                 {
  503.                 t = s+1;
  504.                 s--;
  505.                 nr = tail - t;
  506.                 while (nr--)
  507.                     *s++ = *t++;
  508.                 tail -= 2;
  509.                 *tail = '\0';
  510.                 }
  511.             else
  512.                 s++;
  513.             }
  514.         else
  515.             s++;
  516.         t = s;
  517.         }
  518.     *tail = '\0';
  519.     goto dgetsagain;
  520. }
  521.