test.c - geomyidae-tests - geomyidae tests | |
git clone git://bitreich.org/geomyidae-tests/ git://enlrupgkhuxnvlhsf6lc3fziv5h… | |
Log | |
Files | |
Refs | |
Tags | |
README | |
LICENSE | |
--- | |
test.c (1642B) | |
--- | |
1 #include <errno.h> | |
2 #include <limits.h> | |
3 #include <stdio.h> | |
4 #include <stdlib.h> | |
5 #include <string.h> | |
6 | |
7 const char *counter_path = "test_counter.txt"; | |
8 | |
9 /* read counter */ | |
10 long | |
11 get_test_counter(void) | |
12 { | |
13 FILE *fp; | |
14 char buf[32]; | |
15 size_t n; | |
16 long counter; | |
17 | |
18 fp = fopen(counter_path, "rb+"); | |
19 if (!fp) | |
20 return 0; | |
21 | |
22 n = fread(buf, 1, sizeof(buf), fp); | |
23 if (n < 0 || ferror(fp)) | |
24 return 0; | |
25 buf[n] = '\0'; | |
26 counter = strtol(buf, NULL, 10); | |
27 | |
28 return counter; | |
29 } | |
30 | |
31 /* write new counter */ | |
32 int | |
33 write_test_counter(long counter) | |
34 { | |
35 FILE *fp; | |
36 char buf[32]; | |
37 int ret; | |
38 | |
39 fp = fopen(counter_path, "wb+"); | |
40 if (!fp) { | |
41 fprintf(stderr, "error opening path for writing counter:… | |
42 strerror(errno)); | |
43 return -1; | |
44 } | |
45 | |
46 snprintf(buf, sizeof(buf), "%ld", counter); | |
47 fwrite(buf, 1, strlen(buf), fp); | |
48 ret = (fflush(fp) || ferror(fp)) ? -1 : 0; | |
49 fclose(fp); | |
50 | |
51 return ret; | |
52 } | |
53 | |
54 int | |
55 main(void) | |
56 { | |
57 FILE *fp; | |
58 char path[PATH_MAX]; | |
59 const char *envs[] = { | |
60 "PATH_INFO", | |
61 "PATH_TRANSLATED", | |
62 "QUERY_STRING", | |
63 "SCRIPT_NAME", | |
64 /* TLS */ | |
65 "GOPHERS", | |
66 "HTTPS", | |
67 "SERVER_PROTOCOL", | |
68 /* gopher-specific: */ | |
69 "REQUEST", | |
70 "SEARCHREQUEST", | |
71 "SELECTOR", | |
72 "X_GOPHER_SEARCH", | |
73 "TRAVERSAL" | |
74 }; | |
75 char *p; | |
76 int i; | |
77 long counter; | |
78 | |
79 counter = get_test_counter(); | |
80 | |
81 snprintf(path, sizeof(path), "results/%ld.txt", counter); | |
82 fp = fopen(path, "wb+"); | |
83 if (!fp) { | |
84 fprintf(stderr, "error writing result to path: %s: %s\n", | |
85 path, strerror(errno)); | |
86 return 1; | |
87 } | |
88 | |
89 for (i = 0; i < sizeof(envs) / sizeof(envs[0]); i++) { | |
90 p = getenv(envs[i]); | |
91 if (!p) | |
92 p = "(null)"; | |
93 fprintf(fp, "%s = %s\n", envs[i], p); | |
94 } | |
95 fclose(fp); | |
96 | |
97 write_test_counter(counter + 1); | |
98 | |
99 return 0; | |
100 } |