[go: up one dir, main page]

File: string.c

package info (click to toggle)
cook 2.29-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,656 kB
  • ctags: 4,083
  • sloc: ansic: 47,636; sh: 14,376; makefile: 4,656; yacc: 3,166; perl: 224; awk: 219
file content (249 lines) | stat: -rw-r--r-- 6,073 bytes parent folder | download
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 *      cook - file construction tool
 *      Copyright (C) 1991-1994, 1997, 1998, 2006, 2007 Peter Miller;
 *      All rights reserved.
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program 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 General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 * MANIFEST: functions to implement missing ANSI C <string.h> functions
 *
 * This file contains functions for use with non-ANSI conforming systems
 * to implement absent ANSI functionality.
 */

#include <common/ac/stddef.h>
#include <common/ac/stdlib.h>
#include <common/ac/string.h>
#include <common/ac/stdio.h>

#include <common/main.h>


/*
 *  NAME
 *      strerror - string for error number
 *
 *  SYNOPSIS
 *      char *strerror(int errnum);
 *
 *  DESCRIPTION
 *      The strerror function maps the error number in errnum to an error
 *      message string.
 *
 *  RETURNS
 *      The strerror function returns a pointer to the string, the contents of
 *      which are implementation-defined.  The array pointed to shall not be
 *      modified by the program, but may be overwritten by a subsequent call to
 *      the strerror function.
 *
 *  CAVEAT
 *      Unknown errors will be rendered in the form "Error %d", where %d will
 *      be replaced by a decimal representation of the error number.
 */

#ifndef HAVE_STRERROR

char *
strerror(n)
        int             n;
{
        extern int      sys_nerr;
        extern char     *sys_errlist[];
        static char     buffer[16];

        if (n < 1 || n > sys_nerr)
        {
                sprintf(buffer, "Error %d", n);
                return buffer;
        }
        return sys_errlist[n];
}

#endif /* !HAVE_STRERROR */


#ifndef HAVE_STRCASECMP

int
strcasecmp(s1, s2)
        const char      *s1;
        const char      *s2;
{
        int             c1;
        int             c2;

        for (;;)
        {
                c1 = *s1++;
                if (islower(c1))
                        c1 = toupper(c1);
                c2 = *s2++;
                if (islower(c2))
                        c2 = toupper(c2);
                if (c1 != c2)
                {
                        /*
                         * if s1 is a leading substring of s2, must
                         * return -1, even if the next character of s2
                         * is negative.
                         */
                        if (!c1)
                                return -1;
                        if (c1 < c2)
                                return -1;
                        return 1;
                }
                if (!c1)
                        return 0;
        }
}

#endif /* !HAVE_STRCASECMP */


#ifndef HAVE_STRSIGNAL

char *
strsignal(n)
        int     n;
{
        static char buffer[16];

        switch (n)
        {
#ifdef SIGHUP
        case SIGHUP:
                return "hang up [SIGHUP]";
#endif /* SIGHUP */

#ifdef SIGINT
        case SIGINT:
                return "user interrupt [SIGINT]";
#endif /* SIGINT */

#ifdef SIGQUIT
        case SIGQUIT:
                return "user quit [SIGQUIT]";
#endif /* SIGQUIT */

#ifdef SIGILL
        case SIGILL:
                return "illegal instruction [SIGILL]";
#endif /* SIGILL */

#ifdef SIGTRAP
        case SIGTRAP:
                return "trace trap [SIGTRAP]";
#endif /* SIGTRAP */

#ifdef SIGIOT
        case SIGIOT:
                return "abort [SIGIOT]";
#endif /* SIGIOT */

#ifdef SIGEMT
        case SIGEMT:
                return "EMT instruction [SIGEMT]";
#endif /* SIGEMT */

#ifdef SIGFPE
        case SIGFPE:
                return "floating point exception [SIGFPE]";
#endif /* SIGFPE */

#ifdef SIGKILL
        case SIGKILL:
                return "kill [SIGKILL]";
#endif /* SIGKILL */

#ifdef SIGBUS
        case SIGBUS:
                return "bus error [SIGBUS]";
#endif /* SIGBUS */

#ifdef SIGSEGV
        case SIGSEGV:
                return "segmentation violation [SIGSEGV]";
#endif /* SIGSEGV */

#ifdef SIGSYS
        case SIGSYS:
                return "bad argument to system call [SIGSYS]";
#endif /* SIGSYS */

#ifdef SIGPIPE
        case SIGPIPE:
                return "write on a pipe with no one to read it [SIGPIPE]";
#endif /* SIGPIPE */

#ifdef SIGALRM
        case SIGALRM:
                return "alarm clock [SIGALRM]";
#endif /* SIGALRM */

#ifdef SIGTERM
        case SIGTERM:
                return "software termination [SIGTERM]";
#endif /* SIGTERM */

#ifdef SIGUSR1
        case SIGUSR1:
                return "user defined signal one [SIGUSR1]";
#endif /* SIGUSR1 */

#ifdef SIGUSR2
        case SIGUSR2:
                return "user defined signal two [SIGUSR2]";
#endif /* SIGUSR2 */

#ifdef SIGCLD
        case SIGCLD:
                return "death of child [SIGCLD]";
#endif /* SIGCLD */

#ifdef SIGPWR
        case SIGPWR:
                return "power failure [SIGPWR]";
#endif /* SIGPWR */

        default:
                sprintf(buffer, "signal %d", n);
                return buffer;
        }
}

#endif /* !HAVE_STRSIGNAL */
#ifndef HAVE_STRENDCPY

char *
strendcpy(char *dst, const char *src, const char *end)
{
    if (dst < end)
    {
        /* leave room for terminating NUL */
        end--;
        while (dst < end && *src)
            *dst++ = *src++;
        *dst = '\0';
        if (*src)
        {
            /* return end parameter if truncated */
            dst++;
        }
    }
    return dst;
}

#endif /* !HAVE_STRENDCPY */