* file named AUTHORS, in the root directory of this distribution.
*/
/*
- * $Id: util.c,v 1.42 2006/08/24 01:57:15 paddy_s Exp $
+ * $Id: util.c,v 1.42.2.13 2007/01/24 18:33:29 martinea Exp $
*/
#include "amanda.h"
tok_t tok, pushed_tok;
val_t tokenval;
+keytab_t *keytable;
int conf_line_num, got_parserror;
FILE *conf_conf = (FILE *)NULL;
static int connect_port(struct sockaddr_in *addrp, in_port_t port, char *proto,
struct sockaddr_in *svaddr, int nonblock);
-char conftoken_getc(void);
+int conftoken_getc(void);
int conftoken_ungetc(int c);
/*
}
if(servPort == NULL)
- dbprintf(("%s: connect_port: Try port %d: Available - ",
+ dbprintf(("%s: connect_port: Try port %d: Available - \n",
debug_prefix_time(NULL), port));
else {
- dbprintf(("%s: connect_port: Try port %d: Owned by %s - ",
+ dbprintf(("%s: connect_port: Try port %d: Owned by %s - \n",
debug_prefix_time(NULL), port, servPort->s_name));
}
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),
aclose(s);
errno = save_errno;
if (save_errno == ECONNREFUSED ||
+ save_errno == EHOSTUNREACH ||
+ save_errno == ENETUNREACH ||
save_errno == ETIMEDOUT) {
return -2 ;
}
*(s++) = 'f';
str++;
continue;
+ } else if (*str == '\\') {
+ *(s++) = '\\';
+ *(s++) = '\\';
+ str++;
+ continue;
}
if (*str == '"')
*(s++) = '\\';
/* 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;
}
return;
}
-char
+int
conftoken_getc(void)
{
if(conf_line == NULL)
buf = tkbuf;
token_overflow = 0;
do {
- if (islower(ch)) ch = toupper(ch);
if (buf < tkbuf+sizeof(tkbuf)-1) {
*buf++ = (char)ch;
} else {
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;
}
}
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;
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:
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:
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:
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,
{
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
dump_sockaddr(
struct sockaddr_in * sa)
{
- dbprintf(("%s: (sockaddr_in *)%p = { %d, %hd, %s }\n",
- debug_prefix(NULL), sa, sa->sin_family, sa->sin_port,
+ dbprintf(("%s: (sockaddr_in *)%p = { %d, %d, %s }\n",
+ debug_prefix_time(NULL), sa, sa->sin_family,
+ (int)ntohs(sa->sin_port),
inet_ntoa(sa->sin_addr)));
}
keytab_t *kt;
char *myprefix;
command_option_t *command_option;
+ int duplicate;
if(!command_options) return;
/* 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 &&
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);
}
}
+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