~ubuntu-branches/ubuntu/intrepid/clamsmtp/intrepid

« back to all changes in this revision

Viewing changes to common/compat.c

  • Committer: Bazaar Package Importer
  • Author(s): Chad Walstrom
  • Date: 2004-12-13 12:28:16 UTC
  • Revision ID: james.westby@ubuntu.com-20041213122816-4joiogeyuzgoysl3
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2004, Nate Nielsen
 
3
 * All rights reserved.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without 
 
6
 * modification, are permitted provided that the following conditions 
 
7
 * are met:
 
8
 * 
 
9
 *     * Redistributions of source code must retain the above 
 
10
 *       copyright notice, this list of conditions and the 
 
11
 *       following disclaimer.
 
12
 *     * Redistributions in binary form must reproduce the 
 
13
 *       above copyright notice, this list of conditions and 
 
14
 *       the following disclaimer in the documentation and/or 
 
15
 *       other materials provided with the distribution.
 
16
 *     * The names of contributors to this software may not be 
 
17
 *       used to endorse or promote products derived from this 
 
18
 *       software without specific prior written permission.
 
19
 * 
 
20
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 
21
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 
22
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 
23
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 
24
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 
25
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 
26
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 
 
27
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 
28
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 
29
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
 
30
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
 
31
 * DAMAGE.
 
32
 * 
 
33
 *
 
34
 * CONTRIBUTORS
 
35
 *  Nate Nielsen <nielsen@memberwebs.com>
 
36
 *
 
37
 * 
 
38
 * PORTIONS FROM OPENBSD: -------------------------------------------------
 
39
 *
 
40
 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
 
41
 * All rights reserved.
 
42
 *
 
43
 * Redistribution and use in source and binary forms, with or without
 
44
 * modification, are permitted provided that the following conditions
 
45
 * are met:
 
46
 * 1. Redistributions of source code must retain the above copyright
 
47
 *    notice, this list of conditions and the following disclaimer.
 
48
 * 2. Redistributions in binary form must reproduce the above copyright
 
49
 *    notice, this list of conditions and the following disclaimer in the
 
50
 *    documentation and/or other materials provided with the distribution.
 
51
 * 3. The name of the author may not be used to endorse or promote products
 
52
 *    derived from this software without specific prior written permission.
 
53
 *
 
54
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 
55
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 
56
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 
57
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
58
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
59
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 
60
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 
61
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 
62
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 
63
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
64
 */ 
 
65
 
 
66
 
 
67
 
 
68
#include "usuals.h"
 
69
#include "compat.h"
 
70
 
 
71
#include <ctype.h>
 
72
#include <stdlib.h>
 
73
 
 
74
#ifndef HAVE_REALLOCF
 
75
 
 
76
void* reallocf(void* ptr, size_t size)
 
77
{
 
78
        void* ret = realloc(ptr, size);
 
79
 
 
80
        if(!ret && size)
 
81
                free(ptr);
 
82
 
 
83
        return ret;
 
84
}
 
85
 
 
86
#endif
 
87
 
 
88
#ifndef HAVE_STRLWR
 
89
char* strlwr(char* s)
 
90
{
 
91
    char* t = s;
 
92
    while(*t)
 
93
    {
 
94
        *t = tolower(*t);
 
95
        t++;
 
96
    }
 
97
    return s;
 
98
}
 
99
#endif 
 
100
 
 
101
#ifndef HAVE_STRUPR
 
102
char* strupr(char* s)
 
103
{
 
104
    char* t = s;
 
105
    while(*t)
 
106
    {
 
107
        *t = toupper(*t);
 
108
        t++;
 
109
    }
 
110
    return s;
 
111
}
 
112
#endif 
 
113
 
 
114
#ifndef HAVE_STRLCPY
 
115
 
 
116
size_t strlcpy(char* dst, const char* src, size_t siz)
 
117
{
 
118
        char* d = dst;
 
119
    const char* s = src;
 
120
    size_t n = siz;
 
121
 
 
122
    /* Copy as many bytes as will fit */
 
123
    if(n != 0 && --n != 0) 
 
124
        {
 
125
        do 
 
126
                {
 
127
                if((*d++ = *s++) == 0)
 
128
                break;
 
129
        } 
 
130
                while(--n != 0);
 
131
    }
 
132
                                                                                
 
133
    /* Not enough room in dst, add NUL and traverse rest of src */
 
134
    if(n == 0) 
 
135
        {
 
136
        if(siz != 0)
 
137
                *d = '\0';              /* NUL-terminate dst */
 
138
        while (*s++)
 
139
                ;
 
140
    }
 
141
                                                                                
 
142
    return s - src - 1;    /* count does not include NUL */
 
143
}
 
144
 
 
145
#endif
 
146
                                                                                
 
147
#ifndef HAVE_STRLCAT
 
148
                                                                                
 
149
size_t strlcat(char* dst, const char* src, size_t siz)
 
150
{
 
151
    char* d = dst;
 
152
    const char* s = src;
 
153
    size_t n = siz;
 
154
    size_t dlen;
 
155
                                                                                
 
156
    /* Find the end of dst and adjust bytes left but don't go past end */
 
157
    while(n-- != 0 && *d != '\0')
 
158
        d++;
 
159
    dlen = d - dst;
 
160
    n = siz - dlen;
 
161
                                                                                
 
162
    if(n == 0)
 
163
        return dlen + strlen(s);
 
164
    while(*s != '\0') 
 
165
    {
 
166
        if(n != 1) 
 
167
        {
 
168
            *d++ = *s;
 
169
            n--; 
 
170
        }
 
171
        
 
172
        s++;
 
173
    }
 
174
 
 
175
    *d = '\0';
 
176
                                                                                
 
177
    return dlen + (s - src);       /* count does not include NUL */
 
178
}
 
179
 
 
180
#endif
 
181
 
 
182