libfuncs is collection of code (list, queue, circular buffer, io, logging, etc.). https://georgi.unixsol.org/programs/libfuncs/
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.

misc.c 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * Misc functions
  3. * Copyright (C) 2010 Unix Solutions Ltd.
  4. *
  5. * Released under MIT license.
  6. * See LICENSE-MIT.txt for license terms.
  7. */
  8. #include <sys/time.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. int xstrcmp(char *a, char *b) {
  12. if (!a && b) return 1;
  13. if (!b && a) return 1;
  14. if (a == b) return 0;
  15. return strcmp(a, b);
  16. }
  17. unsigned long long timediff_nsec(struct timespec *start_ts, struct timespec *end_ts) {
  18. unsigned long long nsec;
  19. nsec = (end_ts->tv_sec - start_ts->tv_sec) * 1000000000;
  20. nsec += (end_ts->tv_nsec - start_ts->tv_nsec);
  21. return nsec;
  22. }
  23. unsigned long long timeval_diff_usec(struct timeval *start_ts, struct timeval *end_ts) {
  24. unsigned long long usec;
  25. usec = (end_ts->tv_sec - start_ts->tv_sec) * 1000000;
  26. usec += (end_ts->tv_usec - start_ts->tv_usec);
  27. return usec;
  28. }
  29. unsigned long long timeval_diff_msec(struct timeval *start_ts, struct timeval *end_ts) {
  30. unsigned long long msec;
  31. msec = (end_ts->tv_sec - start_ts->tv_sec) * 1000;
  32. msec += (end_ts->tv_usec - start_ts->tv_usec) / 1000;
  33. return msec;
  34. }
  35. unsigned long long timeval_diff_sec(struct timeval *start_ts, struct timeval *end_ts) {
  36. unsigned long long sec;
  37. sec = (end_ts->tv_sec - start_ts->tv_sec);
  38. sec += (end_ts->tv_usec - start_ts->tv_usec) / 1000000;
  39. return sec;
  40. }