I have the following code which is working but it's kinda annoying to define a variable in each and every line:
char *argv[100];
int argc = 0;
argv[0] = "test";
argc = 1;
char verbose[4], source[20], target[20];
int linenum=0;
while(fgets(line, 256, file) != NULL)
{
char arg[20], value[20];
if(line[0] == '#' || strlen(line) < 6) continue;
linenum++;
if(sscanf(line, "%[^=]=%s", arg, value) != 2)
{
fprintf(stderr,"Syntax error: %s\n",line);
continue;
}
if (value && strcmp(arg,"verbose")==0) {
strncpy(verbose,value,sizeof(verbose) - 1);
argv[argc++] = "-v";
argv[argc++] = verbose;
//argv[argc++] = value; //not working, shows 0
}
if (value && strcmp(arg,"source")==0) {
strncpy(source,value,sizeof(source) - 1);
argv[argc++] = "-s";
argv[argc++] = source;
}
if (value && strcmp(arg,"target")==0) {
strncpy(target,value,sizeof(target) - 1);
argv[argc++] = "-t";
argv[argc++] = target;
}
//and so on
|
|
|
|
}
How can I copy to a single char the "value" from inside the loop ? I mean by avoiding the usage of strncpy()
.