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