update from main archive 970619
[glibc/history.git] / login / programs / error.c
blobe6511442e35943ed1e1679eca5cdea6cfa0b1da7
1 /* Copyright (C) 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <errno.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <syslog.h>
27 #include "utmpd-private.h"
30 /* This variable indicates if we have forked. If set, we log messages
31 via the system logger. Otherwise we simply print the program name
32 and the message to standard error. */
33 int forked = 0;
36 /* Log error message MESSAGE, which is a printf-style format string
37 with optional args.
38 If ERRNUM is nonzero, also log its corresponding system error message.
39 Exit with status STATUS if it is nonzero. */
40 void
41 error (int status, int errnum, const char *message, ...)
43 va_list ap;
44 char *buffer = NULL;
46 va_start (ap, message);
47 vasprintf (&buffer, message, ap);
48 va_end (ap);
50 if (forked)
52 if (errnum == 0)
53 syslog (LOG_ERR, "%s", buffer);
54 else
55 syslog (LOG_ERR, "%s: %s", buffer, strerror (errnum));
57 else
59 if (errnum == 0)
60 fprintf (stderr, "%s: %s\n", program_invocation_name, buffer);
61 else
62 fprintf (stderr, "%s: %s: %s\n", program_invocation_name, buffer,
63 strerror (errnum));
66 if (buffer)
67 free (buffer);
69 if (status)
70 exit (status);
73 /* Log warning message MESSAGE, which is a printf-style format string
74 with optional args.
75 If ERRNUM is nonzero, also log its corresponding system error message. */
76 void
77 warning (int errnum, const char *message, ...)
79 va_list ap;
80 char *buffer = NULL;
82 va_start (ap, message);
83 vasprintf (&buffer, message, ap);
84 va_end (ap);
86 if (forked)
88 if (errnum == 0)
89 syslog (LOG_WARNING, "%s", buffer);
90 else
91 syslog (LOG_WARNING, "%s: %s", buffer, strerror (errnum));
93 else
95 if (errnum == 0)
96 printf ("%s: %s\n", program_invocation_name, buffer);
97 else
98 printf ("%s: %s: %s\n", program_invocation_name, buffer,
99 strerror (errnum));
102 if (buffer)
103 free (buffer);