testsuite: Skip analyzer tests on AIX.
[official-gcc.git] / gcc / testsuite / c-c++-common / analyzer / fd-manpage-getaddrinfo-client.c
blob16da9333074a3de2077d049484721b51b353f871
1 /* Example from getaddrinfo.3 manpage, which has this license:
3 Copyright (c) 2007, 2008 Michael Kerrisk <mtk.manpages@gmail.com>
4 and Copyright (c) 2006 Ulrich Drepper <drepper@redhat.com>
5 A few pieces of an earlier version remain:
6 Copyright 2000, Sam Varshavchik <mrsam@courier-mta.com>
8 Permission is granted to make and distribute verbatim copies of this
9 manual provided the copyright notice and this permission notice are
10 preserved on all copies.
12 Permission is granted to copy and distribute modified versions of this
13 manual under the conditions for verbatim copying, provided that the
14 entire resulting derived work is distributed under the terms of a
15 permission notice identical to this one.
17 Since the Linux kernel and libraries are constantly changing, this
18 manual page may be incorrect or out-of-date. The author(s) assume no
19 responsibility for errors or omissions, or for damages resulting from
20 the use of the information contained herein. The author(s) may not
21 have taken the same level of care in the production of this manual,
22 which is licensed free of charge, as they might when working
23 professionally.
25 Formatted or processed versions of this manual, if unaccompanied by
26 the source, must acknowledge the copyright and authors of this work.
29 /* { dg-require-effective-target sockets } */
30 /* { dg-additional-options "-Wno-analyzer-too-complex" } */
31 /* { dg-skip-if "" { powerpc*-*-aix* } } */
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <netdb.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <string.h>
41 #define BUF_SIZE 500
43 int
44 main(int argc, char *argv[])
46 struct addrinfo hints;
47 struct addrinfo *result, *rp;
48 int sfd, s;
49 size_t len;
50 ssize_t nread;
51 char buf[BUF_SIZE];
53 if (argc < 3) {
54 fprintf(stderr, "Usage: %s host port msg...\n", argv[0]);
55 exit(EXIT_FAILURE);
58 /* Obtain address(es) matching host/port. */
60 memset(&hints, 0, sizeof(hints));
61 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
62 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
63 hints.ai_flags = 0;
64 hints.ai_protocol = 0; /* Any protocol */
66 s = getaddrinfo(argv[1], argv[2], &hints, &result);
67 if (s != 0) {
68 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
69 exit(EXIT_FAILURE);
72 /* getaddrinfo() returns a list of address structures.
73 Try each address until we successfully connect(2).
74 If socket(2) (or connect(2)) fails, we (close the socket
75 and) try the next address. */
77 for (rp = result; rp != NULL; rp = rp->ai_next) {
78 sfd = socket(rp->ai_family, rp->ai_socktype,
79 rp->ai_protocol);
80 if (sfd == -1)
81 continue;
83 if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
84 break; /* Success */
86 close(sfd);
89 freeaddrinfo(result); /* No longer needed */
91 if (rp == NULL) { /* No address succeeded */
92 fprintf(stderr, "Could not connect\n");
93 exit(EXIT_FAILURE);
96 /* Send remaining command-line arguments as separate
97 datagrams, and read responses from server. */
99 for (int j = 3; j < argc; j++) {
100 len = strlen(argv[j]) + 1;
101 /* +1 for terminating null byte */
103 if (len > BUF_SIZE) {
104 fprintf(stderr,
105 "Ignoring long message in argument %d\n", j);
106 continue;
109 if (write(sfd, argv[j], len) != len) {
110 fprintf(stderr, "partial/failed write\n");
111 exit(EXIT_FAILURE);
114 nread = read(sfd, buf, BUF_SIZE);
115 if (nread == -1) {
116 perror("read");
117 exit(EXIT_FAILURE);
120 printf("Received %zd bytes: %s\n", nread, buf);
123 exit(EXIT_SUCCESS);