| [12] | 1 | /* |
|---|
| 2 | * Revision Control Information |
|---|
| 3 | * |
|---|
| 4 | * $Id: strsav.c,v 1.2 2009-04-13 17:03:40 hhkim 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 *s) |
|---|
| 18 | { |
|---|
| 19 | if(s == NIL(char)) { /* added 7/95, for robustness */ |
|---|
| 20 | return s; |
|---|
| 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 * str1, |
|---|
| 64 | char * str2, |
|---|
| 65 | char * str3) |
|---|
| 66 | { |
|---|
| 67 | /*char *str = ALLOC(char, strlen(str1) + strlen(str2) + strlen(str3) + 1);*/ |
|---|
| 68 | char *str; |
|---|
| 69 | int l1, l2, l3, l4; |
|---|
| 70 | |
|---|
| 71 | l1 = strlen(str1); |
|---|
| 72 | l2 = strlen(str2); |
|---|
| 73 | l3 = strlen(str3); |
|---|
| 74 | l4 = l1 + l2 + l3 + 1; |
|---|
| 75 | |
|---|
| 76 | str = ALLOC(char, l4); |
|---|
| 77 | |
|---|
| 78 | (void) strcpy(str, str1); |
|---|
| 79 | (void) strcat(str, str2); |
|---|
| 80 | (void) strcat(str, str3); |
|---|
| 81 | |
|---|
| 82 | return (str); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | /* |
|---|
| 86 | * util_strcat4 -- Creates a new string which is the concatenation of 4 |
|---|
| 87 | * strings. It is the responsibility of the caller to free this string |
|---|
| 88 | * using FREE. |
|---|
| 89 | */ |
|---|
| 90 | char * |
|---|
| 91 | util_strcat4( |
|---|
| 92 | char * str1, |
|---|
| 93 | char * str2, |
|---|
| 94 | char * str3, |
|---|
| 95 | char * str4) |
|---|
| 96 | { |
|---|
| 97 | /*char *str = ALLOC(char, strlen(str1) + strlen(str2) + strlen(str3) + |
|---|
| 98 | strlen(str4) + 1);*/ |
|---|
| 99 | char *str; |
|---|
| 100 | int l1, l2, l3, l4, l5; |
|---|
| 101 | |
|---|
| 102 | l1 = strlen(str1); |
|---|
| 103 | l2 = strlen(str2); |
|---|
| 104 | l3 = strlen(str3); |
|---|
| 105 | l4 = strlen(str4); |
|---|
| 106 | l5 = l1 + l2 + l3 + l4 + 1; |
|---|
| 107 | str = ALLOC(char, l5); |
|---|
| 108 | |
|---|
| 109 | (void) strcpy(str, str1); |
|---|
| 110 | (void) strcat(str, str2); |
|---|
| 111 | (void) strcat(str, str3); |
|---|
| 112 | (void) strcat(str, str4); |
|---|
| 113 | |
|---|
| 114 | return (str); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | #if !HAVE_STRSTR |
|---|
| 119 | /**Function******************************************************************** |
|---|
| 120 | |
|---|
| 121 | Synopsis [required] |
|---|
| 122 | |
|---|
| 123 | Description [optional] |
|---|
| 124 | |
|---|
| 125 | SideEffects [required] |
|---|
| 126 | |
|---|
| 127 | SeeAlso [optional] |
|---|
| 128 | |
|---|
| 129 | ******************************************************************************/ |
|---|
| 130 | char * |
|---|
| 131 | strstr( |
|---|
| 132 | const char * s, |
|---|
| 133 | const char * pat) |
|---|
| 134 | { |
|---|
| 135 | int len; |
|---|
| 136 | |
|---|
| 137 | len = strlen(pat); |
|---|
| 138 | for (; *s != '\0'; ++s) |
|---|
| 139 | if (*s == *pat && memcmp(s, pat, len) == 0) { |
|---|
| 140 | return (char *)s; /* UGH */ |
|---|
| 141 | } |
|---|
| 142 | return NULL; |
|---|
| 143 | } |
|---|
| 144 | #endif /* !HAVE_STRSTR */ |
|---|
| 145 | |
|---|
| 146 | #if !HAVE_STRCHR |
|---|
| 147 | /**Function******************************************************************** |
|---|
| 148 | |
|---|
| 149 | Synopsis [required] |
|---|
| 150 | |
|---|
| 151 | Description [optional] |
|---|
| 152 | |
|---|
| 153 | SideEffects [required] |
|---|
| 154 | |
|---|
| 155 | SeeAlso [optional] |
|---|
| 156 | |
|---|
| 157 | ******************************************************************************/ |
|---|
| 158 | char * |
|---|
| 159 | strchr(const char * s, int c) |
|---|
| 160 | { |
|---|
| 161 | for (; *s != '\0'; s++) { |
|---|
| 162 | if (*s == c) { |
|---|
| 163 | return (char *)s; |
|---|
| 164 | } |
|---|
| 165 | } |
|---|
| 166 | return NULL; |
|---|
| 167 | |
|---|
| 168 | } |
|---|
| 169 | #endif /* !HAVE_STRCHR */ |
|---|