File: strdup.c

package info (click to toggle)
magic 7.5.220-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 17,860 kB
file content (166 lines) | stat: -rw-r--r-- 4,052 bytes parent folder | download | duplicates (2)
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
157
158
159
160
161
162
163
164
165
166
/*
 * strdup.c --
 *
 * Return a malloc'd copy of a string.
 *
 *     ********************************************************************* 
 *     * Copyright (C) 1985, 1990 Regents of the University of California. * 
 *     * Permission to use, copy, modify, and distribute this              * 
 *     * software and its documentation for any purpose and without        * 
 *     * fee is hereby granted, provided that the above copyright          * 
 *     * notice appear in all copies.  The University of California        * 
 *     * makes no representations about the suitability of this            * 
 *     * software for any purpose.  It is provided "as is" without         * 
 *     * express or implied warranty.  Export of this software outside     * 
 *     * of the United States of America may require an export license.    * 
 *     *********************************************************************
 */

#ifndef lint
static char rcsid[] __attribute__ ((unused)) = "$Header: /usr/cvsroot/magic-7.5/utils/strdup.c,v 1.1.1.1 2006/04/10 22:03:14 tim Exp $";
#endif  /* not lint */

#include <stdio.h>
#include <stdlib.h>		/* for strtod */
#include <string.h>
#include <ctype.h>

#include "utils/magic.h"
#include "utils/malloc.h"


/*
 * ----------------------------------------------------------------------------
 * StrDup --
 *
 * Return a malloc'd copy of a string.
 *
 * Results:
 *	Returns a pointer to a newly malloc'd character array just
 *	large enough to hold the supplied string and its trailing
 *	null byte, which contains a copy of the supplied string.
 *
 * Side effects:
 *	malloc's a character array large enough to hold str, and
 *	copies str into it, unless str is NULL.  If str is NULL, no
 *	malloc is done.  Also, if oldstr is non-NULL, then a) if
 *	*oldstr is not NULL, frees the storage allocated to oldstr,
 *	and b) sets *oldstr to the new string allocated, or to
 *	NULL if str is NULL.
 * ----------------------------------------------------------------------------
 */

char *
StrDup(oldstr, str)
    char **oldstr;
    char *str;
{
    char *newstr;

    if (str != NULL)
    {
	newstr = (char *) mallocMagic((unsigned) (strlen(str) + 1));
	(void) strcpy(newstr, str);
    }
    else newstr = NULL;
    if (oldstr != (char **) NULL)
    {
	if (*oldstr != NULL)
	    freeMagic(*oldstr);
	*oldstr = newstr;
    }

    return (newstr);
}


/*
 * ----------------------------------------------------------------------------
 * StrIsWhite:
 *
 *	Check to see if a string is all white space or is a comment.
 *
 * Results:
 *	True if it is all white, false otherwise.
 *
 * Side effects:
 *	none.
 * ----------------------------------------------------------------------------
 */

bool
StrIsWhite(line, commentok)
    char *line;
    bool commentok;	/* TRUE means # comments are considered all-white */
{
    if ( (*line == '#') && commentok)
	return TRUE;
    while(*line)
    {
	if ( !isspace(*line) && (*line != '\n') )
	    return FALSE;
	line++;
    }
    return TRUE;
}


/*
 * ----------------------------------------------------------------------------
 *
 * StrIsInt --
 *
 * 	Check a string for being an integer.
 *
 * Results:
 *	TRUE if the string is a well-formed integer, FALSE otherwise.
 *
 * Side effects:
 *	None.
 *
 * ----------------------------------------------------------------------------
 */

bool
StrIsInt(s)
    char *s;
{
    if (*s == '-' || *s == '+') s++;
    while (*s)
	if (!isdigit(*s++))
	    return (FALSE);

    return (TRUE);
}

/*
 * ----------------------------------------------------------------------------
 *
 * StrIsNumeric --
 *
 *	Check a string for being any numeric value.
 *
 * Results:
 *	TRUE if the string can be interpreted as a numeric value,
 *	FALSE otherwise.
 *
 * Side effects:
 *	None.
 *
 * ----------------------------------------------------------------------------
 */

bool
StrIsNumeric(s)
    char *s;
{
    double result;
    char *endptr;
    
    result = strtod(s, &endptr);

    if (endptr == s) return FALSE;

    return TRUE;
}