"Fossies" - the Fresh Open Source Software Archive

Member "HTTPing-2.9/utils.c" (29 Oct 2022, 2185 Bytes) of package /linux/www/HTTPing-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 "utils.c" see the Fossies "Dox" file reference documentation.

    1 /* Released under AGPL v3 with exception for the OpenSSL library. See license.txt */
    2 
    3 #define _GNU_SOURCE
    4 #include <errno.h>
    5 #include <math.h>
    6 #include <stdarg.h>
    7 #include <stdio.h>
    8 #include <stdlib.h>
    9 #include <string.h>
   10 #include <unistd.h>
   11 #include <sys/time.h>
   12 #include <time.h>
   13 #include <libintl.h>
   14 
   15 #include "error.h"
   16 #include "utils.h"
   17 
   18 double get_ts(void)
   19 {
   20     struct timeval ts;
   21         struct timezone tz;
   22 
   23     if (gettimeofday(&ts, &tz) == -1)
   24         error_exit(gettext("gettimeofday failed"));
   25 
   26     return (double)ts.tv_sec + ((double)ts.tv_usec)/1000000.0 + (double)(tz.tz_minuteswest * 60);
   27 }
   28 
   29 void split_string(const char *in, const char *split, char ***list, int *list_n)
   30 {
   31     char *copy = strdup(in), *pc = copy;
   32     int split_len = strlen(split);
   33 
   34     for(;;)
   35     {
   36         char *term = strstr(pc, split);
   37 
   38         if (term)
   39             *term = 0x00;
   40 
   41         *list = (char **)realloc(*list, (*list_n + 1) * sizeof(char *));
   42         (*list)[*list_n] = strdup(pc);
   43         (*list_n)++;
   44 
   45         if (!term)
   46             break;
   47 
   48         pc = term + split_len;
   49     }
   50 
   51     free(copy);
   52 }
   53 
   54 void free_splitted_string(char **list, int n)
   55 {
   56     int index=0;
   57 
   58     for(index=0; index<n; index++)
   59         free(list[index]);
   60 
   61     free(list);
   62 }
   63 
   64 void str_add(char **to, const char *what, ...)
   65 {
   66     int len_to = *to ? strlen(*to) : 0;
   67     char *buffer = NULL;
   68     int len_what = 0;
   69 
   70         va_list ap;
   71 
   72     /* FIXME: should aprintf here: does solaris have aprintf? */
   73         va_start(ap, what);
   74         len_what = vasprintf(&buffer, what, ap);
   75         va_end(ap);
   76 
   77     *to = (char *)realloc(*to, len_to + len_what + 1);
   78 
   79     memcpy(&(*to)[len_to], buffer, len_what);
   80 
   81     (*to)[len_to + len_what] = 0x00;
   82 
   83     free(buffer);
   84 }
   85 
   86 char * format_value(double value, int digits_sig, int digits_nsig, char abbreviate)
   87 {
   88     char *out = NULL, *mul = "";
   89     double a = fabs(value);
   90     double div = 1.0;
   91 
   92     if (!abbreviate)
   93     {
   94     }
   95     else if (a >= GIGA)
   96     {
   97         div = GIGA;
   98         mul = "G";
   99     }
  100     else if (a >= MEGA)
  101     {
  102         div = MEGA;
  103         mul = "M";
  104     }
  105     else if (a >= KILO)
  106     {
  107         div = KILO;
  108         mul = "k";
  109     }
  110 
  111     if (mul[0])
  112         digits_sig--;
  113 
  114     (void)asprintf(&out, "%*.*f%s", digits_sig, digits_nsig, value / div, mul);
  115 
  116     return out;
  117 }
  118 
  119 void myusleep(useconds_t v)
  120 {
  121     int s = v / 1000000;
  122 
  123     if (s)
  124         sleep(s);
  125 
  126     v %= 1000000;
  127     if (v)
  128         usleep(v);
  129 }