#include <signal.h>
#include <setjmp.h>
#include <unistd.h>
+#include <ctype.h>
+#include <sys/stat.h>
+#include <locale.h>
+#include <sysexits.h>
static struct swit switches[] = {
#define PREPSW 0
switch (smatch(++cp, switches)) {
case AMBIGSW:
ambigsw(cp, switches);
- exit(1);
+ exit(EX_USAGE);
case UNKWNSW:
- adios(NULL, "-%s unknown", cp);
+ adios(EX_USAGE, NULL, "-%s unknown", cp);
case HELPSW:
snprintf(buffer, sizeof(buffer),
"%s [switches] file",
invo_name);
print_help(buffer, switches, 1);
- exit(0);
+ exit(argc == 2 ? EX_OK : EX_USAGE);
+
case VERSIONSW:
print_version(invo_name);
- exit(0);
+ exit(argc == 2 ? EX_OK : EX_USAGE);
case PREPSW:
prepend++;
}
}
- if (!drft)
- adios(NULL, "usage: %s [switches] file", invo_name);
- if ((in = fopen(drft, "r")) == NULL)
- adios(drft, "unable to open");
+ if (!drft) {
+ adios(EX_USAGE, NULL, "usage: %s [switches] file", invo_name);
+ }
+ if ((in = fopen(drft, "r")) == NULL) {
+ adios(EX_IOERR, drft, "unable to open");
+ }
tfile = m_mktemp2(NULL, invo_name, NULL, &out);
- if (tfile == NULL)
- adios("prompter", "unable to create temporary file");
+ if (tfile == NULL) {
+ adios(EX_CANTCREAT, "prompter", "unable to create temporary file");
+ }
chmod(tmpfil, 0600);
strncpy(tmpfil, tfile, sizeof(tmpfil));
switch (state = m_getfld(state, name, field, sizeof(field),
in)) {
case FLD:
- case FLDEOF:
case FLDPLUS:
/*
** Check if the value of field contains
if (i == -1) {
abort:
unlink(tmpfil);
- /* sysexits.h EX_DATAERR */
- exit(1);
+ exit(EX_DATAERR);
}
if (i || (field[0]!='\n' && field[0]!='\0')) {
fprintf(out, "%s:", name);
}
}
- if (state == FLDEOF) { /* moby hack */
- /* draft has no body separator; only headers */
- fprintf(out, "--------\n");
- if (!qbody)
- break;
- printf("--------\n");
- goto has_no_body;
- }
continue;
case BODY:
- case BODYEOF:
case FILEEOF:
fprintf(out, "--------\n");
if (qbody) {
break;
default:
- adios(NULL, "skeleton is poorly formatted");
+ adios(EX_DATAERR, NULL, "skeleton is poorly formatted");
}
break;
}
fclose(out);
SIGNAL(SIGINT, SIG_IGN);
- if ((fdi = open(tmpfil, O_RDONLY)) == NOTOK)
- adios(tmpfil, "unable to re-open");
- if ((fdo = creat(drft, m_gmprot())) == NOTOK)
- adios(drft, "unable to write");
+ if ((fdi = open(tmpfil, O_RDONLY)) == NOTOK) {
+ adios(EX_IOERR, tmpfil, "unable to re-open");
+ }
+ if ((fdo = creat(drft, m_gmprot())) == NOTOK) {
+ adios(EX_IOERR, drft, "unable to write");
+ }
cpydata(fdi, fdo, tmpfil, drft);
close(fdi);
close(fdo);
unlink(tmpfil);
context_save(); /* save the context file */
- return 0;
+ return EX_OK;
}
getln(char *buffer, int n)
{
int c;
- sig_atomic_t psigint;
+ sig_atomic_t psigint = sigint;
char *cp;
cp = buffer;
switch (setjmp(sigenv)) {
case 0:
wtuser = 1;
- psigint = sigint;
break;
default: