T
tmp123
Roman said:Hello, All!
I implemented simple program to eliminate entry from the file having the
following structure (actually it's config file of 'named' DNS package for
those who care and know):
options {
directory "/var/named";
listen-on { 192.168.11.22; 127.0.0.1; };
forwarders { 168.126.63.1; };
pid-file "/var/run/named.pid";
};
controls {
unix "/var/run/ndc" perm 0600 owner 0 group 0;
};
zone "." {
type hint;
file "root.cache";
};
zone "localhost" {
type master;
file "localhost";
};
zone "my.local" {
type master;
file "my.local";
allow-update { 127.0.0.1; 192.168.11.0/24; };
};
...
I bother only about "zone" entries and the goal of code to remove them only.
Here is the source code I'm done with. I'd like to hear reasonable criticism
and useful advices on optimization, bugs etc.
#include <stdio.h>
#include <string.h>
#include <strings.h>
#define BUFSIZE 100
int main(int argc, char **argv)
{
FILE *f; /* original */
FILE *fn; /* new */
char buf[BUFSIZE] = { 0 };
register char *idx;
char sidx[BUFSIZE];
int found = 0; /* set if zone found */
int i = 0;
if (argc != 3) {
fprintf(stderr, "Usage: %s <zonename> <configfile>\n", *argv);
return (0);
}
printf("%s %s\n", argv[1], argv[2]);
/* open original */
if ( (f = fopen(argv[2], "r")) == NULL )
return -1;
if ( (fn = fopen("named.conf.tmp", "w+")) == NULL )
return -1;
while ( !feof(f) ) {
fgets(buf, BUFSIZE, f);
/* skip leading spaces */
for (idx = buf; *idx != '\0' && (*idx == ' ' || *idx == '\t'); idx++)
;
/* look for 'zone' token */
if ( strncasecmp(idx, "zone ", sizeof("zone ") - 1) != 0 ) {
if (!found) {
fprintf(fn, buf);
fflush(fn);
}
continue;
}
idx += sizeof("zone ") - 1;
/* skip spaces inside of token */
for (; *idx != '\0' && (*idx == ' ' || *idx == '\t'); idx++)
;
/* extract token from commas */
if (*idx++ != '"')
continue;
/* put token into buffer to compare */
while (*idx != '\0' && *idx != '"')
sidx[i++] = *idx++;
sidx = '\0'; i = 0;
if (strcasecmp(sidx, argv[1]) != 0) {
found = 0;
fprintf(fn, buf);
fflush(fn);
}
else
found = 1;
}
fclose(f);
fclose(fn);
return 0;
}
Thank you in advance.
With best regards, Roman Mashak. E-mail: (e-mail address removed)
Look for references about "lex" C source generator (or even "yacc" if
you have more complex requirements like this one)