]> granicus.if.org Git - apache/blobdiff - server/util.c
First step in getting Apache to use APR's time libraries. This gets a good
[apache] / server / util.c
index d45246e764c5c5c957b6601048c8d53abe6733be..666d54a6c1bbcac8e361489f044231cdfb8fa835 100644 (file)
@@ -92,19 +92,8 @@ extern int fclose(FILE *);
  */
 #define TEST_CHAR(c, f)        (test_char_table[(unsigned)(c)] & (f))
 
-API_VAR_EXPORT const char ap_month_snames[12][4] =
-{
-    "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
-};
-API_VAR_EXPORT const char ap_day_snames[7][4] =
-{
-    "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
-};
-
 API_EXPORT(char *) ap_get_time()
 {
-  /* ZZZ When we abstract out time, this whole function should change to use
-     AP funcs. */
     time_t t;
     char *time_string;
 
@@ -134,15 +123,17 @@ API_EXPORT(char *) ap_field_noparam(ap_context_t *p, const char *intype)
     }
 }
 
-API_EXPORT(char *) ap_ht_time(ap_context_t *p, time_t t, const char *fmt, int gmt)
+API_EXPORT(char *) ap_ht_time(ap_context_t *p, ap_time_t *t, const char *fmt, int gmt)
 {
-  /* ZZZ this function can be replaced by calls to time formatting routines
-     in APR.  */
     char ts[MAX_STRING_LEN];
     char tf[MAX_STRING_LEN];
-    struct tm *tms;
 
-    tms = (gmt ? gmtime(&t) : localtime(&t));
+    if (gmt) {
+        ap_explode_time(t, APR_UTCTIME);
+    }
+    else {
+        ap_explode_time(t, APR_LOCALTIME);
+    }
     if(gmt) {
        /* Convert %Z to "GMT" and %z to "+0000";
         * on hosts that do not have a time zone string in struct tm,
@@ -178,66 +169,11 @@ API_EXPORT(char *) ap_ht_time(ap_context_t *p, time_t t, const char *fmt, int gm
     }
 
     /* check return code? */
-    strftime(ts, MAX_STRING_LEN, fmt, tms);
+    ap_strftime(ts, MAX_STRING_LEN, fmt, t);
     ts[MAX_STRING_LEN - 1] = '\0';
     return ap_pstrdup(p, ts);
 }
 
-API_EXPORT(char *) ap_gm_timestr_822(ap_context_t *p, time_t sec)
-{
-    struct tm *tms;
-    char *date_str = ap_palloc(p, 48 * sizeof(char));
-    char *date_str_ptr = date_str;
-    int real_year;
-
-    tms = gmtime(&sec);    /* ZZZ replace with AP time routine */
-
-    /* Assumption: this is always 3 */
-    /* i = strlen(ap_day_snames[tms->tm_wday]); */
-    memcpy(date_str_ptr, ap_day_snames[tms->tm_wday], 3);
-    date_str_ptr += 3;
-    *date_str_ptr++ = ',';
-    *date_str_ptr++ = ' ';
-    *date_str_ptr++ = tms->tm_mday / 10 + '0';
-    *date_str_ptr++ = tms->tm_mday % 10 + '0';
-    *date_str_ptr++ = ' ';
-    /* Assumption: this is also always 3 */
-    /* i = strlen(ap_month_snames[tms->tm_mon]); */
-    memcpy(date_str_ptr, ap_month_snames[tms->tm_mon], 3);
-    date_str_ptr += 3;
-    *date_str_ptr++ = ' ';
-    real_year = 1900 + tms->tm_year;
-    /* This routine isn't y10k ready. */
-    *date_str_ptr++ = real_year / 1000 + '0';
-    *date_str_ptr++ = real_year % 1000 / 100 + '0';
-    *date_str_ptr++ = real_year % 100 / 10 + '0';
-    *date_str_ptr++ = real_year % 10 + '0';
-    *date_str_ptr++ = ' ';
-    *date_str_ptr++ = tms->tm_hour / 10 + '0';
-    *date_str_ptr++ = tms->tm_hour % 10 + '0';
-    *date_str_ptr++ = ':';
-    *date_str_ptr++ = tms->tm_min / 10 + '0';
-    *date_str_ptr++ = tms->tm_min % 10 + '0';
-    *date_str_ptr++ = ':';
-    *date_str_ptr++ = tms->tm_sec / 10 + '0';
-    *date_str_ptr++ = tms->tm_sec % 10 + '0';
-    *date_str_ptr++ = ' ';
-    *date_str_ptr++ = 'G';
-    *date_str_ptr++ = 'M';
-    *date_str_ptr++ = 'T';
-    *date_str_ptr = '\0';
-
-    return date_str;
-    /* RFC date format; as strftime '%a, %d %b %Y %T GMT' */
-
-    /* The equivalent using sprintf. Use this for more legible but slower code
-    return ap_psprintf(p,
-               "%s, %.2d %s %d %.2d:%.2d:%.2d GMT", ap_day_snames[tms->tm_wday],
-               tms->tm_mday, ap_month_snames[tms->tm_mon], tms->tm_year + 1900,
-               tms->tm_hour, tms->tm_min, tms->tm_sec);
-    */
-}
-
 /* What a pain in the ass. */
 #if defined(HAVE_GMTOFF)
 API_EXPORT(struct tm *) ap_get_gmtoff(int *tz)
@@ -855,41 +791,40 @@ static void *cfg_getstr(void *buf, size_t bufsiz, void *param)
 }
 
 /* Open a configfile_t as FILE, return open configfile_t struct pointer */
