Fixup fromcvs/togit conversion
[minix-pkgsrc.git] / news / knews / patches / patch-ai
blobe9c0a0450d1c419c9eb590deb9676151a93d3043
1 $NetBSD$
3 --- src/server.c.orig   Fri Jan  9 18:16:22 1998
4 +++ src/server.c        Mon Mar 12 14:11:00 2001
5 @@ -2,19 +2,21 @@
6   *  Copyright (C) 1995, 1996  Karl-Johan Johnsson.
7   */
8  
9 +#include <sys/types.h>
10 +#include <sys/socket.h>
12  #include "global.h"
13  #include "child.h"
14  #include "codes.h"
15  #include "connect.h"
16  #include "file.h"
17  #include "resource.h"
18 +#include "sysdeps.h"
19  #include "server.h"
20  #include "util.h"
21  #include "widgets.h"
22  #include "xutil.h"
24 -#include "sysdeps.h"
26  struct SERVER {
27      int                fd;
28      char       *buffer;
29 @@ -128,37 +130,70 @@
30   *  tell != 0   means tell about errors
31   *  tell >  1   means say what you're doing
32   */
33 -int server_open(SERVER *server, struct SERV_ADDR *addr, int tell)
34 +int server_open(SERVER *server, SERV_ADDR *addr, int tell)
35  {
36 -    long       tmp;
37 +    int        tmp;
39      server_close(server);
40 -    server->fd = open_socket();
41 -    if (server->fd < 0) {
42 -       set_message("Error: Failed to create socket!", True);
43 -       return -1;
44 -    }
45 +    while (addr) {
46 +       server->fd = open_socket(addr);
47 +       if (server->fd < 0) {
48 +           set_message("Error: Failed to create socket!", True);
49 +           return -1;
50 +       }
52 -    if (tell > 1)
53 -       set_message("Server contacted, waiting for response...", False);
54 +       if (tell > 1)
55 +           set_message("Server contacted, waiting for response...", False);
57 -    tmp = connect_socket(server->fd, addr);
58 +       if (connect_socket(server->fd, addr) == 0)
59 +           break;
61 -    if (tmp < 0 && would_block(server->fd, errno)) {
62 -       if (tell == 0)
63 -           return 0; /* don't block */
65 -       tmp = 0;
66 -       do_wait(&server->fd, True, server->quit_func, server);
67 -       if (server->fd < 0)
68 -           return -1;
69 +       if (would_block(server->fd, errno)) {
70 +           if (tell == 0)
71 +               return 0; /* don't block */
73 +           do_wait(&server->fd, True, server->quit_func, server);
74 +           if (server->fd < 0)
75 +               return -1;
76 +           else {
77 +#ifdef __NetBSD__
78 +               socklen_t       length;
79 +               int             error;
80 +#else
81 +               int     length, error;
82 +#endif
83 +               length = sizeof(error);
84 +               if (getsockopt(server->fd, SOL_SOCKET, SO_ERROR,
85 +                              (char *)&error, &length) == 0) {
86 +                   if (error == 0)
87 +                       break;
88 +                   else
89 +                       errno = error;
90 +               }
91 +           }
92 +       }
94 +       server_close(server);
95 +       addr = next_addr(addr);
96 +    }
98 +    if (server->fd < 0) {
99 +       if (tell) {
100 +           char        message[128];
101 +           char        *tmp = error_string(errno);
103 +           if (!tmp)
104 +               tmp = "Connection failed";
105 +           sprintf(message, "Error: %s!", tmp);
106 +           set_message(message, True);
107 +       }
108 +       return -1;
109      }
111      server->buffer[0] = '\0';
112 -    if (tmp >= 0)
113 -       do {
114 -           tmp = read(server->fd, server->buffer, server->len);
115 -       } while (tmp < 0 && errno == EINTR);
116 +    do {
117 +       tmp = read(server->fd, server->buffer, server->len);
118 +    } while (tmp < 0 && errno == EINTR);
120      if (tmp >= 0) {
121         server->buffer[tmp] = '\0';