]> code.citadel.org Git - citadel.git/blob - webcit/tools.c
* Made the header-bar buttons more visible
[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 <sys/time.h>
27 #include "webcit.h"
28
29
30
31
32 char *safestrncpy(char *dest, const char *src, size_t n)
33 {
34         if (dest == NULL || src == NULL) {
35                 fprintf(stderr, "safestrncpy: NULL argument\n");
36                 abort();
37         }
38         strncpy(dest, src, n);
39         dest[n - 1] = 0;
40         return dest;
41 }
42
43
44 /*
45  * num_parms()  -  discover number of parameters...
46  */
47 int num_parms(char *source)
48 {
49         int a;
50         int count = 1;
51
52         for (a = 0; a < strlen(source); ++a)
53                 if (source[a] == '|')
54                         ++count;
55         return (count);
56 }
57
58 /*
59  * extract()  -  extract a parameter from a series of "|" separated...
60  */
61 void extract(char *dest, char *source, int parmnum)
62 {
63         char buf[256];
64         int count = 0;
65         int n;
66
67         if (strlen(source) == 0) {
68                 strcpy(dest, "");
69                 return;
70         }
71         n = num_parms(source);
72
73         if (parmnum >= n) {
74                 strcpy(dest, "");
75                 return;
76         }
77         strcpy(buf, source);
78         if ((parmnum == 0) && (n == 1)) {
79                 strcpy(dest, buf);
80                 for (n = 0; n < strlen(dest); ++n)
81                         if (dest[n] == '|')
82                                 dest[n] = 0;
83                 return;
84         }
85         while (count++ < parmnum)
86                 do {
87                         strcpy(buf, &buf[1]);
88                 } while ((strlen(buf) > 0) && (buf[0] != '|'));
89         if (buf[0] == '|')
90                 strcpy(buf, &buf[1]);
91         for (count = 0; count < strlen(buf); ++count)
92                 if (buf[count] == '|')
93                         buf[count] = 0;
94         strcpy(dest, buf);
95 }
96
97 /*
98  * extract_int()  -  extract an int parm w/o supplying a buffer
99  */
100 int extract_int(char *source, int parmnum)
101 {
102         char buf[256];
103
104         extract(buf, source, parmnum);
105         return (atoi(buf));
106 }
107
108 /*
109  * extract_long()  -  extract an long parm w/o supplying a buffer
110  */
111 long extract_long(char *source, long int parmnum)
112 {
113         char buf[256];
114
115         extract(buf, source, parmnum);
116         return (atol(buf));
117 }
118
119
120 /*
121  * check for the presence of a character within a string (returns count)
122  */
123 int haschar(st, ch)
124 char st[];
125 char ch;
126 {
127         int a, b;
128         b = 0;
129         for (a = 0; a < strlen(st); ++a)
130                 if (st[a] == ch)
131                         ++b;
132         return (b);
133 }
134
135
136 /*
137  * Format a date/time stamp for output 
138  */
139 void fmt_date(char *buf, time_t thetime) {
140         struct tm *tm;
141
142         char *ascmonths[] = {
143                 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
144                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
145         };
146
147         strcpy(buf, "");
148         tm = localtime(&thetime);
149
150         sprintf(buf, "%s %d %d %2d:%02d%s",
151                 ascmonths[tm->tm_mon],
152                 tm->tm_mday,
153                 tm->tm_year + 1900,
154                 ( (tm->tm_hour > 12) ? (tm->tm_hour - 12) : (tm->tm_hour) ),
155                 tm->tm_min,
156                 ( (tm->tm_hour > 12) ? "pm" : "am" )
157         );
158 }