xref: /onnv-gate/usr/src/cmd/filebench/common/utils.c (revision 7946:36d7e7afa9fa)
15184Sek110237 /*
25184Sek110237  * CDDL HEADER START
35184Sek110237  *
45184Sek110237  * The contents of this file are subject to the terms of the
55184Sek110237  * Common Development and Distribution License (the "License").
65184Sek110237  * You may not use this file except in compliance with the License.
75184Sek110237  *
85184Sek110237  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95184Sek110237  * or http://www.opensolaris.org/os/licensing.
105184Sek110237  * See the License for the specific language governing permissions
115184Sek110237  * and limitations under the License.
125184Sek110237  *
135184Sek110237  * When distributing Covered Code, include this CDDL HEADER in each
145184Sek110237  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155184Sek110237  * If applicable, add the following below this CDDL HEADER, with the
165184Sek110237  * fields enclosed by brackets "[]" replaced with your own identifying
175184Sek110237  * information: Portions Copyright [yyyy] [name of copyright owner]
185184Sek110237  *
195184Sek110237  * CDDL HEADER END
205184Sek110237  */
215184Sek110237 /*
226613Sek110237  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
235184Sek110237  * Use is subject to license terms.
246613Sek110237  *
256613Sek110237  * Portions Copyright 2008 Denis Cheng
265184Sek110237  */
275184Sek110237 
285184Sek110237 #include <limits.h>
295184Sek110237 #include <string.h>
305184Sek110237 #include <stdlib.h>
315184Sek110237 #include <stdarg.h>
325184Sek110237 #include <stdio.h>
335184Sek110237 #include <errno.h>
345184Sek110237 #ifdef HAVE_STDINT_H
355184Sek110237 #include <stdint.h>
365184Sek110237 #endif
376613Sek110237 
386613Sek110237 #include "filebench.h"
395184Sek110237 #include "utils.h"
405184Sek110237 #include "parsertypes.h"
415184Sek110237 
425184Sek110237 /*
43*7946SAndrew.W.Wilson@sun.com  * For now, just three routines: one to allocate a string in shared
44*7946SAndrew.W.Wilson@sun.com  * memory, one to emulate a strlcpy() function and one to emulate a
45*7946SAndrew.W.Wilson@sun.com  * strlcat() function, both the second and third only used in non
46*7946SAndrew.W.Wilson@sun.com  * Solaris environments,
475184Sek110237  *
485184Sek110237  */
495184Sek110237 
505184Sek110237 
515184Sek110237 /*
525184Sek110237  * Allocates space for a new string of the same length as
535184Sek110237  * the supplied string "str". Copies the old string into
545184Sek110237  * the new string and returns a pointer to the new string.
555184Sek110237  * Returns NULL if memory allocation for the new string fails.
565184Sek110237  */
575184Sek110237 char *
fb_stralloc(char * str)585184Sek110237 fb_stralloc(char *str)
595184Sek110237 {
605184Sek110237 	char *newstr;
615184Sek110237 
625184Sek110237 	if ((newstr = malloc(strlen(str) + 1)) == NULL)
635184Sek110237 		return (NULL);
645184Sek110237 	(void) strcpy(newstr, str);
655184Sek110237 	return (newstr);
665184Sek110237 }
67*7946SAndrew.W.Wilson@sun.com 
68*7946SAndrew.W.Wilson@sun.com #ifndef sun
69*7946SAndrew.W.Wilson@sun.com 
70*7946SAndrew.W.Wilson@sun.com /*
71*7946SAndrew.W.Wilson@sun.com  * Implements the strlcpy function when compilied for non Solaris
72*7946SAndrew.W.Wilson@sun.com  * operating systems. On solaris the strlcpy() function is used
73*7946SAndrew.W.Wilson@sun.com  * directly.
74*7946SAndrew.W.Wilson@sun.com  */
75*7946SAndrew.W.Wilson@sun.com size_t
fb_strlcpy(char * dst,const char * src,size_t dstsize)76*7946SAndrew.W.Wilson@sun.com fb_strlcpy(char *dst, const char *src, size_t dstsize)
77*7946SAndrew.W.Wilson@sun.com {
78*7946SAndrew.W.Wilson@sun.com 	uint_t i;
79*7946SAndrew.W.Wilson@sun.com 
80*7946SAndrew.W.Wilson@sun.com 	for (i = 0; i < (dstsize - 1); i++) {
81*7946SAndrew.W.Wilson@sun.com 
82*7946SAndrew.W.Wilson@sun.com 		/* quit if at end of source string */
83*7946SAndrew.W.Wilson@sun.com 		if (src[i] == '\0')
84*7946SAndrew.W.Wilson@sun.com 			break;
85*7946SAndrew.W.Wilson@sun.com 
86*7946SAndrew.W.Wilson@sun.com 		dst[i] = src[i];
87*7946SAndrew.W.Wilson@sun.com 	}
88*7946SAndrew.W.Wilson@sun.com 
89*7946SAndrew.W.Wilson@sun.com 	/* set end of dst string to \0 */
90*7946SAndrew.W.Wilson@sun.com 	dst[i] = '\0';
91*7946SAndrew.W.Wilson@sun.com 	i++;
92*7946SAndrew.W.Wilson@sun.com 
93*7946SAndrew.W.Wilson@sun.com 	return (i);
94*7946SAndrew.W.Wilson@sun.com }
95*7946SAndrew.W.Wilson@sun.com 
96*7946SAndrew.W.Wilson@sun.com /*
97*7946SAndrew.W.Wilson@sun.com  * Implements the strlcat function when compilied for non Solaris
98*7946SAndrew.W.Wilson@sun.com  * operating systems. On solaris the strlcat() function is used
99*7946SAndrew.W.Wilson@sun.com  * directly.
100*7946SAndrew.W.Wilson@sun.com  */
101*7946SAndrew.W.Wilson@sun.com size_t
fb_strlcat(char * dst,const char * src,size_t dstsize)102*7946SAndrew.W.Wilson@sun.com fb_strlcat(char *dst, const char *src, size_t dstsize)
103*7946SAndrew.W.Wilson@sun.com {
104*7946SAndrew.W.Wilson@sun.com 	uint_t i, j;
105*7946SAndrew.W.Wilson@sun.com 
106*7946SAndrew.W.Wilson@sun.com 	/* find the end of the current destination string */
107*7946SAndrew.W.Wilson@sun.com 	for (i = 0; i < (dstsize - 1); i++) {
108*7946SAndrew.W.Wilson@sun.com 		if (dst[i] == '\0')
109*7946SAndrew.W.Wilson@sun.com 			break;
110*7946SAndrew.W.Wilson@sun.com 	}
111*7946SAndrew.W.Wilson@sun.com 
112*7946SAndrew.W.Wilson@sun.com 	/* append the source string to the destination string */
113*7946SAndrew.W.Wilson@sun.com 	for (j = 0; i < (dstsize - 1); i++) {
114*7946SAndrew.W.Wilson@sun.com 		if (src[j] == '\0')
115*7946SAndrew.W.Wilson@sun.com 			break;
116*7946SAndrew.W.Wilson@sun.com 
117*7946SAndrew.W.Wilson@sun.com 		dst[i] = src[j];
118*7946SAndrew.W.Wilson@sun.com 		j++;
119*7946SAndrew.W.Wilson@sun.com 	}
120*7946SAndrew.W.Wilson@sun.com 
121*7946SAndrew.W.Wilson@sun.com 	/* set end of dst string to \0 */
122*7946SAndrew.W.Wilson@sun.com 	dst[i] = '\0';
123*7946SAndrew.W.Wilson@sun.com 	i++;
124*7946SAndrew.W.Wilson@sun.com 
125*7946SAndrew.W.Wilson@sun.com 	return (i);
126*7946SAndrew.W.Wilson@sun.com }
127*7946SAndrew.W.Wilson@sun.com 
128*7946SAndrew.W.Wilson@sun.com #endif /* !sun */
129