]> code.citadel.org Git - citadel.git/blobdiff - webcit/tools.c
* Header file adjustments to make it work on FreeBSD
[citadel.git] / webcit / tools.c
index 6a2bad457162b5458809fcf46b18896b8dc7d843..86fd68ec1e351f47eeddf8fc8e98fac886f32478 100644 (file)
@@ -1,19 +1,30 @@
 /*
- * tools.c -- Miscellaneous routines 
+ * $Id$
+ *
+ * Miscellaneous routines 
  */
 
 #include <ctype.h>
 #include <stdlib.h>
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
 #include <stdio.h>
+#ifdef HAVE_FCNTL_H
 #include <fcntl.h>
+#endif
 #include <signal.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <sys/socket.h>
+#ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
+#endif
+#ifdef HAVE_LIMITS_H
 #include <limits.h>
+#endif
 #include <netinet/in.h>
+#include <arpa/inet.h>
 #include <netdb.h>
 #include <string.h>
 #include <pwd.h>
 #include <stdarg.h>
 #include <pthread.h>
 #include <signal.h>
-#include <sys/time.h>
 #include "webcit.h"
 #include "webserver.h"
 
+
 typedef unsigned char byte;
 
 #define FALSE 0
 #define TRUE 1
 
-char *ascmonths[] = {
-       "Jan", "Feb", "Mar", "Apr", "May", "Jun",
-       "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
-};
-
-char *ascdays[] = {
-       "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
-};
-
 static byte dtable[256];       /* base64 encode / decode table */
 
 char *safestrncpy(char *dest, const char *src, size_t n)
 {
        if (dest == NULL || src == NULL) {
-               lprintf(1, "safestrncpy: NULL argument\n");
                abort();
        }
        strncpy(dest, src, n);
@@ -72,30 +73,36 @@ int num_tokens(char *source, char tok)
 }
 
 /*
- * extract_token()  -  a smarter string tokenizer
+ * extract_token() - a string tokenizer
  */
-void extract_token(char *dest, char *source, int parmnum, char separator)
+void extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen)
 {
-       int i;
-       int len;
-       int curr_parm;
-
-       strcpy(dest, "");
-       len = 0;
-       curr_parm = 0;
-
-       if (strlen(source) == 0) {
-               return;
+       char *d;                /* dest */
+       const char *s;          /* source */
+       int count = 0;
+       int len = 0;
+
+       dest[0] = 0;
+
+       /* Locate desired parameter */
+       s = source;
+       while (count < parmnum) {
+               /* End of string, bail! */
+               if (!*s) {
+                       s = NULL;
+                       break;
+               }
+               if (*s == separator) {
+                       count++;
+               }
+               s++;
        }
+       if (!s) return;         /* Parameter not found */
 
-       for (i = 0; i < strlen(source); ++i) {
-               if (source[i] == separator) {
-                       ++curr_parm;
-               } else if (curr_parm == parmnum) {
-                       dest[len + 1] = 0;
-                       dest[len++] = source[i];
-               }
+       for (d = dest; *s && *s != separator && ++len<maxlen; s++, d++) {
+               *d = *s;
        }
+       *d = 0;
 }
 
 
@@ -145,23 +152,23 @@ void remove_token(char *source, int parmnum, char separator)
 /*
  * extract_int()  -  extract an int parm w/o supplying a buffer
  */
-int extract_int(char *source, int parmnum)
+int extract_int(const char *source, int parmnum)
 {
-       char buf[SIZ];
-
-       extract_token(buf, source, parmnum, '|');
-       return (atoi(buf));
+       char buf[32];
+       
+       extract_token(buf, source, parmnum, '|', sizeof buf);
+       return(atoi(buf));
 }
 
 /*
  * extract_long()  -  extract an long parm w/o supplying a buffer
  */
-long extract_long(char *source, long int parmnum)
+long extract_long(const char *source, int parmnum)
 {
-       char buf[SIZ];
-
-       extract_token(buf, source, parmnum, '|');
-       return (atol(buf));
+       char buf[32];
+       
+       extract_token(buf, source, parmnum, '|', sizeof buf);
+       return(atol(buf));
 }
 
 
@@ -169,15 +176,6 @@ long extract_long(char *source, long int parmnum)
 
 
 
-
-
-
-
-
-
-
-
-
 /*
  * check for the presence of a character within a string (returns count)
  */
@@ -194,77 +192,6 @@ char ch;
 }
 
 
-/*
- * Format a date/time stamp for output 
- */
-void fmt_date(char *buf, time_t thetime)
-{
-       struct tm *tm;
-       int hour;
-
-       strcpy(buf, "");
-       tm = localtime(&thetime);
-       hour = tm->tm_hour;
-       if (hour == 0)
-               hour = 12;
-       else if (hour > 12)
-               hour = hour - 12;
-
-       sprintf(buf, "%s %d %d %2d:%02d%s",
-               ascmonths[tm->tm_mon],
-               tm->tm_mday,
-               tm->tm_year + 1900,
-               hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
-           );
-}
-
-
-
-/*
- * Format TIME ONLY for output 
- */
-void fmt_time(char *buf, time_t thetime)
-{
-       struct tm *tm;
-       int hour;
-
-       strcpy(buf, "");
-       tm = localtime(&thetime);
-       hour = tm->tm_hour;
-       if (hour == 0)
-               hour = 12;
-       else if (hour > 12)
-               hour = hour - 12;
-
-       sprintf(buf, "%d:%02d%s",
-               hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
-           );
-}
-
-
-
-
-/*
- * Format a date/time stamp to the format used in HTTP headers
- */
-void httpdate(char *buf, time_t thetime)
-{
-       struct tm *tm;
-
-       strcpy(buf, "");
-       tm = localtime(&thetime);
-
-       sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d",
-               ascdays[tm->tm_wday],
-               tm->tm_mday,
-               ascmonths[tm->tm_mon],
-               tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec);
-}
-
-
-
-
-
 /*
  * Utility function to "readline" from memory
  * (returns new pointer)
@@ -312,8 +239,10 @@ int pattern2(char *search, char *patn)
  */
 void striplt(char *buf)
 {
+       if (strlen(buf) == 0) return;
        while ((strlen(buf) > 0) && (isspace(buf[0])))
                strcpy(buf, &buf[1]);
+       if (strlen(buf) == 0) return;
        while (isspace(buf[strlen(buf) - 1]))
                buf[strlen(buf) - 1] = 0;
 }
