File: err.c

package info (click to toggle)
pdsh 1.7-6-8
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,024 kB
  • ctags: 517
  • sloc: ansic: 6,164; sh: 5,783; makefile: 201; exp: 107; perl: 91
file content (183 lines) | stat: -rw-r--r-- 4,738 bytes parent folder | download
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*****************************************************************************\
 *
 *  $Id: err.c,v 1.15 2002/09/26 20:48:22 garlick Exp $
 *  $Source: /chaos/cvs/pdsh/err.c,v $
 *
 *  Copyright (C) 1998-2002 The Regents of the University of California.
 *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
 *  Written by Jim Garlick (garlick@llnl.gov>
 *  UCRL-CODE-980038
 *  
 *  This file is part of PDSH, a parallel remote shell program.
 *  For details, see <http://www.llnl.gov/linux/pdsh/>.
 *  
 *  PDSH is free software; you can redistribute it and/or modify it under
 *  the terms of the GNU General Public License as published by the Free
 *  Software Foundation; either version 2 of the License, or (at your option)
 *  any later version.
 *  
 *  PDSH is distributed in the hope that it will be useful, but WITHOUT 
 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
 *  for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with PDSH; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
\*****************************************************************************/

/*
 * Error printing routines with variable arguments.
 */

#if     HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdarg.h>
#include <netdb.h>
#include <string.h>
#include <assert.h>
#if	HAVE_UNISTD_H
#include <unistd.h>	/* gethostname */
#endif
#include <ctype.h>
#include <stdlib.h>	/* exit */

#include "xstring.h"
#include "xmalloc.h"
#include "dsh.h"

static char *prog = NULL;
static char *host = NULL;

/*
 * Call this before calling err() or errx().  Sets hostname and program name 
 * for %H, %p, and %P.
 *   str (IN)	program name
 */
void 
err_init(char *str)
{
        char thishost[MAXHOSTNAMELEN];
        char *p;
 
	gethostname(thishost, MAXHOSTNAMELEN);
	if ((p = strchr(thishost, '.')) != NULL)
		*p = '\0';
	host = Strdup(thishost);
	prog = Strdup(str);
}

/*
 * Free heap storage allocated by err_init()
 */
void 
err_cleanup(void)
{
	Free((void **)&prog);
	Free((void **)&host);
}

/* 
 * _verr() is like vfprintf, but handles (only) the following formats:
 * following formats:
 * %s	string
 * %S   string, but treat as hostname and truncate after dot
 * %c	character
 * %m	string (sys_errlist[errno])
 * %d   int	
 * %z   equivalent to %.3d 
 * %p   program name with @host attached
 * %P   program name
 * %H   hostname for this host
 */
static void 
_verr(FILE *stream, char *format, va_list ap) 
{
	char *buf = NULL;
	char *q;
	int percent = 0;
	char tmpstr[LINEBUFSIZE];

	/* Note: snprintf silently truncates if argument exceeds size of tmpstr */

	assert(prog != NULL && host != NULL);
	while (format && *format) {			/* iterate thru chars */
		if (percent == 1) { 
			percent = 0;
			if (*format == 's') {		/* %s - string */
				xstrcat(&buf, va_arg(ap, char *));
			} else if (*format == 'S') {	/* %S - string, trunc */
				snprintf(tmpstr, sizeof(tmpstr), "%s", 
						va_arg(ap, char *));
				if (!isdigit(*tmpstr) 
						&& (q = strchr(tmpstr, '.')))
					*q = '\0';
				xstrcat(&buf, tmpstr);
			} else if (*format == 'z') {	/* %z - same as %.3d */
				snprintf(tmpstr, sizeof(tmpstr), "%.3d", 
						va_arg(ap, int));
				xstrcat(&buf, tmpstr);
			} else if (*format == 'c') {	/* %c - character */
				xstrcatchar(&buf, va_arg(ap, int));
			} else if (*format == 'd') {	/* %d - integer */
				snprintf(tmpstr, sizeof(tmpstr), "%d", 
						va_arg(ap, int));
				xstrcat(&buf, tmpstr);
			} else if (*format == 'm') {	/* %m - error code */
				xstrerrorcat(&buf);
			} else if (*format == 'P') {	/* %P - prog name */
				assert(prog != NULL);
				xstrcat(&buf, prog);
			} else if (*format == 'H') {	/* %H - this host */
				assert(host != NULL);
				xstrcat(&buf, host);
			} else if (*format == 'p') {	/* %p - prog@host */
				assert(prog != NULL);
				assert(host != NULL);
				snprintf(tmpstr, sizeof(tmpstr), "%s@%s", prog, host);
				xstrcat(&buf, tmpstr);
			} else				/* pass thru */
				xstrcatchar(&buf, *format);
		} else if (*format == '%')
			percent = 1;			/* remember % */
		else
			xstrcatchar(&buf, *format);	/* pass thru */
		format++;
	}

	fputs(buf, stream);				/* print it */
	Free((void **)&buf);				/* clean up */
}

void 
err(char *format, ...)
{
	va_list	ap;

	va_start(ap, format);
	_verr(stderr, format, ap);
	va_end(ap);
}

void 
errx(char *format, ...)
{
	va_list	ap;

	va_start(ap, format);
	_verr(stderr, format, ap);
	va_end(ap);
	exit(1);
}

void 
out(char *format, ...)
{
	va_list	ap;

	va_start(ap, format);
	_verr(stdout, format, ap);
	va_end(ap);
}