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

105
src/mm.c
View File

@ -95,11 +95,15 @@ oserr(char *s, char *r){
* from main. */
#define terminate \
for (i = 0; i < 2; ++i) { \
for(j = 0; j < files[i].s; ++j) \
if(files[i].files[j] != stdin \
for (j = 0; j < files[i].s; ++j) { \
if ( \
files[i].files[j] != stdin \
&& files[i].files[j] != stdout \
&& files[i].files[j] != stderr) \
&& files[i].files[j] != stderr \
) { \
fclose(files[i].files[j]); \
} \
} \
free(files[i].files); \
free(files[i].names); \
} \
@ -133,53 +137,64 @@ int main(int argc, char *argv[]){
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)
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 (Files_append(&files[1], stderr, stderr_name) != NULL) {
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;
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;
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;
if (signal(SIGINT, SIG_IGN) != SIG_ERR) { break; }
retval = oserr(argv[0], "-n");
terminate;
case 'u':
@ -189,6 +204,8 @@ int main(int argc, char *argv[]){
retval = usage(argv[0]);
terminate;
}
}
}
if (optind != argc) {
retval = usage(argv[0]);
@ -200,35 +217,51 @@ int main(int argc, char *argv[]){
/* 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));
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 */
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;