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