File: errno.c

package info (click to toggle)
shhmsg 1.4.1-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 148 kB
  • ctags: 78
  • sloc: ansic: 319; makefile: 134; sh: 10
file content (111 lines) | stat: -rw-r--r-- 3,389 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* $Id: errno.c,v 1.6 2002/03/02 19:37:36 sverrehu Exp $ */
/*------------------------------------------------------------------------
 |  FILE            errno.c
 |  MODULE OF       shhmsg - library for displaying messages.
 |
 |  DESCRIPTION     The functions in this file, are for displaying errno-
 |                  dependant error messages.
 |
 |  WRITTEN BY      Sverre H. Huseby <shh@thathost.com>
 +----------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>

#include "internal.h"
#include "shhmsg.h"

#ifndef HAVE_STRERROR
extern const char *const sys_errlist[];
#define strerror(x) sys_errlist[x]
#endif

/*-----------------------------------------------------------------------+
|  PUBLIC FUNCTIONS                                                      |
+-----------------------------------------------------------------------*/

/*------------------------------------------------------------------------
 |
 |  NAME          msgPerror
 |
 |  FUNCTION      Show an errno-dependant error message.
 |
 |  SYNOPSIS      #include "shhmsg.h"
 |                void msgPerror(const char *format, ...);
 |
 |  INPUT         format, ...
 |                        Arguments used as with printf(). If NULL,
 |                        the name of the program is printed.
 |
 |  DESCRIPTION   Prints the given text followed by ": " and the
 |                correct errormessage on the _msgErrorStream.
 |		  May add the program name before anything is printed.
 |
 |                If errno indicates "no error", nothing is printed.
 */
void
msgPerror(const char *format, ...)
{
    va_list ap;
    int en;

    if (!(en = errno))
        return;
    fflush(stdout);
    if (format && *format) {
	if (_msgShowNameAlways)
	    fprintf(GET_ERROR_STREAM, "%s: ", msgGetName());
	va_start(ap, format);
	vfprintf(GET_ERROR_STREAM, format, ap);
	va_end(ap);
    } else
	fprintf(GET_ERROR_STREAM, msgGetName());
    fprintf(GET_ERROR_STREAM, ": %s\n", strerror(en));
}

/*------------------------------------------------------------------------
 |
 |  NAME          msgFatalPerror
 |
 |  FUNCTION      Show an errno-dependant error message and abort the program.
 |
 |  SYNOPSIS      #include "shhmsg.h"
 |                void msgFatalPerror(const char *format, ...);
 |
 |  INPUT         format, ...
 |                        Arguments used as with printf(). If NULL,
 |                        the name of the program is printed.
 |
 |  RETURNS       Nothing, never returns.
 |
 |  DESCRIPTION   Prints the given text followed by ": " and the
 |                correct errormessage on the _msgErrorStream, and then
 |                aborts the program.
 |		  May add the program name before anything is printed.
 |
 |                If errno indicates "no error", nothing is printed,
 |                and the program keeps running.
 */
void
msgFatalPerror(const char *format, ...)
{
    va_list ap;
    int en;

    if (!(en = errno))
        return;
    fflush(stdout);
    if (format && *format) {
	if (_msgShowNameAlways)
	    fprintf(GET_ERROR_STREAM, "%s: ", msgGetName());
	va_start(ap, format);
	vfprintf(GET_ERROR_STREAM, format, ap);
	va_end(ap);
    } else
	fprintf(GET_ERROR_STREAM, msgGetName());
    fprintf(GET_ERROR_STREAM, ": %s\n", strerror(en));
    exit(99);
}