annotate lib-src/gnuslib.c @ 371:cc15677e0335 r21-2b1

Import from CVS: tag r21-2b1
author cvs
date Mon, 13 Aug 2007 11:03:08 +0200
parents 972bbb6d6ca2
children 74fd4e045ea6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 /* -*-C-*-
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 Common library code for the GNU Emacs server and client.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 This file is part of GNU Emacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 Copying is permitted under those conditions described by the GNU
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 General Public License.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 Copyright (C) 1989 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 Author: Andy Norman (ange@hplb.hpl.hp.com), based on
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 'etc/server.c' and 'etc/emacsclient.c' from the 18.52 GNU
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 Emacs distribution.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 Please mail bugs and suggestions to the author at the above address.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 /* HISTORY
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 * 11-Nov-1990 bristor@simba
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 * Added EOT stuff.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 * This file incorporates new features added by Bob Weiner <weiner@mot.com>,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 * Darrell Kindred <dkindred@cmu.edu> and Arup Mukherjee <arup@cmu.edu>.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 * Please see the note at the end of the README file for details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 * (If gnuserv came bundled with your emacs, the README file is probably
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 * ../etc/gnuserv.README relative to the directory containing this file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 #if 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 static char rcsid [] = "!Header: gnuslib.c,v 2.4 95/02/16 11:57:37 arup alpha !";
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35
207
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 175
diff changeset
36 #include "gnuserv.h"
108
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
37 #include <errno.h>
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 #ifdef SYSV_IPC
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 static int connect_to_ipc_server (void);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 #ifdef UNIX_DOMAIN_SOCKETS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 static int connect_to_unix_server (void);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 #ifdef INTERNET_DOMAIN_SOCKETS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 static int connect_to_internet_server (char *serverhost, u_short port);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 /* On some systems, e.g. DGUX, inet_addr returns a 'struct in_addr'. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 #ifdef HAVE_BROKEN_INET_ADDR
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 # define IN_ADDR struct in_addr
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 # define NUMERIC_ADDR_ERROR (numeric_addr.s_addr == -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 # if (LONGBITS > 32)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 # define IN_ADDR unsigned int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 # else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 # define IN_ADDR unsigned long
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 # endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 # define NUMERIC_ADDR_ERROR (numeric_addr == (IN_ADDR) -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 #include <stdlib.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 #include <sys/types.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 #include <sys/stat.h>
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
66 #ifdef HAVE_UNISTD_H
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 #include <unistd.h>
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
68 #endif /* HAVE_UNISTD_H */
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
69 #ifdef HAVE_STRING_H
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 #include <string.h>
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
71 #endif /* HAVE_STRING_H */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 #include <arpa/inet.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74
259
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents: 207
diff changeset
75 char *tmpdir = NULL;
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents: 207
diff changeset
76
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 char *progname = NULL;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 363
diff changeset
79 int make_connection(hostarg, portarg, s)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 363
diff changeset
80 char *hostarg;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 363
diff changeset
81 int portarg;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 363
diff changeset
82 int *s;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 #ifdef INTERNET_DOMAIN_SOCKETS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 char *ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 if (hostarg == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 hostarg = getenv("GNU_HOST");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 if (portarg == 0 && (ptr=getenv("GNU_PORT")) != NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 portarg = atoi(ptr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 if (hostarg != NULL) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 /* hostname was given explicitly, via cmd line arg or GNU_HOST,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 * so obey it. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 #ifdef UNIX_DOMAIN_SOCKETS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 if (!strcmp(hostarg, "unix")) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 *s = connect_to_unix_server();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 return (int) CONN_UNIX;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 #endif /* UNIX_DOMAIN_SOCKETS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 #ifdef INTERNET_DOMAIN_SOCKETS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 *s = connect_to_internet_server(hostarg, portarg);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 return (int) CONN_INTERNET;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 #ifdef SYSV_IPC
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 return -1; /* hostarg should always be NULL for SYSV_IPC */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 } else {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 /* no hostname given. Use unix-domain/sysv-ipc, or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 * internet-domain connection to local host if they're not available. */
155
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 149
diff changeset
111 #if defined(UNIX_DOMAIN_SOCKETS)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 *s = connect_to_unix_server();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 return (int) CONN_UNIX;
155
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 149
diff changeset
114 #elif defined(SYSV_IPC)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 *s = connect_to_ipc_server();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 return (int) CONN_IPC;
155
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 149
diff changeset
117 #elif defined(INTERNET_DOMAIN_SOCKETS)
175
2d532a89d707 Import from CVS: tag r20-3b14
cvs
parents: 171
diff changeset
118 {
2d532a89d707 Import from CVS: tag r20-3b14
cvs
parents: 171
diff changeset
119 char localhost[HOSTNAMSZ];
2d532a89d707 Import from CVS: tag r20-3b14
cvs
parents: 171
diff changeset
120 gethostname(localhost,HOSTNAMSZ); /* use this host by default */
2d532a89d707 Import from CVS: tag r20-3b14
cvs
parents: 171
diff changeset
121 *s = connect_to_internet_server(localhost, portarg);
2d532a89d707 Import from CVS: tag r20-3b14
cvs
parents: 171
diff changeset
122 return (int) CONN_INTERNET;
2d532a89d707 Import from CVS: tag r20-3b14
cvs
parents: 171
diff changeset
123 }
155
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 149
diff changeset
124 #endif /* IPC type */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 #ifdef SYSV_IPC
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 connect_to_ipc_server -- establish connection with server process via SYSV IPC
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 Returns msqid for server if successful.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 static int connect_to_ipc_server (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 int s; /* connected msqid */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 key_t key; /* message key */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 char buf[GSERV_BUFSZ+1]; /* buffer for filename */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138
259
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents: 207
diff changeset
139 sprintf(buf,"%s/gsrv%d",tmpdir,(int)geteuid());
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 creat(buf,0600);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 if ((key = ftok(buf,1)) == -1) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 perror(progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 fprintf(stderr, "%s: unable to get ipc key from %s\n",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 progname, buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 exit(1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 if ((s = msgget(key,0600)) == -1) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 perror(progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 fprintf(stderr,"%s: unable to access msg queue\n",progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 exit(1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 }; /* if */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 return(s);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 } /* connect_to_ipc_server */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 disconnect_from_ipc_server -- inform the server that sending has finished,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 and wait for its reply.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 void disconnect_from_ipc_server(s,msgp,echo)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 int s;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 struct msgbuf *msgp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 int echo;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 int len; /* length of received message */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 send_string(s,EOT_STR); /* EOT terminates this message */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 msgp->mtype = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 if(msgsnd(s,msgp,strlen(msgp->mtext)+1,0) < 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 perror(progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 fprintf(stderr,"%s: unable to send message to server\n",progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 exit(1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 }; /* if */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 if((len = msgrcv(s,msgp,GSERV_BUFSZ,getpid(),0)) < 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 perror(progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 fprintf(stderr,"%s: unable to receive message from server\n",progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 exit(1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 }; /* if */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 if (echo) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 msgp->mtext[len] = '\0'; /* string terminate message */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 fputs(msgp->mtext, stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 if (msgp->mtext[len-1] != '\n') putchar ('\n');
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 }; /* if */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 } /* disconnect_from_ipc_server */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 #endif /* SYSV_IPC */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 #if defined(INTERNET_DOMAIN_SOCKETS) || defined(UNIX_DOMAIN_SOCKETS)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 send_string -- send string to socket.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 */
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 363
diff changeset
199 void send_string(s,msg)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 363
diff changeset
200 int s;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 363
diff changeset
201 CONST char *msg;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 #if 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 if (send(s,msg,strlen(msg),0) < 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 perror(progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 fprintf(stderr,"%s: unable to send\n",progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 exit(1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 }; /* if */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 int len, left=strlen(msg);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 while (left > 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 if ((len=write(s,msg,min2(left,GSERV_BUFSZ))) < 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 /* XEmacs addition: robertl@arnet.com */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 if (errno == EPIPE) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 return ;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 perror(progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 fprintf(stderr,"%s: unable to send\n",progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 exit(1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 }; /* if */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 left -= len;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 msg += len;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 }; /* while */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 } /* send_string */
108
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
226
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
227 /*
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
228 read_line -- read a \n terminated line from a socket
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
229 */
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 155
diff changeset
230 int read_line(int s, char *dest)
108
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
231 {
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
232 int length;
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
233 int offset=0;
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
234 char buffer[GSERV_BUFSZ+1];
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
235
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
236 while ((length=read(s,buffer+offset,1)>0) && buffer[offset]!='\n'
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
237 && buffer[offset] != EOT_CHR) {
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
238 offset += length;
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
239 if (offset >= GSERV_BUFSZ)
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
240 break;
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
241 }
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
242 buffer[offset] = '\0';
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
243 strcpy(dest,buffer);
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
244 return 1;
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
245 } /* read_line */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 #endif /* INTERNET_DOMAIN_SOCKETS || UNIX_DOMAIN_SOCKETS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 #ifdef UNIX_DOMAIN_SOCKETS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 connect_to_unix_server -- establish connection with server process via a unix-
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 domain socket. Returns socket descriptor for server
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 if successful.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 static int connect_to_unix_server (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 int s; /* connected socket descriptor */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 struct sockaddr_un server; /* for unix connections */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 if ((s = socket(AF_UNIX,SOCK_STREAM,0)) < 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 perror(progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 fprintf(stderr,"%s: unable to create socket\n",progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 exit(1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 }; /* if */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 server.sun_family = AF_UNIX;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 #ifdef HIDE_UNIX_SOCKET
259
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents: 207
diff changeset
268 sprintf(server.sun_path,"%s/gsrvdir%d/gsrv",tmpdir,(int)geteuid());
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 #else /* HIDE_UNIX_SOCKET */
259
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents: 207
diff changeset
270 sprintf(server.sun_path,"%s/gsrv%d",tmpdir,(int)geteuid());
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 #endif /* HIDE_UNIX_SOCKET */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 if (connect(s,(struct sockaddr *)&server,strlen(server.sun_path)+2) < 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 perror(progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 fprintf(stderr,"%s: unable to connect to local\n",progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 exit(1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 }; /* if */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 return(s);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 } /* connect_to_unix_server */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 #endif /* UNIX_DOMAIN_SOCKETS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 #ifdef INTERNET_DOMAIN_SOCKETS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 internet_addr -- return the internet addr of the hostname or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 internet address passed. Return -1 on error.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 */
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 363
diff changeset
289 int internet_addr(host)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 363
diff changeset
290 char *host;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 struct hostent *hp; /* pointer to host info for remote host */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 IN_ADDR numeric_addr; /* host address */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 numeric_addr = inet_addr(host);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 if (!NUMERIC_ADDR_ERROR)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 return numeric_addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 else if ((hp = gethostbyname(host)) != NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 return ((struct in_addr *)(hp->h_addr))->s_addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 return -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 } /* internet_addr */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 #ifdef AUTH_MAGIC_COOKIE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 # include <X11/X.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 # include <X11/Xauth.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 static Xauth *server_xauth = NULL;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 connect_to_internet_server -- establish connection with server process via
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 an internet domain socket. Returns socket
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 descriptor for server if successful.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 static int connect_to_internet_server (char *serverhost, u_short port)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 int s; /* connected socket descriptor */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 struct servent *sp; /* pointer to service information */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 struct sockaddr_in peeraddr_in; /* for peer socket address */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 char buf[512]; /* temporary buffer */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 /* clear out address structures */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 memset((char *)&peeraddr_in,0,sizeof(struct sockaddr_in));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 /* Set up the peer address to which we will connect. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 peeraddr_in.sin_family = AF_INET;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 /* look up the server host's internet address */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 if ((peeraddr_in.sin_addr.s_addr = internet_addr(serverhost)) == -1) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 fprintf(stderr,"%s: unable to find %s in /etc/hosts or from YP\n",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 progname,serverhost);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 exit(1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 }; /* if */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 if (port == 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 if ((sp = getservbyname ("gnuserv","tcp")) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 peeraddr_in.sin_port = htons(DEFAULT_PORT+getuid());
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 peeraddr_in.sin_port = sp->s_port;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 } /* if */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 peeraddr_in.sin_port = htons(port);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 /* Create the socket. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 if ((s = socket (AF_INET,SOCK_STREAM, 0))== -1) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 perror(progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 fprintf(stderr,"%s: unable to create socket\n",progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 exit(1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 }; /* if */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 /* Try to connect to the remote server at the address
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 * which was just built into peeraddr.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 if (connect(s, (struct sockaddr *)&peeraddr_in,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 sizeof(struct sockaddr_in)) == -1) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 perror(progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 fprintf(stderr, "%s: unable to connect to remote\n",progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 exit(1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 }; /* if */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 #ifdef AUTH_MAGIC_COOKIE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 /* send credentials using MIT-MAGIC-COOKIE-1 protocol */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 server_xauth =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 XauGetAuthByAddr(FamilyInternet,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 sizeof(peeraddr_in.sin_addr.s_addr),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 (char *) &peeraddr_in.sin_addr.s_addr,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 strlen(MCOOKIE_SCREEN), MCOOKIE_SCREEN,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 strlen(MCOOKIE_X_NAME), MCOOKIE_X_NAME);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 if (server_xauth && server_xauth->data) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 sprintf(buf, "%s\n%d\n", MCOOKIE_NAME, server_xauth->data_length);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 write (s, buf, strlen(buf));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 write (s, server_xauth->data, server_xauth->data_length);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 return (s);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 #endif /* AUTH_MAGIC_COOKIE */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 sprintf (buf, "%s\n", DEFAUTH_NAME);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 write (s, buf, strlen(buf));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 return(s);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 } /* connect_to_internet_server */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 #endif /* INTERNET_DOMAIN_SOCKETS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 #if defined(INTERNET_DOMAIN_SOCKETS) || defined(UNIX_DOMAIN_SOCKETS)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 disconnect_from_server -- inform the server that sending has finished, and wait for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 its reply.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 */
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 363
diff changeset
398 void disconnect_from_server(s,echo)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 363
diff changeset
399 int s;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 363
diff changeset
400 int echo;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 #if 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 char buffer[REPLYSIZ+1];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 char buffer[GSERV_BUFSZ+1];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 int add_newline = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 int length;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 send_string(s,EOT_STR); /* make sure server gets string */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 #if !defined (linux) && !defined (_SCO_DS)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 * shutdown is completely hozed under linux. If s is a unix domain socket,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 * you'll get EOPNOTSUPP back from it. If s is an internet socket, you get
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 * a broken pipe when you try to read a bit later. The latter
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 * problem is fixed for linux versions >= 1.1.46, but the problem
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 * with unix sockets persists. Sigh.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 if (shutdown(s,1) == -1) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 perror(progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 fprintf(stderr, "%s: unable to shutdown socket\n",progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 exit(1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 }; /* if */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 #if 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 while((length = recv(s,buffer,REPLYSIZ,0)) > 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 buffer[length] = '\0';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431 if (echo) fputs(buffer,stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 add_newline = (buffer[length-1] != '\n');
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 }; /* while */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 #else
108
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
435 while ((length = read(s,buffer,GSERV_BUFSZ)) > 0 ||
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
436 (length == -1 && errno == EINTR)) {
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
437 if (length) {
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
438 buffer[length] = '\0';
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
439 if (echo) {
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
440 fputs(buffer,stdout);
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
441 add_newline = (buffer[length-1] != '\n');
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
442 }; /* if */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 }; /* if */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 }; /* while */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447 if (echo && add_newline) putchar('\n');
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 if(length < 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 perror(progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 fprintf(stderr,"%s: unable to read the reply from the server\n",progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 exit(1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 }; /* if */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 } /* disconnect_from_server */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 #endif /* INTERNET_DOMAIN_SOCKETS || UNIX_DOMAIN_SOCKETS */