| 1 | /* |
|---|
| 2 | * Revision Control Information |
|---|
| 3 | * |
|---|
| 4 | * $Id: strsav.c,v 1.7 2009/01/05 21:04:04 fabio Exp $ |
|---|
| 5 | * |
|---|
| 6 | */ |
|---|
| 7 | /* LINTLIBRARY */ |
|---|
| 8 | |
|---|
| 9 | #include <stdio.h> |
|---|
| 10 | #include "util.h" |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | /* |
|---|
| 14 | * util_strsav -- save a copy of a string |
|---|
| 15 | */ |
|---|
| 16 | char * |
|---|
| 17 | util_strsav(char const *s) |
|---|
| 18 | { |
|---|
| 19 | if(s == NIL(char)) { /* added 7/95, for robustness */ |
|---|
| 20 | return NIL(char); |
|---|
| 21 | } |
|---|
| 22 | else { |
|---|
| 23 | return strcpy(ALLOC(char, strlen(s)+1), s); |
|---|
| 24 | } |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | /* |
|---|
| 28 | * util_inttostr -- converts an integer into a string |
|---|
| 29 | */ |
|---|
| 30 | char * |
|---|
| 31 | util_inttostr(int i) |
|---|
| 32 | { |
|---|
| 33 | unsigned int mod, len; |
|---|
| 34 | char *s; |
|---|
| 35 | |
|---|
| 36 | if (i == 0) |
|---|
| 37 | len = 1; |
|---|
| 38 | else { |
|---|
| 39 | if (i < 0) { |
|---|
| 40 | len = 1; |
|---|
| 41 | mod = -i; |
|---|
| 42 | } |
|---|
| 43 | else { |
|---|
| 44 | len = 0; |
|---|
| 45 | mod = i; |
|---|
| 46 | } |
|---|
| 47 | len += (unsigned) floor(log10(mod)) + 1; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | s = ALLOC(char, len + 1); |
|---|
| 51 | sprintf(s, "%d", i); |
|---|
| 52 | |
|---|
| 53 | return s; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | /* |
|---|
| 57 | * util_strcat3 -- Creates a new string which is the concatenation of 3 |
|---|
| 58 | * strings. It is the responsibility of the caller to free this string |
|---|
| 59 | * using FREE. |
|---|
| 60 | */ |
|---|
| 61 | char * |
|---|
| 62 | util_strcat3( |
|---|
| 63 | char const * str1, |
|---|
| 64 | char const * str2, |
|---|
| 65 | char const * str3) |
|---|
| 66 | { |
|---|
| 67 | char *str = ALLOC(char, strlen(str1) + strlen(str2) + strlen(str3) + 1); |
|---|
| 68 | |
|---|
| 69 | (void) strcpy(str, str1); |
|---|
| 70 | (void) strcat(str, str2); |
|---|
| 71 | (void) strcat(str, str3); |
|---|
| 72 | |
|---|
| 73 | return (str); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | /* |
|---|
| 77 | * util_strcat4 -- Creates a new string which is the concatenation of 4 |
|---|
| 78 | * strings. It is the responsibility of the caller to free this string |
|---|
| 79 | * using FREE. |
|---|
| 80 | */ |
|---|
| 81 | char * |
|---|
| 82 | util_strcat4( |
|---|
| 83 | char const * str1, |
|---|
| 84 | char const * str2, |
|---|
| 85 | char const * str3, |
|---|
| 86 | char const * str4) |
|---|
| 87 | { |
|---|
| 88 | char *str = ALLOC(char, strlen(str1) + strlen(str2) + strlen(str3) + |
|---|
| 89 | strlen(str4) + 1); |
|---|
| 90 | |
|---|
| 91 | (void) strcpy(str, str1); |
|---|
| 92 | (void) strcat(str, str2); |
|---|
| 93 | (void) strcat(str, str3); |
|---|
| 94 | (void) strcat(str, str4); |
|---|
| 95 | |
|---|
| 96 | return (str); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | #if !HAVE_STRSTR |
|---|
| 101 | /**Function******************************************************************** |
|---|
| 102 | |
|---|
| 103 | Synopsis [required] |
|---|
| 104 | |
|---|
| 105 | Description [optional] |
|---|
| 106 | |
|---|
| 107 | SideEffects [required] |
|---|
| 108 | |
|---|
| 109 | SeeAlso [optional] |
|---|
| 110 | |
|---|
| 111 | ******************************************************************************/ |
|---|
| 112 | char * |
|---|
| 113 | strstr( |
|---|
| 114 | char const * s, |
|---|
| 115 | char const * pat) |
|---|
| 116 | { |
|---|
| 117 | int len; |
|---|
| 118 | |
|---|
| 119 | len = strlen(pat); |
|---|
| 120 | for (; *s != '\0'; ++s) |
|---|
| 121 | if (*s == *pat && memcmp(s, pat, len) == 0) { |
|---|
| 122 | return (char *)s; /* UGH */ |
|---|
| 123 | } |
|---|
| 124 | return NULL; |
|---|
| 125 | } |
|---|
| 126 | #endif /* !HAVE_STRSTR */ |
|---|
| 127 | |
|---|
| 128 | #if !HAVE_STRCHR |
|---|
| 129 | /**Function******************************************************************** |
|---|
| 130 | |
|---|
| 131 | Synopsis [required] |
|---|
| 132 | |
|---|
| 133 | Description [optional] |
|---|
| 134 | |
|---|
| 135 | SideEffects [required] |
|---|
| 136 | |
|---|
| 137 | SeeAlso [optional] |
|---|
| 138 | |
|---|
| 139 | ******************************************************************************/ |
|---|
| 140 | char * |
|---|
| 141 | strchr(char const * s, int c) |
|---|
| 142 | { |
|---|
| 143 | for (; *s != '\0'; s++) { |
|---|
| 144 | if (*s == c) { |
|---|
| 145 | return (char *)s; |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | return NULL; |
|---|
| 149 | |
|---|
| 150 | } |
|---|
| 151 | #endif /* !HAVE_STRCHR */ |
|---|