]>
Commit | Line | Data |
---|---|---|
68687902 | 1 | /* |
2 | * $Source$ | |
3 | * $Author$ | |
4 | * $Header$ | |
5 | * | |
6 | * Copyright (C) 1987 by the Massachusetts Institute of Technology | |
babbc197 | 7 | * For copying and distribution information, please see the file |
8 | * <mit-copyright.h>. | |
68687902 | 9 | * |
10 | * Miscellaneous string functions. | |
11 | */ | |
12 | ||
13 | #ifndef lint | |
14 | static char *rcsid_strs_c = "$Header$"; | |
15 | #endif lint | |
16 | ||
babbc197 | 17 | #include <mit-copyright.h> |
0fed7e9b | 18 | #include <sys/types.h> |
501a4b9d | 19 | #include <string.h> |
0fed7e9b | 20 | #include <ctype.h> |
21 | ||
22 | extern char *malloc(), *realloc(); | |
68687902 | 23 | |
24 | /* | |
25 | * Random string functions which should be in the C library.. | |
26 | */ | |
27 | ||
28 | /* | |
29 | * Make a copy of a string. | |
30 | */ | |
31 | char * | |
32 | strsave(s) | |
33 | char *s; | |
34 | { | |
0fed7e9b | 35 | register int len; |
36 | register char *p; | |
37 | /* Kludge for sloppy string semantics */ | |
38 | if (!s) { | |
39 | printf("NULL != \"\" !!!!\r\n"); | |
40 | p = malloc(1); | |
070955b9 | 41 | *p = '\0'; |
0fed7e9b | 42 | return p; |
43 | } | |
44 | len = strlen(s) + 1; | |
45 | p = malloc((u_int)len); | |
68687902 | 46 | if (p) bcopy(s, p, len); |
47 | return p; | |
48 | } | |
49 | /* | |
50 | * Trim whitespace off both ends of a string. | |
51 | */ | |
9ca8ff13 | 52 | char *strtrim(save) |
53 | register char *save; | |
68687902 | 54 | { |
9ca8ff13 | 55 | register char *t, *s; |
56 | ||
57 | s = save; | |
68687902 | 58 | while (isspace(*s)) s++; |
59 | /* skip to end of string */ | |
9ca8ff13 | 60 | if (*s == '\0') { |
61 | *save = '\0'; | |
62 | return(save); | |
63 | } | |
68687902 | 64 | |
65 | for (t = s; *t; t++) continue; | |
66 | while (t > s) { | |
67 | --t; | |
68 | if (!isspace(*t)) { | |
69 | t++; | |
70 | break; | |
71 | } | |
72 | } | |
73 | *t = '\0'; | |
74 | return s; | |
75 | } | |
77f073b6 | 76 | |
77 | ||
78 | /* Modify a string for all of the letters to be uppercase. */ | |
79 | ||
80 | char *uppercase(s) | |
81 | char *s; | |
82 | { | |
83 | register char *p; | |
84 | ||
85 | for (p = s; *p; p++) | |
86 | if (islower(*p)) | |
87 | *p = toupper(*p); | |
88 | return(s); | |
89 | } | |
bc5b5d78 | 90 | |
91 | ||
92 | char *lowercase(s) | |
93 | char *s; | |
94 | { | |
95 | register char *p; | |
96 | ||
97 | for (p = s; *p; p++) | |
98 | if (isupper(*p)) | |
99 | *p = tolower(*p); | |
100 | return(s); | |
101 | } | |
501a4b9d | 102 | |
103 | ||
104 | #ifdef NEED_STRCASECMP | |
105 | ||
106 | /* Case independant string comparison. Only compile this if your C | |
107 | * library doesn't have a local routine which is faster. | |
108 | */ | |
109 | ||
110 | static char map[] = { | |
111 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, | |
112 | 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, | |
113 | ' ', '!', '"', '#', '$', '%', '&', '\'', | |
114 | '(', ')', '*', '+', ',', '-', '.', '/', | |
115 | '0', '1', '2', '3', '4', '5', '6', '7', | |
116 | '8', '9', ':', ';', '<', '=', '>', '?', | |
117 | '@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', | |
118 | 'h', 'i', 'k', 'j', 'l', 'm', 'n', 'o', | |
119 | 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', | |
120 | 'x', 'y', 'z', '[', '\\', ']', '^', '_', | |
121 | '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', | |
122 | 'h', 'i', 'k', 'j', 'l', 'm', 'n', 'o', | |
123 | 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', | |
124 | 'x', 'y', 'z', '{', '|', '}', '~', 127, | |
125 | 128, 129, 130, 131, 132, 133, 134, 135, | |
126 | 136, 137, 138, 139, 140, 141, 142, 143, | |
127 | 144, 145, 146, 147, 148, 149, 150, 151, | |
128 | 152, 153, 154, 155, 156, 157, 158, 159, | |
129 | 160, 161, 162, 163, 164, 165, 166, 167, | |
130 | 168, 169, 170, 171, 172, 173, 174, 175, | |
131 | 176, 177, 178, 179, 180, 181, 182, 183, | |
132 | 184, 185, 186, 187, 188, 189, 190, 191, | |
133 | 192, 193, 194, 195, 196, 197, 198, 199, | |
134 | 200, 201, 202, 203, 204, 205, 206, 207, | |
135 | 208, 209, 210, 211, 212, 213, 214, 215, | |
136 | 216, 217, 218, 219, 220, 221, 222, 223, | |
137 | 224, 225, 226, 227, 228, 229, 230, 231, | |
138 | 232, 233, 234, 235, 236, 237, 238, 239, | |
139 | 240, 241, 242, 243, 244, 245, 246, 247, | |
140 | 248, 249, 250, 251, 252, 253, 254, 255 | |
141 | }; | |
142 | ||
143 | ||
144 | int strcasecmp(s1, s2) | |
145 | char *s1; | |
146 | char *s2; | |
147 | { | |
148 | while (map[*s1] == map[*s2]) { | |
149 | if (*s1 == 0) | |
150 | return(0); | |
151 | s1++; | |
152 | s2++; | |
153 | } | |
154 | return(map[*s1] - map[*s2]); | |
155 | } | |
156 | ||
157 | #endif /* NEED_STRCASECMP */ |