"Fossies" - the Fresh Open Source Software Archive

Member "vnstat-2.9/src/datacache.c" (6 Jul 2019, 3189 Bytes) of package /linux/misc/vnstat-2.9.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. For more information about "datacache.c" see the Fossies "Dox" file reference documentation.

    1 #include "common.h"
    2 #include "datacache.h"
    3 
    4 int datacache_add(datacache **dc, const char *interface, const short sync)
    5 {
    6     datacache *newdc;
    7 
    8     newdc = malloc(sizeof(datacache));
    9     if (newdc == NULL) {
   10         return 0;
   11     }
   12     newdc->next = *dc;
   13     *dc = newdc;
   14 
   15     strncpy_nt((*dc)->interface, interface, 32);
   16     (*dc)->active = 1;
   17     (*dc)->filled = 0;
   18     (*dc)->syncneeded = sync;
   19     (*dc)->currx = 0;
   20     (*dc)->curtx = 0;
   21     (*dc)->updated = time(NULL);
   22     (*dc)->log = NULL;
   23 
   24     return 1;
   25 }
   26 
   27 int datacache_remove(datacache **dc, const char *interface)
   28 {
   29     int ret = 0;
   30     datacache *dc_prev, *dc_head;
   31 
   32     dc_head = *dc;
   33     dc_prev = *dc;
   34 
   35     if (*dc == NULL) {
   36         return ret;
   37     }
   38 
   39     /* handle list head remove */
   40     if (strcmp((*dc)->interface, interface) == 0) {
   41         *dc = (*dc)->next;
   42         xferlog_clear(&dc_prev->log);
   43         free(dc_prev);
   44         return 1;
   45     }
   46 
   47     *dc = (*dc)->next;
   48 
   49     /* handle other locations */
   50     while (*dc != NULL) {
   51         if (strcmp((*dc)->interface, interface) == 0) {
   52             dc_prev->next = (*dc)->next;
   53             xferlog_clear(&(*dc)->log);
   54             free(*dc);
   55             ret = 1;
   56             break;
   57         }
   58         dc_prev = *dc;
   59         *dc = (*dc)->next;
   60     }
   61 
   62     *dc = dc_head;
   63     return ret;
   64 }
   65 
   66 void datacache_clear(datacache **dc)
   67 {
   68     datacache *dc_prev;
   69 
   70     while (*dc != NULL) {
   71         dc_prev = *dc;
   72         *dc = (*dc)->next;
   73         xferlog_clear(&dc_prev->log);
   74         free(dc_prev);
   75     }
   76 }
   77 
   78 int datacache_count(datacache **dc)
   79 {
   80     int count = 0;
   81     datacache *cacheiterator = *dc;
   82 
   83     while (cacheiterator != NULL) {
   84         count++;
   85         cacheiterator = cacheiterator->next;
   86     }
   87     return count;
   88 }
   89 
   90 int datacache_activecount(datacache **dc)
   91 {
   92     int count = 0;
   93     datacache *cacheiterator = *dc;
   94 
   95     while (cacheiterator != NULL) {
   96         if (cacheiterator->active) {
   97             count++;
   98         }
   99         cacheiterator = cacheiterator->next;
  100     }
  101     return count;
  102 }
  103 
  104 void datacache_debug(datacache **dc)
  105 {
  106     int i = 1;
  107     datacache *cacheiterator = *dc;
  108 
  109     if (cacheiterator == NULL) {
  110         printf("cache: empty\n");
  111         return;
  112     }
  113 
  114     printf("cache: ");
  115     while (cacheiterator != NULL) {
  116         printf(" %d: \"%s\" (", i, cacheiterator->interface);
  117         xferlog_debug(&cacheiterator->log, 0);
  118         printf(")  ");
  119         cacheiterator = cacheiterator->next;
  120         i++;
  121     }
  122     printf("\n");
  123 }
  124 
  125 int xferlog_add(xferlog **log, const time_t timestamp, const uint64_t rx, const uint64_t tx)
  126 {
  127     xferlog *newlog;
  128 
  129     if (*log == NULL || (*log)->timestamp != timestamp) {
  130         newlog = malloc(sizeof(xferlog));
  131         if (newlog == NULL) {
  132             return 0;
  133         }
  134         newlog->next = *log;
  135         *log = newlog;
  136 
  137         newlog->timestamp = timestamp;
  138         newlog->rx = 0;
  139         newlog->tx = 0;
  140     }
  141 
  142     (*log)->rx += rx;
  143     (*log)->tx += tx;
  144 
  145     return 1;
  146 }
  147 
  148 void xferlog_clear(xferlog **log)
  149 {
  150     xferlog *log_prev;
  151 
  152     while (*log != NULL) {
  153         log_prev = *log;
  154         *log = (*log)->next;
  155         free(log_prev);
  156     }
  157 }
  158 
  159 void xferlog_debug(xferlog **log, const int newline)
  160 {
  161     int i = 1;
  162     xferlog *logiterator = *log;
  163 
  164     if (newline && logiterator == NULL) {
  165         printf("  xferlog: empty\n");
  166         return;
  167     }
  168 
  169     if (newline) {
  170         printf("  xferlog: ");
  171     }
  172     while (logiterator != NULL) {
  173         printf("%d: %" PRIu64 " - %" PRIu64 " / %" PRIu64 "", i, (uint64_t)logiterator->timestamp, logiterator->rx, logiterator->tx);
  174         if (logiterator->next != NULL) {
  175             printf(", ");
  176         }
  177         logiterator = logiterator->next;
  178         i++;
  179     }
  180     if (newline) {
  181         printf("\n");
  182     }
  183 }