"Fossies" - the Fresh Open Source Software Archive

Member "mlmmj-1.3.0/contrib/receivestrip/mlmmj_src/mygetline.c" (13 Mar 2012, 2092 Bytes) of package /linux/privat/mlmmj-1.3.0.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* Copyright (C) 2004 Morten K. Poulsen <morten at afdelingp.dk>
    2  * Copyright (C) 2004 Mads Martin Joergensen <mmj at mmj.dk>
    3  *
    4  * $Id$
    5  *
    6  * Permission is hereby granted, free of charge, to any person obtaining a copy
    7  * of this software and associated documentation files (the "Software"), to
    8  * deal in the Software without restriction, including without limitation the
    9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
   10  * sell copies of the Software, and to permit persons to whom the Software is
   11  * furnished to do so, subject to the following conditions:
   12  *
   13  * The above copyright notice and this permission notice shall be included in
   14  * all copies or substantial portions of the Software.
   15  *
   16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
   22  * IN THE SOFTWARE.
   23  */
   24 
   25 #include <stdlib.h>
   26 #include <string.h>
   27 #include <unistd.h>
   28 #include <errno.h>
   29 #include <stdio.h>
   30 
   31 #include "mygetline.h"
   32 #include "memory.h"
   33 
   34 static char *mygetuntil(int fd, int eof)
   35 {
   36     size_t i = 0, res, buf_size = BUFSIZE;  /* initial buffer size */
   37     char *buf, ch;
   38 
   39     buf = mymalloc(buf_size);
   40     buf[0] = '\0';
   41     while(1) {  
   42         res = read(fd, &ch, 1);
   43         if(res < 0) {
   44             if(errno == EINTR)
   45                 continue;
   46             else {
   47                 myfree(buf);
   48                 return NULL;
   49             }
   50         }
   51         if(res == 0) {
   52             if(buf[0]) {
   53                 buf[i] = '\0';
   54                 return buf;
   55             } else {
   56                 myfree(buf);
   57                 return NULL;
   58             }
   59         }
   60 
   61         if(i == buf_size - 1) {
   62             buf_size *= 2;
   63             buf = myrealloc(buf, buf_size);
   64         }
   65         buf[i++] = ch;
   66         if(eof != EOF && ch == eof) {
   67             buf[i] = '\0';
   68             return buf;
   69         }
   70     }
   71 }
   72 
   73 char *mygetline(int fd)
   74 {
   75     return mygetuntil(fd, '\n');
   76 }
   77 
   78 char *mygetcontent(int fd)
   79 {
   80     return mygetuntil(fd, EOF);
   81 }