From d17f9381dde3c887d5b3bfd58cd0eb77c40fd133 Mon Sep 17 00:00:00 2001 From: christos Date: Sun, 7 Apr 2013 22:54:26 +0000 Subject: [PATCH] Add a small example. --- lib/libc/net/getifaddrs.3 | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/libc/net/getifaddrs.3 b/lib/libc/net/getifaddrs.3 index f698186c4d0b..8bf89fedc255 100644 --- a/lib/libc/net/getifaddrs.3 +++ b/lib/libc/net/getifaddrs.3 @@ -1,4 +1,4 @@ -.\" $NetBSD: getifaddrs.3,v 1.12 2010/03/22 19:30:54 joerg Exp $ +.\" $NetBSD: getifaddrs.3,v 1.13 2013/04/07 22:54:26 christos Exp $ .\" BSDI getifaddrs.3,v 2.5 2000/02/23 14:51:59 dab Exp .\" .\" Copyright (c) 1995, 1999 @@ -21,7 +21,7 @@ .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. -.Dd April 21, 2009 +.Dd April 7, 2013 .Dt GETIFADDRS 3 .Os .Sh NAME @@ -133,6 +133,36 @@ Upon successful completion, a value of 0 is returned. Otherwise, a value of -1 is returned and .Va errno is set to indicate the error. +.Sh EXAMPLES +The following example program prints a list of all addresses configured +on the system. +.Bd -literal -offset indent +#include \*[Lt]sys/types.h\*[Gt] +#include \*[Lt]sys/socket.h\*[Gt] +#include \*[Lt]stdio.h\*[Gt] +#include \*[Lt]ifaddrs.h\*[Gt] +#include \*[Lt]util.h\*[Gt] +#include \*[Lt]err.h\*[Gt] +#include \*[Lt]stdlib.h\*[Gt] + +int +main(int argc, char *argv[]) +{ + struct ifaddrs *ifa, *a; + + if (getifaddrs(\*[Am]ifa) == -1) + err(EXIT_FAILURE, "getifaddrs"); + + for (a = ifa; a; a = a->ifa_next) { + char buf[1024]; + sockaddr_snprintf(buf, sizeof(buf), "%f %a", + a->ifa_addr); + printf("%s %x %s\\n", a->ifa_name, a->ifa_flags, buf); + } + freeifaddrs(ifa); + return EXIT_SUCCESS; +} +.Ed .Sh ERRORS The .Fn getifaddrs