dstring in read_args_from_file

This commit is contained in:
Ian Jauslin 2023-11-03 16:45:51 -04:00
parent 9d232a8fca
commit f9aac70796

View File

@ -833,10 +833,7 @@ int args_from_file(
dstring* utfile_str, dstring* utfile_str,
char* file_str char* file_str
){ ){
char* line; dstring line;
unsigned int len=256;
unsigned int pos=0;
char* line_realloc;
char c; char c;
// open file // open file
@ -847,7 +844,7 @@ int args_from_file(
} }
// allocate line buffer // allocate line buffer
line=calloc(sizeof(char), len); dstring_init(&line,64);
while(1){ while(1){
c=fgetc(file); c=fgetc(file);
@ -861,11 +858,11 @@ int args_from_file(
if(c=='\n' || c=='\r'){ if(c=='\n' || c=='\r'){
// read entry // read entry
// ignore lines with fewer than three characters // ignore lines with fewer than three characters
if(pos>=3){ if(line.length>=3){
// find line starting with "#!" // find line starting with "#!"
if(line[0]=='#' && line[1]=='!'){ if(line.string[0]=='#' && line.string[1]=='!'){
wordexp_t pwordexp; wordexp_t pwordexp;
wordexp(line+2,&pwordexp,0); wordexp(line.string+2,&pwordexp,0);
// read arguments // read arguments
read_args(pwordexp.we_wordc, (const char **)pwordexp.we_wordv, params, command, nthreads, savefile_str, utfile_str, NULL); read_args(pwordexp.we_wordc, (const char **)pwordexp.we_wordv, params, command, nthreads, savefile_str, utfile_str, NULL);
wordfree(&pwordexp); wordfree(&pwordexp);
@ -873,32 +870,15 @@ int args_from_file(
break; break;
} }
} }
// reset buffer // reset buffer
pos=0; line.length=0;
line[pos]='\0';
} }
// add to buffer // add to buffer
else { else {
// check that there is room in buffer dstring_append(c,&line);
if(pos==len){
// too short: reallocate
line_realloc=calloc(sizeof(char), 2*len);
for(pos=0;pos<len;pos++){
line_realloc[pos]=line[pos];
}
free(line);
line=line_realloc;
len=2*len;
}
// add c to line
line[pos]=c;
pos++;
line[pos]='\0';
} }
} }
free(line); dstring_free(line);
// close file // close file
fclose(file); fclose(file);