Wrapped IO
Extend the basic System IO functions as needed for the sort application.
Notes
Mostly this is a convient join point for adding error handling
and other application extensions.
- Define xfopen()
-
Function definition
- Segment Source
- 335: static FILE *
336: xfopen (const char *file, const char *how)
337: {
338: FILE *fp;
339:
340: if (strcmp (file, "-") == 0)
341: {
342: fp = stdin;
343: }
344: else
345: {
346: if ((fp = fopen (file, how)) == NULL)
347: {
348: error (0, errno, "%s", file);
349: cleanup ();
350: exit (SORT_FAILURE);
351: }
352: }
353:
354: if (fp == stdin)
355: have_read_stdin = 1;
356: return fp;
357: }
358:
- Define xfclose()
-
Function definition
- Notes
-
Very nice special case support for both
and .
This extra behavior isn't really part of
any other concern.
- Segment Source
- 359: static void
360: xfclose (FILE *fp)
361: {
362: if (fp == stdin)
363: {
364: /* Allow reading stdin from tty more than once. */
365: if (feof (fp))
366: clearerr (fp);
367: }
368: else if (fp == stdout)
369: {
370: if (fflush (fp) != 0)
371: {
372: error (0, errno, _("flushing file"));
373: cleanup ();
374: exit (SORT_FAILURE);
375: }
376: }
377: else
378: {
379: if (fclose (fp) != 0)
380: {
381: error (0, errno, _("error closing file"));
382: cleanup ();
383: exit (SORT_FAILURE);
384: }
385: }
386: }
387: