Imported Upstream version 2.5.1p3
[debian/amanda] / common-src / util.c
index 3564d6b195b87da5d9c6bdf83c7df6ae4b49057f..d168b6f1c3143a7956f24c347baf01bcb23a1232 100644 (file)
@@ -24,7 +24,7 @@
  * file named AUTHORS, in the root directory of this distribution.
  */
 /*
- * $Id: util.c,v 1.42.2.4 2006/09/21 11:12:21 martinea Exp $
+ * $Id: util.c,v 1.42.2.13 2007/01/24 18:33:29 martinea Exp $
  */
 
 #include "amanda.h"
@@ -293,7 +293,7 @@ connect_port(
     if (connect(s, (struct sockaddr *)svaddr,
                (socklen_t)sizeof(*svaddr)) == -1 && !nonblock) {
        save_errno = errno;
-       dbprintf(("%s: connect_portrange: connect from %s.%d failed\n",
+       dbprintf(("%s: connect_portrange: connect from %s.%d failed: %s\n",
                  debug_prefix_time(NULL),
                  inet_ntoa(addrp->sin_addr),
                  ntohs(addrp->sin_port),
@@ -498,6 +498,11 @@ quote_string(
                 *(s++) = 'f';
                str++;
                continue;
+           } else if (*str == '\\') {
+                *(s++) = '\\';
+                *(s++) = '\\';
+               str++;
+               continue;
            }
             if (*str == '"')
                 *(s++) = '\\';
@@ -1271,7 +1276,7 @@ lookup_keyword(
     /* switch to binary search if performance warrants */
 
     for(kwp = keytable; kwp->keyword != NULL; kwp++) {
-       if (strcmp(kwp->keyword, str) == 0) break;
+       if (strcasecmp(kwp->keyword, str) == 0) break;
     }
     return kwp->token;
 }
@@ -1371,7 +1376,6 @@ get_conftoken(
            buf = tkbuf;
            token_overflow = 0;
            do {
-               if (islower(ch)) ch = toupper(ch);
                if (buf < tkbuf+sizeof(tkbuf)-1) {
                    *buf++ = (char)ch;
                } else {
@@ -1750,9 +1754,9 @@ copy_val_t(
            break;
 
        case CONFTYPE_EXINCLUDE:
-           valdst->v.exinclude.type = valsrc->v.exinclude.type;
            valdst->v.exinclude.optional = valsrc->v.exinclude.optional;
-           valdst->v.exinclude.sl = duplicate_sl(valsrc->v.exinclude.sl);
+           valdst->v.exinclude.sl_list = duplicate_sl(valsrc->v.exinclude.sl_list);
+           valdst->v.exinclude.sl_file = duplicate_sl(valsrc->v.exinclude.sl_file);
            break;
        }
     }
@@ -1792,7 +1796,8 @@ free_val_t(
            break;
 
        case CONFTYPE_EXINCLUDE:
-           free_sl(val->v.exinclude.sl);
+           free_sl(val->v.exinclude.sl_list);
+           free_sl(val->v.exinclude.sl_file);
            break;
     }
     val->seen = 0;
@@ -1815,11 +1820,9 @@ static char buffer_conf_print[1025];
 
 char *
 conf_print(
-    val_t *val)
+    val_t *val,
+    int    str_need_quote)
 {
-    struct tm *stm;
-    int pos;
-
     buffer_conf_print[0] = '\0';
     switch(val->type) {
     case CONFTYPE_INT:
@@ -1857,26 +1860,31 @@ conf_print(
        break;
 
     case CONFTYPE_STRING:
-       buffer_conf_print[0] = '"';
-       if(val->v.s) {
-           strncpy(&buffer_conf_print[1], val->v.s,
-                       SIZEOF(buffer_conf_print) - 1);
-           buffer_conf_print[SIZEOF(buffer_conf_print) - 2] = '\0';
-           buffer_conf_print[strlen(buffer_conf_print)] = '"';
+       if(str_need_quote) {
+           buffer_conf_print[0] = '"';
+           if(val->v.s) {
+               strncpy(&buffer_conf_print[1], val->v.s,
+                       SIZEOF(buffer_conf_print) - 1);
+               buffer_conf_print[SIZEOF(buffer_conf_print) - 2] = '\0';
+               buffer_conf_print[strlen(buffer_conf_print)] = '"';
+           } else {
+               buffer_conf_print[1] = '"';
+               buffer_conf_print[2] = '\0';
+           }
        } else {
-           buffer_conf_print[1] = '"';
-           buffer_conf_print[2] = '\0';
+           if(val->v.s) {
+               strncpy(&buffer_conf_print[0], val->v.s,
+                       SIZEOF(buffer_conf_print));
+               buffer_conf_print[SIZEOF(buffer_conf_print) - 1] = '\0';
+           } else {
+               buffer_conf_print[0] = '\0';
+           }
        }
        break;
 
     case CONFTYPE_TIME:
-       stm = localtime(&val->v.t);
-       if (stm) {
-           snprintf(buffer_conf_print, SIZEOF(buffer_conf_print),
-                    "%d%02d%02d", stm->tm_hour, stm->tm_min, stm->tm_sec);
-       } else {
-           strcpy(buffer_conf_print, "00000");
-       }
+       snprintf(buffer_conf_print, SIZEOF(buffer_conf_print),
+                "%2d%02d", (int)val->v.t/100, (int)val->v.t % 100);
        break;
 
     case CONFTYPE_SL:
@@ -1884,15 +1892,7 @@ conf_print(
        break;
 
     case CONFTYPE_EXINCLUDE:
-       buffer_conf_print[0] = '\0';
-       if(val->v.exinclude.type == 0)
-           strncpy(buffer_conf_print, "LIST ", SIZEOF(buffer_conf_print));
-       else
-           strncpy(buffer_conf_print, "FILE ", SIZEOF(buffer_conf_print));
-       pos = 5;
-       if(val->v.exinclude.optional == 1)
-           strncpy(&buffer_conf_print[pos], "OPTIONAL ", SIZEOF(buffer_conf_print));
-       pos += 9;
+       strcpy(buffer_conf_print, "ERROR: use print_conf_exinclude");
        break;
 
     case CONFTYPE_BOOL:
@@ -2034,6 +2034,54 @@ conf_print(
     return buffer_conf_print;
 }
 
+char *
+conf_print_exinclude(
+    val_t *val,
+    int    str_need_quote,
+    int    file)
+{
+    int    pos;
+    sl_t  *sl;
+    sle_t *excl;
+
+    (void)str_need_quote;
+    buffer_conf_print[0] = '\0';
+    if (val->type != CONFTYPE_EXINCLUDE) {
+       strcpy(buffer_conf_print, "ERROR: conf_print_exinclude called for type != CONFTYPE_EXINCLUDE");
+       return buffer_conf_print;
+    }
+
+    if (file == 0) {
+       sl = val->v.exinclude.sl_list;
+       strncpy(buffer_conf_print, "LIST ", SIZEOF(buffer_conf_print));
+       pos = 5;
+    } else {
+       sl = val->v.exinclude.sl_file;
+       strncpy(buffer_conf_print, "FILE ", SIZEOF(buffer_conf_print));
+       pos = 5;
+    }
+
+    if(val->v.exinclude.optional == 1) {
+       strncpy(&buffer_conf_print[pos], "OPTIONAL ", SIZEOF(buffer_conf_print)-pos);
+       pos += 9;
+    }
+
+    if( sl != NULL) {
+       for(excl = sl->first; excl != NULL; excl = excl->next) {
+           if (pos + 3 + strlen(excl->name) < SIZEOF(buffer_conf_print)) {
+               buffer_conf_print[pos++] = ' ';
+               buffer_conf_print[pos++] = '"';
+               strcpy(&buffer_conf_print[pos], excl->name);
+               pos += strlen(excl->name);
+               buffer_conf_print[pos++] = '"';
+           }
+       }
+    }
+
+    buffer_conf_print[SIZEOF(buffer_conf_print) - 1] = '\0';
+    return buffer_conf_print;
+}
+
 void
 conf_init_string(
     val_t *val,
@@ -2228,9 +2276,9 @@ conf_init_exinclude(
 {
     val->seen = 0;
     val->type = CONFTYPE_EXINCLUDE;
-    val->v.exinclude.type = 0;
     val->v.exinclude.optional = 0;
-    val->v.exinclude.sl = NULL;
+    val->v.exinclude.sl_list = NULL;
+    val->v.exinclude.sl_file = NULL;
 }
 
 void
@@ -2590,6 +2638,7 @@ command_overwrite(
     keytab_t        *kt;
     char            *myprefix;
     command_option_t *command_option;
+    int                      duplicate;
 
     if(!command_options) return;
 
@@ -2602,10 +2651,15 @@ command_overwrite(
            /* NOTREACHED */
        }
 
-        for(command_option = command_options; command_option->name != NULL;
+       for(command_option = command_options; command_option->name != NULL;
                                                            command_option++) {
            myprefix = stralloc2(prefix, kt->keyword);
            if(strcasecmp(myprefix, command_option->name) == 0) {
+               duplicate = 0;
+               if (command_option->used == 0 &&
+                   valarray[np->parm].seen == -2) {
+                   duplicate = 1;
+               }
                command_option->used = 1;
                valarray[np->parm].seen = -2;
                if(np->type == CONFTYPE_STRING &&
@@ -2623,8 +2677,12 @@ command_overwrite(
                amfree(conf_line);
                conf_line = conf_char = NULL;
 
-               if(np->validate)
+               if (np->validate)
                    np->validate(np, &valarray[np->parm]);
+               if (duplicate == 1) {
+                   fprintf(stderr,"Duplicate %s option, using %s\n",
+                           command_option->name, command_option->value);
+               }
            }
            amfree(myprefix);
        }
@@ -2643,6 +2701,64 @@ free_new_argv(
 }
 
 
+int copy_file(
+    char  *dst,
+    char  *src,
+    char **errmsg)
+{
+    int     infd, outfd;
+    int     save_errno;
+    ssize_t nb;
+    char    buf[32768];
+    char   *quoted;
+
+    if ((infd = open(src, O_RDONLY)) == -1) {
+       save_errno = errno;
+       quoted = quote_string(src);
+       *errmsg = vstralloc("Can't open file ", quoted, " for reading: %s",
+                           strerror(save_errno));
+       amfree(quoted);
+       return -1;
+    }
+
+    if ((outfd = open(dst, O_WRONLY|O_CREAT, 0600)) == -1) {
+       save_errno = errno;
+       quoted = quote_string(dst);
+       *errmsg = vstralloc("Can't open file ", quoted, " for writting: %s",
+                           strerror(save_errno));
+       amfree(quoted);
+       close(infd);
+       return -1;
+    }
+
+    while((nb=read(infd, &buf, SIZEOF(buf))) > 0) {
+       if(fullwrite(outfd,&buf,(size_t)nb) < nb) {
+           save_errno = errno;
+           quoted = quote_string(dst);
+           *errmsg = vstralloc("Error writing to \"", quoted, "\":",
+                               strerror(save_errno));
+           amfree(quoted);
+           close(infd);
+           close(outfd);
+           return -1;
+       }
+    }
+
+    if (nb < 0) {
+       save_errno = errno;
+       quoted = quote_string(src);
+       *errmsg = vstralloc("Error reading from \"", quoted, "\":",
+                           strerror(save_errno));
+       amfree(quoted);
+       close(infd);
+       close(outfd);
+       return -1;
+    }
+
+    close(infd);
+    close(outfd);
+    return 0;
+}
 #ifndef HAVE_LIBREADLINE
 /*
  * simple readline() replacements