"Fossies" - the Fresh Open Source Software Archive

Member "nss-mdns-0.10/src/util.c" (27 Apr 2006, 4807 Bytes) of package /linux/misc/dns/old/nss-mdns-0.10.tar.gz:


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 /* $Id: util.c 91 2006-04-27 23:24:34Z lennart $ */
    2 
    3 /***
    4   This file is part of nss-mdns.
    5  
    6   nss-mdns is free software; you can redistribute it and/or modify it
    7   under the terms of the GNU Lesser General Public License as
    8   published by the Free Software Foundation; either version 2 of the
    9   License, or (at your option) any later version.
   10  
   11   nss-mdns is distributed in the hope that it will be useful, but
   12   WITHOUT ANY WARRANTY; without even the implied warranty of
   13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   14   General Public License for more details.
   15  
   16   You should have received a copy of the GNU Lesser General Public
   17   License along with nss-mdns; if not, write to the Free Software
   18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
   19   USA.
   20 ***/
   21 
   22 #ifdef HAVE_CONFIG_H
   23 #include <config.h>
   24 #endif
   25 
   26 #include <sys/select.h>
   27 #include <errno.h>
   28 #include <string.h>
   29 #include <assert.h>
   30 #include <fcntl.h>
   31 
   32 #include "util.h"
   33 
   34 #ifdef ENABLE_LEGACY
   35 
   36 /* Calculate the difference between the two specfified timeval
   37  * timestamsps. */
   38 usec_t timeval_diff(const struct timeval *a, const struct timeval *b) {
   39     usec_t r;
   40     assert(a && b);
   41 
   42     /* Check which whan is the earlier time and swap the two arguments if reuqired. */
   43     if (timeval_cmp(a, b) < 0) {
   44         const struct timeval *c;
   45         c = a;
   46         a = b;
   47         b = c;
   48     }
   49 
   50     /* Calculate the second difference*/
   51     r = ((usec_t) a->tv_sec - b->tv_sec)* 1000000;
   52 
   53     /* Calculate the microsecond difference */
   54     if (a->tv_usec > b->tv_usec)
   55         r += ((usec_t) a->tv_usec - b->tv_usec);
   56     else if (a->tv_usec < b->tv_usec)
   57         r -= ((usec_t) b->tv_usec - a->tv_usec);
   58 
   59     return r;
   60 }
   61 
   62 /* Compare the two timeval structs and return 0 when equal, negative when a < b, positive otherwse */
   63 int timeval_cmp(const struct timeval *a, const struct timeval *b) {
   64     assert(a && b);
   65 
   66     if (a->tv_sec < b->tv_sec)
   67         return -1;
   68 
   69     if (a->tv_sec > b->tv_sec)
   70         return 1;
   71 
   72     if (a->tv_usec < b->tv_usec)
   73         return -1;
   74 
   75     if (a->tv_usec > b->tv_usec)
   76         return 1;
   77 
   78     return 0;
   79 }
   80 
   81 /* Return the time difference between now and the specified timestamp */
   82 usec_t timeval_age(const struct timeval *tv) {
   83     struct timeval now;
   84     assert(tv);
   85     gettimeofday(&now, NULL);
   86     return timeval_diff(&now, tv);
   87 }
   88 
   89 /* Add the specified time inmicroseconds to the specified timeval structure */
   90 void timeval_add(struct timeval *tv, usec_t v) {
   91     unsigned long secs;
   92     assert(tv);
   93     
   94     secs = (v/1000000);
   95     tv->tv_sec += (unsigned long) secs;
   96     v -= secs*1000000;
   97 
   98     tv->tv_usec += v;
   99 
  100     /* Normalize */
  101     while (tv->tv_usec >= 1000000) {
  102         tv->tv_sec++;
  103         tv->tv_usec -= 1000000;
  104     }
  105 }
  106 
  107 int set_nonblock(int fd) {
  108     int n;
  109     assert(fd >= 0);
  110 
  111     if ((n = fcntl(fd, F_GETFL)) < 0)
  112         return -1;
  113 
  114     if (n & O_NONBLOCK)
  115         return 0;
  116 
  117     return fcntl(fd, F_SETFL, n|O_NONBLOCK);
  118 }
  119 
  120 int wait_for_write(int fd, struct timeval *end) {
  121     struct timeval now;
  122 
  123     if (end)
  124         gettimeofday(&now, NULL);
  125     
  126     for (;;) {
  127         struct timeval tv;
  128         fd_set fds;
  129         int r;
  130         
  131         FD_ZERO(&fds);
  132         FD_SET(fd, &fds);
  133 
  134         if (end) {
  135             if (timeval_cmp(&now, end) >= 0)
  136                 return 1;
  137 
  138             tv.tv_sec = tv.tv_usec = 0;
  139             timeval_add(&tv, timeval_diff(end, &now));
  140         }
  141 
  142         if ((r = select(fd+1, NULL, &fds, NULL, end ? &tv : NULL)) < 0) {
  143             if (errno != EINTR)
  144                 return -1;
  145         } else if (r == 0)
  146             return 1;
  147         else {
  148             if (FD_ISSET(fd, &fds))
  149                 return 0;
  150         }
  151 
  152         if (end)
  153             gettimeofday(&now, NULL);
  154     }
  155 }
  156 
  157 int wait_for_read(int fd, struct timeval *end) {
  158     struct timeval now;
  159 
  160     if (end)
  161         gettimeofday(&now, NULL);
  162 
  163     for (;;) {
  164         struct timeval tv;
  165         fd_set fds;
  166         int r;
  167         
  168         FD_ZERO(&fds);
  169         FD_SET(fd, &fds);
  170 
  171         if (end) {
  172             if (timeval_cmp(&now, end) >= 0)
  173                 return 1;
  174             
  175             tv.tv_sec = tv.tv_usec = 0;
  176             timeval_add(&tv, timeval_diff(end, &now));
  177         }
  178         
  179         if ((r = select(fd+1, &fds, NULL, NULL, end ? &tv : NULL)) < 0) {
  180             if (errno != EINTR)
  181                 return -1;
  182         } else if (r == 0) 
  183             return 1;
  184         else {
  185             
  186             if (FD_ISSET(fd, &fds))
  187                 return 0;
  188         }
  189 
  190         if (end)
  191             gettimeofday(&now, NULL);
  192     }
  193 }
  194 
  195 #endif
  196 
  197 int set_cloexec(int fd) {
  198     int n;
  199     assert(fd >= 0);
  200     
  201     if ((n = fcntl(fd, F_GETFD)) < 0)
  202         return -1;
  203 
  204     if (n & FD_CLOEXEC)
  205         return 0;
  206 
  207     return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
  208 }
  209