mm(1): formatting
This commit is contained in:
parent
26b0c93f4d
commit
c7c6ca2c60
105
src/mm.c
105
src/mm.c
@ -95,11 +95,15 @@ oserr(char *s, char *r){
|
|||||||
* 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] != stdin \
|
||||||
&& files[i].files[j] != stdout \
|
&& files[i].files[j] != stdout \
|
||||||
&& files[i].files[j] != stderr) \
|
&& 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); \
|
||||||
} \
|
} \
|
||||||
@ -133,53 +137,64 @@ int main(int argc, char *argv[]){
|
|||||||
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;
|
break;
|
||||||
case 'e':
|
case 'e':
|
||||||
if(Files_append(&files[1], stderr, stderr_name) != NULL)
|
if (Files_append(&files[1], stderr, stderr_name) != NULL) {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
retval = oserr(argv[0], "-e");
|
retval = oserr(argv[0], "-e");
|
||||||
terminate;
|
terminate;
|
||||||
case 'i':
|
case 'i':
|
||||||
if((strcmp(optarg, "-") == 0 && Files_append(&files[0],
|
if (
|
||||||
stdin, stdin_name) != NULL)
|
(strcmp(optarg, "-") == 0
|
||||||
|| Files_open(&files[0], optarg) != NULL)
|
&& Files_append(&files[0], stdin, stdin_name) != NULL)
|
||||||
break;
|
|| Files_open(&files[0], optarg) != NULL
|
||||||
|
) { break; }
|
||||||
|
|
||||||
retval = oserr(argv[0], optarg);
|
retval = oserr(argv[0], optarg);
|
||||||
terminate;
|
terminate;
|
||||||
case 'o':
|
case 'o':
|
||||||
if((strcmp(optarg, "-") == 0 && Files_append(&files[1],
|
if (
|
||||||
stdout, stdout_name) != NULL)
|
(strcmp(optarg, "-") == 0
|
||||||
|| Files_open(&files[1], optarg) != NULL)
|
&& Files_append(&files[1], stdout, stdout_name) != NULL)
|
||||||
break;
|
|| Files_open(&files[1], optarg) != NULL
|
||||||
|
) { break; }
|
||||||
/* does not exist, so try to create it */
|
/* does not exist, so try to create it */
|
||||||
if (errno == ENOENT) {
|
if (errno == ENOENT) {
|
||||||
files[1].mode = wharsh;
|
files[1].mode = wharsh;
|
||||||
|
|
||||||
if (Files_open(&files[1], optarg) != NULL) {
|
if (Files_open(&files[1], optarg) != NULL) {
|
||||||
files[1].mode = fmode[1];
|
files[1].mode = fmode[1];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
retval = oserr(argv[0], optarg);
|
retval = oserr(argv[0], optarg);
|
||||||
terminate;
|
terminate;
|
||||||
case 'n':
|
case 'n':
|
||||||
if(signal(SIGINT, SIG_IGN) != SIG_ERR)
|
if (signal(SIGINT, SIG_IGN) != SIG_ERR) { break; }
|
||||||
break;
|
|
||||||
retval = oserr(argv[0], "-n");
|
retval = oserr(argv[0], "-n");
|
||||||
terminate;
|
terminate;
|
||||||
case 'u':
|
case 'u':
|
||||||
@ -189,6 +204,8 @@ int main(int argc, char *argv[]){
|
|||||||
retval = usage(argv[0]);
|
retval = usage(argv[0]);
|
||||||
terminate;
|
terminate;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (optind != argc) {
|
if (optind != argc) {
|
||||||
retval = usage(argv[0]);
|
retval = usage(argv[0]);
|
||||||
@ -200,35 +217,51 @@ int main(int argc, char *argv[]){
|
|||||||
|
|
||||||
/* 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;
|
||||||
|
Loading…
Reference in New Issue
Block a user