"Fossies" - the Fresh Open Source Software Archive

Member "rgdbm-2.1.42/xfr.c" (20 Jun 2007, 2042 Bytes) of package /linux/privat/old/rgdbm-2.1.42.tgz:


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. For more information about "xfr.c" see the Fossies "Dox" file reference documentation.

    1 /* 
    2  * RGDBM basic i/o function (C) Peter T. Breuer 2007.
    3  *
    4  * This code is released under the GNU General Public Licence (GPL)
    5  * version 2 or such later version as the FSF may release in the
    6  * fullness of time (when this licence automatically becomes THAT
    7  * licence). See GPL_V2 file in the code archive for details.
    8  */
    9 
   10 
   11 #include "config.h"
   12 
   13 #ifdef HAVE_UNISTD_H
   14 # include <unistd.h>             /* for read, write etc. */
   15 #else
   16 extern int read();
   17 extern int write();
   18 #endif          /* defined(HAVE_UNISTD_H) */
   19 #ifdef HAVE_SYS_TIME_H
   20 # include <sys/time.h>           /* for struct timeval */
   21 #endif /* defined(HAVE_SYS_TIME_H) */
   22 #ifdef HAVE_TIME_H
   23 # include <time.h>               /* also for struct timeval */
   24 #endif /* defined(HAVE_TIME_H) */
   25 #include <errno.h>              /* for EINVAL etc. */
   26 
   27 #ifdef HAVE_GDBM_H         /* won't get far without gdbm.h */
   28 # include <gdbm.h>
   29 #elif defined(HAVE_NDBM_H) /* worth checking for ndbm too */
   30 # include <ndbm.h>
   31 #elif defined(HAVE_DBM_H)  /* even worth checking for dbm */
   32 # include <dbm.h>
   33 #endif
   34 
   35 #include "io.h"
   36 #include "xfr.h"
   37 
   38 #ifndef MIN
   39 # define MIN(x,y) ({ typeof(x) _x = (x); typeof(y) _y = (y); (_x<_y)?_x:_y; })
   40 #endif
   41 
   42 /*
   43  * Transfer data in either direction. @rw = RGDBM_WRITE, RGDBM_READ
   44  */
   45 int
   46 rgdbm_xfr(struct io_ctx *ctx, char *buf, size_t len, int rw) {
   47 
   48     int n, tot = 0;
   49 
   50     while (tot < len) {
   51 
   52         if (rw) {
   53                 n = ctx->write(ctx, buf + tot , len - tot);
   54         } else {
   55             if (buf) {
   56                 n = ctx->read(ctx, buf + tot , len - tot);
   57             } else {
   58                 // skip
   59                 static char skipbuf[1024];
   60                 n = ctx->read(ctx, skipbuf, MIN(len - tot, sizeof(skipbuf)));
   61             }
   62         }
   63 
   64         while (n == 0) {
   65 
   66             struct timeval tv = { .tv_sec = 1, .tv_usec = 0, };
   67             n = ctx->select(ctx, rw, &tv);
   68         }
   69 
   70         if (n < 0)  {
   71             int err = -errno;
   72             return err < 0 ? err : -EINVAL;
   73         }
   74 
   75         tot += n;
   76     }
   77     return tot;
   78 }
   79