Back to index

glibc  2.9
inet_neta.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1996,1999 by Internet Software Consortium.
00003  *
00004  * Permission to use, copy, modify, and distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
00009  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
00010  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
00011  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00012  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00013  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00014  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00015  * SOFTWARE.
00016  */
00017 
00018 #if defined(LIBC_SCCS) && !defined(lint)
00019 static const char rcsid[] = "$BINDId: inet_neta.c,v 1.6 1999/01/08 19:23:45 vixie Exp $";
00020 #endif
00021 
00022 #include <sys/types.h>
00023 #include <sys/socket.h>
00024 #include <netinet/in.h>
00025 #include <arpa/inet.h>
00026 
00027 #include <errno.h>
00028 #include <stdio.h>
00029 #include <string.h>
00030 
00031 #ifdef SPRINTF_CHAR
00032 # define SPRINTF(x) strlen(sprintfx)
00033 #else
00034 # define SPRINTF(x) ((size_t)sprintf x)
00035 #endif
00036 
00037 /*
00038  * char *
00039  * inet_neta(src, dst, size)
00040  *     format a u_long network number into presentation format.
00041  * return:
00042  *     pointer to dst, or NULL if an error occurred (check errno).
00043  * note:
00044  *     format of ``src'' is as for inet_network().
00045  * author:
00046  *     Paul Vixie (ISC), July 1996
00047  */
00048 char *
00049 inet_neta(src, dst, size)
00050        u_int32_t src;
00051        char *dst;
00052        size_t size;
00053 {
00054        char *odst = dst;
00055        char *tp;
00056 
00057        while (src & 0xffffffff) {
00058               u_char b = (src & 0xff000000) >> 24;
00059 
00060               src <<= 8;
00061               if (b) {
00062                      if (size < sizeof "255.")
00063                             goto emsgsize;
00064                      tp = dst;
00065                      dst += SPRINTF((dst, "%u", b));
00066                      if (src != 0L) {
00067                             *dst++ = '.';
00068                             *dst = '\0';
00069                      }
00070                      size -= (size_t)(dst - tp);
00071               }
00072        }
00073        if (dst == odst) {
00074               if (size < sizeof "0.0.0.0")
00075                      goto emsgsize;
00076               strcpy(dst, "0.0.0.0");
00077        }
00078        return (odst);
00079 
00080  emsgsize:
00081        __set_errno (EMSGSIZE);
00082        return (NULL);
00083 }