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

1 by Stephan Hermann
Import upstream version 203
1
/* $XTermId: xstrings.c,v 1.22 2005/01/14 01:50:03 tom Exp $ */
2
3
/* $XFree86: xc/programs/xterm/xstrings.c,v 1.9 2005/01/14 01:50:03 dickey Exp $ */
4
5
/************************************************************
6
7
Copyright 2000-2004,2005 by Thomas E. Dickey
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
58
int
59
x_strcasecmp(const char *s1, const char *s2)
60
{
61
    unsigned len = strlen(s1);
62
63
    if (len != strlen(s2))
64
	return 1;
65
66
    while (len-- != 0) {
67
	int c1 = toupper(CharOf(*s1));
68
	int c2 = toupper(CharOf(*s2));
69
	if (c1 != c2)
70
	    return 1;
71
	s1++, s2++;
72
    }
73
74
    return 0;
75
}
76
77
/*
78
 * Allocates a copy of a string
79
 */
80
char *
81
x_strdup(const char *s)
82
{
83
    char *result = 0;
84
85
    if (s != 0) {
86
	char *t = CastMallocN(char, strlen(s));
87
	if (t != 0) {
88
	    strcpy(t, s);
89
	}
90
	result = t;
91
    }
92
    return result;
93
}
94
95
/*
96
 * Returns a pointer to the first occurrence of s2 in s1,
97
 * or NULL if there are none.
98
 */
99
char *
100
x_strindex(char *s1, char *s2)
101
{
102
    char *s3;
103
    size_t s2len = strlen(s2);
104
105
    while ((s3 = strchr(s1, *s2)) != NULL) {
106
	if (strncmp(s3, s2, s2len) == 0)
107
	    return (s3);
108
	s1 = ++s3;
109
    }
110
    return (NULL);
111
}
112
113
/*
114
 * Trims leading/trailing spaces from the string, returns a copy of it if it
115
 * is modified.
116
 */
117
char *
118
x_strtrim(char *s)
119
{
120
    char *base = s;
121
    char *d;
122
123
    if (s != 0 && *s != '\0') {
124
	char *t = x_strdup(base);
125
	s = t;
126
	d = s;
127
	while (isspace(CharOf(*s))) {
128
	    ++s;
129
	}
130
	while ((*d++ = *s++) != '\0') {
131
	    ;
132
	}
133
	if (*t != '\0') {
134
	    s = t + strlen(t);
135
	    while (s != t && isspace(CharOf(s[-1]))) {
136
		*--s = '\0';
137
	    }
138
	}
139
	if (!strcmp(t, base)) {
140
	    free(t);
141
	} else {
142
	    base = t;
143
	}
144
    }
145
    return base;
146
}