~ubuntu-branches/ubuntu/intrepid/xterm/intrepid-security

1.1.6 by Julien Cristau
Import upstream version 226
1
/* $XTermId: xstrings.c,v 1.26 2007/06/09 13:43:00 tom Exp $ */
1 by Stephan Hermann
Import upstream version 203
2
1.1.2 by Scott James Remnant
Import upstream version 210
3
/* $XFree86: xc/programs/xterm/xstrings.c,v 1.10 2006/02/13 01:14:59 dickey Exp $ */
1 by Stephan Hermann
Import upstream version 203
4
5
/************************************************************
6
1.1.6 by Julien Cristau
Import upstream version 226
7
Copyright 2000-2006,2007 by Thomas E. Dickey
1 by Stephan Hermann
Import upstream version 203
8
9
                        All Rights Reserved
10
11
Permission is hereby granted, free of charge, to any person obtaining a
12
copy of this software and associated documentation files (the
13
"Software"), to deal in the Software without restriction, including
14
without limitation the rights to use, copy, modify, merge, publish,
15
distribute, sublicense, and/or sell copies of the Software, and to
16
permit persons to whom the Software is furnished to do so, subject to
17
the following conditions:
18
19
The above copyright notice and this permission notice shall be included
20
in all copies or substantial portions of the Software.
21
22
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25
IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
26
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30
Except as contained in this notice, the name(s) of the above copyright
31
holders shall not be used in advertising or otherwise to promote the
32
sale, use or other dealings in this Software without prior written
33
authorization.
34
35
********************************************************/
36
37
#include <xterm.h>
38
39
#include <sys/types.h>
40
#include <string.h>
41
#include <ctype.h>
42
43
#include <xstrings.h>
44
45
char *
46
x_basename(char *name)
47
{
48
    char *cp;
49
50
    cp = strrchr(name, '/');
51
#ifdef __UNIXOS2__
52
    if (cp == 0)
53
	cp = strrchr(name, '\\');
54
#endif
55
    return (cp ? cp + 1 : name);
56
}
57
1.1.2 by Scott James Remnant
Import upstream version 210
58
char *
1.1.6 by Julien Cristau
Import upstream version 226
59
x_getenv(const char *name)
60
{
61
    return x_nonempty(getenv(name));
62
}
63
64
/*
65
 * Check if the given string is nonnull/nonempty.  If so, return a pointer
66
 * to the beginning of its content, otherwise return null.
67
 */
68
char *
69
x_nonempty(char *s)
70
{
71
    if (s != 0) {
72
	if (*s == '\0') {
73
	    s = 0;
74
	} else {
75
	    s = x_skip_blanks(s);
76
	    if (*s == '\0')
77
		s = 0;
78
	}
79
    }
80
    return s;
81
}
82
83
char *
1.1.2 by Scott James Remnant
Import upstream version 210
84
x_skip_blanks(char *s)
85
{
86
    while (isspace(CharOf(*s)))
87
	++s;
88
    return s;
89
}
90
91
char *
92
x_skip_nonblanks(char *s)
93
{
94
    while (*s != '\0' && !isspace(CharOf(*s)))
95
	++s;
96
    return s;
97
}
98
1 by Stephan Hermann
Import upstream version 203
99
int
100
x_strcasecmp(const char *s1, const char *s2)
101
{
102
    unsigned len = strlen(s1);
103
104
    if (len != strlen(s2))
105
	return 1;
106
1.1.2 by Scott James Remnant
Import upstream version 210
107
    return x_strncasecmp(s1, s2, len);
108
}
109
110
int
111
x_strncasecmp(const char *s1, const char *s2, unsigned n)
112
{
113
    while (n-- != 0) {
1 by Stephan Hermann
Import upstream version 203
114
	int c1 = toupper(CharOf(*s1));
115
	int c2 = toupper(CharOf(*s2));
116
	if (c1 != c2)
117
	    return 1;
1.1.2 by Scott James Remnant
Import upstream version 210
118
	if (c1 == 0)
119
	    break;
1 by Stephan Hermann
Import upstream version 203
120
	s1++, s2++;
121
    }
122
123
    return 0;
124
}
125
126
/*
127
 * Allocates a copy of a string
128
 */
129
char *
130
x_strdup(const char *s)
131
{
132
    char *result = 0;
133
134
    if (s != 0) {
135
	char *t = CastMallocN(char, strlen(s));
136
	if (t != 0) {
137
	    strcpy(t, s);
138
	}
139
	result = t;
140
    }
141
    return result;
142
}
143
144
/*
145
 * Returns a pointer to the first occurrence of s2 in s1,
146
 * or NULL if there are none.
147
 */
148
char *
149
x_strindex(char *s1, char *s2)
150
{
151
    char *s3;
152
    size_t s2len = strlen(s2);
153
154
    while ((s3 = strchr(s1, *s2)) != NULL) {
155
	if (strncmp(s3, s2, s2len) == 0)
156
	    return (s3);
157
	s1 = ++s3;
158
    }
159
    return (NULL);
160
}
161
162
/*
1.1.2 by Scott James Remnant
Import upstream version 210
163
 * Trims leading/trailing spaces from a copy of the string.
1 by Stephan Hermann
Import upstream version 203
164
 */
165
char *
166
x_strtrim(char *s)
167
{
168
    char *base = s;
169
    char *d;
170
171
    if (s != 0 && *s != '\0') {
172
	char *t = x_strdup(base);
173
	s = t;
174
	d = s;
1.1.2 by Scott James Remnant
Import upstream version 210
175
	s = x_skip_blanks(s);
1 by Stephan Hermann
Import upstream version 203
176
	while ((*d++ = *s++) != '\0') {
177
	    ;
178
	}
179
	if (*t != '\0') {
180
	    s = t + strlen(t);
181
	    while (s != t && isspace(CharOf(s[-1]))) {
182
		*--s = '\0';
183
	    }
184
	}
1.1.2 by Scott James Remnant
Import upstream version 210
185
	base = t;
1 by Stephan Hermann
Import upstream version 203
186
    }
187
    return base;
188
}