File: xstring.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 (305 lines) | stat: -rw-r--r-- 7,213 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*****************************************************************************\
 *
 *  $Id: xstring.c,v 1.20 2002/09/26 20:48:22 garlick Exp $
 *  $Source: /chaos/cvs/pdsh/xstring.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.
\*****************************************************************************/

/*
 * Heap-oriented string functions.
 */

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

#include <string.h>
#if 	HAVE_STRERROR_R && !HAVE_DECL_STRERROR_R
char *strerror_r(int, char *, int);
#endif
#include <errno.h>
#if	HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <assert.h>

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

#define SPACES "\n\t "
 
#define XFGETS_CHUNKSIZE 32

/*
 * Zap leading and trailing occurrences of characters in 'verboten'.
 *   str (IN/OUT)	string
 *   verboten (IN)	list of characters to be zapped (if NULL, zap spaces)
 */
void 
xstrcln(char *str, char *verboten)
{
	char *p;
	char *base = str;

	if (verboten == NULL)
		verboten = SPACES;

	/* move pointer past initial 'verboten' characters */
	while (str != NULL && *str != '\0' && strchr(verboten, *str) != NULL)
		str++;
	
	/* overwrite trailing 'verboten' characters with nulls */
	if (str != NULL && strlen(str) > 0) {
		p = str + strlen(str) - 1;
		while (p > str && *p != '\0' && strchr(verboten, *p) != NULL)
			*p-- = '\0';
	}

	/* move string */
	assert(str >= base);
	memmove(base, str, strlen(str));
	while (str-- > base)
		base[strlen(base) - 1] = '\0';
}

/*
 * Similar to fgets(), but accepts a pointer to a dynamically allocated buffer
 * for the line and expands it as needed.  The buffer, if it has non-zero 
 * length, will always be terminated by a carriage return. 
 * EOF or error can be returned with valid data in the buffer.
 *   str (IN/OUT)	buffer where input is stored (xfgets may resize)
 *   stream (IN)	stream to read from
 *   RETURN		0 = EOF, -1 = error, 1 = connection still open.
 */
int 
xfgets(char **str, FILE *stream)
{
	int check_err = 0;
	int rv = 1;
	int nread = 0;

	/*
	 * Initialize buffer space if a pointer-to-null was passed in.
	 */	
	if (*str == NULL) {
		*str = Malloc(XFGETS_CHUNKSIZE);
	}

	/*
	 * Read a line's worth of characters, or up to EOF or error.
	 * Expand buffer if necessary.
	 */
	do {
		/* allocate more buffer space if needed */
		if (nread == Size(*str) - 2) {
			int newsize = Size(*str) + XFGETS_CHUNKSIZE;

			Realloc((void **)str, newsize);
			assert(Size(*str) == newsize);
		}
		/* read a character -- quit loop if we get EOF or error */
		if (fread(*str + nread, 1, 1, stream) != 1) {
			check_err = 1;
			break;
		}
		nread++;

	}  while (*(*str + nread - 1) != '\n');

	*(*str + nread) = '\0'; /* NULL termination */

	/*
	 * Determine if return value needs to be EOF (0) or error (-1).
	 */
	if (check_err) {
		if (feof(stream))
			rv = 0;
		else if (ferror(stream))
			rv = -1;

		/* add a terminating \n */
		if (strlen(*str) > 0)
			strcat(*str, "\n");
	}

	return rv;
}

/*
 * Same as above except it uses read() instead of fread().
 *   str (IN/OUT)	buffer where input is stored (xfgets may resize)
 *   fd (IN)		file descriptor to read from
 *   RETURN		0 = EOF, -1 = error, 1 = connection still open.
 */
int 
xfgets2(char **str, FILE *stream)
{
	int check_err = 0;
	int rv = 1;
	int nread = 0;
	int fd = fileno(stream);

	/*
	 * Initialize buffer space if a pointer-to-null was passed in.
	 */	
	if (*str == NULL)
		*str = Malloc(XFGETS_CHUNKSIZE);

	/*
	 * Read a line's worth of characters, or up to EOF or error.
	 * Expand buffer if necessary.
	 */
	do {
		/* allocate more buffer space if needed */
		if (nread == Size(*str) - 2) {
			int newsize = Size(*str) + XFGETS_CHUNKSIZE;

			Realloc((void **)str, newsize);
			assert(Size(*str) == newsize);
		}
		/* read a character -- quit loop if we get EOF or error */
		if ((rv = read(fd, *str + nread, 1)) != 1) {
			check_err = 1;
			break;
		}
		nread++;

	}  while (*(*str + nread - 1) != '\n');

	*(*str + nread) = '\0'; /* NULL termination */

	/*
	 * Determine if return value needs to be EOF (0) or error (-1).
	 */
	if (check_err) {
		/* add a terminating \n */
		if (strlen(*str) > 0)
			strcat(*str, "\n");
	}

	return rv;
}

/*
 * Ensure that a string has enough space to add 'needed' characters.
 * If the string is uninitialized, it should be NULL.
 */
static void 
_makespace(char **str, int needed)
{
	int used;

	if (*str == NULL)
		*str = Malloc(needed + 1);
	else {
		used = strlen(*str) + 1;
		while (used + needed > Size(*str)) {
			int newsize = Size(*str) + XFGETS_CHUNKSIZE;

			Realloc((void **)str, newsize);
			assert(Size(*str) == newsize);
		}
	}
}
		
/* 
 * Concatenate str2 onto str1, expanding str1 as needed.
 *   str1 (IN/OUT)	target string (pointer to in case of expansion)
 *   str2 (IN)		source string
 */
void 
xstrcat(char **str1, char *str2)
{
	_makespace(str1, strlen(str2));
	strcat(*str1, str2);
}

/* 
 * Copy str2 to str1, expanding str1 as needed.
 *   str1 (IN/OUT)	target string (pointer to in case of expansion)
 *   str2 (IN)		source string
 */
void 
xstrcpy(char **str1, char *str2)
{
	_makespace(str1, strlen(str2));
	strcpy(*str1, str2);
}

static void 
_strcatchar(char *str, char c)
{
	int len = strlen(str);

	str[len++] = c;
	str[len] = '\0';
}

/* 
 * Add a character to str, expanding str1 as needed.
 *   str1 (IN/OUT)	target string (pointer to in case of expansion)
 *   size (IN/OUT)	size of str1 (pointer to in case of expansion)
 *   c (IN)		character to add
 */
void 
xstrcatchar(char **str, char c)
{
	_makespace(str, 1);
	_strcatchar(*str, c);
}

void 
xstrerrorcat(char **buf)
{
#if HAVE_STRERROR_R
#  if HAVE_WORKING_STRERROR_R || STRERROR_R_CHAR_P
        char errbuf[64];
        char *err = strerror_r(errno, errbuf, 64);
#  else
	char err[64];
	strerror_r(errno, err, 64);
#  endif
#elif HAVE_STRERROR
        char *err = strerror(errno);
#else
        extern char *sys_errlist[];
        char *err = sys_errlist[errno];
#endif
        xstrcat(buf, err);
}


/* 
 * Replacement for libc basename
 *   path (IN)		path possibly containing '/' characters
 *   RETURN		last component of path
 */
char *
xbasename(char *path)
{
	char *p;

	p = strrchr(path , '/');
	return (p ? (p + 1) : path);
}