home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / yagirc-0.51.tar.gz / yagirc-0.51.tar / yagirc-0.51 / misc.c < prev    next >
C/C++ Source or Header  |  1998-05-06  |  2KB  |  107 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #include <glib.h>
  5.  
  6. #include <unistd.h>
  7. #include <errno.h>
  8.  
  9. #include "network.h"
  10.  
  11. GList *glist_case_find_string(GList *list, char *str)
  12. {
  13.     for (list = g_list_first(list); list != NULL; list = list->next)
  14.     {
  15.         if (strcasecmp((char *) list->data, str) == 0)
  16.             return list;
  17.     }
  18.  
  19.     return NULL;
  20. }
  21.  
  22. GList *glist_find_string(GList *list, char *str)
  23. {
  24.     for (list = g_list_first(list); list != NULL; list = list->next)
  25.     {
  26.         if (strcmp((char *) list->data, str) == 0)
  27.             return list;
  28.     }
  29.  
  30.     return NULL;
  31. }
  32.  
  33. /* Read line from somewhere..
  34.  
  35.     socket : 0 = read from file/pipe, 1 = read from socket
  36.     handle : file/socket handle to read from
  37.     str    : where to put line
  38.     buf    : temp buffer
  39.     bufsize: temp buffer size
  40.     bufpos : current position in temp buffer
  41. */
  42. int read_line(int socket, int handle, char *str, char *buf, int bufsize, int *bufpos)
  43. {
  44.     int len, pos, bufs;
  45.  
  46.     if (handle == -1 || str == NULL || buf == NULL || bufpos == NULL) return -1;
  47.  
  48.     *str = '\0';
  49.  
  50.     if (socket)
  51.     {
  52.         len = net_receive(handle, buf+*bufpos, bufsize-*bufpos);
  53.     }
  54.     else
  55.     {
  56.         len = read(handle, buf+*bufpos, bufsize-*bufpos);
  57.     }
  58.     if (len < 0 && *bufpos != 0)
  59.     {
  60.         /* error/connection lost but still something in buffer.. */
  61.         for (pos = 0; pos < *bufpos; pos++)
  62.         {
  63.             if (buf[pos] == 13 || buf[pos] == 10)
  64.             {
  65.                 len = 0;
  66.                 break;
  67.             }
  68.         }
  69.     }
  70.  
  71.     if (len < 0)
  72.     {
  73.         if (errno == EAGAIN || errno == EWOULDBLOCK) return 0;
  74.         return -1;
  75.     }
  76.  
  77.     bufs = len+*bufpos;
  78.     if (bufs == 0) return 0; /* nothing came.. */
  79.  
  80.     for (pos = 0; pos < bufs; pos++)
  81.     {
  82.         if (buf[pos] == 13 || buf[pos] == 10)
  83.         {
  84.             /* end of line */
  85.             memcpy(str, buf, pos); str[pos] = '\0';
  86.  
  87.             if (buf[pos] == 13 && pos+1 < len && buf[pos+1] == 10) pos++;
  88.  
  89.             memmove(buf, buf+pos+1, bufs-(pos+1));
  90.             *bufpos = bufs-(pos+1);
  91.             return 1;
  92.         }
  93.     }
  94.  
  95.     if (len+*bufpos == bufsize)
  96.     {
  97.         /* buffer overrun... */
  98.         memcpy(str, buf, bufsize-1); str[bufsize-1] = '\0';
  99.         *bufpos = 0;
  100.         return 1;
  101.     }
  102.  
  103.     /* EOL wasn't found, wait for more data.. */
  104.     *bufpos = bufs;
  105.     return 0;
  106. }
  107.