diff --git a/docs/mm.1 b/docs/mm.1 index e84ac66..2244588 100644 --- a/docs/mm.1 +++ b/docs/mm.1 @@ -56,7 +56,7 @@ writing to that particular output, and if there are more outputs specified, continues, eventually exiting unsuccessfully. .PP On error mm prints a diagnostic message and exits with the appropriate -sysexits.h(3) status. +sysexits.h(3) status. .SH BUGS diff --git a/src/mm.c b/src/mm.c index 5e81f7f..ff62148 100644 --- a/src/mm.c +++ b/src/mm.c @@ -132,53 +132,57 @@ int main(int argc, char *argv[]){ k = 0; - 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 > 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; - 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], "-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) + 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; - retval = oserr(argv[0], "-n"); - terminate; - case 'u': - k = 1; - break; - default: - fprintf(stderr, "Usage: %s (-aenu) (-i [input])..." - " (-o [output])...\n", argv[0]); - retval = EX_USAGE; - terminate; - } + default: + fprintf(stderr, "Usage: %s (-aenu) (-i [input])..." + " (-o [output])...\n", argv[0]); + retval = EX_USAGE; + terminate; + } files[0].s += files[0].s == 0; files[1].s += files[1].s == 0; @@ -202,6 +206,8 @@ int main(int argc, char *argv[]){ 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));