2 * tools.c -- Miscellaneous routines
13 #include <sys/types.h>
15 #include <sys/socket.h>
18 #include <netinet/in.h>
32 char *safestrncpy(char *dest, const char *src, size_t n)
34 if (dest == NULL || src == NULL) {
35 fprintf(stderr, "safestrncpy: NULL argument\n");
38 strncpy(dest, src, n);
45 * num_parms() - discover number of parameters...
47 int num_parms(char *source)
52 for (a = 0; a < strlen(source); ++a)
59 * extract() - extract a parameter from a series of "|" separated...
61 void extract(char *dest, char *source, int parmnum)
67 if (strlen(source) == 0) {
71 n = num_parms(source);
78 if ((parmnum == 0) && (n == 1)) {
80 for (n = 0; n < strlen(dest); ++n)
85 while (count++ < parmnum)
88 } while ((strlen(buf) > 0) && (buf[0] != '|'));
91 for (count = 0; count < strlen(buf); ++count)
92 if (buf[count] == '|')
98 * extract_int() - extract an int parm w/o supplying a buffer
100 int extract_int(char *source, int parmnum)
104 extract(buf, source, parmnum);
109 * extract_long() - extract an long parm w/o supplying a buffer
111 long extract_long(char *source, long int parmnum)
115 extract(buf, source, parmnum);
121 * check for the presence of a character within a string (returns count)
129 for (a = 0; a < strlen(st); ++a)
137 * Format a date/time stamp for output
139 void fmt_date(char *buf, time_t thetime) {
142 char *ascmonths[] = {
143 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
144 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
148 tm = localtime(&thetime);
150 sprintf(buf, "%s %d %d %2d:%02d%s",
151 ascmonths[tm->tm_mon],
154 ( (tm->tm_hour > 12) ? (tm->tm_hour - 12) : (tm->tm_hour) ),
156 ( (tm->tm_hour > 12) ? "pm" : "am" )