File: mystring.c

package info (click to toggle)
afterstep 2.2.12-18.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,184 kB
  • sloc: ansic: 201,695; sh: 5,894; xml: 3,721; makefile: 2,094; perl: 1,558; cpp: 811
file content (156 lines) | stat: -rw-r--r-- 3,162 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/****************************************************************************
 *
 * Copyright (c) 2000 Sasha Vasko <sasha at aftercode.net>
 * some previous unknown authors that has never left their copyrights :)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 ****************************************************************************/

#include "config.h"

#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#include "astypes.h"
#include "output.h"
#include "safemalloc.h"
#include "mystring.h"
#include "audit.h"

int
mystrcasecmp (const char *s1, const char *s2)
{
	int          c1, c2;
	register int i = 0 ;

	if (s1 == NULL || s2 == NULL)
		return (s1 == s2) ? 0 : ((s1==NULL)?1:-1);
	while (s1[i])
	{
		/* in some BSD implementations, tolower(c) is not defined
		 * unless isupper(c) is true */
		c1 = s1[i];
		if (isupper (c1))
			c1 = tolower (c1);
		c2 = s2[i];
		if (isupper (c2))
			c2 = tolower (c2);

		++i ;
		if (c1 != c2)
			return (c1 - c2);
	}
	return -s2[i];
}

int
mystrncasecmp (const char *s1, const char *s2, size_t n)
{
	register int  c1, c2;
	register int i = 0 ;

	if (s1 == NULL || s2 == NULL)
		return (s1 == s2) ? 0 : ((s1==NULL)?1:-1);
	while( i < n )
	{
		c1 = s1[i], c2 = s2[i];
		++i ;
		if (c1==0)
			return -c2;
		if (isupper (c1))
			c1 = tolower(c1);
		if (isupper (c2))
			c2 = tolower(c2);
		if (c1 != c2)
			return (c1 - c2);
	}
	return 0;
}

/* safe version of STRCMP - checks for NULL strings : */
int
mystrcmp (const char *s1, const char *s2)
{
	register int i = 0 ;

	if (s1 == NULL || s2 == NULL)
		return (s1 == s2) ? 0 : ((s1==NULL)?1:-1);
	while (s1[i])
	{
		register int d = s1[i]-s2[i];
		if( d != 0 )
			return d;
		++i ;
	}
	return -s2[i];
}


#undef mystrdup
#undef mystrndup
#undef safemalloc
void* safemalloc(size_t);
char         *
mystrdup (const char *str)
{
	char         *c = NULL;

	if (str)
	{
		c = safemalloc (strlen (str) + 1);
		strcpy (c, str);
	}
	return c;
}

char         *
mystrndup (const char *str, size_t n)
{
	char         *c = NULL;

	if (str)
	{
		c = safemalloc (n + 1);
		if( n > 0 )
			strncpy (c, str, n);
		c[n] = '\0';
	}
	return c;
}

/* very usefull utility function to update the value of any string property */
void
set_string (char **target, char *string)
{
    if (target != NULL)
	{
		if (*target != string)
		{
			if (*target)
				free (*target);
			*target = string;
		}
	}
}

void destroy_string(char **ps)
{ 
	if(ps && *(ps))
	{
		free(*(ps)); 
		*(ps)=NULL;
	}
}