1 5184 ek110237 /* 2 5184 ek110237 * CDDL HEADER START 3 5184 ek110237 * 4 5184 ek110237 * The contents of this file are subject to the terms of the 5 5184 ek110237 * Common Development and Distribution License (the "License"). 6 5184 ek110237 * You may not use this file except in compliance with the License. 7 5184 ek110237 * 8 5184 ek110237 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 5184 ek110237 * or http://www.opensolaris.org/os/licensing. 10 5184 ek110237 * See the License for the specific language governing permissions 11 5184 ek110237 * and limitations under the License. 12 5184 ek110237 * 13 5184 ek110237 * When distributing Covered Code, include this CDDL HEADER in each 14 5184 ek110237 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 5184 ek110237 * If applicable, add the following below this CDDL HEADER, with the 16 5184 ek110237 * fields enclosed by brackets "[]" replaced with your own identifying 17 5184 ek110237 * information: Portions Copyright [yyyy] [name of copyright owner] 18 5184 ek110237 * 19 5184 ek110237 * CDDL HEADER END 20 5184 ek110237 */ 21 5184 ek110237 /* 22 6613 ek110237 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23 5184 ek110237 * Use is subject to license terms. 24 6613 ek110237 * 25 6613 ek110237 * Portions Copyright 2008 Denis Cheng 26 5184 ek110237 */ 27 5184 ek110237 28 5184 ek110237 #include <limits.h> 29 5184 ek110237 #include <string.h> 30 5184 ek110237 #include <stdlib.h> 31 5184 ek110237 #include <stdarg.h> 32 5184 ek110237 #include <stdio.h> 33 5184 ek110237 #include <errno.h> 34 5184 ek110237 #ifdef HAVE_STDINT_H 35 5184 ek110237 #include <stdint.h> 36 5184 ek110237 #endif 37 6613 ek110237 38 6613 ek110237 #include "filebench.h" 39 5184 ek110237 #include "utils.h" 40 5184 ek110237 #include "parsertypes.h" 41 5184 ek110237 42 5184 ek110237 /* 43 7946 Andrew * For now, just three routines: one to allocate a string in shared 44 7946 Andrew * memory, one to emulate a strlcpy() function and one to emulate a 45 7946 Andrew * strlcat() function, both the second and third only used in non 46 7946 Andrew * Solaris environments, 47 5184 ek110237 * 48 5184 ek110237 */ 49 5184 ek110237 50 5184 ek110237 51 5184 ek110237 /* 52 5184 ek110237 * Allocates space for a new string of the same length as 53 5184 ek110237 * the supplied string "str". Copies the old string into 54 5184 ek110237 * the new string and returns a pointer to the new string. 55 5184 ek110237 * Returns NULL if memory allocation for the new string fails. 56 5184 ek110237 */ 57 5184 ek110237 char * 58 5184 ek110237 fb_stralloc(char *str) 59 5184 ek110237 { 60 5184 ek110237 char *newstr; 61 5184 ek110237 62 5184 ek110237 if ((newstr = malloc(strlen(str) + 1)) == NULL) 63 5184 ek110237 return (NULL); 64 5184 ek110237 (void) strcpy(newstr, str); 65 5184 ek110237 return (newstr); 66 5184 ek110237 } 67 7946 Andrew 68 7946 Andrew #ifndef sun 69 7946 Andrew 70 7946 Andrew /* 71 7946 Andrew * Implements the strlcpy function when compilied for non Solaris 72 7946 Andrew * operating systems. On solaris the strlcpy() function is used 73 7946 Andrew * directly. 74 7946 Andrew */ 75 7946 Andrew size_t 76 7946 Andrew fb_strlcpy(char *dst, const char *src, size_t dstsize) 77 7946 Andrew { 78 7946 Andrew uint_t i; 79 7946 Andrew 80 7946 Andrew for (i = 0; i < (dstsize - 1); i++) { 81 7946 Andrew 82 7946 Andrew /* quit if at end of source string */ 83 7946 Andrew if (src[i] == '\0') 84 7946 Andrew break; 85 7946 Andrew 86 7946 Andrew dst[i] = src[i]; 87 7946 Andrew } 88 7946 Andrew 89 7946 Andrew /* set end of dst string to \0 */ 90 7946 Andrew dst[i] = '\0'; 91 7946 Andrew i++; 92 7946 Andrew 93 7946 Andrew return (i); 94 7946 Andrew } 95 7946 Andrew 96 7946 Andrew /* 97 7946 Andrew * Implements the strlcat function when compilied for non Solaris 98 7946 Andrew * operating systems. On solaris the strlcat() function is used 99 7946 Andrew * directly. 100 7946 Andrew */ 101 7946 Andrew size_t 102 7946 Andrew fb_strlcat(char *dst, const char *src, size_t dstsize) 103 7946 Andrew { 104 7946 Andrew uint_t i, j; 105 7946 Andrew 106 7946 Andrew /* find the end of the current destination string */ 107 7946 Andrew for (i = 0; i < (dstsize - 1); i++) { 108 7946 Andrew if (dst[i] == '\0') 109 7946 Andrew break; 110 7946 Andrew } 111 7946 Andrew 112 7946 Andrew /* append the source string to the destination string */ 113 7946 Andrew for (j = 0; i < (dstsize - 1); i++) { 114 7946 Andrew if (src[j] == '\0') 115 7946 Andrew break; 116 7946 Andrew 117 7946 Andrew dst[i] = src[j]; 118 7946 Andrew j++; 119 7946 Andrew } 120 7946 Andrew 121 7946 Andrew /* set end of dst string to \0 */ 122 7946 Andrew dst[i] = '\0'; 123 7946 Andrew i++; 124 7946 Andrew 125 7946 Andrew return (i); 126 7946 Andrew } 127 7946 Andrew 128 7946 Andrew #endif /* !sun */ 129