File: support.c

package info (click to toggle)
libident 0.32-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,464 kB
  • sloc: sh: 8,260; ansic: 840; makefile: 27
file content (80 lines) | stat: -rw-r--r-- 1,207 bytes parent folder | download | duplicates (3)
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
/*
** support.c
**
** Author: Pr Emanuelsson <pell@lysator.liu.se>
** Hacked by: Peter Eriksson <pen@lysator.liu.se>
** Also by: Philip Hazel <ph10@cus.cam.ac.uk>
*/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#define IN_LIBIDENT_SRC
#include "ident.h"


char *id_strdup (char *str)
{
    char *cp;

    cp = (char *) malloc(strlen(str)+1);
    if (cp == NULL)
    {
#ifdef DEBUG
	perror("libident: malloc");
#endif
        return NULL;
    }

    strcpy(cp, str);

    return cp;
}


char *id_strtok (char *cp, char *cs, char *dc)
{
    static char *bp = 0;
    
    if (cp)
	bp = cp;
    
    /*
    ** No delimitor cs - return whole buffer and point at end
    */
    if (!cs)
    {
	while (*bp)
	    bp++;
	return cp;  /* Fix by PH - was "return cs" */
    }
    
    /*
    ** Skip leading spaces
    */
    while (isspace((unsigned char)*bp))
	bp++;
    
    /*
    ** No token found?
    */
    if (!*bp)
	return 0;
    
    cp = bp;
    while (*bp && !strchr(cs, *bp))
	bp++;
    
    /*
    ** Remove trailing spaces
    */
    *dc = *bp;
    for (dc = bp-1; dc > cp && isspace((unsigned char)*dc); dc--)
	;
    *++dc = '\0';
    
    bp++;
    
    return cp;
}