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
1 changed files with 8 additions and 28 deletions

View File

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