#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
+#define NDEBUG 1
+#include <assert.h>
#include "system.h"
#include "long-options.h"
#include "error.h"
#define DEFAULT_TMPDIR "/tmp"
#endif
+/* Use this as exit status in case of error, not EXIT_FAILURE. This
+ is necessary because EXIT_FAILURE is usually 1 and POSIX requires
+ that sort exit with status 1 IFF invoked with -c and the input is
+ not properly sorted. Any other irregular exit must exit with a
+ status code greater than 1. */
+#define SORT_FAILURE 2
+
/* The kind of blanks for '-b' to skip in various options. */
enum blanktype { bl_start, bl_end, bl_both };
")
, DEFAULT_TMPDIR);
}
+ /* Don't use EXIT_FAILURE here in case it is defined to be 1.
+ POSIX requires that sort return 1 IFF invoked with -c and
+ the input is not properly sorted. */
+ assert (status == 0 || status == SORT_FAILURE);
exit (status);
}
{
error (0, 0, _("virtual memory exhausted"));
cleanup ();
- exit (2);
+ exit (SORT_FAILURE);
}
return p;
}
{
error (0, 0, _("virtual memory exhausted"));
cleanup ();
- exit (2);
+ exit (SORT_FAILURE);
}
return p;
}
{
error (0, errno, "%s", file);
cleanup ();
- exit (2);
+ exit (SORT_FAILURE);
}
return fp;
{
error (0, errno, "%s", file);
cleanup ();
- exit (2);
+ exit (SORT_FAILURE);
}
}
{
error (0, errno, _("flushing file"));
cleanup ();
- exit (2);
+ exit (SORT_FAILURE);
}
}
else
{
error (0, errno, _("error closing file"));
cleanup ();
- exit (2);
+ exit (SORT_FAILURE);
}
}
}
{
error (0, errno, _("write error"));
cleanup ();
- exit (2);
+ exit (SORT_FAILURE);
}
}
{
error (0, errno, _("read error"));
cleanup ();
- exit (2);
+ exit (SORT_FAILURE);
}
buf->used += cc;
}
static void
badfieldspec (const char *s)
{
- error (2, 0, _("invalid field specification `%s'"), s);
+ error (SORT_FAILURE, 0, _("invalid field specification `%s'"), s);
}
/* Handle interrupts and hangups. */
if (digits[UCHAR (*s)] || (*s == '.' && digits[UCHAR (s[1])]))
{
if (!key)
- usage (2);
+ usage (SORT_FAILURE);
for (t = 0; digits[UCHAR (*s)]; ++s)
t = t * 10 + *s - '0';
t2 = 0;
else
{
if (i == argc - 1)
- error (2, 0, _("option `-k' requires an argument"));
+ error (SORT_FAILURE, 0,
+ _("option `-k' requires an argument"));
else
s = argv[++i];
}
else
{
if (i == argc - 1)
- error (2, 0, _("option `-o' requires an argument"));
+ error (SORT_FAILURE, 0,
+ _("option `-o' requires an argument"));
else
outfile = argv[++i];
}
goto outer;
}
else
- error (2, 0, _("option `-t' requires an argument"));
+ error (SORT_FAILURE, 0,
+ _("option `-t' requires an argument"));
break;
case 'T':
if (s[1])
if (i < argc - 1)
temp_file_prefix = argv[++i];
else
- error (2, 0, _("option `-T' requires an argument"));
+ error (SORT_FAILURE, 0,
+ _("option `-T' requires an argument"));
}
goto outer;
/* break; */
default:
fprintf (stderr, _("%s: unrecognized option `-%c'\n"),
argv[0], *s);
- usage (2);
+ usage (SORT_FAILURE);
}
if (*s)
++s;
}
if (checkonly)
- exit (check (files, nfiles) != 0);
+ {
+ /* POSIX requires that sort return 1 IFF invoked with -c and the
+ input is not properly sorted. */
+ exit (check (files, nfiles) == 0 ? 0 : 1);
+ }
if (strcmp (outfile, "-"))
{
{
error (0, errno, "%s", files[i]);
cleanup ();
- exit (2);
+ exit (SORT_FAILURE);
}
if (S_ISREG (instat.st_mode)
&& (instat.st_ino != outstat.st_ino
{
error (0, errno, "%s", files[i]);
cleanup ();
- exit (2);
+ exit (SORT_FAILURE);
}
xfclose (ofp);
xfclose (fp);
Solaris, Ultrix, and Irix. This premature fflush makes the output
reappear. --karl@cs.umb.edu */
if (fflush (ofp) < 0)
- error (1, errno, _("%s: write error"), outfile);
+ error (SORT_FAILURE, errno, _("%s: write error"), outfile);
if (have_read_stdin && fclose (stdin) == EOF)
- error (1, errno, outfile);
+ error (SORT_FAILURE, errno, outfile);
if (ferror (stdout) || fclose (stdout) == EOF)
- error (1, errno, _("%s: write error"), outfile);
+ error (SORT_FAILURE, errno, _("%s: write error"), outfile);
- exit (0);
+ exit (EXIT_SUCCESS);
}