]> code.citadel.org Git - citadel.git/blob - webcit/tools.c
* Rewrote the HTTP engine and application coupling to run in a worker thread
[citadel.git] / webcit / tools.c
1 /*
2  * tools.c -- Miscellaneous routines 
3  */
4
5
6
7 #include <ctype.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <signal.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <sys/socket.h>
16 #include <sys/time.h>
17 #include <limits.h>
18 #include <netinet/in.h>
19 #include <netdb.h>
20 #include <string.h>
21 #include <pwd.h>
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <pthread.h>
25 #include <signal.h>
26 #include "webcit.h"
27
28
29
30
31 char *safestrncpy(char *dest, const char *src, size_t n)
32 {
33         if (dest == NULL || src == NULL) {
34                 fprintf(stderr, "safestrncpy: NULL argument\n");
35                 abort();
36         }
37         strncpy(dest, src, n);
38         dest[n - 1] = 0;
39         return dest;
40 }
41
42
43 /*
44  * num_parms()  -  discover number of parameters...
45  */
46 int num_parms(char *source)
47 {
48         int a;
49         int count = 1;
50
51         for (a = 0; a < strlen(source); ++a)
52                 if (source[a] == '|')
53                         ++count;
54         return (count);
55 }
56
57 /*
58  * extract()  -  extract a parameter from a series of "|" separated...
59  */
60 void extract(char *dest, char *source, int parmnum)
61 {
62         char buf[256];
63         int count = 0;
64         int n;
65
66         if (strlen(source) == 0) {
67                 strcpy(dest, "");
68                 return;
69         }
70         n = num_parms(source);
71
72         if (parmnum >= n) {
73                 strcpy(dest, "");
74                 return;
75         }
76         strcpy(buf, source);
77         if ((parmnum == 0) && (n == 1)) {
78                 strcpy(dest, buf);
79                 for (n = 0; n < strlen(dest); ++n)
80                         if (dest[n] == '|')
81                                 dest[n] = 0;
82                 return;
83         }
84         while (count++ < parmnum)
85                 do {
86                         strcpy(buf, &buf[1]);
87                 } while ((strlen(buf) > 0) && (buf[0] != '|'));
88         if (buf[0] == '|')
89                 strcpy(buf, &buf[1]);
90         for (count = 0; count < strlen(buf); ++count)
91                 if (buf[count] == '|')
92                         buf[count] = 0;
93         strcpy(dest, buf);
94 }
95
96 /*
97  * extract_int()  -  extract an int parm w/o supplying a buffer
98  */
99 int extract_int(char *source, int parmnum)
100 {
101         char buf[256];
102
103         extract(buf, source, parmnum);
104         return (atoi(buf));
105 }
106
107 /*
108  * extract_long()  -  extract an long parm w/o supplying a buffer
109  */
110 long extract_long(char *source, long int parmnum)
111 {
112         char buf[256];
113
114         extract(buf, source, parmnum);
115         return (atol(buf));
116 }
117
118
119 /*
120  * check for the presence of a character within a string (returns count)
121  */
122 int haschar(st, ch)
123 char st[];
124 char ch;
125 {
126         int a, b;
127         b = 0;
128         for (a = 0; a < strlen(st); ++a)
129                 if (st[a] == ch)
130                         ++b;
131         return (b);
132 }