more cleanup
This commit is contained in:
parent
cebb888918
commit
45d571233e
93
mm/mm.c
93
mm/mm.c
@ -91,44 +91,28 @@ oserr(char *s, char *r){
|
|||||||
return EX_OSERR;
|
return EX_OSERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Destructs the files[2] struct used by main by closing its files and freeing
|
/* Hijacks i and j from main and destructs the files[2] struct used by main by
|
||||||
* its files and names arrays. Returns the destructed files. */
|
/* closing its files and freeing its files and names arrays, returning retval
|
||||||
static struct Files *
|
* from main. */
|
||||||
terminate(struct Files *files){
|
#define terminate \
|
||||||
size_t i;
|
for(i = 0; i < 2; ++i){ \
|
||||||
size_t j;
|
for(j = 0; j < files[i].s; ++j) \
|
||||||
|
if(files[i].files[j] != stdin \
|
||||||
for(i = 0; i < 2; ++i){
|
&& files[i].files[j] != stdout \
|
||||||
for(j = 0; j < files[i].s; ++j)
|
&& files[i].files[j] != stderr) \
|
||||||
if(files[i].files[j] != stdin
|
fclose(files[i].files[j]); \
|
||||||
&& files[i].files[j] != stdout
|
free(files[i].files); \
|
||||||
&& files[i].files[j] != stderr)
|
free(files[i].names); \
|
||||||
fclose(files[i].files[j]);
|
} \
|
||||||
free(files[i].files);
|
return retval
|
||||||
free(files[i].names);
|
|
||||||
}
|
|
||||||
|
|
||||||
return files;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]){
|
int main(int argc, char *argv[]){
|
||||||
int c;
|
int c;
|
||||||
|
struct Files files[2]; /* {read, write} */
|
||||||
size_t i;
|
size_t i;
|
||||||
size_t j;
|
size_t j;
|
||||||
size_t k;
|
size_t k; /* loop index but also unbuffer status */
|
||||||
struct Files files[2]; /* {read, write} */
|
int retval;
|
||||||
int s; /* scratch variable */
|
|
||||||
|
|
||||||
/* The simple invocation (without given arguments) requires no memory
|
|
||||||
* allocations or use of the files structs and is therefore treated as
|
|
||||||
* a special case. To do the same with all bells and whistles call
|
|
||||||
* $ mm -i - -o - */
|
|
||||||
if(argc < 2){ /* simple invocation */
|
|
||||||
while((c = getc(stdin)) != EOF)
|
|
||||||
if(putc(c, stdout) == EOF)
|
|
||||||
return 1;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Initializes the files structs with their default values, standard
|
/* Initializes the files structs with their default values, standard
|
||||||
* input and standard output. If an input or an output is specified
|
* input and standard output. If an input or an output is specified
|
||||||