[BACK]Return to inet_neta.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libc / inet

Annotation of src/lib/libc/inet/inet_neta.c, Revision 1.3

1.3     ! abs         1: /*     $NetBSD: inet_neta.c,v 1.2 2012/03/13 21:13:38 christos Exp $   */
1.1       christos    2:
                      3: /*
                      4:  * Copyright (c) 1996 by Internet Software Consortium.
                      5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
                     11:  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
                     12:  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
                     13:  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
                     14:  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
                     15:  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
                     16:  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
                     17:  * SOFTWARE.
                     18:  */
                     19:
                     20: #include <sys/cdefs.h>
                     21: #if defined(LIBC_SCCS) && !defined(lint)
                     22: #if 0
                     23: static const char rcsid[] = "Id: inet_neta.c,v 8.2 1996/08/08 06:54:44 vixie Exp ";
                     24: #else
1.3     ! abs        25: __RCSID("$NetBSD: inet_neta.c,v 1.2 2012/03/13 21:13:38 christos Exp $");
1.1       christos   26: #endif
                     27: #endif
                     28:
                     29: #include "namespace.h"
                     30: #include <sys/types.h>
                     31: #include <sys/socket.h>
                     32: #include <netinet/in.h>
                     33: #include <arpa/inet.h>
                     34:
                     35: #include <assert.h>
                     36: #include <errno.h>
                     37: #include <stdio.h>
                     38: #include <string.h>
                     39:
                     40: #ifdef __weak_alias
                     41: __weak_alias(inet_neta,_inet_neta)
                     42: #endif
                     43:
                     44: /*
                     45:  * char *
                     46:  * inet_neta(src, dst, size)
                     47:  *     format a u_long network number into presentation format.
                     48:  * return:
                     49:  *     pointer to dst, or NULL if an error occurred (check errno).
                     50:  * note:
                     51:  *     format of ``src'' is as for inet_network().
                     52:  * author:
                     53:  *     Paul Vixie (ISC), July 1996
                     54:  */
                     55: char *
1.3     ! abs        56: inet_neta(u_long src, char *dst, size_t size)
1.1       christos   57: {
                     58:        char *odst = dst;
                     59:        char *ep;
                     60:        int advance;
                     61:
                     62:        _DIAGASSERT(dst != NULL);
                     63:
                     64:        if (src == 0x00000000) {
                     65:                if (size < sizeof "0.0.0.0")
                     66:                        goto emsgsize;
                     67:                strlcpy(dst, "0.0.0.0", size);
                     68:                return dst;
                     69:        }
                     70:        ep = dst + size;
                     71:        if (ep <= dst)
                     72:                goto emsgsize;
1.2       christos   73:        while (src & 0xffffffffUL) {
                     74:                u_char b = (u_char)((src & 0xff000000UL) >> 24);
1.1       christos   75:
                     76:                src <<= 8;
                     77:                if (b || src) {
                     78:                        advance = snprintf(dst, (size_t)(ep - dst), "%u", b);
                     79:                        if (advance <= 0 || advance >= ep - dst)
                     80:                                goto emsgsize;
                     81:                        dst += advance;
                     82:                        if (src != 0L) {
                     83:                                if (dst + 1 >= ep)
                     84:                                        goto emsgsize;
                     85:                                *dst++ = '.';
                     86:                                *dst = '\0';
                     87:                        }
                     88:                }
                     89:        }
                     90:        return (odst);
                     91:
                     92:  emsgsize:
                     93:        errno = EMSGSIZE;
                     94:        return (NULL);
                     95: }

CVSweb <webmaster@jp.NetBSD.org>