-API_EXPORT(configfile_t *) ap_pcfg_openfile(ap_context_t *p, const char *name)
+API_EXPORT(ap_status_t) ap_pcfg_openfile(configfile_t **ret_cfg, ap_context_t *p, const char *name)
 {
     configfile_t *new_cfg;
     ap_file_t *file;
-    int saved_errno;
     ap_status_t stat;
     ap_filetype_e type;
 
     if (name == NULL) {
         ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, NULL,
                "Internal error: pcfg_openfile() called with NULL filename");
-        return NULL;
+        return APR_EBADF;
     }
 
     if (!ap_os_is_filename_valid(name)) {
         ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, NULL,
                     "Access to config file %s denied: not a valid filename",
                     name);
-       errno = EACCES;
-        return NULL;
+        return APR_EACCES;
     }
+
     stat = ap_open(&file, name, APR_READ | APR_BUFFERED, APR_OS_DEFAULT, p);
 #ifdef DEBUG
-    saved_errno = errno;
     ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, NULL,
                 "Opening config file %s (%s)",
                 name, (stat != APR_SUCCESS) ? strerror(errno) : "successful");
-    errno = saved_errno;
 #endif
     if (stat != APR_SUCCESS)
-        return NULL;
+        return stat;
 
-    if (ap_get_filetype(&type, file) == APR_SUCCESS &&
-        type != APR_REG &&
+    stat = ap_get_filetype(&type, file);
+    if (stat != APR_SUCCESS)
+        return stat;
+
+    if (type != APR_REG &&
 #if defined(WIN32) || defined(OS2)
         !(strcasecmp(name, "nul") == 0 ||
           (strlen(name) >= 4 &&
@@ -897,13 +832,11 @@ API_EXPORT(configfile_t *) ap_pcfg_openfile(ap_context_t *p, const char *name)
 #else
         strcmp(name, "/dev/null") != 0) {
 #endif /* WIN32 || OS2 */
-       saved_errno = errno;
         ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, NULL,
                     "Access to file %s denied by server: not a regular file",
                     name);
         ap_close(file);
-       errno = saved_errno;
-        return NULL;
+        return APR_EBADF;
     }
 
     new_cfg = ap_palloc(p, sizeof(*new_cfg));
@@ -913,7 +846,8 @@ API_EXPORT(configfile_t *) ap_pcfg_openfile(ap_context_t *p, const char *name)
     new_cfg->getstr = (void *(*)(void *, size_t, void *)) cfg_getstr;
     new_cfg->close = (int (*)(void *)) cfg_close;
     new_cfg->line_number = 0;
-    return new_cfg;
+    *ret_cfg = new_cfg;
+    return APR_SUCCESS;
 }
 
 
@@ -1667,7 +1601,6 @@ API_EXPORT(int) ap_is_directory(const char *path)
 {
     struct stat finfo;
 
-    /* ZZZ replace with AP File Info func. */
     if (stat(path, &finfo) == -1)
        return 0;               /* in error condition, just return no */
 
@@ -1707,13 +1640,13 @@ API_EXPORT(int) ap_is_url(const char *u)
     return (x ? 1 : 0);                /* If the first character is ':', it's broken, too */
 }
 
-#ifdef NEED_STRDUP
+#ifndef HAVE_STRDUP
 char *strdup(const char *str)
 {
     char *sdup;
 
     if (!(sdup = (char *) malloc(strlen(str) + 1))) {
-       fprintf(stderr, "Ouch!  Out of memory in our strdup()!\n");
+       ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Ouch!  Out of memory in our strdup()!");
        return NULL;
     }
     sdup = strcpy(sdup, str);
@@ -1723,7 +1656,7 @@ char *strdup(const char *str)
 #endif
 
 /* The following two routines were donated for SVR4 by Andreas Vogel */
-#ifdef NEED_STRCASECMP
+#ifndef HAVE_STRCASECMP
 int strcasecmp(const char *a, const char *b)
 {
     const char *p = a;
@@ -1742,7 +1675,7 @@ int strcasecmp(const char *a, const char *b)
 
 #endif
 
-#ifdef NEED_STRNCASECMP
+#ifndef HAVE_STRNCASECMP
 int strncasecmp(const char *a, const char *b, int n)
 {
     const char *p = a;
@@ -1763,7 +1696,7 @@ int strncasecmp(const char *a, const char *b, int n)
 #endif
 
 /* The following routine was donated for UTS21 by dwd@bell-labs.com */
-#ifdef NEED_STRSTR
+#ifndef HAVE_STRSTR
 char *strstr(char *s1, char *s2)
 {
     char *p1, *p2;
@@ -1792,7 +1725,7 @@ char *strstr(char *s1, char *s2)
 }
 #endif
 
-#ifdef NEED_INITGROUPS
+#ifndef HAVE_INITGROUPS
 int initgroups(const char *name, gid_t basegid)
 {
 #if defined(QNX) || defined(MPE) || defined(BEOS) || defined(_OSD_POSIX) || defined(TPF) || defined(__TANDEM)
@@ -1823,7 +1756,7 @@ int initgroups(const char *name, gid_t basegid)
 }
 #endif /* def NEED_INITGROUPS */
 
-#ifdef NEED_WAITPID
+#ifndef HAVE_WAITPID
 /* From ikluft@amdahl.com
  * this is not ideal but it works for SVR3 variants
  * Modified by dwd@bell-labs.com to call wait3 instead of wait because
@@ -1884,7 +1817,7 @@ API_EXPORT(uid_t) ap_uname2id(const char *name)
        return (atoi(&name[1]));
 
     if (!(ent = getpwnam(name))) {
-       fprintf(stderr, "%s: bad user name %s\n", ap_server_argv0, name);
+       ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "%s: bad user name %s", ap_server_argv0, name);
        exit(1);
     }
     return (ent->pw_uid);
@@ -1902,7 +1835,7 @@ API_EXPORT(gid_t) ap_gname2id(const char *name)
        return (atoi(&name[1]));
 
     if (!(ent = getgrnam(name))) {
-       fprintf(stderr, "%s: bad group name %s\n", ap_server_argv0, name);
+       ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "%s: bad group name %s", ap_server_argv0, name);
        exit(1);
     }
     return (ent->gr_gid);
@@ -1916,7 +1849,6 @@ API_EXPORT(gid_t) ap_gname2id(const char *name)
  */
 unsigned long ap_get_virthost_addr(char *w, unsigned short *ports)
 {
-  /* ZZZ Redesign for AP func changes */
     struct hostent *hep;
     unsigned long my_addr;
     char *p;
@@ -1946,14 +1878,14 @@ unsigned long ap_get_virthost_addr(char *w, unsigned short *ports)
     hep = gethostbyname(w);
 
     if ((!hep) || (hep->h_addrtype != AF_INET || !hep->h_addr_list[0])) {
-       fprintf(stderr, "Cannot resolve host name %s --- exiting!\n", w);
+       ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Cannot resolve host name %s --- exiting!", w);
        exit(1);
     }
 
     if (hep->h_addr_list[1]) {
-       fprintf(stderr, "Host %s has multiple addresses ---\n", w);
-       fprintf(stderr, "you must choose one explicitly for use as\n");
-       fprintf(stderr, "a virtual host.  Exiting!!!\n");
+       ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Host %s has multiple addresses ---", w);
+       ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "you must choose one explicitly for use as");
+       ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "a virtual host.  Exiting!!!");
        exit(1);
     }
 
@@ -1988,7 +1920,6 @@ char *ap_get_local_host(ap_context_t *a)
     char *server_hostname;
     struct hostent *p;
 
-    /* ZZZ change to use AP funcs. */
 #ifdef BEOS
     if (gethostname(str, sizeof(str) - 1) == 0)
 #else
@@ -2000,9 +1931,11 @@ char *ap_get_local_host(ap_context_t *a)
     }
     str[MAXHOSTNAMELEN] = '\0';
     if ((!(p = gethostbyname(str))) || (!(server_hostname = find_fqdn(a, p)))) {
-       fprintf(stderr, "%s: cannot determine local host name.\n",
+       ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, 
+                     "%s: cannot determine local host name.",
                ap_server_argv0);
-       fprintf(stderr, "Use the ServerName directive to set it manually.\n");
+       ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, 
+                     "Use the ServerName directive to set it manually.");
        exit(1);
     }
 
@@ -2101,7 +2034,7 @@ char *ap_double_quotes(ap_context_t *p, char *str)
 #endif
 
 
-#ifdef NEED_STRERROR
+#ifndef HAVE_STRERROR
 char *
      strerror(int err)
 {
@@ -2114,7 +2047,7 @@ char *
 }
 #endif
 
-#if defined(NEED_DIFFTIME)
+#ifndef HAVE_DIFFTIME
 double difftime(time_t time1, time_t time0)
 {
     return (time1 - time0);