DD2-monitor/core/utils.c

98 lines
1.8 KiB
C
Raw Normal View History

2018-12-05 16:00:11 +01:00
/*
2019-01-17 11:51:07 +01:00
* core/utils.c
2018-12-05 16:00:11 +01:00
*/
#include <stdio.h>
2018-12-13 19:39:06 +01:00
#include <unistd.h>
2018-12-05 16:00:11 +01:00
#include <stdlib.h>
#include <string.h>
2019-05-20 18:20:18 +02:00
#include <ctype.h>
2018-12-13 19:39:06 +01:00
#include <time.h>
2018-12-05 17:58:55 +01:00
#include <sys/time.h>
2018-12-05 16:00:11 +01:00
2019-05-20 18:20:18 +02:00
#include "utils.h"
2018-12-05 16:04:09 +01:00
extern int verbosity;
2018-12-13 19:39:06 +01:00
/* --------------------------------------------------------------- */
2019-05-20 18:20:18 +02:00
int special_dumper(FILE *fp, unsigned char octet)
{
static int idx = 0;
static unsigned char buffer[16];
int foo, c;
#if DEBUG_LEVEL > 1
fprintf(stderr, "%s $%x %c\n", __func__, octet, octet);
#endif
buffer[idx++] = octet;
if (idx==16) {
fprintf(fp, "dmp : ");
for (foo=0; foo<16; foo++) {
fprintf(fp, "%02x ", buffer[foo]);
if (7==foo) fprintf(fp, " ");
}
fprintf(fp, " - |");
for (foo=0; foo<16; foo++) {
c = buffer[foo];
fprintf(fp, "%c", isprint(c) ? c : ' ');
}
fprintf(fp, "|\n"); fflush(fp);
idx = 0;
}
return 0;
}
/* ---------------------------------------------------------------- */
/* maybe not a really good initialisation... */
2018-12-13 19:39:06 +01:00
int seed_my_rand(int foo)
{
long v1, v2;
v1 = getpid(); v2 = time(NULL);
return v1 ^ v2;
}
2018-12-05 17:58:55 +01:00
/* --------------------------------------------------------------- */
2018-12-06 21:06:34 +01:00
int random1000(int type)
2018-12-05 16:00:11 +01:00
{
2019-01-27 13:03:59 +01:00
int value, foo;
2019-01-17 11:51:07 +01:00
#if DEBUG_LEVEL > 1
fprintf(stderr, ">>> %s(%d)\n", __func__, type);
#endif
2018-12-06 21:06:34 +01:00
switch (type) {
case 0:
value = rand() % 1000;
break;
case 1:
value = (rand()%1000 + rand()%1000) / 2;
break;
2019-01-27 13:03:59 +01:00
case 4:
value = 0;
for (foo=0; foo<4; foo++)
value += rand() % 1000;
value /= 4;
break;
2018-12-06 21:06:34 +01:00
default:
value = -1;
break;
}
2018-12-05 16:00:11 +01:00
return value;
}
2018-12-05 17:58:55 +01:00
/* --------------------------------------------------------------- */
double dtime(void)
{
struct timeval tv;
int foo;
foo = gettimeofday(&tv, NULL);
if (foo) fprintf(stderr, "got %d in %s\n", foo, __func__);
return (double)tv.tv_sec + (double)tv.tv_usec / 1e6;
}
/* --------------------------------------------------------------- */