From c7c6ca2c60d92e58fda3375c4a76ad0d051b2511 Mon Sep 17 00:00:00 2001 From: emma Date: Sat, 13 Jul 2024 17:23:31 -0600 Subject: [PATCH] mm(1): formatting --- src/mm.c | 187 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 110 insertions(+), 77 deletions(-) diff --git a/src/mm.c b/src/mm.c index 01fd400..0aced76 100644 --- a/src/mm.c +++ b/src/mm.c @@ -59,9 +59,9 @@ static char *wharsh = "wb"; * returning the FILE if successful and NULL if not, allocating more memory in * the files buffers as needed. */ static FILE * -Files_append(struct Files *files, FILE *file, char *name){ +Files_append(struct Files *files, FILE *file, char *name) { - if(file == NULL || (files->s == files->a + if (file == NULL || (files->s == files->a && ((files->files = realloc(files->files, (files->a += (files->a == 0) ? ALLOC_INITIAL @@ -83,7 +83,7 @@ Files_append(struct Files *files, FILE *file, char *name){ /* Prints a diagnostic message based on errno and returns an exit status * appropriate for an OS error. */ static int -oserr(char *s, char *r){ +oserr(char *s, char *r) { fprintf(stderr, "%s: %s: %s\n", s, r, strerror(errno)); @@ -94,12 +94,16 @@ oserr(char *s, char *r){ * closing its files and freeing its files and names arrays, returning retval * from main. */ #define terminate \ - for(i = 0; i < 2; ++i){ \ - for(j = 0; j < files[i].s; ++j) \ - if(files[i].files[j] != stdin \ - && files[i].files[j] != stdout \ - && files[i].files[j] != stderr) \ + for (i = 0; i < 2; ++i) { \ + for (j = 0; j < files[i].s; ++j) { \ + if ( \ + files[i].files[j] != stdin \ + && files[i].files[j] != stdout \ + && files[i].files[j] != stderr \ + ) { \ fclose(files[i].files[j]); \ + } \ + } \ free(files[i].files); \ free(files[i].names); \ } \ @@ -107,14 +111,14 @@ oserr(char *s, char *r){ /* Prints a usage text, in which s is the program being run (i.e. argv[0]), and * returns an exit status appropriate for a usage error. */ -int usage(char *s){ +int usage(char *s) { fprintf(stderr, "Usage: %s [-aenu] [-i input]... [-o output]...\n", s); return EX_USAGE; } -int main(int argc, char *argv[]){ +int main(int argc, char *argv[]) { int c; struct Files files[2]; /* {read, write} */ size_t i; @@ -127,70 +131,83 @@ int main(int argc, char *argv[]){ * 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){ + for (i = 0; i < 2; ++i) { files[i].a = 0; files[i].s = 0; files[i].mode = fmode[i]; files[i].files = NULL; files[i].names = NULL; - Files_append(&files[i], i == 0 ? stdin : stdout, - i == 0 ? stdin_name : stdout_name); + + Files_append( + &files[i], + i == 0 ? stdin : stdout, + i == 0 ? stdin_name : stdout_name + ); + files[i].s = 0; } k = 0; - if(argc > 0) - program_name = argv[0]; + if (argc > 0) { program_name = argv[0]; } - if(argc > 1) - while((c = getopt(argc, argv, "aehi:no:u")) != -1) - switch(c){ - case 'a': /* "rb+" -> "ab" */ - files[1].mode[0] = 'a'; - files[1].mode[2] = '\0'; - break; - case 'e': - if(Files_append(&files[1], stderr, stderr_name) != NULL) + if (argc > 1) { + while ((c = getopt(argc, argv, "aehi:no:u")) != -1) { + switch (c){ + case 'a': /* "rb+" -> "ab" */ + files[1].mode[0] = 'a'; + files[1].mode[2] = '\0'; break; - retval = oserr(argv[0], "-e"); - terminate; - case 'i': - if((strcmp(optarg, "-") == 0 && Files_append(&files[0], - stdin, stdin_name) != NULL) - || Files_open(&files[0], optarg) != NULL) - break; - retval = oserr(argv[0], optarg); - terminate; - case 'o': - if((strcmp(optarg, "-") == 0 && Files_append(&files[1], - stdout, stdout_name) != NULL) - || Files_open(&files[1], optarg) != NULL) - break; - /* does not exist, so try to create it */ - if(errno == ENOENT){ - files[1].mode = wharsh; - if(Files_open(&files[1], optarg) != NULL){ - files[1].mode = fmode[1]; + case 'e': + if (Files_append(&files[1], stderr, stderr_name) != NULL) { break; } - } - retval = oserr(argv[0], optarg); - terminate; - case 'n': - if(signal(SIGINT, SIG_IGN) != SIG_ERR) - break; - retval = oserr(argv[0], "-n"); - terminate; - case 'u': - k = 1; - break; - default: - retval = usage(argv[0]); - terminate; - } - if(optind != argc){ + retval = oserr(argv[0], "-e"); + terminate; + case 'i': + if ( + (strcmp(optarg, "-") == 0 + && Files_append(&files[0], stdin, stdin_name) != NULL) + || Files_open(&files[0], optarg) != NULL + ) { break; } + + retval = oserr(argv[0], optarg); + terminate; + case 'o': + if ( + (strcmp(optarg, "-") == 0 + && Files_append(&files[1], stdout, stdout_name) != NULL) + || Files_open(&files[1], optarg) != NULL + ) { break; } + /* does not exist, so try to create it */ + if (errno == ENOENT) { + files[1].mode = wharsh; + + if (Files_open(&files[1], optarg) != NULL) { + files[1].mode = fmode[1]; + break; + } + } + + retval = oserr(argv[0], optarg); + terminate; + case 'n': + if (signal(SIGINT, SIG_IGN) != SIG_ERR) { break; } + + retval = oserr(argv[0], "-n"); + terminate; + case 'u': + k = 1; + break; + default: + retval = usage(argv[0]); + terminate; + } + } + } + + if (optind != argc) { retval = usage(argv[0]); terminate; } @@ -199,37 +216,53 @@ int main(int argc, char *argv[]){ files[1].s += files[1].s == 0; /* Unbuffer files. */ - if(k){ - for(i = 0; - i < files[0].s; - setvbuf(files[0].files[i++], NULL, _IONBF, 0)); - for(i = 0; - i < files[1].s; - setvbuf(files[1].files[i++], NULL, _IONBF, 0)); + if (k) { + for ( + i = 0; i < files[0].s; setvbuf(files[0].files[i++], NULL, _IONBF, 0) + ); + for ( + i = 0; i < files[1].s; setvbuf(files[1].files[i++], NULL, _IONBF, 0) + ); } retval = EX_OK; /* Actual program loop. */ - 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 */ - if(putc(c, files[1].files[j]) == EOF){ + 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 */ + if (putc(c, files[1].files[j]) == EOF) { /* notebook's full */ retval = EX_IOERR; - fprintf(stderr, "%s: %s: %s\n", - program_name, files[1].names[j], strerror(errno)); - if(fclose(files[1].files[j]) == EOF) - fprintf(stderr, "%s: %s: %s\n", - program_name, files[1].names[j], strerror(errno)); + fprintf( + stderr, + "%s: %s: %s\n", + program_name, + files[1].names[j], + strerror(errno) + ); + + if (fclose(files[1].files[j]) == EOF) { + fprintf( + stderr, + "%s: %s: %s\n", + program_name, + files[1].names[j], + strerror(errno) + ); + } + /* massage out the tense muscle */ for(k = j--; k < files[1].s - 1; ++k){ files[1].files[k] = files[1].files[k+1]; files[1].names[k] = files[1].names[k+1]; } - if(--files[1].s == 0) - terminate; + + if(--files[1].s == 0) { terminate; } } + } + } + } terminate; }