more shortening
This commit is contained in:
parent
2501cd52d8
commit
88a9425ad7
27
mm/mm.c
27
mm/mm.c
@ -61,9 +61,6 @@ static char *wharsh = "wb";
|
||||
static FILE *
|
||||
Files_append(struct Files *files, FILE *file, char *name){
|
||||
|
||||
if(files->s == -1)
|
||||
files->s = 0;
|
||||
|
||||
if(file == NULL || (files->s == files->a
|
||||
&& ((files->files = realloc(files->files,
|
||||
(files->a += (files->a == 0)
|
||||
@ -123,8 +120,8 @@ int main(int argc, char *argv[]){
|
||||
int s; /* scratch variable */
|
||||
|
||||
/* The simple invocation (without given arguments) requires no memory
|
||||
* allocations or use of the io structs and is therefore treated as a
|
||||
* special case. To do the same with all bells and whistles call
|
||||
* 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)
|
||||
@ -134,11 +131,9 @@ int main(int argc, char *argv[]){
|
||||
}
|
||||
|
||||
/* Initializes the files structs with their default values, standard
|
||||
* input and standard output. Both files->s are set to -1, a special
|
||||
* value indicating that they're at their defaults and that their FILE
|
||||
* pointers won't need to be closed. If an input or an output is
|
||||
* specified these initial values will be overwritten, so to, say, use
|
||||
* mm(1) equivalently to tee(1p), -o - will need to be specified before
|
||||
* input and standard output. If an input or an output is specified
|
||||
* these initial values will be overwritten, so to, say, use mm(1)
|
||||
* equivalently to tee(1p), -o - will need to be specified before
|
||||
* additional files to ensure standard output is still written. */
|
||||
for(i = 0; i < 2; ++i){
|
||||
files[i].a = 0;
|
||||
@ -148,7 +143,7 @@ int main(int argc, char *argv[]){
|
||||
files[i].names = NULL;
|
||||
Files_append(&files[i], i == 0 ? stdin : stdout,
|
||||
i == 0 ? stdin_name : stdout_name);
|
||||
files[i].s = -1;
|
||||
files[i].s = 0;
|
||||
}
|
||||
|
||||
s = 0; /* Refers to whether or not files will be unbuffered. */
|
||||
@ -211,6 +206,9 @@ int main(int argc, char *argv[]){
|
||||
return EX_USAGE;
|
||||
}
|
||||
|
||||
files[0].s += files[0].s == 0;
|
||||
files[1].s += files[1].s == 0;
|
||||
|
||||
/* Unbuffer files. */
|
||||
if(s){ for( i = 0;
|
||||
i < files[0].s;
|
||||
@ -220,11 +218,6 @@ int main(int argc, char *argv[]){
|
||||
setvbuf(files[1].files[i++], NULL, _IONBF, 0));
|
||||
}
|
||||
|
||||
if(files[0].s == -1)
|
||||
files[0].s = 1;
|
||||
if(files[1].s == -1)
|
||||
files[1].s = 1;
|
||||
|
||||
/* Reflects whether (s==0) or not (s==1) the full length of the inputs
|
||||
* could be written to all outputs, which is the return value for main
|
||||
* to maintain feature parity with tee(1p). */
|
||||
@ -234,8 +227,8 @@ int main(int argc, char *argv[]){
|
||||
for(i = 0; i < files[0].s; ++i) /* iterate ins */
|
||||
while((c = getc(files[0].files[i])) != EOF) /* iterate chars */
|
||||
for(j = 0; j < files[1].s; ++j) /* iterate outs */
|
||||
/* notebook's full */
|
||||
if(putc(c, files[1].files[j]) == EOF){
|
||||
/* notebook's full */
|
||||
s = 1;
|
||||
if(fclose(files[1].files[j]) == EOF)
|
||||
fprintf(stderr, "%s: %s: %s\n",
|
||||
|
Loading…
Reference in New Issue
Block a user