File: string.c

package info (click to toggle)
c2man 2.41-11
  • links: PTS
  • area: main
  • in suites: potato
  • size: 800 kB
  • ctags: 863
  • sloc: ansic: 6,559; sh: 5,236; yacc: 839; lex: 621; makefile: 229; perl: 81
file content (82 lines) | stat: -rw-r--r-- 1,428 bytes parent folder | download | duplicates (6)
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
/* $Id: string.c,v 2.0.1.2 1994/01/07 07:05:10 greyham Exp $
 *
 * Some string handling routines
 */
#include "c2man.h"
#include <ctype.h>

/* Copy the string into an allocated memory block.
 * Return a pointer to the copy.
 */
char *
strduplicate (s)
const char *s;	/* The string to copy. May be NULL */
{
    char *dest;

    if (!s)	return NULL;
    
    if ((dest = malloc((unsigned)(strlen(s)+1))) == NULL)
	outmem();

    strcpy(dest, s);
    return dest;
}

#ifndef HAS_STRSTR

/* Return a pointer to the first occurence of the substring 
 * within the string, or NULL if not found.
 */
char *
strstr (src, key)
const char *src, *key;
{
    char *s;
    int keylen;

    keylen = strlen(key);
    s = strchr(src, *key);
    while (s != NULL) {
	if (strncmp(s, key, keylen) == 0)
	    return s;
	s = strchr(s+1, *key);
    }
    return NULL;
}

#endif

/* compare two strings case insensitively, for up to n chars */
int strncmpi(s1, s2, n)
const char *s1, *s2;
size_t n;
{
    while(n--)
    {
	char c1 = *s1, c2 = *s2;

	if (c1 == '\0' && c2 == '\0')	break;

	if (isalpha(c1) && isupper(c1))	c1 = tolower(c1);
	if (isalpha(c2) && isupper(c2))	c2 = tolower(c2);

	if (c1 < c2)	return -1;
	if (c1 > c2)	return 1;
	s1++; s2++;
    }
    return 0;
}

/* convert string to upper case */
char *strtoupper(in)
char *in;
{
    char *s;

    for (s = in; *s; s++)
    {
	if (islower(*s))	*s = toupper(*s);
    }
    return in;
}