@@ -323,8 +252,7 @@ void striplt(char *buf)
  * Determine whether the specified message number is contained within the
  * specified set.
  */
-int is_msg_in_mset(char *mset, long msgnum)
-{
+int is_msg_in_mset(char *mset, long msgnum) {
        int num_sets;
        int s;
        char setstr[SIZ], lostr[SIZ], histr[SIZ];       /* was 1024 */
@@ -334,30 +262,30 @@ int is_msg_in_mset(char *mset, long msgnum)
         * Now set it for all specified messages.
         */
        num_sets = num_tokens(mset, ',');
-       for (s = 0; s < num_sets; ++s) {
-               extract_token(setstr, mset, s, ',');
+       for (s=0; s<num_sets; ++s) {
+               extract_token(setstr, mset, s, ',', sizeof setstr);
 
-               extract_token(lostr, setstr, 0, ':');
+               extract_token(lostr, setstr, 0, ':', sizeof lostr);
                if (num_tokens(setstr, ':') >= 2) {
-                       extract_token(histr, setstr, 1, ':');
+                       extract_token(histr, setstr, 1, ':', sizeof histr);
                        if (!strcmp(histr, "*")) {
-                               snprintf(histr, sizeof histr, "%ld",
-                                        LONG_MAX);
+                               snprintf(histr, sizeof histr, "%ld", LONG_MAX);
                        }
-               } else {
+               } 
+               else {
                        strcpy(histr, lostr);
                }
                lo = atol(lostr);
                hi = atol(histr);
 
-               if ((msgnum >= lo) && (msgnum <= hi))
-                       return (1);
+               if ((msgnum >= lo) && (msgnum <= hi)) return(1);
        }
 
-       return (0);
+       return(0);
 }
 
 
+
 /*
  * Strip a boundarized substring out of a string (for example, remove
  * parentheses and anything inside them).
@@ -526,7 +454,7 @@ int CtdlDecodeBase64(char *dest, const char *source, size_t length)
                                return (dpos);
                        }
                        if (dtable[c] & 0x80) {
-                               /* Ignoring errors: discard invalid character. */
+                               /* Ignoring errors: discard invalid character */
                                i--;
                                continue;
                        }
@@ -549,3 +477,20 @@ int CtdlDecodeBase64(char *dest, const char *source, size_t length)
                }
        }
 }
+
+
+
+/*
+ * Generate a new, globally unique UID parameter for a calendar etc. object
+ */
+void generate_uuid(char *buf) {
+       static int seq = 0;
+
+       sprintf(buf, "%s-%lx-%x-%x",
+               serv_info.serv_nodename,
+               (long)time(NULL),
+               getpid(),
+               (seq++)
+       );
+}
+