mm(1): formatting

This commit is contained in:
Emma Tebibyte 2024-07-13 17:23:31 -06:00
parent 26b0c93f4d
commit c7c6ca2c60
Signed by: emma
GPG Key ID: 06FA419A1698C270

187
src/mm.c
View File

@ -59,9 +59,9 @@ static char *wharsh = "wb";
* returning the FILE if successful and NULL if not, allocating more memory in * returning the FILE if successful and NULL if not, allocating more memory in
* the files buffers as needed. */ * the files buffers as needed. */
static FILE * 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->files = realloc(files->files,
(files->a += (files->a == 0) (files->a += (files->a == 0)
? ALLOC_INITIAL ? 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 /* Prints a diagnostic message based on errno and returns an exit status
* appropriate for an OS error. */ * appropriate for an OS error. */
static int static int
oserr(char *s, char *r){ oserr(char *s, char *r) {
fprintf(stderr, "%s: %s: %s\n", s, r, strerror(errno)); 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 * closing its files and freeing its files and names arrays, returning retval
* from main. */ * from main. */
#define terminate \ #define terminate \
for(i = 0; i < 2; ++i){ \ for (i = 0; i < 2; ++i) { \
for(j = 0; j < files[i].s; ++j) \ for (j = 0; j < files[i].s; ++j) { \
if(files[i].files[j] != stdin \ if ( \
&& files[i].files[j] != stdout \ files[i].files[j] != stdin \
&& files[i].files[j] != stderr) \ && files[i].files[j] != stdout \
&& files[i].files[j] != stderr \
) { \
fclose(files[i].files[j]); \ fclose(files[i].files[j]); \
} \
} \
free(files[i].files); \ free(files[i].files); \
free(files[i].names); \ 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 /* 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. */ * 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); fprintf(stderr, "Usage: %s [-aenu] [-i input]... [-o output]...\n", s);
return EX_USAGE; return EX_USAGE;
} }
int main(int argc, char *argv[]){ int main(int argc, char *argv[]) {
int c; int c;
struct Files files[2]; /* {read, write} */ struct Files files[2]; /* {read, write} */
size_t i; 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) * these initial values will be overwritten, so to, say, use mm(1)
* equivalently to tee(1p), -o - will need to be specified before * equivalently to tee(1p), -o - will need to be specified before
* additional files to ensure standard output is still written. */ * 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].a = 0;
files[i].s = 0; files[i].s = 0;
files[i].mode = fmode[i]; files[i].mode = fmode[i];
files[i].files = NULL; files[i].files = NULL;
files[i].names = 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; files[i].s = 0;
} }
k = 0; k = 0;
if(argc > 0) if (argc > 0) { program_name = argv[0]; }
program_name = argv[0];
if(argc > 1) if (argc > 1) {
while((c = getopt(argc, argv, "aehi:no:u")) != -1) while ((c = getopt(argc, argv, "aehi:no:u")) != -1) {
switch(c){ switch (c){
case 'a': /* "rb+" -> "ab" */ case 'a': /* "rb+" -> "ab" */
files[1].mode[0] = 'a'; files[1].mode[0] = 'a';
files[1].mode[2] = '\0'; files[1].mode[2] = '\0';
break;
case 'e':
if(Files_append(&files[1], stderr, stderr_name) != NULL)
break; break;
retval = oserr(argv[0], "-e"); case 'e':
terminate; if (Files_append(&files[1], stderr, stderr_name) != NULL) {
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; 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]); retval = usage(argv[0]);
terminate; terminate;
} }
@ -199,37 +216,53 @@ int main(int argc, char *argv[]){
files[1].s += files[1].s == 0; files[1].s += files[1].s == 0;
/* Unbuffer files. */ /* Unbuffer files. */
if(k){ if (k) {
for(i = 0; for (
i < files[0].s; i = 0; i < files[0].s; setvbuf(files[0].files[i++], NULL, _IONBF, 0)
setvbuf(files[0].files[i++], NULL, _IONBF, 0)); );
for(i = 0; for (
i < files[1].s; i = 0; i < files[1].s; setvbuf(files[1].files[i++], NULL, _IONBF, 0)
setvbuf(files[1].files[i++], NULL, _IONBF, 0)); );
} }
retval = EX_OK; retval = EX_OK;
/* Actual program loop. */ /* Actual program loop. */
for(i = 0; i < files[0].s; ++i) /* iterate ins */ for (i = 0; i < files[0].s; ++i) { /* iterate ins */
while((c = getc(files[0].files[i])) != EOF) /* iterate chars */ while ((c = getc(files[0].files[i])) != EOF) { /* iterate chars */
for(j = 0; j < files[1].s; ++j) /* iterate outs */ for (j = 0; j < files[1].s; ++j) { /* iterate outs */
if(putc(c, files[1].files[j]) == EOF){ if (putc(c, files[1].files[j]) == EOF) {
/* notebook's full */ /* notebook's full */
retval = EX_IOERR; retval = EX_IOERR;
fprintf(stderr, "%s: %s: %s\n", fprintf(
program_name, files[1].names[j], strerror(errno)); stderr,
if(fclose(files[1].files[j]) == EOF) "%s: %s: %s\n",
fprintf(stderr, "%s: %s: %s\n", program_name,
program_name, files[1].names[j], strerror(errno)); 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 */ /* massage out the tense muscle */
for(k = j--; k < files[1].s - 1; ++k){ for(k = j--; k < files[1].s - 1; ++k){
files[1].files[k] = files[1].files[k+1]; files[1].files[k] = files[1].files[k+1];
files[1].names[k] = files[1].names[k+1]; files[1].names[k] = files[1].names[k+1];
} }
if(--files[1].s == 0)
terminate; if(--files[1].s == 0) { terminate; }
} }
}
}
}
terminate; terminate;
} }