Monitoring du Phytotron
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.c 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * core/utils.c
  3. */
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include <time.h>
  10. #include <sys/time.h>
  11. #include "utils.h"
  12. extern int verbosity;
  13. /* --------------------------------------------------------------- */
  14. int special_dumper(FILE *fp, unsigned char octet)
  15. {
  16. static int idx = 0;
  17. static unsigned char buffer[16];
  18. int foo, c;
  19. #if DEBUG_LEVEL > 1
  20. fprintf(stderr, "%s $%x %c\n", __func__, octet, octet);
  21. #endif
  22. buffer[idx++] = octet;
  23. if (idx==16) {
  24. fprintf(fp, "dmp : ");
  25. for (foo=0; foo<16; foo++) {
  26. fprintf(fp, "%02x ", buffer[foo]);
  27. if (7==foo) fprintf(fp, " ");
  28. }
  29. fprintf(fp, " - |");
  30. for (foo=0; foo<16; foo++) {
  31. c = buffer[foo];
  32. fprintf(fp, "%c", isprint(c) ? c : ' ');
  33. }
  34. fprintf(fp, "|\n"); fflush(fp);
  35. idx = 0;
  36. }
  37. return 0;
  38. }
  39. /* ---------------------------------------------------------------- */
  40. /* maybe not a really good initialisation... */
  41. int seed_my_rand(int foo)
  42. {
  43. long v1, v2;
  44. v1 = getpid(); v2 = time(NULL);
  45. return v1 ^ v2;
  46. }
  47. /* --------------------------------------------------------------- */
  48. int random1000(int type)
  49. {
  50. int value, foo;
  51. #if DEBUG_LEVEL > 1
  52. fprintf(stderr, ">>> %s(%d)\n", __func__, type);
  53. #endif
  54. switch (type) {
  55. case 0:
  56. value = rand() % 1000;
  57. break;
  58. case 1:
  59. value = (rand()%1000 + rand()%1000) / 2;
  60. break;
  61. case 4:
  62. value = 0;
  63. for (foo=0; foo<4; foo++)
  64. value += rand() % 1000;
  65. value /= 4;
  66. break;
  67. default:
  68. value = -1;
  69. break;
  70. }
  71. return value;
  72. }
  73. /* --------------------------------------------------------------- */
  74. double dtime(void)
  75. {
  76. struct timeval tv;
  77. int foo;
  78. foo = gettimeofday(&tv, NULL);
  79. if (foo) fprintf(stderr, "got %d in %s\n", foo, __func__);
  80. return (double)tv.tv_sec + (double)tv.tv_usec / 1e6;
  81. }
  82. /* --------------------------------------------------------------- */