1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
/*
* parseconf.c - parse /etc/dumpconfig.c
* Created by: Troy Heber (troy.heber@hp.com)
*
* Copyright 2004 Hewlett-Packard Development Company, L.P.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "parseconf.h"
struct pconf *
parseconf(char * config_file)
{
int i;
FILE *fp;
struct pconf *conf;
char line[MAXLINE];
char *p, *val;
char *parse[LIST_SIZE] = {"DUMP_ACTIVE",
"DUMPDEV",
"DUMPDIR",
"DUMP_LEVEL",
"DUMP_FLAGS",
"DUMP_COMPRESS",
"PANIC_TIMEOUT",
"TARGET_HOST",
"TARGET_PORT",
"SOURCE_PORT",
"ETH_ADDRESS"
};
if ((conf=malloc(sizeof(struct pconf))) == NULL){
perror("FATAL ERROR: Couldn't allocate enough memory");
exit(10);
}
conf->dump_active[0] = '\0';
conf->dumpdev[0] = '\0';
conf->dumpdir[0] = '\0';
conf->dump_level[0]= '\0';
conf->dump_flags[0]= '\0';
conf->dump_compress[0]= '\0';
conf->panic_timeout[0] = '\0';
conf->target_host[0] = '\0';
conf->target_port[0] = '\0';
conf->source_port[0] = '\0';
conf->eth_address[0] = '\0';
if((fp = fopen(config_file, "r")) != NULL){
while (fgets(line,sizeof(line),fp) != NULL)
{
// Ignore comments
if ((p = strchr(line, '#')) != NULL)
*p = '\0';
// Find '='
if ((p = strchr(line, '=')) != NULL){
*p = '\0';
val = p + 1;
while(*val == ' ' || *val == '\t')
val++;
}else // Bad line or comment
continue;
for (i=0; i < LIST_SIZE; i++){
if(strstr(line,parse[i])){
if ((p = strchr(val,'\n'))) // kill the newline char
*p = '\0';
switch (i)
{
case 0: // DUMP_ACTIVE
strcpy(conf->dump_active, val);
break;
case 1: // DUMPDEV
strcpy(conf->dumpdev, val);
break;
case 2: // DUMPDIR
strcpy(conf->dumpdir, val);
break;
case 3: // DUMP_LEVEL
strcpy(conf->dump_level, val);
break;
case 4: // DUMP_FLAGS
strcpy(conf->dump_flags, val);
break;
case 5: // DUMP_COMPRESS
strcpy(conf->dump_compress, val);
break;
case 6: // PANIC_TIMEOUT
strcpy(conf->panic_timeout, val);
break;
case 7: // TARGET_HOST",
strcpy(conf->target_host, val);
break;
case 8: // TARGET_PORT",
strcpy(conf->target_port, val);
break;
case 10: // SOURCE_PORT",
strcpy(conf->source_port, val);
break;
case 11: // ETH_ADDRESS"
strcpy(conf->eth_address, val);
break;
}
}// if
}// for
}// while
fclose(fp);
}else
return NULL;
return conf;
}
|