ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/hopm/branches/1.0.x/src/compat.c
Revision: 5064
Committed: Mon Dec 22 14:21:48 2014 UTC (9 years, 3 months ago) by michael
Content type: text/x-csrc
Original Path: hopm/trunk/src/compat.c
File size: 4007 byte(s)
Log Message:
- Add strlcpy() and strlcat() for system that don't have these

File Contents

# User Rev Content
1 michael 5052 /*
2     Copyright (C) 2002 Andy Smith
3    
4     This program is free software; you can redistribute it and/or
5     modify it under the terms of the GNU General Public License
6     as published by the Free Software Foundation; either version 2
7     of the License, or (at your option) any later version.
8    
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     GNU General Public License for more details.
13    
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the
16    
17     Free Software Foundation, Inc.
18     59 Temple Place - Suite 330
19     Boston, MA 02111-1307, USA
20     */
21    
22     #include "setup.h"
23    
24     #include <stdio.h>
25    
26     #ifdef STDC_HEADERS
27     # include <string.h>
28     #endif
29    
30     #ifndef HAVE_INET_ATON
31     # include <netinet/in.h>
32     #endif
33    
34     #include "compat.h"
35    
36     #ifndef HAVE_INET_ATON
37     int bopm_inet_aton(const char *cp, struct in_addr *inp)
38     {
39     unsigned int a1, a2, a3, a4;
40     unsigned long ret;
41    
42     if (strcmp(cp, "255.255.255.255") == 0)
43     {
44     inp->s_addr = (unsigned) -1;
45     return 0;
46     }
47    
48     if (sscanf(cp, "%u.%u.%u.%u", &a1, &a2, &a3, &a4) != 4 ||
49     a1 > 255 || a2 > 255 || a3 > 255 || a4 > 255)
50     {
51     return 0;
52     }
53    
54     ret = (a1 << 24) | (a2 << 16) | (a3 << 8) | a4;
55    
56     inp->s_addr = htonl(ret);
57    
58     if (inp->s_addr == (unsigned) -1)
59     {
60     return 0;
61     }
62     return 1;
63     }
64     #endif
65 michael 5064
66     /*
67     * strlcat and strlcpy were ripped from openssh 2.5.1p2
68     * They had the following Copyright info:
69     *
70     *
71     * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
72     * All rights reserved.
73     *
74     * Redistribution and use in source and binary forms, with or without
75     * modification, are permitted provided that the following conditions
76     * are met:
77     * 1. Redistributions of source code must retain the above copyright
78     * notice, this list of conditions and the following disclaimer.
79     * 2. Redistributions in binary form must reproduce the above copyright
80     * notice, this list of conditions and the following disclaimer in the
81     * documentation and/or other materials provided with the distribution.
82     * 3. The name of the author may not be used to endorse or promote products
83     * derived from this software without specific prior written permission.
84     *
85     * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
86     * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
87     * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
88     * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
89     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
91     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
92     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
93     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
94     * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
95     */
96    
97     #ifndef HAVE_STRLCAT
98     size_t
99     strlcat(char *dst, const char *src, size_t siz)
100     {
101     char *d = dst;
102     const char *s = src;
103     size_t n = siz, dlen;
104    
105     while (n-- != 0 && *d != '\0')
106     d++;
107    
108     dlen = d - dst;
109     n = siz - dlen;
110    
111     if (n == 0)
112     return dlen + strlen(s);
113    
114     while (*s != '\0')
115     {
116     if (n != 1)
117     {
118     *d++ = *s;
119     n--;
120     }
121    
122     s++;
123     }
124    
125     *d = '\0';
126     return dlen + (s - src); /* count does not include NUL */
127     }
128     #endif
129    
130     #ifndef HAVE_STRLCPY
131     size_t
132     strlcpy(char *dst, const char *src, size_t siz)
133     {
134     char *d = dst;
135     const char *s = src;
136     size_t n = siz;
137    
138     /* Copy as many bytes as will fit */
139     if (n != 0 && --n != 0)
140     {
141     do
142     {
143     if ((*d++ = *s++) == 0)
144     break;
145     } while (--n != 0);
146     }
147    
148     /* Not enough room in dst, add NUL and traverse rest of src */
149     if (n == 0)
150     {
151     if (siz != 0)
152     *d = '\0'; /* NUL-terminate dst */
153     while (*s++)
154     ;
155     }
156    
157     return s - src - 1; /* count does not include NUL */
158     }
159     #endif