Browse Source

Add misc funcs dealing with time and strings.

Georgi Chorbadzhiyski 12 years ago
parent
commit
d3078406a8
4 changed files with 53 additions and 1 deletions
  1. 1
    1
      Makefile
  2. 1
    0
      libfuncs.h
  3. 38
    0
      misc.c
  4. 13
    0
      misc.h

+ 1
- 1
Makefile View File

@@ -5,7 +5,7 @@ CFLAGS = -ggdb -Wall -Wextra -Wshadow -Wformat-security -O2
5 5
 RM = /bin/rm -f
6 6
 Q=@
7 7
 
8
-OBJS = queue.o list.o io.o log.o http_response.o asyncdns.o server.o
8
+OBJS = queue.o list.o io.o log.o http_response.o asyncdns.o server.o misc.o
9 9
 PROG = libfuncs.a
10 10
 
11 11
 all: $(PROG)

+ 1
- 0
libfuncs.h View File

@@ -38,5 +38,6 @@
38 38
 #include "log.h"
39 39
 #include "queue.h"
40 40
 #include "server.h"
41
+#include "misc.h"
41 42
 
42 43
 #endif

+ 38
- 0
misc.c View File

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

+ 13
- 0
misc.h View File

@@ -0,0 +1,13 @@
1
+#ifndef MISC_H
2
+#define MISC_H
3
+
4
+#include <sys/time.h>
5
+
6
+int xstrcmp(char *a, char *b);
7
+
8
+unsigned long long timediff_nsec(struct timespec *start_ts, struct timespec *end_ts);
9
+unsigned long long timeval_diff_usec(struct timeval *start_ts, struct timeval *end_ts);
10
+unsigned long long timeval_diff_msec(struct timeval *start_ts, struct timeval *end_ts);
11
+unsigned long long timeval_diff_sec (struct timeval *start_ts, struct timeval *end_ts);
12
+
13
+#endif

Loading…
Cancel
Save