xref: /openbsd-src/gnu/usr.bin/cvs/src/client.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /* JT thinks BeOS is worth the trouble. */
2 
3 /* CVS client-related stuff.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.  */
14 
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif /* HAVE_CONFIG_H */
18 
19 #include <assert.h>
20 #include "cvs.h"
21 #include "getline.h"
22 #include "edit.h"
23 #include "buffer.h"
24 
25 #ifdef CLIENT_SUPPORT
26 
27 #include "md5.h"
28 
29 #if defined(AUTH_CLIENT_SUPPORT) || HAVE_KERBEROS || defined(SOCK_ERRNO) || defined(SOCK_STRERROR)
30 #  ifdef HAVE_WINSOCK_H
31 #    include <winsock.h>
32 #  else /* No winsock.h */
33 #    include <sys/socket.h>
34 #    include <netinet/in.h>
35 #    include <netdb.h>
36 #  endif /* No winsock.h */
37 #endif
38 
39 /* If SOCK_ERRNO is defined, then send()/recv() and other socket calls
40    do not set errno, but that this macro should be used to obtain an
41    error code.  This probably doesn't make sense unless
42    NO_SOCKET_TO_FD is also defined. */
43 #ifndef SOCK_ERRNO
44 #define SOCK_ERRNO errno
45 #endif
46 
47 /* If SOCK_STRERROR is defined, then the error codes returned by
48    socket operations are not known to strerror, and this macro must be
49    used instead to convert those error codes to strings. */
50 #ifndef SOCK_STRERROR
51 #  define SOCK_STRERROR strerror
52 
53 #  if STDC_HEADERS
54 #    include <string.h>
55 #  endif
56 
57 #  ifndef strerror
58 extern char *strerror ();
59 #  endif
60 #endif /* ! SOCK_STRERROR */
61 
62 #if HAVE_KERBEROS
63 #define CVS_PORT 1999
64 
65 #include <krb.h>
66 
67 extern char *krb_realmofhost ();
68 #ifndef HAVE_KRB_GET_ERR_TEXT
69 #define krb_get_err_text(status) krb_err_txt[status]
70 #endif /* HAVE_KRB_GET_ERR_TEXT */
71 
72 /* Information we need if we are going to use Kerberos encryption.  */
73 static C_Block kblock;
74 static Key_schedule sched;
75 
76 #endif /* HAVE_KERBEROS */
77 
78 #ifdef HAVE_GSSAPI
79 
80 #ifdef HAVE_GSSAPI_H
81 #include <gssapi.h>
82 #endif
83 #ifdef HAVE_GSSAPI_GSSAPI_H
84 #include <gssapi/gssapi.h>
85 #endif
86 #ifdef HAVE_GSSAPI_GSSAPI_GENERIC_H
87 #include <gssapi/gssapi_generic.h>
88 #endif
89 
90 #ifndef HAVE_GSS_C_NT_HOSTBASED_SERVICE
91 #define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
92 #endif
93 
94 /* This is needed for GSSAPI encryption.  */
95 static gss_ctx_id_t gcontext;
96 
97 static int connect_to_gserver PROTO((int, struct hostent *));
98 
99 #endif /* HAVE_GSSAPI */
100 
101 static void add_prune_candidate PROTO((char *));
102 
103 /* All the commands.  */
104 int add PROTO((int argc, char **argv));
105 int admin PROTO((int argc, char **argv));
106 int checkout PROTO((int argc, char **argv));
107 int commit PROTO((int argc, char **argv));
108 int diff PROTO((int argc, char **argv));
109 int history PROTO((int argc, char **argv));
110 int import PROTO((int argc, char **argv));
111 int cvslog PROTO((int argc, char **argv));
112 int patch PROTO((int argc, char **argv));
113 int release PROTO((int argc, char **argv));
114 int cvsremove PROTO((int argc, char **argv));
115 int rtag PROTO((int argc, char **argv));
116 int status PROTO((int argc, char **argv));
117 int tag PROTO((int argc, char **argv));
118 int update PROTO((int argc, char **argv));
119 
120 /* All the response handling functions.  */
121 static void handle_ok PROTO((char *, int));
122 static void handle_error PROTO((char *, int));
123 static void handle_valid_requests PROTO((char *, int));
124 static void handle_checked_in PROTO((char *, int));
125 static void handle_new_entry PROTO((char *, int));
126 static void handle_checksum PROTO((char *, int));
127 static void handle_copy_file PROTO((char *, int));
128 static void handle_updated PROTO((char *, int));
129 static void handle_merged PROTO((char *, int));
130 static void handle_patched PROTO((char *, int));
131 static void handle_rcs_diff PROTO((char *, int));
132 static void handle_removed PROTO((char *, int));
133 static void handle_remove_entry PROTO((char *, int));
134 static void handle_set_static_directory PROTO((char *, int));
135 static void handle_clear_static_directory PROTO((char *, int));
136 static void handle_set_sticky PROTO((char *, int));
137 static void handle_clear_sticky PROTO((char *, int));
138 static void handle_set_checkin_prog PROTO((char *, int));
139 static void handle_set_update_prog PROTO((char *, int));
140 static void handle_module_expansion PROTO((char *, int));
141 static void handle_wrapper_rcs_option PROTO((char *, int));
142 static void handle_m PROTO((char *, int));
143 static void handle_e PROTO((char *, int));
144 static void handle_f PROTO((char *, int));
145 static void handle_notified PROTO((char *, int));
146 
147 static size_t try_read_from_server PROTO ((char *, size_t));
148 #endif /* CLIENT_SUPPORT */
149 
150 #ifdef CLIENT_SUPPORT
151 
152 /* We need to keep track of the list of directories we've sent to the
153    server.  This list, along with the current CVSROOT, will help us
154    decide which command-line arguments to send.  */
155 List *dirs_sent_to_server = NULL;
156 
157 static int is_arg_a_parent_or_listed_dir PROTO((Node *, void *));
158 
159 static int
160 is_arg_a_parent_or_listed_dir (n, d)
161     Node *n;
162     void *d;
163 {
164     char *directory = n->key;	/* name of the dir sent to server */
165     char *this_argv_elem = (char *) d;	/* this argv element */
166 
167     /* Say we should send this argument if the argument matches the
168        beginning of a directory name sent to the server.  This way,
169        the server will know to start at the top of that directory
170        hierarchy and descend. */
171 
172     if (strncmp (directory, this_argv_elem, strlen (this_argv_elem)) == 0)
173 	return 1;
174 
175     return 0;
176 }
177 
178 static int arg_should_not_be_sent_to_server PROTO((char *));
179 
180 /* Return nonzero if this argument should not be sent to the
181    server. */
182 
183 static int
184 arg_should_not_be_sent_to_server (arg)
185     char *arg;
186 {
187     /* Decide if we should send this directory name to the server.  We
188        should always send argv[i] if:
189 
190        1) the list of directories sent to the server is empty (as it
191        will be for checkout, etc.).
192 
193        2) the argument is "."
194 
195        3) the argument is a file in the cwd and the cwd is checked out
196        from the current root
197 
198        4) the argument lies within one of the paths in
199        dirs_sent_to_server.
200 
201        4) */
202 
203     if (list_isempty (dirs_sent_to_server))
204 	return 0;		/* always send it */
205 
206     if (strcmp (arg, ".") == 0)
207 	return 0;		/* always send it */
208 
209     /* We should send arg if it is one of the directories sent to the
210        server or the parent of one; this tells the server to descend
211        the hierarchy starting at this level. */
212     if (isdir (arg))
213     {
214 	if (walklist (dirs_sent_to_server, is_arg_a_parent_or_listed_dir, arg))
215 	    return 0;
216 
217 	/* If arg wasn't a parent, we don't know anything about it (we
218 	   would have seen something related to it during the
219 	   send_files phase).  Don't send it.  */
220 	return 1;
221     }
222 
223     /* Try to decide whether we should send arg to the server by
224        checking the contents of the corresponding CVSADM directory. */
225     {
226 	char *t, *this_root;
227 
228 	/* Calculate "dirname arg" */
229 	for (t = arg + strlen (arg) - 1; t >= arg; t--)
230 	{
231 	    if (ISDIRSEP(*t))
232 		break;
233 	}
234 
235 	/* Now we're either poiting to the beginning of the
236 	   string, or we found a path separator. */
237 	if (t >= arg)
238 	{
239 	    /* Found a path separator.  */
240 	    char c = *t;
241 	    *t = '\0';
242 
243 	    /* First, check to see if we sent this directory to the
244                server, because it takes less time than actually
245                opening the stuff in the CVSADM directory.  */
246 	    if (walklist (dirs_sent_to_server, is_arg_a_parent_or_listed_dir,
247 			  arg))
248 	    {
249 		*t = c;		/* make sure to un-truncate the arg */
250 		return 0;
251 	    }
252 
253 	    /* Since we didn't find it in the list, check the CVSADM
254                files on disk.  */
255 	    this_root = Name_Root (arg, (char *) NULL);
256 	    *t = c;
257 	}
258 	else
259 	{
260 	    /* We're at the beginning of the string.  Look at the
261                CVSADM files in cwd.  */
262 	    this_root = Name_Root ((char *) NULL, (char *) NULL);
263 	}
264 
265 	/*
266 	 * This is so bogus!  Means if you have checked out from
267 	 * a replica of a repository, and then when you want to
268 	 * check it in to the real (read/write) repository, the
269 	 * file will be skipped!
270 	 */
271 #if 0
272 	/* Now check the value for root. */
273 	if (this_root && current_root
274 	    && (strcmp (this_root, current_root) != 0))
275 	{
276 	    /* Don't send this, since the CVSROOTs don't match. */
277 	    free (this_root);
278 	    return 1;
279 	}
280 #endif
281 	free (this_root);
282     }
283 
284     /* OK, let's send it. */
285     return 0;
286 }
287 
288 
289 #endif /* CLIENT_SUPPORT */
290 
291 #if defined(CLIENT_SUPPORT) || defined(SERVER_SUPPORT)
292 
293 /* Shared with server.  */
294 
295 /*
296  * Return a malloc'd, '\0'-terminated string
297  * corresponding to the mode in SB.
298  */
299 char *
300 #ifdef __STDC__
301 mode_to_string (mode_t mode)
302 #else /* ! __STDC__ */
303 mode_to_string (mode)
304 	mode_t mode;
305 #endif /* __STDC__ */
306 {
307     char buf[18], u[4], g[4], o[4];
308     int i;
309 
310     i = 0;
311     if (mode & S_IRUSR) u[i++] = 'r';
312     if (mode & S_IWUSR) u[i++] = 'w';
313     if (mode & S_IXUSR) u[i++] = 'x';
314     u[i] = '\0';
315 
316     i = 0;
317     if (mode & S_IRGRP) g[i++] = 'r';
318     if (mode & S_IWGRP) g[i++] = 'w';
319     if (mode & S_IXGRP) g[i++] = 'x';
320     g[i] = '\0';
321 
322     i = 0;
323     if (mode & S_IROTH) o[i++] = 'r';
324     if (mode & S_IWOTH) o[i++] = 'w';
325     if (mode & S_IXOTH) o[i++] = 'x';
326     o[i] = '\0';
327 
328     sprintf(buf, "u=%s,g=%s,o=%s", u, g, o);
329     return xstrdup(buf);
330 }
331 
332 /*
333  * Change mode of FILENAME to MODE_STRING.
334  * Returns 0 for success or errno code.
335  * If RESPECT_UMASK is set, then honor the umask.
336  */
337 int
338 change_mode (filename, mode_string, respect_umask)
339     char *filename;
340     char *mode_string;
341     int respect_umask;
342 {
343 #ifdef CHMOD_BROKEN
344     char *p;
345     int writeable = 0;
346 
347     /* We can only distinguish between
348          1) readable
349          2) writeable
350          3) Picasso's "Blue Period"
351        We handle the first two. */
352     p = mode_string;
353     while (*p != '\0')
354     {
355 	if ((p[0] == 'u' || p[0] == 'g' || p[0] == 'o') && p[1] == '=')
356 	{
357 	    char *q = p + 2;
358 	    while (*q != ',' && *q != '\0')
359 	    {
360 		if (*q == 'w')
361 		    writeable = 1;
362 		++q;
363 	    }
364 	}
365 	/* Skip to the next field.  */
366 	while (*p != ',' && *p != '\0')
367 	    ++p;
368 	if (*p == ',')
369 	    ++p;
370     }
371 
372     /* xchmod honors the umask for us.  In the !respect_umask case, we
373        don't try to cope with it (probably to handle that well, the server
374        needs to deal with modes in data structures, rather than via the
375        modes in temporary files).  */
376     xchmod (filename, writeable);
377 	return 0;
378 
379 #else /* ! CHMOD_BROKEN */
380 
381     char *p;
382     mode_t mode = 0;
383     mode_t oumask;
384 
385     p = mode_string;
386     while (*p != '\0')
387     {
388 	if ((p[0] == 'u' || p[0] == 'g' || p[0] == 'o') && p[1] == '=')
389 	{
390 	    int can_read = 0, can_write = 0, can_execute = 0;
391 	    char *q = p + 2;
392 	    while (*q != ',' && *q != '\0')
393 	    {
394 		if (*q == 'r')
395 		    can_read = 1;
396 		else if (*q == 'w')
397 		    can_write = 1;
398 		else if (*q == 'x')
399 		    can_execute = 1;
400 		++q;
401 	    }
402 	    if (p[0] == 'u')
403 	    {
404 		if (can_read)
405 		    mode |= S_IRUSR;
406 		if (can_write)
407 		    mode |= S_IWUSR;
408 		if (can_execute)
409 		    mode |= S_IXUSR;
410 	    }
411 	    else if (p[0] == 'g')
412 	    {
413 		if (can_read)
414 		    mode |= S_IRGRP;
415 		if (can_write)
416 		    mode |= S_IWGRP;
417 		if (can_execute)
418 		    mode |= S_IXGRP;
419 	    }
420 	    else if (p[0] == 'o')
421 	    {
422 		if (can_read)
423 		    mode |= S_IROTH;
424 		if (can_write)
425 		    mode |= S_IWOTH;
426 		if (can_execute)
427 		    mode |= S_IXOTH;
428 	    }
429 	}
430 	/* Skip to the next field.  */
431 	while (*p != ',' && *p != '\0')
432 	    ++p;
433 	if (*p == ',')
434 	    ++p;
435     }
436 
437     if (respect_umask)
438     {
439 	oumask = umask (0);
440 	(void) umask (oumask);
441 	mode &= ~oumask;
442     }
443 
444     if (chmod (filename, mode) < 0)
445 	return errno;
446     return 0;
447 #endif /* ! CHMOD_BROKEN */
448 }
449 
450 #endif /* CLIENT_SUPPORT or SERVER_SUPPORT */
451 
452 #ifdef CLIENT_SUPPORT
453 
454 int client_prune_dirs;
455 
456 static List *ignlist = (List *) NULL;
457 
458 /* Buffer to write to the server.  */
459 static struct buffer *to_server;
460 /* The stream underlying to_server, if we are using a stream.  */
461 static FILE *to_server_fp;
462 
463 /* Buffer used to read from the server.  */
464 static struct buffer *from_server;
465 /* The stream underlying from_server, if we are using a stream.  */
466 static FILE *from_server_fp;
467 
468 /* Process ID of rsh subprocess.  */
469 static int rsh_pid = -1;
470 
471 
472 /* We want to be able to log data sent between us and the server.  We
473    do it using log buffers.  Each log buffer has another buffer which
474    handles the actual I/O, and a file to log information to.
475 
476    This structure is the closure field of a log buffer.  */
477 
478 struct log_buffer
479 {
480     /* The underlying buffer.  */
481     struct buffer *buf;
482     /* The file to log information to.  */
483     FILE *log;
484 };
485 
486 static struct buffer *log_buffer_initialize
487   PROTO((struct buffer *, FILE *, int, void (*) (struct buffer *)));
488 static int log_buffer_input PROTO((void *, char *, int, int, int *));
489 static int log_buffer_output PROTO((void *, const char *, int, int *));
490 static int log_buffer_flush PROTO((void *));
491 static int log_buffer_block PROTO((void *, int));
492 static int log_buffer_shutdown PROTO((void *));
493 
494 /* Create a log buffer.  */
495 
496 static struct buffer *
497 log_buffer_initialize (buf, fp, input, memory)
498      struct buffer *buf;
499      FILE *fp;
500      int input;
501      void (*memory) PROTO((struct buffer *));
502 {
503     struct log_buffer *n;
504 
505     n = (struct log_buffer *) xmalloc (sizeof *n);
506     n->buf = buf;
507     n->log = fp;
508     return buf_initialize (input ? log_buffer_input : NULL,
509 			   input ? NULL : log_buffer_output,
510 			   input ? NULL : log_buffer_flush,
511 			   log_buffer_block,
512 			   log_buffer_shutdown,
513 			   memory,
514 			   n);
515 }
516 
517 /* The input function for a log buffer.  */
518 
519 static int
520 log_buffer_input (closure, data, need, size, got)
521      void *closure;
522      char *data;
523      int need;
524      int size;
525      int *got;
526 {
527     struct log_buffer *lb = (struct log_buffer *) closure;
528     int status;
529     size_t n_to_write;
530 
531     if (lb->buf->input == NULL)
532 	abort ();
533 
534     status = (*lb->buf->input) (lb->buf->closure, data, need, size, got);
535     if (status != 0)
536 	return status;
537 
538     if (*got > 0)
539     {
540 	n_to_write = *got;
541 	if (fwrite (data, 1, n_to_write, lb->log) != n_to_write)
542 	    error (0, errno, "writing to log file");
543     }
544 
545     return 0;
546 }
547 
548 /* The output function for a log buffer.  */
549 
550 static int
551 log_buffer_output (closure, data, have, wrote)
552      void *closure;
553      const char *data;
554      int have;
555      int *wrote;
556 {
557     struct log_buffer *lb = (struct log_buffer *) closure;
558     int status;
559     size_t n_to_write;
560 
561     if (lb->buf->output == NULL)
562 	abort ();
563 
564     status = (*lb->buf->output) (lb->buf->closure, data, have, wrote);
565     if (status != 0)
566 	return status;
567 
568     if (*wrote > 0)
569     {
570 	n_to_write = *wrote;
571 	if (fwrite (data, 1, n_to_write, lb->log) != n_to_write)
572 	    error (0, errno, "writing to log file");
573     }
574 
575     return 0;
576 }
577 
578 /* The flush function for a log buffer.  */
579 
580 static int
581 log_buffer_flush (closure)
582      void *closure;
583 {
584     struct log_buffer *lb = (struct log_buffer *) closure;
585 
586     if (lb->buf->flush == NULL)
587 	abort ();
588 
589     /* We don't really have to flush the log file here, but doing it
590        will let tail -f on the log file show what is sent to the
591        network as it is sent.  */
592     if (fflush (lb->log) != 0)
593         error (0, errno, "flushing log file");
594 
595     return (*lb->buf->flush) (lb->buf->closure);
596 }
597 
598 /* The block function for a log buffer.  */
599 
600 static int
601 log_buffer_block (closure, block)
602      void *closure;
603      int block;
604 {
605     struct log_buffer *lb = (struct log_buffer *) closure;
606 
607     if (block)
608 	return set_block (lb->buf);
609     else
610 	return set_nonblock (lb->buf);
611 }
612 
613 /* The shutdown function for a log buffer.  */
614 
615 static int
616 log_buffer_shutdown (closure)
617      void *closure;
618 {
619     struct log_buffer *lb = (struct log_buffer *) closure;
620     int retval;
621 
622     retval = buf_shutdown (lb->buf);
623     if (fclose (lb->log) < 0)
624 	error (0, errno, "closing log file");
625     return retval;
626 }
627 
628 #ifdef NO_SOCKET_TO_FD
629 
630 /* Under certain circumstances, we must communicate with the server
631    via a socket using send() and recv().  This is because under some
632    operating systems (OS/2 and Windows 95 come to mind), a socket
633    cannot be converted to a file descriptor -- it must be treated as a
634    socket and nothing else.
635 
636    We may also need to deal with socket routine error codes differently
637    in these cases.  This is handled through the SOCK_ERRNO and
638    SOCK_STRERROR macros. */
639 
640 static int use_socket_style = 0;
641 static int server_sock;
642 
643 /* These routines implement a buffer structure which uses send and
644    recv.  The buffer is always in blocking mode so we don't implement
645    the block routine.  */
646 
647 /* Note that it is important that these routines always handle errors
648    internally and never return a positive errno code, since it would in
649    general be impossible for the caller to know in general whether any
650    error code came from a socket routine (to decide whether to use
651    SOCK_STRERROR or simply strerror to print an error message). */
652 
653 /* We use an instance of this structure as the closure field.  */
654 
655 struct socket_buffer
656 {
657     /* The socket number.  */
658     int socket;
659 };
660 
661 static struct buffer *socket_buffer_initialize
662   PROTO ((int, int, void (*) (struct buffer *)));
663 static int socket_buffer_input PROTO((void *, char *, int, int, int *));
664 static int socket_buffer_output PROTO((void *, const char *, int, int *));
665 static int socket_buffer_flush PROTO((void *));
666 
667 /* Create a buffer based on a socket.  */
668 
669 static struct buffer *
670 socket_buffer_initialize (socket, input, memory)
671      int socket;
672      int input;
673      void (*memory) PROTO((struct buffer *));
674 {
675     struct socket_buffer *n;
676 
677     n = (struct socket_buffer *) xmalloc (sizeof *n);
678     n->socket = socket;
679     return buf_initialize (input ? socket_buffer_input : NULL,
680 			   input ? NULL : socket_buffer_output,
681 			   input ? NULL : socket_buffer_flush,
682 			   (int (*) PROTO((void *, int))) NULL,
683 			   (int (*) PROTO((void *))) NULL,
684 			   memory,
685 			   n);
686 }
687 
688 /* The buffer input function for a buffer built on a socket.  */
689 
690 static int
691 socket_buffer_input (closure, data, need, size, got)
692      void *closure;
693      char *data;
694      int need;
695      int size;
696      int *got;
697 {
698     struct socket_buffer *sb = (struct socket_buffer *) closure;
699     int nbytes;
700 
701     /* I believe that the recv function gives us exactly the semantics
702        we want.  If there is a message, it returns immediately with
703        whatever it could get.  If there is no message, it waits until
704        one comes in.  In other words, it is not like read, which in
705        blocking mode normally waits until all the requested data is
706        available.  */
707 
708     *got = 0;
709 
710     do
711     {
712 
713 	/* Note that for certain (broken?) networking stacks, like
714 	   VMS's UCX (not sure what version, problem reported with
715 	   recv() in 1997), and (according to windows-NT/config.h)
716 	   Windows NT 3.51, we must call recv or send with a
717 	   moderately sized buffer (say, less than 200K or something),
718 	   or else there may be network errors (somewhat hard to
719 	   produce, e.g. WAN not LAN or some such).  buf_read_data
720 	   makes sure that we only recv() BUFFER_DATA_SIZE bytes at
721 	   a time.  */
722 
723 	nbytes = recv (sb->socket, data, size, 0);
724 	if (nbytes < 0)
725 	    error (1, 0, "reading from server: %s", SOCK_STRERROR (SOCK_ERRNO));
726 	if (nbytes == 0)
727 	{
728 	    /* End of file (for example, the server has closed
729 	       the connection).  If we've already read something, we
730 	       just tell the caller about the data, not about the end of
731 	       file.  If we've read nothing, we return end of file.  */
732 	    if (*got == 0)
733 		return -1;
734 	    else
735 		return 0;
736 	}
737 	need -= nbytes;
738 	size -= nbytes;
739 	data += nbytes;
740 	*got += nbytes;
741     }
742     while (need > 0);
743 
744     return 0;
745 }
746 
747 /* The buffer output function for a buffer built on a socket.  */
748 
749 static int
750 socket_buffer_output (closure, data, have, wrote)
751      void *closure;
752      const char *data;
753      int have;
754      int *wrote;
755 {
756     struct socket_buffer *sb = (struct socket_buffer *) closure;
757 
758     *wrote = have;
759 
760     /* See comment in socket_buffer_input regarding buffer size we pass
761        to send and recv.  */
762 
763 #ifdef SEND_NEVER_PARTIAL
764     /* If send() never will produce a partial write, then just do it.  This
765        is needed for systems where its return value is something other than
766        the number of bytes written.  */
767     if (send (sb->socket, data, have, 0) < 0)
768 	error (1, 0, "writing to server socket: %s", SOCK_STRERROR (SOCK_ERRNO));
769 #else
770     while (have > 0)
771     {
772 	int nbytes;
773 
774 	nbytes = send (sb->socket, data, have, 0);
775 	if (nbytes < 0)
776 	    error (1, 0, "writing to server socket: %s", SOCK_STRERROR (SOCK_ERRNO));
777 
778 	have -= nbytes;
779 	data += nbytes;
780     }
781 #endif
782 
783     return 0;
784 }
785 
786 /* The buffer flush function for a buffer built on a socket.  */
787 
788 /*ARGSUSED*/
789 static int
790 socket_buffer_flush (closure)
791      void *closure;
792 {
793     /* Nothing to do.  Sockets are always flushed.  */
794     return 0;
795 }
796 
797 #endif /* NO_SOCKET_TO_FD */
798 
799 /*
800  * Read a line from the server.  Result does not include the terminating \n.
801  *
802  * Space for the result is malloc'd and should be freed by the caller.
803  *
804  * Returns number of bytes read.
805  */
806 static int
807 read_line (resultp)
808     char **resultp;
809 {
810     int status;
811     char *result;
812     int len;
813 
814     status = buf_flush (to_server, 1);
815     if (status != 0)
816 	error (1, status, "writing to server");
817 
818     status = buf_read_line (from_server, &result, &len);
819     if (status != 0)
820     {
821 	if (status == -1)
822 	    error (1, 0, "end of file from server (consult above messages if any)");
823 	else if (status == -2)
824 	    error (1, 0, "out of memory");
825 	else
826 	    error (1, status, "reading from server");
827     }
828 
829     if (resultp != NULL)
830 	*resultp = result;
831     else
832 	free (result);
833 
834     return len;
835 }
836 
837 #endif /* CLIENT_SUPPORT */
838 
839 
840 #if defined(CLIENT_SUPPORT) || defined(SERVER_SUPPORT)
841 
842 /*
843  * Zero if compression isn't supported or requested; non-zero to indicate
844  * a compression level to request from gzip.
845  */
846 int gzip_level;
847 
848 /*
849  * Level of compression to use when running gzip on a single file.
850  */
851 int file_gzip_level;
852 
853 #endif /* CLIENT_SUPPORT or SERVER_SUPPORT */
854 
855 #ifdef CLIENT_SUPPORT
856 
857 /*
858  * The Repository for the top level of this command (not necessarily
859  * the CVSROOT, just the current directory at the time we do it).
860  */
861 static char *toplevel_repos = NULL;
862 
863 /* Working directory when we first started.  Note: we could speed things
864    up on some systems by using savecwd.h here instead of just always
865    storing a name.  */
866 char *toplevel_wd;
867 
868 static void
869 handle_ok (args, len)
870     char *args;
871     int len;
872 {
873     return;
874 }
875 
876 static void
877 handle_error (args, len)
878     char *args;
879     int len;
880 {
881     int something_printed;
882 
883     /*
884      * First there is a symbolic error code followed by a space, which
885      * we ignore.
886      */
887     char *p = strchr (args, ' ');
888     if (p == NULL)
889     {
890 	error (0, 0, "invalid data from cvs server");
891 	return;
892     }
893     ++p;
894 
895     /* Next we print the text of the message from the server.  We
896        probably should be prefixing it with "server error" or some
897        such, because if it is something like "Out of memory", the
898        current behavior doesn't say which machine is out of
899        memory.  */
900 
901     len -= p - args;
902     something_printed = 0;
903     for (; len > 0; --len)
904     {
905 	something_printed = 1;
906 	putc (*p++, stderr);
907     }
908     if (something_printed)
909 	putc ('\n', stderr);
910 }
911 
912 static void
913 handle_valid_requests (args, len)
914     char *args;
915     int len;
916 {
917     char *p = args;
918     char *q;
919     struct request *rq;
920     do
921     {
922 	q = strchr (p, ' ');
923 	if (q != NULL)
924 	    *q++ = '\0';
925 	for (rq = requests; rq->name != NULL; ++rq)
926 	{
927 	    if (strcmp (rq->name, p) == 0)
928 		break;
929 	}
930 	if (rq->name == NULL)
931 	    /*
932 	     * It is a request we have never heard of (and thus never
933 	     * will want to use).  So don't worry about it.
934 	     */
935 	    ;
936 	else
937 	{
938 	    if (rq->flags & RQ_ENABLEME)
939 	    {
940 		/*
941 		 * Server wants to know if we have this, to enable the
942 		 * feature.
943 		 */
944 		send_to_server (rq->name, 0);
945                 send_to_server ("\012", 0);
946 	    }
947 	    else
948 		rq->flags |= RQ_SUPPORTED;
949 	}
950 	p = q;
951     } while (q != NULL);
952     for (rq = requests; rq->name != NULL; ++rq)
953     {
954 	if ((rq->flags & RQ_SUPPORTED)
955 	    || (rq->flags & RQ_ENABLEME))
956 	    continue;
957 	if (rq->flags & RQ_ESSENTIAL)
958 	    error (1, 0, "request `%s' not supported by server", rq->name);
959     }
960 }
961 
962 /* This variable holds the result of Entries_Open, so that we can
963    close Entries_Close on it when we move on to a new directory, or
964    when we finish.  */
965 static List *last_entries;
966 
967 /*
968  * Do all the processing for PATHNAME, where pathname consists of the
969  * repository and the filename.  The parameters we pass to FUNC are:
970  * DATA is just the DATA parameter which was passed to
971  * call_in_directory; ENT_LIST is a pointer to an entries list (which
972  * we manage the storage for); SHORT_PATHNAME is the pathname of the
973  * file relative to the (overall) directory in which the command is
974  * taking place; and FILENAME is the filename portion only of
975  * SHORT_PATHNAME.  When we call FUNC, the curent directory points to
976  * the directory portion of SHORT_PATHNAME.  */
977 
978 static char *last_dir_name;
979 
980 static void
981 call_in_directory (pathname, func, data)
982     char *pathname;
983     void (*func) PROTO((char *data, List *ent_list, char *short_pathname,
984 			  char *filename));
985     char *data;
986 {
987     char *dir_name;
988     char *filename;
989     /* This is what we get when we hook up the directory (working directory
990        name) from PATHNAME with the filename from REPOSNAME.  For example:
991        pathname: ccvs/src/
992        reposname: /u/src/master/ccvs/foo/ChangeLog
993        short_pathname: ccvs/src/ChangeLog
994        */
995     char *short_pathname;
996     char *p;
997 
998     /*
999      * Do the whole descent in parallel for the repositories, so we
1000      * know what to put in CVS/Repository files.  I'm not sure the
1001      * full hair is necessary since the server does a similar
1002      * computation; I suspect that we only end up creating one
1003      * directory at a time anyway.
1004      *
1005      * Also note that we must *only* worry about this stuff when we
1006      * are creating directories; `cvs co foo/bar; cd foo/bar; cvs co
1007      * CVSROOT; cvs update' is legitimate, but in this case
1008      * foo/bar/CVSROOT/CVS/Repository is not a subdirectory of
1009      * foo/bar/CVS/Repository.
1010      */
1011     char *reposname;
1012     char *short_repos;
1013     char *reposdirname;
1014     char *rdirp;
1015     int reposdirname_absolute;
1016 
1017     reposname = NULL;
1018     read_line (&reposname);
1019     assert (reposname != NULL);
1020 
1021     reposdirname_absolute = 0;
1022     if (strncmp (reposname, toplevel_repos, strlen (toplevel_repos)) != 0)
1023     {
1024 	reposdirname_absolute = 1;
1025 	short_repos = reposname;
1026     }
1027     else
1028     {
1029 	short_repos = reposname + strlen (toplevel_repos) + 1;
1030 	if (short_repos[-1] != '/')
1031 	{
1032 	    reposdirname_absolute = 1;
1033 	    short_repos = reposname;
1034 	}
1035     }
1036     reposdirname = xstrdup (short_repos);
1037     p = strrchr (reposdirname, '/');
1038     if (p == NULL)
1039     {
1040 	reposdirname = xrealloc (reposdirname, 2);
1041 	reposdirname[0] = '.'; reposdirname[1] = '\0';
1042     }
1043     else
1044 	*p = '\0';
1045 
1046     dir_name = xstrdup (pathname);
1047     p = strrchr (dir_name, '/');
1048     if (p == NULL)
1049     {
1050 	dir_name = xrealloc (dir_name, 2);
1051 	dir_name[0] = '.'; dir_name[1] = '\0';
1052     }
1053     else
1054 	*p = '\0';
1055     if (client_prune_dirs)
1056 	add_prune_candidate (dir_name);
1057 
1058     filename = strrchr (short_repos, '/');
1059     if (filename == NULL)
1060 	filename = short_repos;
1061     else
1062 	++filename;
1063 
1064     short_pathname = xmalloc (strlen (pathname) + strlen (filename) + 5);
1065     strcpy (short_pathname, pathname);
1066     strcat (short_pathname, filename);
1067 
1068     if (last_dir_name == NULL
1069 	|| strcmp (last_dir_name, dir_name) != 0)
1070     {
1071 	int newdir;
1072 
1073 	if (strcmp (command_name, "export") != 0)
1074 	    if (last_entries)
1075 		Entries_Close (last_entries);
1076 
1077 	if (last_dir_name)
1078 	    free (last_dir_name);
1079 	last_dir_name = dir_name;
1080 
1081 	if (toplevel_wd == NULL)
1082 	{
1083 	    toplevel_wd = xgetwd ();
1084 	    if (toplevel_wd == NULL)
1085 		error (1, errno, "could not get working directory");
1086 	}
1087 
1088 	if (CVS_CHDIR (toplevel_wd) < 0)
1089 	    error (1, errno, "could not chdir to %s", toplevel_wd);
1090 	newdir = 0;
1091 
1092 	/* Create the CVS directory at the top level if needed.  The
1093 	   isdir seems like an unneeded system call, but it *does*
1094 	   need to be called both if the CVS_CHDIR below succeeds
1095 	   (e.g.  "cvs co .") or if it fails (e.g. basicb-1a in
1096 	   testsuite).  We only need to do this for the "." case,
1097 	   since the server takes care of forcing this directory to be
1098 	   created in all other cases.  If we don't create CVSADM
1099 	   here, the call to Entries_Open below will fail.  FIXME:
1100 	   perhaps this means that we should change our algorithm
1101 	   below that calls Create_Admin instead of having this code
1102 	   here? */
1103 	if (/* I think the reposdirname_absolute case has to do with
1104 	       things like "cvs update /foo/bar".  In any event, the
1105 	       code below which tries to put toplevel_repos into
1106 	       CVS/Repository is almost surely unsuited to
1107 	       the reposdirname_absolute case.  */
1108 	    !reposdirname_absolute
1109 	    && (strcmp (dir_name, ".") == 0)
1110 	    && ! isdir (CVSADM))
1111 	{
1112 	    char *repo;
1113 	    char *r;
1114 
1115 	    newdir = 1;
1116 
1117 	    repo = xmalloc (strlen (toplevel_repos)
1118 			    + 10);
1119 	    strcpy (repo, toplevel_repos);
1120 	    r = repo + strlen (repo);
1121 	    if (r[-1] != '.' || r[-2] != '/')
1122 	        strcpy (r, "/.");
1123 
1124 	    Create_Admin (".", ".", repo, (char *) NULL,
1125 			  (char *) NULL, 0, 1, 1);
1126 
1127 	    free (repo);
1128 	}
1129 
1130 	if ( CVS_CHDIR (dir_name) < 0)
1131 	{
1132 	    char *dir;
1133 	    char *dirp;
1134 
1135 	    if (! existence_error (errno))
1136 		error (1, errno, "could not chdir to %s", dir_name);
1137 
1138 	    /* Directory does not exist, we need to create it.  */
1139 	    newdir = 1;
1140 
1141 	    /* Provided we are willing to assume that directories get
1142 	       created one at a time, we could simplify this a lot.
1143 	       Do note that one aspect still would need to walk the
1144 	       dir_name path: the checking for "fncmp (dir, CVSADM)".  */
1145 
1146 	    dir = xmalloc (strlen (dir_name) + 1);
1147 	    dirp = dir_name;
1148 	    rdirp = reposdirname;
1149 
1150 	    /* This algorithm makes nested directories one at a time
1151                and create CVS administration files in them.  For
1152                example, we're checking out foo/bar/baz from the
1153                repository:
1154 
1155 	       1) create foo, point CVS/Repository to <root>/foo
1156 	       2)     .. foo/bar                   .. <root>/foo/bar
1157 	       3)     .. foo/bar/baz               .. <root>/foo/bar/baz
1158 
1159 	       As you can see, we're just stepping along DIR_NAME (with
1160 	       DIRP) and REPOSDIRNAME (with RDIRP) respectively.
1161 
1162 	       We need to be careful when we are checking out a
1163 	       module, however, since DIR_NAME and REPOSDIRNAME are not
1164 	       going to be the same.  Since modules will not have any
1165 	       slashes in their names, we should watch the output of
1166 	       STRCHR to decide whether or not we should use STRCHR on
1167 	       the RDIRP.  That is, if we're down to a module name,
1168 	       don't keep picking apart the repository directory name.  */
1169 
1170 	    do
1171 	    {
1172 		dirp = strchr (dirp, '/');
1173 		if (dirp)
1174 		{
1175 		    strncpy (dir, dir_name, dirp - dir_name);
1176 		    dir[dirp - dir_name] = '\0';
1177 		    /* Skip the slash.  */
1178 		    ++dirp;
1179 		    if (rdirp == NULL)
1180 			/* This just means that the repository string has
1181 			   fewer components than the dir_name string.  But
1182 			   that is OK (e.g. see modules3-8 in testsuite).  */
1183 			;
1184 		    else
1185 			rdirp = strchr (rdirp, '/');
1186 		}
1187 		else
1188 		{
1189 		    /* If there are no more slashes in the dir name,
1190                        we're down to the most nested directory -OR- to
1191                        the name of a module.  In the first case, we
1192                        should be down to a DIRP that has no slashes,
1193                        so it won't help/hurt to do another STRCHR call
1194                        on DIRP.  It will definitely hurt, however, if
1195                        we're down to a module name, since a module
1196                        name can point to a nested directory (that is,
1197                        DIRP will still have slashes in it.  Therefore,
1198                        we should set it to NULL so the routine below
1199                        copies the contents of REMOTEDIRNAME onto the
1200                        root repository directory (does this if rdirp
1201                        is set to NULL, because we used to do an extra
1202                        STRCHR call here). */
1203 
1204 		    rdirp = NULL;
1205 		    strcpy (dir, dir_name);
1206 		}
1207 
1208 		if (fncmp (dir, CVSADM) == 0)
1209 		{
1210 		    error (0, 0, "cannot create a directory named %s", dir);
1211 		    error (0, 0, "because CVS uses \"%s\" for its own uses",
1212 			   CVSADM);
1213 		    error (1, 0, "rename the directory and try again");
1214 		}
1215 
1216 		if (mkdir_if_needed (dir))
1217 		{
1218 		    /* It already existed, fine.  Just keep going.  */
1219 		}
1220 		else if (strcmp (command_name, "export") == 0)
1221 		    /* Don't create CVSADM directories if this is export.  */
1222 		    ;
1223 		else
1224 		{
1225 		    /*
1226 		     * Put repository in CVS/Repository.  For historical
1227 		     * (pre-CVS/Root) reasons, this is an absolute pathname,
1228 		     * but what really matters is the part of it which is
1229 		     * relative to cvsroot.
1230 		     */
1231 		    char *repo;
1232 		    char *r, *b;
1233 
1234 		    repo = xmalloc (strlen (reposdirname)
1235 				    + strlen (toplevel_repos)
1236 				    + 80);
1237 		    if (reposdirname_absolute)
1238 			r = repo;
1239 		    else
1240 		    {
1241 			strcpy (repo, toplevel_repos);
1242 			strcat (repo, "/");
1243 			r = repo + strlen (repo);
1244 		    }
1245 
1246 		    if (rdirp)
1247 		    {
1248 			/* See comment near start of function; the only
1249 			   way that the server can put the right thing
1250 			   in each CVS/Repository file is to create the
1251 			   directories one at a time.  I think that the
1252 			   CVS server has been doing this all along.  */
1253 			error (0, 0, "\
1254 warning: server is not creating directories one at a time");
1255 			strncpy (r, reposdirname, rdirp - reposdirname);
1256 			r[rdirp - reposdirname] = '\0';
1257 		    }
1258 		    else
1259 			strcpy (r, reposdirname);
1260 
1261 		    Create_Admin (dir, dir, repo,
1262 				  (char *)NULL, (char *)NULL, 0, 0, 1);
1263 		    free (repo);
1264 
1265 		    b = strrchr (dir, '/');
1266 		    if (b == NULL)
1267 			Subdir_Register ((List *) NULL, (char *) NULL, dir);
1268 		    else
1269 		    {
1270 			*b = '\0';
1271 			Subdir_Register ((List *) NULL, dir, b + 1);
1272 			*b = '/';
1273 		    }
1274 		}
1275 
1276 		if (rdirp != NULL)
1277 		{
1278 		    /* Skip the slash.  */
1279 		    ++rdirp;
1280 		}
1281 
1282 	    } while (dirp != NULL);
1283 	    free (dir);
1284 	    /* Now it better work.  */
1285 	    if ( CVS_CHDIR (dir_name) < 0)
1286 		error (1, errno, "could not chdir to %s", dir_name);
1287 	}
1288 
1289 	if (strcmp (command_name, "export") != 0)
1290 	{
1291 	    last_entries = Entries_Open (0, dir_name);
1292 
1293 	    /* If this is a newly created directory, we will record
1294 	       all subdirectory information, so call Subdirs_Known in
1295 	       case there are no subdirectories.  If this is not a
1296 	       newly created directory, it may be an old working
1297 	       directory from before we recorded subdirectory
1298 	       information in the Entries file.  We force a search for
1299 	       all subdirectories now, to make sure our subdirectory
1300 	       information is up to date.  If the Entries file does
1301 	       record subdirectory information, then this call only
1302 	       does list manipulation.  */
1303 	    if (newdir)
1304 		Subdirs_Known (last_entries);
1305 	    else
1306 	    {
1307 		List *dirlist;
1308 
1309 		dirlist = Find_Directories ((char *) NULL, W_LOCAL,
1310 					    last_entries);
1311 		dellist (&dirlist);
1312 	    }
1313 	}
1314     }
1315     else
1316 	free (dir_name);
1317     free (reposdirname);
1318     (*func) (data, last_entries, short_pathname, filename);
1319     free (short_pathname);
1320     free (reposname);
1321 }
1322 
1323 static void
1324 copy_a_file (data, ent_list, short_pathname, filename)
1325     char *data;
1326     List *ent_list;
1327     char *short_pathname;
1328     char *filename;
1329 {
1330     char *newname;
1331 #ifdef USE_VMS_FILENAMES
1332     char *p;
1333 #endif
1334 
1335     read_line (&newname);
1336 
1337 #ifdef USE_VMS_FILENAMES
1338     /* Mogrify the filename so VMS is happy with it. */
1339     for(p = newname; *p; p++)
1340        if(*p == '.' || *p == '#') *p = '_';
1341 #endif
1342     /* cvsclient.texi has said for a long time that newname must be in the
1343        same directory.  Wouldn't want a malicious or buggy server overwriting
1344        ~/.profile, /etc/passwd, or anything like that.  */
1345     if (last_component (newname) != newname)
1346 	error (1, 0, "protocol error: Copy-file tried to specify directory");
1347 
1348     if (unlink_file (newname) && !existence_error (errno))
1349 	error (0, errno, "unable to remove %s", newname);
1350     copy_file (filename, newname);
1351     free (newname);
1352 }
1353 
1354 static void
1355 handle_copy_file (args, len)
1356     char *args;
1357     int len;
1358 {
1359     call_in_directory (args, copy_a_file, (char *)NULL);
1360 }
1361 
1362 
1363 static void read_counted_file PROTO ((char *, char *));
1364 
1365 /* Read from the server the count for the length of a file, then read
1366    the contents of that file and write them to FILENAME.  FULLNAME is
1367    the name of the file for use in error messages.  FIXME-someday:
1368    extend this to deal with compressed files and make update_entries
1369    use it.  On error, gives a fatal error.  */
1370 static void
1371 read_counted_file (filename, fullname)
1372     char *filename;
1373     char *fullname;
1374 {
1375     char *size_string;
1376     size_t size;
1377     char *buf;
1378 
1379     /* Pointers in buf to the place to put data which will be read,
1380        and the data which needs to be written, respectively.  */
1381     char *pread;
1382     char *pwrite;
1383     /* Number of bytes left to read and number of bytes in buf waiting to
1384        be written, respectively.  */
1385     size_t nread;
1386     size_t nwrite;
1387 
1388     FILE *fp;
1389 
1390     read_line (&size_string);
1391     if (size_string[0] == 'z')
1392 	error (1, 0, "\
1393 protocol error: compressed files not supported for that operation");
1394     /* FIXME: should be doing more error checking, probably.  Like using
1395        strtoul and making sure we used up the whole line.  */
1396     size = atoi (size_string);
1397     free (size_string);
1398 
1399     /* A more sophisticated implementation would use only a limited amount
1400        of buffer space (8K perhaps), and read that much at a time.  We allocate
1401        a buffer for the whole file only to make it easy to keep track what
1402        needs to be read and written.  */
1403     buf = xmalloc (size);
1404 
1405     /* FIXME-someday: caller should pass in a flag saying whether it
1406        is binary or not.  I haven't carefully looked into whether
1407        CVS/Template files should use local text file conventions or
1408        not.  */
1409     fp = CVS_FOPEN (filename, "wb");
1410     if (fp == NULL)
1411 	error (1, errno, "cannot write %s", fullname);
1412     nread = size;
1413     nwrite = 0;
1414     pread = buf;
1415     pwrite = buf;
1416     while (nread > 0 || nwrite > 0)
1417     {
1418 	size_t n;
1419 
1420 	if (nread > 0)
1421 	{
1422 	    n = try_read_from_server (pread, nread);
1423 	    nread -= n;
1424 	    pread += n;
1425 	    nwrite += n;
1426 	}
1427 
1428 	if (nwrite > 0)
1429 	{
1430 	    n = fwrite (pwrite, 1, nwrite, fp);
1431 	    if (ferror (fp))
1432 		error (1, errno, "cannot write %s", fullname);
1433 	    nwrite -= n;
1434 	    pwrite += n;
1435 	}
1436     }
1437     free (buf);
1438     if (fclose (fp) < 0)
1439 	error (1, errno, "cannot close %s", fullname);
1440 }
1441 
1442 /* OK, we want to swallow the "U foo.c" response and then output it only
1443    if we can update the file.  In the future we probably want some more
1444    systematic approach to parsing tagged text, but for now we keep it
1445    ad hoc.  "Why," I hear you cry, "do we not just look at the
1446    Update-existing and Created responses?"  That is an excellent question,
1447    and the answer is roughly conservatism/laziness--I haven't read through
1448    update.c enough to figure out the exact correspondence or lack thereof
1449    between those responses and a "U foo.c" line (note that Merged, from
1450    join_file, can be either "C foo" or "U foo" depending on the context).  */
1451 /* Nonzero if we have seen +updated and not -updated.  */
1452 static int updated_seen;
1453 /* Filename from an "fname" tagged response within +updated/-updated.  */
1454 static char *updated_fname;
1455 
1456 /* This struct is used to hold data when reading the +importmergecmd
1457    and -importmergecmd tags.  We put the variables in a struct only
1458    for namespace issues.  FIXME: As noted above, we need to develop a
1459    more systematic approach.  */
1460 static struct
1461 {
1462     /* Nonzero if we have seen +importmergecmd and not -importmergecmd.  */
1463     int seen;
1464     /* Number of conflicts, from a "conflicts" tagged response.  */
1465     int conflicts;
1466     /* First merge tag, from a "mergetag1" tagged response.  */
1467     char *mergetag1;
1468     /* Second merge tag, from a "mergetag2" tagged response.  */
1469     char *mergetag2;
1470     /* Repository, from a "repository" tagged response.  */
1471     char *repository;
1472 } importmergecmd;
1473 
1474 /* Nonzero if we should arrange to return with a failure exit status.  */
1475 static int failure_exit;
1476 
1477 
1478 /*
1479  * The time stamp of the last file we registered.
1480  */
1481 static time_t last_register_time;
1482 
1483 /*
1484  * The Checksum response gives the checksum for the file transferred
1485  * over by the next Updated, Merged or Patch response.  We just store
1486  * it here, and then check it in update_entries.
1487  */
1488 
1489 static int stored_checksum_valid;
1490 static unsigned char stored_checksum[16];
1491 
1492 static void
1493 handle_checksum (args, len)
1494     char *args;
1495     int len;
1496 {
1497     char *s;
1498     char buf[3];
1499     int i;
1500 
1501     if (stored_checksum_valid)
1502         error (1, 0, "Checksum received before last one was used");
1503 
1504     s = args;
1505     buf[2] = '\0';
1506     for (i = 0; i < 16; i++)
1507     {
1508         char *bufend;
1509 
1510 	buf[0] = *s++;
1511 	buf[1] = *s++;
1512 	stored_checksum[i] = (char) strtol (buf, &bufend, 16);
1513 	if (bufend != buf + 2)
1514 	    break;
1515     }
1516 
1517     if (i < 16 || *s != '\0')
1518         error (1, 0, "Invalid Checksum response: `%s'", args);
1519 
1520     stored_checksum_valid = 1;
1521 }
1522 
1523 /* Mode that we got in a "Mode" response (malloc'd), or NULL if none.  */
1524 static char *stored_mode;
1525 
1526 static void handle_mode PROTO ((char *, int));
1527 
1528 static void
1529 handle_mode (args, len)
1530     char *args;
1531     int len;
1532 {
1533     if (stored_mode != NULL)
1534 	error (1, 0, "protocol error: duplicate Mode");
1535     stored_mode = xstrdup (args);
1536 }
1537 
1538 /* Nonzero if time was specified in Mod-time.  */
1539 static int stored_modtime_valid;
1540 /* Time specified in Mod-time.  */
1541 static time_t stored_modtime;
1542 
1543 static void handle_mod_time PROTO ((char *, int));
1544 
1545 static void
1546 handle_mod_time (args, len)
1547     char *args;
1548     int len;
1549 {
1550     if (stored_modtime_valid)
1551 	error (0, 0, "protocol error: duplicate Mod-time");
1552     stored_modtime = get_date (args, NULL);
1553     if (stored_modtime == (time_t) -1)
1554 	error (0, 0, "protocol error: cannot parse date %s", args);
1555     else
1556 	stored_modtime_valid = 1;
1557 }
1558 
1559 /*
1560  * If we receive a patch, but the patch program fails to apply it, we
1561  * want to request the original file.  We keep a list of files whose
1562  * patches have failed.
1563  */
1564 
1565 char **failed_patches;
1566 int failed_patches_count;
1567 
1568 struct update_entries_data
1569 {
1570     enum {
1571       /*
1572        * We are just getting an Entries line; the local file is
1573        * correct.
1574        */
1575       UPDATE_ENTRIES_CHECKIN,
1576       /* We are getting the file contents as well.  */
1577       UPDATE_ENTRIES_UPDATE,
1578       /*
1579        * We are getting a patch against the existing local file, not
1580        * an entire new file.
1581        */
1582       UPDATE_ENTRIES_PATCH,
1583       /*
1584        * We are getting an RCS change text (diff -n output) against
1585        * the existing local file, not an entire new file.
1586        */
1587       UPDATE_ENTRIES_RCS_DIFF
1588     } contents;
1589 
1590     enum {
1591 	/* We are replacing an existing file.  */
1592 	UPDATE_ENTRIES_EXISTING,
1593 	/* We are creating a new file.  */
1594 	UPDATE_ENTRIES_NEW,
1595 	/* We don't know whether it is existing or new.  */
1596 	UPDATE_ENTRIES_EXISTING_OR_NEW
1597     } existp;
1598 
1599     /*
1600      * String to put in the timestamp field or NULL to use the timestamp
1601      * of the file.
1602      */
1603     char *timestamp;
1604 };
1605 
1606 /* Update the Entries line for this file.  */
1607 static void
1608 update_entries (data_arg, ent_list, short_pathname, filename)
1609     char *data_arg;
1610     List *ent_list;
1611     char *short_pathname;
1612     char *filename;
1613 {
1614     char *entries_line;
1615     struct update_entries_data *data = (struct update_entries_data *)data_arg;
1616 
1617     char *cp;
1618     char *user;
1619     char *vn;
1620     /* Timestamp field.  Always empty according to the protocol.  */
1621     char *ts;
1622     char *options = NULL;
1623     char *tag = NULL;
1624     char *date = NULL;
1625     char *tag_or_date;
1626     char *scratch_entries = NULL;
1627     int bin;
1628 
1629 #ifdef UTIME_EXPECTS_WRITABLE
1630     int change_it_back = 0;
1631 #endif
1632 
1633     read_line (&entries_line);
1634 
1635     /*
1636      * Parse the entries line.
1637      */
1638     scratch_entries = xstrdup (entries_line);
1639 
1640     if (scratch_entries[0] != '/')
1641         error (1, 0, "bad entries line `%s' from server", entries_line);
1642     user = scratch_entries + 1;
1643     if ((cp = strchr (user, '/')) == NULL)
1644         error (1, 0, "bad entries line `%s' from server", entries_line);
1645     *cp++ = '\0';
1646     vn = cp;
1647     if ((cp = strchr (vn, '/')) == NULL)
1648         error (1, 0, "bad entries line `%s' from server", entries_line);
1649     *cp++ = '\0';
1650 
1651     ts = cp;
1652     if ((cp = strchr (ts, '/')) == NULL)
1653         error (1, 0, "bad entries line `%s' from server", entries_line);
1654     *cp++ = '\0';
1655     options = cp;
1656     if ((cp = strchr (options, '/')) == NULL)
1657         error (1, 0, "bad entries line `%s' from server", entries_line);
1658     *cp++ = '\0';
1659     tag_or_date = cp;
1660 
1661     /* If a slash ends the tag_or_date, ignore everything after it.  */
1662     cp = strchr (tag_or_date, '/');
1663     if (cp != NULL)
1664         *cp = '\0';
1665     if (*tag_or_date == 'T')
1666         tag = tag_or_date + 1;
1667     else if (*tag_or_date == 'D')
1668         date = tag_or_date + 1;
1669 
1670     /* Done parsing the entries line. */
1671 
1672     if (data->contents == UPDATE_ENTRIES_UPDATE
1673 	|| data->contents == UPDATE_ENTRIES_PATCH
1674 	|| data->contents == UPDATE_ENTRIES_RCS_DIFF)
1675     {
1676 	char *size_string;
1677 	char *mode_string;
1678 	int size;
1679 	char *buf;
1680 	char *temp_filename;
1681 	int use_gzip;
1682 	int patch_failed;
1683 
1684 	read_line (&mode_string);
1685 
1686 	read_line (&size_string);
1687 	if (size_string[0] == 'z')
1688 	{
1689 	    use_gzip = 1;
1690 	    size = atoi (size_string+1);
1691 	}
1692 	else
1693 	{
1694 	    use_gzip = 0;
1695 	    size = atoi (size_string);
1696 	}
1697 	free (size_string);
1698 
1699 	/* Note that checking this separately from writing the file is
1700 	   a race condition: if the existence or lack thereof of the
1701 	   file changes between now and the actual calls which
1702 	   operate on it, we lose.  However (a) there are so many
1703 	   cases, I'm reluctant to try to fix them all, (b) in some
1704 	   cases the system might not even have a system call which
1705 	   does the right thing, and (c) it isn't clear this needs to
1706 	   work.  */
1707 	if (data->existp == UPDATE_ENTRIES_EXISTING
1708 	    && !isfile (filename))
1709 	    /* Emit a warning and update the file anyway.  */
1710 	    error (0, 0, "warning: %s unexpectedly disappeared",
1711 		   short_pathname);
1712 
1713 	if (data->existp == UPDATE_ENTRIES_NEW
1714 	    && isfile (filename))
1715 	{
1716 	    /* Emit a warning and refuse to update the file; we don't want
1717 	       to clobber a user's file.  */
1718 	    size_t nread;
1719 	    size_t toread;
1720 
1721 	    /* size should be unsigned, but until we get around to fixing
1722 	       that, work around it.  */
1723 	    size_t usize;
1724 
1725 	    char buf[8192];
1726 
1727 	    /* This error might be confusing; it isn't really clear to
1728 	       the user what to do about it.  Keep in mind that it has
1729 	       several causes: (1) something/someone creates the file
1730 	       during the time that CVS is running, (2) the repository
1731 	       has two files whose names clash for the client because
1732 	       of case-insensitivity or similar causes, (3) a special
1733 	       case of this is that a file gets renamed for example
1734 	       from a.c to A.C.  A "cvs update" on a case-insensitive
1735 	       client will get this error.  Repeating the update takes
1736 	       care of the problem, but is it clear to the user what
1737 	       is going on and what to do about it?, (4) the client
1738 	       has a file which the server doesn't know about (e.g. "?
1739 	       foo" file), and that name clashes with a file the
1740 	       server does know about, (5) classify.c will print the same
1741 	       message for other reasons.
1742 
1743 	       I hope the above paragraph makes it clear that making this
1744 	       clearer is not a one-line fix.  */
1745 	    error (0, 0, "move away %s; it is in the way", short_pathname);
1746 	    if (updated_fname != NULL)
1747 	    {
1748 		cvs_output ("C ", 0);
1749 		cvs_output (updated_fname, 0);
1750 		cvs_output ("\n", 1);
1751 	    }
1752 	    failure_exit = 1;
1753 
1754 	discard_file_and_return:
1755 	    /* Now read and discard the file contents.  */
1756 	    usize = size;
1757 	    nread = 0;
1758 	    while (nread < usize)
1759 	    {
1760 		toread = usize - nread;
1761 		if (toread > sizeof buf)
1762 		    toread = sizeof buf;
1763 
1764 		nread += try_read_from_server (buf, toread);
1765 		if (nread == usize)
1766 		    break;
1767 	    }
1768 
1769 	    free (mode_string);
1770 	    free (scratch_entries);
1771 	    free (entries_line);
1772 
1773 	    /* The Mode, Mod-time, and Checksum responses should not carry
1774 	       over to a subsequent Created (or whatever) response, even
1775 	       in the error case.  */
1776 	    if (stored_mode != NULL)
1777 	    {
1778 		free (stored_mode);
1779 		stored_mode = NULL;
1780 	    }
1781 	    stored_modtime_valid = 0;
1782 	    stored_checksum_valid = 0;
1783 
1784 	    if (updated_fname != NULL)
1785 	    {
1786 		free (updated_fname);
1787 		updated_fname = NULL;
1788 	    }
1789 	    return;
1790 	}
1791 
1792 	temp_filename = xmalloc (strlen (filename) + 80);
1793 #ifdef USE_VMS_FILENAMES
1794         /* A VMS rename of "blah.dat" to "foo" to implies a
1795            destination of "foo.dat" which is unfortinate for CVS */
1796        sprintf (temp_filename, "%s_new_", filename);
1797 #else
1798 #ifdef _POSIX_NO_TRUNC
1799 	sprintf (temp_filename, ".new.%.9s", filename);
1800 #else /* _POSIX_NO_TRUNC */
1801 	sprintf (temp_filename, ".new.%s", filename);
1802 #endif /* _POSIX_NO_TRUNC */
1803 #endif /* USE_VMS_FILENAMES */
1804 
1805 	buf = xmalloc (size);
1806 
1807         /* Some systems, like OS/2 and Windows NT, end lines with CRLF
1808            instead of just LF.  Format translation is done in the C
1809            library I/O funtions.  Here we tell them whether or not to
1810            convert -- if this file is marked "binary" with the RCS -kb
1811            flag, then we don't want to convert, else we do (because
1812            CVS assumes text files by default). */
1813 
1814 	if (options)
1815 	    bin = !(strcmp (options, "-kb"));
1816 	else
1817 	    bin = 0;
1818 
1819 	if (data->contents == UPDATE_ENTRIES_RCS_DIFF)
1820 	{
1821 	    /* This is an RCS change text.  We just hold the change
1822 	       text in memory.  */
1823 
1824 	    if (use_gzip)
1825 		error (1, 0,
1826 		       "server error: gzip invalid with RCS change text");
1827 
1828 	    read_from_server (buf, size);
1829 	}
1830 	else
1831 	{
1832 	    int fd;
1833 
1834 	    fd = CVS_OPEN (temp_filename,
1835 			   (O_WRONLY | O_CREAT | O_TRUNC
1836 			    | (bin ? OPEN_BINARY : 0)),
1837 			   0777);
1838 
1839 	    if (fd < 0)
1840 	    {
1841 		/* I can see a case for making this a fatal error; for
1842 		   a condition like disk full or network unreachable
1843 		   (for a file server), carrying on and giving an
1844 		   error on each file seems unnecessary.  But if it is
1845 		   a permission problem, or some such, then it is
1846 		   entirely possible that future files will not have
1847 		   the same problem.  */
1848 		error (0, errno, "cannot write %s", short_pathname);
1849 		goto discard_file_and_return;
1850 	    }
1851 
1852 	    if (size > 0)
1853 	    {
1854 		read_from_server (buf, size);
1855 
1856 		if (use_gzip)
1857 		{
1858 		    if (gunzip_and_write (fd, short_pathname,
1859 					  (unsigned char *) buf, size))
1860 			error (1, 0, "aborting due to compression error");
1861 		}
1862 		else if (write (fd, buf, size) != size)
1863 		    error (1, errno, "writing %s", short_pathname);
1864 	    }
1865 
1866 	    if (close (fd) < 0)
1867 		error (1, errno, "writing %s", short_pathname);
1868 	}
1869 
1870 	/* This is after we have read the file from the net (a change
1871 	   from previous versions, where the server would send us
1872 	   "M U foo.c" before Update-existing or whatever), but before
1873 	   we finish writing the file (arguably a bug).  The timing
1874 	   affects a user who wants status info about how far we have
1875 	   gotten, and also affects whether "U foo.c" appears in addition
1876 	   to various error messages.  */
1877 	if (updated_fname != NULL)
1878 	{
1879 	    cvs_output ("U ", 0);
1880 	    cvs_output (updated_fname, 0);
1881 	    cvs_output ("\n", 1);
1882 	    free (updated_fname);
1883 	    updated_fname = 0;
1884 	}
1885 
1886 	patch_failed = 0;
1887 
1888 	if (data->contents == UPDATE_ENTRIES_UPDATE)
1889 	{
1890 	    rename_file (temp_filename, filename);
1891 	}
1892 	else if (data->contents == UPDATE_ENTRIES_PATCH)
1893 	{
1894 	    /* You might think we could just leave Patched out of
1895 	       Valid-responses and not get this response.  However, if
1896 	       memory serves, the CVS 1.9 server bases this on -u
1897 	       (update-patches), and there is no way for us to send -u
1898 	       or not based on whether the server supports "Rcs-diff".
1899 
1900 	       Fall back to transmitting entire files.  */
1901 	    patch_failed = 1;
1902 	}
1903 	else
1904 	{
1905 	    char *filebuf;
1906 	    size_t filebufsize;
1907 	    size_t nread;
1908 	    char *patchedbuf;
1909 	    size_t patchedlen;
1910 
1911 	    /* Handle UPDATE_ENTRIES_RCS_DIFF.  */
1912 
1913 	    if (!isfile (filename))
1914 	        error (1, 0, "patch original file %s does not exist",
1915 		       short_pathname);
1916 	    filebuf = NULL;
1917 	    filebufsize = 0;
1918 	    nread = 0;
1919 
1920 	    get_file (filename, short_pathname, bin ? FOPEN_BINARY_READ : "r",
1921 		      &filebuf, &filebufsize, &nread);
1922 	    /* At this point the contents of the existing file are in
1923                FILEBUF, and the length of the contents is in NREAD.
1924                The contents of the patch from the network are in BUF,
1925                and the length of the patch is in SIZE.  */
1926 
1927 	    if (! rcs_change_text (short_pathname, filebuf, nread, buf, size,
1928 				   &patchedbuf, &patchedlen))
1929 		patch_failed = 1;
1930 	    else
1931 	    {
1932 		if (stored_checksum_valid)
1933 		{
1934 		    struct cvs_MD5Context context;
1935 		    unsigned char checksum[16];
1936 
1937 		    /* We have a checksum.  Check it before writing
1938 		       the file out, so that we don't have to read it
1939 		       back in again.  */
1940 		    cvs_MD5Init (&context);
1941 		    cvs_MD5Update (&context,
1942 				   (unsigned char *) patchedbuf, patchedlen);
1943 		    cvs_MD5Final (checksum, &context);
1944 		    if (memcmp (checksum, stored_checksum, 16) != 0)
1945 		    {
1946 			error (0, 0,
1947 			       "checksum failure after patch to %s; will refetch",
1948 			       short_pathname);
1949 
1950 			patch_failed = 1;
1951 		    }
1952 
1953 		    stored_checksum_valid = 0;
1954 		}
1955 
1956 		if (! patch_failed)
1957 		{
1958 		    FILE *e;
1959 
1960 		    e = open_file (temp_filename,
1961 				   bin ? FOPEN_BINARY_WRITE : "w");
1962 		    if (fwrite (patchedbuf, 1, patchedlen, e) != patchedlen)
1963 			error (1, errno, "cannot write %s", temp_filename);
1964 		    if (fclose (e) == EOF)
1965 			error (1, errno, "cannot close %s", temp_filename);
1966 		    rename_file (temp_filename, filename);
1967 		}
1968 
1969 		free (patchedbuf);
1970 	    }
1971 
1972 	    free (filebuf);
1973 	}
1974 
1975 	free (temp_filename);
1976 
1977 	if (stored_checksum_valid && ! patch_failed)
1978 	{
1979 	    FILE *e;
1980 	    struct cvs_MD5Context context;
1981 	    unsigned char buf[8192];
1982 	    unsigned len;
1983 	    unsigned char checksum[16];
1984 
1985 	    /*
1986 	     * Compute the MD5 checksum.  This will normally only be
1987 	     * used when receiving a patch, so we always compute it
1988 	     * here on the final file, rather than on the received
1989 	     * data.
1990 	     *
1991 	     * Note that if the file is a text file, we should read it
1992 	     * here using text mode, so its lines will be terminated the same
1993 	     * way they were transmitted.
1994 	     */
1995 	    e = CVS_FOPEN (filename, "r");
1996 	    if (e == NULL)
1997 	        error (1, errno, "could not open %s", short_pathname);
1998 
1999 	    cvs_MD5Init (&context);
2000 	    while ((len = fread (buf, 1, sizeof buf, e)) != 0)
2001 		cvs_MD5Update (&context, buf, len);
2002 	    if (ferror (e))
2003 		error (1, errno, "could not read %s", short_pathname);
2004 	    cvs_MD5Final (checksum, &context);
2005 
2006 	    fclose (e);
2007 
2008 	    stored_checksum_valid = 0;
2009 
2010 	    if (memcmp (checksum, stored_checksum, 16) != 0)
2011 	    {
2012 	        if (data->contents != UPDATE_ENTRIES_PATCH)
2013 		    error (1, 0, "checksum failure on %s",
2014 			   short_pathname);
2015 
2016 		error (0, 0,
2017 		       "checksum failure after patch to %s; will refetch",
2018 		       short_pathname);
2019 
2020 		patch_failed = 1;
2021 	    }
2022 	}
2023 
2024 	if (patch_failed)
2025 	{
2026 	    /* Save this file to retrieve later.  */
2027 	    failed_patches = (char **) xrealloc ((char *) failed_patches,
2028 						 ((failed_patches_count + 1)
2029 						  * sizeof (char *)));
2030 	    failed_patches[failed_patches_count] = xstrdup (short_pathname);
2031 	    ++failed_patches_count;
2032 
2033 	    stored_checksum_valid = 0;
2034 
2035 	    free (mode_string);
2036 	    free (buf);
2037 	    free (scratch_entries);
2038 	    free (entries_line);
2039 
2040 	    return;
2041 	}
2042 
2043         {
2044 	    int status = change_mode (filename, mode_string, 1);
2045 	    if (status != 0)
2046 		error (0, status, "cannot change mode of %s", short_pathname);
2047 	}
2048 
2049 	free (mode_string);
2050 	free (buf);
2051     }
2052 
2053     if (stored_mode != NULL)
2054     {
2055 	change_mode (filename, stored_mode, 1);
2056 	free (stored_mode);
2057 	stored_mode = NULL;
2058     }
2059 
2060     if (stored_modtime_valid)
2061     {
2062 	struct utimbuf t;
2063 
2064 	memset (&t, 0, sizeof (t));
2065 	/* There is probably little point in trying to preserved the
2066 	   actime (or is there? What about Checked-in?).  */
2067 	t.modtime = t.actime = stored_modtime;
2068 
2069 #ifdef UTIME_EXPECTS_WRITABLE
2070 	if (!iswritable (filename))
2071 	{
2072 	    xchmod (filename, 1);
2073 	    change_it_back = 1;
2074 	}
2075 #endif  /* UTIME_EXPECTS_WRITABLE  */
2076 
2077 	if (utime (filename, &t) < 0)
2078 	    error (0, errno, "cannot set time on %s", filename);
2079 
2080 #ifdef UTIME_EXPECTS_WRITABLE
2081 	if (change_it_back == 1)
2082 	{
2083 	    xchmod (filename, 0);
2084 	    change_it_back = 0;
2085 	}
2086 #endif  /*  UTIME_EXPECTS_WRITABLE  */
2087 
2088 	stored_modtime_valid = 0;
2089     }
2090 
2091     /*
2092      * Process the entries line.  Do this after we've written the file,
2093      * since we need the timestamp.
2094      */
2095     if (strcmp (command_name, "export") != 0)
2096     {
2097 	char *local_timestamp;
2098 	char *file_timestamp;
2099 
2100 	(void) time (&last_register_time);
2101 
2102 	local_timestamp = data->timestamp;
2103 	if (local_timestamp == NULL || ts[0] == '+')
2104 	    file_timestamp = time_stamp (filename);
2105 	else
2106 	    file_timestamp = NULL;
2107 
2108 	/*
2109 	 * These special version numbers signify that it is not up to
2110 	 * date.  Create a dummy timestamp which will never compare
2111 	 * equal to the timestamp of the file.
2112 	 */
2113 	if (vn[0] == '\0' || vn[0] == '0' || vn[0] == '-')
2114 	    local_timestamp = "dummy timestamp";
2115 	else if (local_timestamp == NULL)
2116 	{
2117 	    local_timestamp = file_timestamp;
2118 
2119 	    /* Checking for command_name of "commit" doesn't seem like
2120 	       the cleanest way to handle this, but it seem to roughly
2121 	       parallel what the :local: code which calls
2122 	       mark_up_to_date ends up amounting to.  Some day, should
2123 	       think more about what the Checked-in response means
2124 	       vis-a-vis both Entries and Base and clarify
2125 	       cvsclient.texi accordingly.  */
2126 
2127 	    if (!strcmp (command_name, "commit"))
2128 		mark_up_to_date (filename);
2129 	}
2130 
2131 	Register (ent_list, filename, vn, local_timestamp,
2132 		  options, tag, date, ts[0] == '+' ? file_timestamp : NULL);
2133 
2134 	if (file_timestamp)
2135 	    free (file_timestamp);
2136 
2137     }
2138     free (scratch_entries);
2139     free (entries_line);
2140 }
2141 
2142 static void
2143 handle_checked_in (args, len)
2144     char *args;
2145     int len;
2146 {
2147     struct update_entries_data dat;
2148     dat.contents = UPDATE_ENTRIES_CHECKIN;
2149     dat.existp = UPDATE_ENTRIES_EXISTING_OR_NEW;
2150     dat.timestamp = NULL;
2151     call_in_directory (args, update_entries, (char *)&dat);
2152 }
2153 
2154 static void
2155 handle_new_entry (args, len)
2156     char *args;
2157     int len;
2158 {
2159     struct update_entries_data dat;
2160     dat.contents = UPDATE_ENTRIES_CHECKIN;
2161     dat.existp = UPDATE_ENTRIES_EXISTING_OR_NEW;
2162     dat.timestamp = "dummy timestamp from new-entry";
2163     call_in_directory (args, update_entries, (char *)&dat);
2164 }
2165 
2166 static void
2167 handle_updated (args, len)
2168     char *args;
2169     int len;
2170 {
2171     struct update_entries_data dat;
2172     dat.contents = UPDATE_ENTRIES_UPDATE;
2173     dat.existp = UPDATE_ENTRIES_EXISTING_OR_NEW;
2174     dat.timestamp = NULL;
2175     call_in_directory (args, update_entries, (char *)&dat);
2176 }
2177 
2178 static void handle_created PROTO((char *, int));
2179 
2180 static void
2181 handle_created (args, len)
2182     char *args;
2183     int len;
2184 {
2185     struct update_entries_data dat;
2186     dat.contents = UPDATE_ENTRIES_UPDATE;
2187     dat.existp = UPDATE_ENTRIES_NEW;
2188     dat.timestamp = NULL;
2189     call_in_directory (args, update_entries, (char *)&dat);
2190 }
2191 
2192 static void handle_update_existing PROTO((char *, int));
2193 
2194 static void
2195 handle_update_existing (args, len)
2196     char *args;
2197     int len;
2198 {
2199     struct update_entries_data dat;
2200     dat.contents = UPDATE_ENTRIES_UPDATE;
2201     dat.existp = UPDATE_ENTRIES_EXISTING;
2202     dat.timestamp = NULL;
2203     call_in_directory (args, update_entries, (char *)&dat);
2204 }
2205 
2206 static void
2207 handle_merged (args, len)
2208     char *args;
2209     int len;
2210 {
2211     struct update_entries_data dat;
2212     dat.contents = UPDATE_ENTRIES_UPDATE;
2213     /* Think this could be UPDATE_ENTRIES_EXISTING, but just in case...  */
2214     dat.existp = UPDATE_ENTRIES_EXISTING_OR_NEW;
2215     dat.timestamp = "Result of merge";
2216     call_in_directory (args, update_entries, (char *)&dat);
2217 }
2218 
2219 static void
2220 handle_patched (args, len)
2221      char *args;
2222      int len;
2223 {
2224     struct update_entries_data dat;
2225     dat.contents = UPDATE_ENTRIES_PATCH;
2226     /* Think this could be UPDATE_ENTRIES_EXISTING, but just in case...  */
2227     dat.existp = UPDATE_ENTRIES_EXISTING_OR_NEW;
2228     dat.timestamp = NULL;
2229     call_in_directory (args, update_entries, (char *)&dat);
2230 }
2231 
2232 static void
2233 handle_rcs_diff (args, len)
2234      char *args;
2235      int len;
2236 {
2237     struct update_entries_data dat;
2238     dat.contents = UPDATE_ENTRIES_RCS_DIFF;
2239     /* Think this could be UPDATE_ENTRIES_EXISTING, but just in case...  */
2240     dat.existp = UPDATE_ENTRIES_EXISTING_OR_NEW;
2241     dat.timestamp = NULL;
2242     call_in_directory (args, update_entries, (char *)&dat);
2243 }
2244 
2245 static void
2246 remove_entry (data, ent_list, short_pathname, filename)
2247     char *data;
2248     List *ent_list;
2249     char *short_pathname;
2250     char *filename;
2251 {
2252     Scratch_Entry (ent_list, filename);
2253 }
2254 
2255 static void
2256 handle_remove_entry (args, len)
2257     char *args;
2258     int len;
2259 {
2260     call_in_directory (args, remove_entry, (char *)NULL);
2261 }
2262 
2263 static void
2264 remove_entry_and_file (data, ent_list, short_pathname, filename)
2265     char *data;
2266     List *ent_list;
2267     char *short_pathname;
2268     char *filename;
2269 {
2270     Scratch_Entry (ent_list, filename);
2271     /* Note that we don't ignore existence_error's here.  The server
2272        should be sending Remove-entry rather than Removed in cases
2273        where the file does not exist.  And if the user removes the
2274        file halfway through a cvs command, we should be printing an
2275        error.  */
2276     if (unlink_file (filename) < 0)
2277 	error (0, errno, "unable to remove %s", short_pathname);
2278 }
2279 
2280 static void
2281 handle_removed (args, len)
2282     char *args;
2283     int len;
2284 {
2285     call_in_directory (args, remove_entry_and_file, (char *)NULL);
2286 }
2287 
2288 /* Is this the top level (directory containing CVSROOT)?  */
2289 static int
2290 is_cvsroot_level (pathname)
2291     char *pathname;
2292 {
2293     if (strcmp (toplevel_repos, CVSroot_directory) != 0)
2294 	return 0;
2295 
2296     return strchr (pathname, '/') == NULL;
2297 }
2298 
2299 static void
2300 set_static (data, ent_list, short_pathname, filename)
2301     char *data;
2302     List *ent_list;
2303     char *short_pathname;
2304     char *filename;
2305 {
2306     FILE *fp;
2307     fp = open_file (CVSADM_ENTSTAT, "w+");
2308     if (fclose (fp) == EOF)
2309         error (1, errno, "cannot close %s", CVSADM_ENTSTAT);
2310 }
2311 
2312 static void
2313 handle_set_static_directory (args, len)
2314     char *args;
2315     int len;
2316 {
2317     if (strcmp (command_name, "export") == 0)
2318     {
2319 	/* Swallow the repository.  */
2320 	read_line (NULL);
2321 	return;
2322     }
2323     call_in_directory (args, set_static, (char *)NULL);
2324 }
2325 
2326 static void
2327 clear_static (data, ent_list, short_pathname, filename)
2328     char *data;
2329     List *ent_list;
2330     char *short_pathname;
2331     char *filename;
2332 {
2333     if (unlink_file (CVSADM_ENTSTAT) < 0 && ! existence_error (errno))
2334         error (1, errno, "cannot remove file %s", CVSADM_ENTSTAT);
2335 }
2336 
2337 static void
2338 handle_clear_static_directory (pathname, len)
2339     char *pathname;
2340     int len;
2341 {
2342     if (strcmp (command_name, "export") == 0)
2343     {
2344 	/* Swallow the repository.  */
2345 	read_line (NULL);
2346 	return;
2347     }
2348 
2349     if (is_cvsroot_level (pathname))
2350     {
2351         /*
2352 	 * Top level (directory containing CVSROOT).  This seems to normally
2353 	 * lack a CVS directory, so don't try to create files in it.
2354 	 */
2355 	return;
2356     }
2357     call_in_directory (pathname, clear_static, (char *)NULL);
2358 }
2359 
2360 static void
2361 set_sticky (data, ent_list, short_pathname, filename)
2362     char *data;
2363     List *ent_list;
2364     char *short_pathname;
2365     char *filename;
2366 {
2367     char *tagspec;
2368     FILE *f;
2369 
2370     read_line (&tagspec);
2371 
2372     /* FIXME-update-dir: error messages should include the directory.  */
2373     f = CVS_FOPEN (CVSADM_TAG, "w+");
2374     if (f == NULL)
2375     {
2376 	/* Making this non-fatal is a bit of a kludge (see dirs2
2377 	   in testsuite).  A better solution would be to avoid having
2378 	   the server tell us about a directory we shouldn't be doing
2379 	   anything with anyway (e.g. by handling directory
2380 	   addition/removal better).  */
2381 	error (0, errno, "cannot open %s", CVSADM_TAG);
2382 	free (tagspec);
2383 	return;
2384     }
2385     if (fprintf (f, "%s\n", tagspec) < 0)
2386 	error (1, errno, "writing %s", CVSADM_TAG);
2387     if (fclose (f) == EOF)
2388 	error (1, errno, "closing %s", CVSADM_TAG);
2389     free (tagspec);
2390 }
2391 
2392 static void
2393 handle_set_sticky (pathname, len)
2394     char *pathname;
2395     int len;
2396 {
2397     if (strcmp (command_name, "export") == 0)
2398     {
2399 	/* Swallow the repository.  */
2400 	read_line (NULL);
2401         /* Swallow the tag line.  */
2402 	read_line (NULL);
2403 	return;
2404     }
2405     if (is_cvsroot_level (pathname))
2406     {
2407         /*
2408 	 * Top level (directory containing CVSROOT).  This seems to normally
2409 	 * lack a CVS directory, so don't try to create files in it.
2410 	 */
2411 
2412 	/* Swallow the repository.  */
2413 	read_line (NULL);
2414         /* Swallow the tag line.  */
2415 	read_line (NULL);
2416 	return;
2417     }
2418 
2419     call_in_directory (pathname, set_sticky, (char *)NULL);
2420 }
2421 
2422 static void
2423 clear_sticky (data, ent_list, short_pathname, filename)
2424     char *data;
2425     List *ent_list;
2426     char *short_pathname;
2427     char *filename;
2428 {
2429     if (unlink_file (CVSADM_TAG) < 0 && ! existence_error (errno))
2430 	error (1, errno, "cannot remove %s", CVSADM_TAG);
2431 }
2432 
2433 static void
2434 handle_clear_sticky (pathname, len)
2435     char *pathname;
2436     int len;
2437 {
2438     if (strcmp (command_name, "export") == 0)
2439     {
2440 	/* Swallow the repository.  */
2441 	read_line (NULL);
2442 	return;
2443     }
2444 
2445     if (is_cvsroot_level (pathname))
2446     {
2447         /*
2448 	 * Top level (directory containing CVSROOT).  This seems to normally
2449 	 * lack a CVS directory, so don't try to create files in it.
2450 	 */
2451 	return;
2452     }
2453 
2454     call_in_directory (pathname, clear_sticky, (char *)NULL);
2455 }
2456 
2457 
2458 static void template PROTO ((char *, List *, char *, char *));
2459 
2460 static void
2461 template (data, ent_list, short_pathname, filename)
2462     char *data;
2463     List *ent_list;
2464     char *short_pathname;
2465     char *filename;
2466 {
2467     /* FIXME: should be computing second argument from CVSADM_TEMPLATE
2468        and short_pathname.  */
2469     read_counted_file (CVSADM_TEMPLATE, "<CVS/Template file>");
2470 }
2471 
2472 static void handle_template PROTO ((char *, int));
2473 
2474 static void
2475 handle_template (pathname, len)
2476     char *pathname;
2477     int len;
2478 {
2479     call_in_directory (pathname, template, NULL);
2480 }
2481 
2482 
2483 struct save_prog {
2484     char *name;
2485     char *dir;
2486     struct save_prog *next;
2487 };
2488 
2489 static struct save_prog *checkin_progs;
2490 static struct save_prog *update_progs;
2491 
2492 /*
2493  * Unlike some responses this doesn't include the repository.  So we can't
2494  * just call call_in_directory and have the right thing happen; we save up
2495  * the requests and do them at the end.
2496  */
2497 static void
2498 handle_set_checkin_prog (args, len)
2499     char *args;
2500     int len;
2501 {
2502     char *prog;
2503     struct save_prog *p;
2504 
2505     read_line (&prog);
2506     if (strcmp (command_name, "export") == 0)
2507 	return;
2508 
2509     p = (struct save_prog *) xmalloc (sizeof (struct save_prog));
2510     p->next = checkin_progs;
2511     p->dir = xstrdup (args);
2512     p->name = prog;
2513     checkin_progs = p;
2514 }
2515 
2516 static void
2517 handle_set_update_prog (args, len)
2518     char *args;
2519     int len;
2520 {
2521     char *prog;
2522     struct save_prog *p;
2523 
2524     read_line (&prog);
2525     if (strcmp (command_name, "export") == 0)
2526 	return;
2527 
2528     p = (struct save_prog *) xmalloc (sizeof (struct save_prog));
2529     p->next = update_progs;
2530     p->dir = xstrdup (args);
2531     p->name = prog;
2532     update_progs = p;
2533 }
2534 
2535 static void do_deferred_progs PROTO((void));
2536 
2537 static void
2538 do_deferred_progs ()
2539 {
2540     struct save_prog *p;
2541     struct save_prog *q;
2542 
2543     char *fname;
2544     FILE *f;
2545 
2546     if (toplevel_wd != NULL)
2547     {
2548 	if (CVS_CHDIR (toplevel_wd) < 0)
2549 	    error (1, errno, "could not chdir to %s", toplevel_wd);
2550     }
2551     for (p = checkin_progs; p != NULL; )
2552     {
2553 	fname = xmalloc (strlen (p->dir) + sizeof CVSADM_CIPROG + 10);
2554 	sprintf (fname, "%s/%s", p->dir, CVSADM_CIPROG);
2555 	f = open_file (fname, "w");
2556 	if (fprintf (f, "%s\n", p->name) < 0)
2557 	    error (1, errno, "writing %s", fname);
2558 	if (fclose (f) == EOF)
2559 	    error (1, errno, "closing %s", fname);
2560 	free (p->name);
2561 	free (p->dir);
2562 	q = p->next;
2563 	free (p);
2564 	p = q;
2565 	free (fname);
2566     }
2567     checkin_progs = NULL;
2568     for (p = update_progs; p != NULL; )
2569     {
2570 	fname = xmalloc (strlen (p->dir) + sizeof CVSADM_UPROG + 10);
2571 	sprintf (fname, "%s/%s", p->dir, CVSADM_UPROG);
2572 	f = open_file (fname, "w");
2573 	if (fprintf (f, "%s\n", p->name) < 0)
2574 	    error (1, errno, "writing %s", fname);
2575 	if (fclose (f) == EOF)
2576 	    error (1, errno, "closing %s", fname);
2577 	free (p->name);
2578 	free (p->dir);
2579 	q = p->next;
2580 	free (p);
2581 	p = q;
2582 	free (fname);
2583     }
2584     update_progs = NULL;
2585 }
2586 
2587 struct save_dir {
2588     char *dir;
2589     struct save_dir *next;
2590 };
2591 
2592 struct save_dir *prune_candidates;
2593 
2594 static void
2595 add_prune_candidate (dir)
2596     char *dir;
2597 {
2598     struct save_dir *p;
2599 
2600     if ((dir[0] == '.' && dir[1] == '\0')
2601 	|| (prune_candidates != NULL
2602 	    && strcmp (dir, prune_candidates->dir) == 0))
2603 	return;
2604     p = (struct save_dir *) xmalloc (sizeof (struct save_dir));
2605     p->dir = xstrdup (dir);
2606     p->next = prune_candidates;
2607     prune_candidates = p;
2608 }
2609 
2610 static void process_prune_candidates PROTO((void));
2611 
2612 static void
2613 process_prune_candidates ()
2614 {
2615     struct save_dir *p;
2616     struct save_dir *q;
2617 
2618     if (toplevel_wd != NULL)
2619     {
2620 	if (CVS_CHDIR (toplevel_wd) < 0)
2621 	    error (1, errno, "could not chdir to %s", toplevel_wd);
2622     }
2623     for (p = prune_candidates; p != NULL; )
2624     {
2625 	if (isemptydir (p->dir, 1))
2626 	{
2627 	    char *b;
2628 
2629 	    if (unlink_file_dir (p->dir) < 0)
2630 		error (0, errno, "cannot remove %s", p->dir);
2631 	    b = strrchr (p->dir, '/');
2632 	    if (b == NULL)
2633 		Subdir_Deregister ((List *) NULL, (char *) NULL, p->dir);
2634 	    else
2635 	    {
2636 		*b = '\0';
2637 		Subdir_Deregister ((List *) NULL, p->dir, b + 1);
2638 	    }
2639 	}
2640 	free (p->dir);
2641 	q = p->next;
2642 	free (p);
2643 	p = q;
2644     }
2645     prune_candidates = NULL;
2646 }
2647 
2648 /* Send a Repository line.  */
2649 
2650 static char *last_repos;
2651 static char *last_update_dir;
2652 
2653 static void send_repository PROTO((char *, char *, char *));
2654 
2655 static void
2656 send_repository (dir, repos, update_dir)
2657     char *dir;
2658     char *repos;
2659     char *update_dir;
2660 {
2661     char *adm_name;
2662 
2663     /* FIXME: this is probably not the best place to check; I wish I
2664      * knew where in here's callers to really trap this bug.  To
2665      * reproduce the bug, just do this:
2666      *
2667      *       mkdir junk
2668      *       cd junk
2669      *       cvs -d some_repos update foo
2670      *
2671      * Poof, CVS seg faults and dies!  It's because it's trying to
2672      * send a NULL string to the server but dies in send_to_server.
2673      * That string was supposed to be the repository, but it doesn't
2674      * get set because there's no CVSADM dir, and somehow it's not
2675      * getting set from the -d argument either... ?
2676      */
2677     if (repos == NULL)
2678     {
2679         /* Lame error.  I want a real fix but can't stay up to track
2680            this down right now. */
2681         error (1, 0, "no repository");
2682     }
2683 
2684     if (update_dir == NULL || update_dir[0] == '\0')
2685 	update_dir = ".";
2686 
2687     if (last_repos != NULL
2688 	&& strcmp (repos, last_repos) == 0
2689 	&& last_update_dir != NULL
2690 	&& strcmp (update_dir, last_update_dir) == 0)
2691 	/* We've already sent it.  */
2692 	return;
2693 
2694     if (client_prune_dirs)
2695 	add_prune_candidate (update_dir);
2696 
2697     /* Add a directory name to the list of those sent to the
2698        server. */
2699     if (update_dir && (*update_dir != '\0')
2700 	&& (strcmp (update_dir, ".") != 0)
2701 	&& (findnode (dirs_sent_to_server, update_dir) == NULL))
2702     {
2703 	Node *n;
2704 	n = getnode ();
2705 	n->type = NT_UNKNOWN;
2706 	n->key = xstrdup (update_dir);
2707 	n->data = NULL;
2708 
2709 	if (addnode (dirs_sent_to_server, n))
2710 	    error (1, 0, "cannot add directory %s to list", n->key);
2711     }
2712 
2713     /* 80 is large enough for any of CVSADM_*.  */
2714     adm_name = xmalloc (strlen (dir) + 80);
2715 
2716     send_to_server ("Directory ", 0);
2717     {
2718 	/* Send the directory name.  I know that this
2719 	   sort of duplicates code elsewhere, but each
2720 	   case seems slightly different...  */
2721 	char buf[1];
2722 	char *p = update_dir;
2723 	while (*p != '\0')
2724 	{
2725 	    assert (*p != '\012');
2726 	    if (ISDIRSEP (*p))
2727 	    {
2728 		buf[0] = '/';
2729 		send_to_server (buf, 1);
2730 	    }
2731 	    else
2732 	    {
2733 		buf[0] = *p;
2734 		send_to_server (buf, 1);
2735 	    }
2736 	    ++p;
2737 	}
2738     }
2739     send_to_server ("\012", 1);
2740     send_to_server (repos, 0);
2741     send_to_server ("\012", 1);
2742 
2743     if (supported_request ("Static-directory"))
2744     {
2745 	adm_name[0] = '\0';
2746 	if (dir[0] != '\0')
2747 	{
2748 	    strcat (adm_name, dir);
2749 	    strcat (adm_name, "/");
2750 	}
2751 	strcat (adm_name, CVSADM_ENTSTAT);
2752 	if (isreadable (adm_name))
2753 	{
2754 	    send_to_server ("Static-directory\012", 0);
2755 	}
2756     }
2757     if (supported_request ("Sticky"))
2758     {
2759 	FILE *f;
2760 	if (dir[0] == '\0')
2761 	    strcpy (adm_name, CVSADM_TAG);
2762 	else
2763 	    sprintf (adm_name, "%s/%s", dir, CVSADM_TAG);
2764 
2765 	f = CVS_FOPEN (adm_name, "r");
2766 	if (f == NULL)
2767 	{
2768 	    if (! existence_error (errno))
2769 		error (1, errno, "reading %s", adm_name);
2770 	}
2771 	else
2772 	{
2773 	    char line[80];
2774 	    char *nl = NULL;
2775 	    send_to_server ("Sticky ", 0);
2776 	    while (fgets (line, sizeof (line), f) != NULL)
2777 	    {
2778 		send_to_server (line, 0);
2779 		nl = strchr (line, '\n');
2780 		if (nl != NULL)
2781 		    break;
2782 	    }
2783 	    if (nl == NULL)
2784                 send_to_server ("\012", 1);
2785 	    if (fclose (f) == EOF)
2786 		error (0, errno, "closing %s", adm_name);
2787 	}
2788     }
2789     if (supported_request ("Checkin-prog"))
2790     {
2791 	FILE *f;
2792 	if (dir[0] == '\0')
2793 	    strcpy (adm_name, CVSADM_CIPROG);
2794 	else
2795 	    sprintf (adm_name, "%s/%s", dir, CVSADM_CIPROG);
2796 
2797 	f = CVS_FOPEN (adm_name, "r");
2798 	if (f == NULL)
2799 	{
2800 	    if (! existence_error (errno))
2801 		error (1, errno, "reading %s", adm_name);
2802 	}
2803 	else
2804 	{
2805 	    char line[80];
2806 	    char *nl = NULL;
2807 
2808 	    send_to_server ("Checkin-prog ", 0);
2809 
2810 	    while (fgets (line, sizeof (line), f) != NULL)
2811 	    {
2812 		send_to_server (line, 0);
2813 
2814 		nl = strchr (line, '\n');
2815 		if (nl != NULL)
2816 		    break;
2817 	    }
2818 	    if (nl == NULL)
2819 		send_to_server ("\012", 1);
2820 	    if (fclose (f) == EOF)
2821 		error (0, errno, "closing %s", adm_name);
2822 	}
2823     }
2824     if (supported_request ("Update-prog"))
2825     {
2826 	FILE *f;
2827 	if (dir[0] == '\0')
2828 	    strcpy (adm_name, CVSADM_UPROG);
2829 	else
2830 	    sprintf (adm_name, "%s/%s", dir, CVSADM_UPROG);
2831 
2832 	f = CVS_FOPEN (adm_name, "r");
2833 	if (f == NULL)
2834 	{
2835 	    if (! existence_error (errno))
2836 		error (1, errno, "reading %s", adm_name);
2837 	}
2838 	else
2839 	{
2840 	    char line[80];
2841 	    char *nl = NULL;
2842 
2843 	    send_to_server ("Update-prog ", 0);
2844 
2845 	    while (fgets (line, sizeof (line), f) != NULL)
2846 	    {
2847 		send_to_server (line, 0);
2848 
2849 		nl = strchr (line, '\n');
2850 		if (nl != NULL)
2851 		    break;
2852 	    }
2853 	    if (nl == NULL)
2854 		send_to_server ("\012", 1);
2855 	    if (fclose (f) == EOF)
2856 		error (0, errno, "closing %s", adm_name);
2857 	}
2858     }
2859     free (adm_name);
2860     if (last_repos != NULL)
2861 	free (last_repos);
2862     if (last_update_dir != NULL)
2863 	free (last_update_dir);
2864     last_repos = xstrdup (repos);
2865     last_update_dir = xstrdup (update_dir);
2866 }
2867 
2868 /* Send a Repository line and set toplevel_repos.  */
2869 
2870 void
2871 send_a_repository (dir, repository, update_dir)
2872     char *dir;
2873     char *repository;
2874     char *update_dir;
2875 {
2876     if (toplevel_repos == NULL && repository != NULL)
2877     {
2878 	if (update_dir[0] == '\0'
2879 	    || (update_dir[0] == '.' && update_dir[1] == '\0'))
2880 	    toplevel_repos = xstrdup (repository);
2881 	else
2882 	{
2883 	    /*
2884 	     * Get the repository from a CVS/Repository file if update_dir
2885 	     * is absolute.  This is not correct in general, because
2886 	     * the CVS/Repository file might not be the top-level one.
2887 	     * This is for cases like "cvs update /foo/bar" (I'm not
2888 	     * sure it matters what toplevel_repos we get, but it does
2889 	     * matter that we don't hit the "internal error" code below).
2890 	     */
2891 	    if (update_dir[0] == '/')
2892 		toplevel_repos = Name_Repository (update_dir, update_dir);
2893 	    else
2894 	    {
2895 		/*
2896 		 * Guess the repository of that directory by looking at a
2897 		 * subdirectory and removing as many pathname components
2898 		 * as are in update_dir.  I think that will always (or at
2899 		 * least almost always) be 1.
2900 		 *
2901 		 * So this deals with directories which have been
2902 		 * renamed, though it doesn't necessarily deal with
2903 		 * directories which have been put inside other
2904 		 * directories (and cvs invoked on the containing
2905 		 * directory).  I'm not sure the latter case needs to
2906 		 * work.
2907 		 *
2908 		 * 21 Aug 1998: Well, Mr. Above-Comment-Writer, it
2909 		 * does need to work after all.  When we are using the
2910 		 * client in a multi-cvsroot environment, it will be
2911 		 * fairly common that we have the above case (e.g.,
2912 		 * cwd checked out from one repository but
2913 		 * subdirectory checked out from another).  We can't
2914 		 * assume that by walking up a directory in our wd we
2915 		 * necessarily walk up a directory in the repository.
2916 		 */
2917 		/*
2918 		 * This gets toplevel_repos wrong for "cvs update ../foo"
2919 		 * but I'm not sure toplevel_repos matters in that case.
2920 		 */
2921 
2922 		int repository_len, update_dir_len;
2923 
2924 		strip_trailing_slashes (update_dir);
2925 
2926 		repository_len = strlen (repository);
2927 		update_dir_len = strlen (update_dir);
2928 
2929 		/* Try to remove the path components in UPDATE_DIR
2930                    from REPOSITORY.  If the path elements don't exist
2931                    in REPOSITORY, or the removal of those path
2932                    elements mean that we "step above"
2933                    CVSroot_directory, set toplevel_repos to
2934                    CVSroot_directory. */
2935 		if ((repository_len > update_dir_len)
2936 		    && (strcmp (repository + repository_len - update_dir_len,
2937 				update_dir) == 0)
2938 		    /* TOPLEVEL_REPOS shouldn't be above CVSroot_directory */
2939 		    && ((repository_len - update_dir_len)
2940 			> strlen (CVSroot_directory)))
2941 		{
2942 		    /* The repository name contains UPDATE_DIR.  Set
2943                        toplevel_repos to the repository name without
2944                        UPDATE_DIR. */
2945 
2946 		    toplevel_repos = xmalloc (repository_len - update_dir_len);
2947 		    /* Note that we don't copy the trailing '/'.  */
2948 		    strncpy (toplevel_repos, repository,
2949 			     repository_len - update_dir_len - 1);
2950 		    toplevel_repos[repository_len - update_dir_len - 1] = '\0';
2951 		}
2952 		else
2953 		{
2954 		    toplevel_repos = xstrdup (CVSroot_directory);
2955 		}
2956 	    }
2957 	}
2958     }
2959 
2960     send_repository (dir, repository, update_dir);
2961 }
2962 
2963 /* The "expanded" modules.  */
2964 static int modules_count;
2965 static int modules_allocated;
2966 static char **modules_vector;
2967 
2968 static void
2969 handle_module_expansion (args, len)
2970     char *args;
2971     int len;
2972 {
2973     if (modules_vector == NULL)
2974     {
2975 	modules_allocated = 1; /* Small for testing */
2976 	modules_vector = (char **) xmalloc
2977 	  (modules_allocated * sizeof (modules_vector[0]));
2978     }
2979     else if (modules_count >= modules_allocated)
2980     {
2981 	modules_allocated *= 2;
2982 	modules_vector = (char **) xrealloc
2983 	  ((char *) modules_vector,
2984 	   modules_allocated * sizeof (modules_vector[0]));
2985     }
2986     modules_vector[modules_count] = xmalloc (strlen (args) + 1);
2987     strcpy (modules_vector[modules_count], args);
2988     ++modules_count;
2989 }
2990 
2991 /* Original, not "expanded" modules.  */
2992 static int module_argc;
2993 static char **module_argv;
2994 
2995 void
2996 client_expand_modules (argc, argv, local)
2997     int argc;
2998     char **argv;
2999     int local;
3000 {
3001     int errs;
3002     int i;
3003 
3004     module_argc = argc;
3005     module_argv = (char **) xmalloc ((argc + 1) * sizeof (module_argv[0]));
3006     for (i = 0; i < argc; ++i)
3007 	module_argv[i] = xstrdup (argv[i]);
3008     module_argv[argc] = NULL;
3009 
3010     for (i = 0; i < argc; ++i)
3011 	send_arg (argv[i]);
3012     send_a_repository ("", CVSroot_directory, "");
3013 
3014     send_to_server ("expand-modules\012", 0);
3015 
3016     errs = get_server_responses ();
3017     if (last_repos != NULL)
3018         free (last_repos);
3019     last_repos = NULL;
3020     if (last_update_dir != NULL)
3021         free (last_update_dir);
3022     last_update_dir = NULL;
3023     if (errs)
3024 	error (errs, 0, "cannot expand modules");
3025 }
3026 
3027 void
3028 client_send_expansions (local, where, build_dirs)
3029     int local;
3030     char *where;
3031     int build_dirs;
3032 {
3033     int i;
3034     char *argv[1];
3035 
3036     /* Send the original module names.  The "expanded" module name might
3037        not be suitable as an argument to a co request (e.g. it might be
3038        the result of a -d argument in the modules file).  It might be
3039        cleaner if we genuinely expanded module names, all the way to a
3040        local directory and repository, but that isn't the way it works
3041        now.  */
3042     send_file_names (module_argc, module_argv, 0);
3043 
3044     for (i = 0; i < modules_count; ++i)
3045     {
3046 	argv[0] = where ? where : modules_vector[i];
3047 	if (isfile (argv[0]))
3048 	    send_files (1, argv, local, 0, build_dirs ? SEND_BUILD_DIRS : 0);
3049     }
3050     send_a_repository ("", CVSroot_directory, "");
3051 }
3052 
3053 void
3054 client_nonexpanded_setup ()
3055 {
3056     send_a_repository ("", CVSroot_directory, "");
3057 }
3058 
3059 /* Receive a cvswrappers line from the server; it must be a line
3060    containing an RCS option (e.g., "*.exe   -k 'b'").
3061 
3062    Note that this doesn't try to handle -t/-f options (which are a
3063    whole separate issue which noone has thought much about, as far
3064    as I know).
3065 
3066    We need to know the keyword expansion mode so we know whether to
3067    read the file in text or binary mode.  */
3068 
3069 static void
3070 handle_wrapper_rcs_option (args, len)
3071     char *args;
3072     int len;
3073 {
3074     char *p;
3075 
3076     /* Enforce the notes in cvsclient.texi about how the response is not
3077        as free-form as it looks.  */
3078     p = strchr (args, ' ');
3079     if (p == NULL)
3080 	goto handle_error;
3081     if (*++p != '-'
3082 	|| *++p != 'k'
3083 	|| *++p != ' '
3084 	|| *++p != '\'')
3085 	goto handle_error;
3086     if (strchr (p, '\'') == NULL)
3087 	goto handle_error;
3088 
3089     /* Add server-side cvswrappers line to our wrapper list. */
3090     wrap_add (args, 0);
3091     return;
3092  handle_error:
3093     error (0, errno, "protocol error: ignoring invalid wrappers %s", args);
3094 }
3095 
3096 
3097 static void
3098 handle_m (args, len)
3099     char *args;
3100     int len;
3101 {
3102     /* In the case where stdout and stderr point to the same place,
3103        fflushing stderr will make output happen in the correct order.
3104        Often stderr will be line-buffered and this won't be needed,
3105        but not always (is that true?  I think the comment is probably
3106        based on being confused between default buffering between
3107        stdout and stderr.  But I'm not sure).  */
3108     fflush (stderr);
3109     fwrite (args, len, sizeof (*args), stdout);
3110     putc ('\n', stdout);
3111 }
3112 
3113 static void handle_mbinary PROTO ((char *, int));
3114 
3115 static void
3116 handle_mbinary (args, len)
3117     char *args;
3118     int len;
3119 {
3120     char *size_string;
3121     size_t size;
3122     size_t totalread;
3123     size_t nread;
3124     size_t toread;
3125     char buf[8192];
3126 
3127     /* See comment at handle_m about (non)flush of stderr.  */
3128 
3129     /* Get the size.  */
3130     read_line (&size_string);
3131     size = atoi (size_string);
3132     free (size_string);
3133 
3134     /* OK, now get all the data.  The algorithm here is that we read
3135        as much as the network wants to give us in
3136        try_read_from_server, and then we output it all, and then
3137        repeat, until we get all the data.  */
3138     totalread = 0;
3139     while (totalread < size)
3140     {
3141 	toread = size - totalread;
3142 	if (toread > sizeof buf)
3143 	    toread = sizeof buf;
3144 
3145 	nread = try_read_from_server (buf, toread);
3146 	cvs_output_binary (buf, nread);
3147 	totalread += nread;
3148     }
3149 }
3150 
3151 static void
3152 handle_e (args, len)
3153     char *args;
3154     int len;
3155 {
3156     /* In the case where stdout and stderr point to the same place,
3157        fflushing stdout will make output happen in the correct order.  */
3158     fflush (stdout);
3159     fwrite (args, len, sizeof (*args), stderr);
3160     putc ('\n', stderr);
3161 }
3162 
3163 /*ARGSUSED*/
3164 static void
3165 handle_f (args, len)
3166     char *args;
3167     int len;
3168 {
3169     fflush (stderr);
3170 }
3171 
3172 static void handle_mt PROTO ((char *, int));
3173 
3174 static void
3175 handle_mt (args, len)
3176     char *args;
3177     int len;
3178 {
3179     char *p;
3180     char *tag = args;
3181     char *text;
3182 
3183     /* See comment at handle_m for more details.  */
3184     fflush (stderr);
3185 
3186     p = strchr (args, ' ');
3187     if (p == NULL)
3188 	text = NULL;
3189     else
3190     {
3191 	*p++ = '\0';
3192 	text = p;
3193     }
3194 
3195     switch (tag[0])
3196     {
3197 	case '+':
3198 	    if (strcmp (tag, "+updated") == 0)
3199 		updated_seen = 1;
3200 	    else if (strcmp (tag, "+importmergecmd") == 0)
3201 		importmergecmd.seen = 1;
3202 	    break;
3203 	case '-':
3204 	    if (strcmp (tag, "-updated") == 0)
3205 		updated_seen = 0;
3206 	    else if (strcmp (tag, "-importmergecmd") == 0)
3207 	    {
3208 		char buf[80];
3209 
3210 		/* Now that we have gathered the information, we can
3211                    output the suggested merge command.  */
3212 
3213 		if (importmergecmd.conflicts == 0
3214 		    || importmergecmd.mergetag1 == NULL
3215 		    || importmergecmd.mergetag2 == NULL
3216 		    || importmergecmd.repository == NULL)
3217 		{
3218 		    error (0, 0,
3219 			   "invalid server: incomplete importmergecmd tags");
3220 		    break;
3221 		}
3222 
3223 		sprintf (buf, "\n%d conflicts created by this import.\n",
3224 			 importmergecmd.conflicts);
3225 		cvs_output (buf, 0);
3226 		cvs_output ("Use the following command to help the merge:\n\n",
3227 			    0);
3228 		cvs_output ("\t", 1);
3229 		cvs_output (program_name, 0);
3230 		if (CVSroot_cmdline != NULL)
3231 		{
3232 		    cvs_output (" -d ", 0);
3233 		    cvs_output (CVSroot_cmdline, 0);
3234 		}
3235 		cvs_output (" checkout -j", 0);
3236 		cvs_output (importmergecmd.mergetag1, 0);
3237 		cvs_output (" -j", 0);
3238 		cvs_output (importmergecmd.mergetag2, 0);
3239 		cvs_output (" ", 1);
3240 		cvs_output (importmergecmd.repository, 0);
3241 		cvs_output ("\n\n", 0);
3242 
3243 		/* Clear the static variables so that everything is
3244                    ready for any subsequent importmergecmd tag.  */
3245 		importmergecmd.conflicts = 0;
3246 		free (importmergecmd.mergetag1);
3247 		importmergecmd.mergetag1 = NULL;
3248 		free (importmergecmd.mergetag2);
3249 		importmergecmd.mergetag2 = NULL;
3250 		free (importmergecmd.repository);
3251 		importmergecmd.repository = NULL;
3252 
3253 		importmergecmd.seen = 0;
3254 	    }
3255 	    break;
3256 	default:
3257 	    if (updated_seen)
3258 	    {
3259 		if (strcmp (tag, "fname") == 0)
3260 		{
3261 		    if (updated_fname != NULL)
3262 		    {
3263 			/* Output the previous message now.  This can happen
3264 			   if there was no Update-existing or other such
3265 			   response, due to the -n global option.  */
3266 			cvs_output ("U ", 0);
3267 			cvs_output (updated_fname, 0);
3268 			cvs_output ("\n", 1);
3269 			free (updated_fname);
3270 		    }
3271 		    updated_fname = xstrdup (text);
3272 		}
3273 		/* Swallow all other tags.  Either they are extraneous
3274 		   or they reflect future extensions that we can
3275 		   safely ignore.  */
3276 	    }
3277 	    else if (importmergecmd.seen)
3278 	    {
3279 		if (strcmp (tag, "conflicts") == 0)
3280 		    importmergecmd.conflicts = atoi (text);
3281 		else if (strcmp (tag, "mergetag1") == 0)
3282 		    importmergecmd.mergetag1 = xstrdup (text);
3283 		else if (strcmp (tag, "mergetag2") == 0)
3284 		    importmergecmd.mergetag2 = xstrdup (text);
3285 		else if (strcmp (tag, "repository") == 0)
3286 		    importmergecmd.repository = xstrdup (text);
3287 		/* Swallow all other tags.  Either they are text for
3288                    which we are going to print our own version when we
3289                    see -importmergecmd, or they are future extensions
3290                    we can safely ignore.  */
3291 	    }
3292 	    else if (strcmp (tag, "newline") == 0)
3293 		printf ("\n");
3294 	    else if (text != NULL)
3295 		printf ("%s", text);
3296     }
3297 }
3298 
3299 #endif /* CLIENT_SUPPORT */
3300 #if defined(CLIENT_SUPPORT) || defined(SERVER_SUPPORT)
3301 
3302 /* This table must be writeable if the server code is included.  */
3303 struct response responses[] =
3304 {
3305 #ifdef CLIENT_SUPPORT
3306 #define RSP_LINE(n, f, t, s) {n, f, t, s}
3307 #else /* ! CLIENT_SUPPORT */
3308 #define RSP_LINE(n, f, t, s) {n, s}
3309 #endif /* CLIENT_SUPPORT */
3310 
3311     RSP_LINE("ok", handle_ok, response_type_ok, rs_essential),
3312     RSP_LINE("error", handle_error, response_type_error, rs_essential),
3313     RSP_LINE("Valid-requests", handle_valid_requests, response_type_normal,
3314        rs_essential),
3315     RSP_LINE("Checked-in", handle_checked_in, response_type_normal,
3316        rs_essential),
3317     RSP_LINE("New-entry", handle_new_entry, response_type_normal, rs_optional),
3318     RSP_LINE("Checksum", handle_checksum, response_type_normal, rs_optional),
3319     RSP_LINE("Copy-file", handle_copy_file, response_type_normal, rs_optional),
3320     RSP_LINE("Updated", handle_updated, response_type_normal, rs_essential),
3321     RSP_LINE("Created", handle_created, response_type_normal, rs_optional),
3322     RSP_LINE("Update-existing", handle_update_existing, response_type_normal,
3323        rs_optional),
3324     RSP_LINE("Merged", handle_merged, response_type_normal, rs_essential),
3325     RSP_LINE("Patched", handle_patched, response_type_normal, rs_optional),
3326     RSP_LINE("Rcs-diff", handle_rcs_diff, response_type_normal, rs_optional),
3327     RSP_LINE("Mode", handle_mode, response_type_normal, rs_optional),
3328     RSP_LINE("Mod-time", handle_mod_time, response_type_normal, rs_optional),
3329     RSP_LINE("Removed", handle_removed, response_type_normal, rs_essential),
3330     RSP_LINE("Remove-entry", handle_remove_entry, response_type_normal,
3331        rs_optional),
3332     RSP_LINE("Set-static-directory", handle_set_static_directory,
3333        response_type_normal,
3334        rs_optional),
3335     RSP_LINE("Clear-static-directory", handle_clear_static_directory,
3336        response_type_normal,
3337        rs_optional),
3338     RSP_LINE("Set-sticky", handle_set_sticky, response_type_normal,
3339        rs_optional),
3340     RSP_LINE("Clear-sticky", handle_clear_sticky, response_type_normal,
3341        rs_optional),
3342     RSP_LINE("Template", handle_template, response_type_normal,
3343        rs_optional),
3344     RSP_LINE("Set-checkin-prog", handle_set_checkin_prog, response_type_normal,
3345        rs_optional),
3346     RSP_LINE("Set-update-prog", handle_set_update_prog, response_type_normal,
3347        rs_optional),
3348     RSP_LINE("Notified", handle_notified, response_type_normal, rs_optional),
3349     RSP_LINE("Module-expansion", handle_module_expansion, response_type_normal,
3350        rs_optional),
3351     RSP_LINE("Wrapper-rcsOption", handle_wrapper_rcs_option,
3352        response_type_normal,
3353        rs_optional),
3354     RSP_LINE("M", handle_m, response_type_normal, rs_essential),
3355     RSP_LINE("Mbinary", handle_mbinary, response_type_normal, rs_optional),
3356     RSP_LINE("E", handle_e, response_type_normal, rs_essential),
3357     RSP_LINE("F", handle_f, response_type_normal, rs_optional),
3358     RSP_LINE("MT", handle_mt, response_type_normal, rs_optional),
3359     /* Possibly should be response_type_error.  */
3360     RSP_LINE(NULL, NULL, response_type_normal, rs_essential)
3361 
3362 #undef RSP_LINE
3363 };
3364 
3365 #endif /* CLIENT_SUPPORT or SERVER_SUPPORT */
3366 #ifdef CLIENT_SUPPORT
3367 
3368 /*
3369  * If LEN is 0, then send_to_server() computes string's length itself.
3370  *
3371  * Therefore, pass the real length when transmitting data that might
3372  * contain 0's.
3373  */
3374 void
3375 send_to_server (str, len)
3376      char *str;
3377      size_t len;
3378 {
3379     static int nbytes;
3380 
3381     if (len == 0)
3382 	len = strlen (str);
3383 
3384     buf_output (to_server, str, len);
3385 
3386     /* There is no reason not to send data to the server, so do it
3387        whenever we've accumulated enough information in the buffer to
3388        make it worth sending.  */
3389     nbytes += len;
3390     if (nbytes >= 2 * BUFFER_DATA_SIZE)
3391     {
3392 	int status;
3393 
3394         status = buf_send_output (to_server);
3395 	if (status != 0)
3396 	    error (1, status, "error writing to server");
3397 	nbytes = 0;
3398     }
3399 }
3400 
3401 /* Read up to LEN bytes from the server.  Returns actual number of
3402    bytes read, which will always be at least one; blocks if there is
3403    no data available at all.  Gives a fatal error on EOF or error.  */
3404 static size_t
3405 try_read_from_server (buf, len)
3406     char *buf;
3407     size_t len;
3408 {
3409     int status, nread;
3410     char *data;
3411 
3412     status = buf_read_data (from_server, len, &data, &nread);
3413     if (status != 0)
3414     {
3415 	if (status == -1)
3416 	    error (1, 0,
3417 		   "end of file from server (consult above messages if any)");
3418 	else if (status == -2)
3419 	    error (1, 0, "out of memory");
3420 	else
3421 	    error (1, status, "reading from server");
3422     }
3423 
3424     memcpy (buf, data, nread);
3425 
3426     return nread;
3427 }
3428 
3429 /*
3430  * Read LEN bytes from the server or die trying.
3431  */
3432 void
3433 read_from_server (buf, len)
3434     char *buf;
3435     size_t len;
3436 {
3437     size_t red = 0;
3438     while (red < len)
3439     {
3440 	red += try_read_from_server (buf + red, len - red);
3441 	if (red == len)
3442 	    break;
3443     }
3444 }
3445 
3446 /*
3447  * Get some server responses and process them.  Returns nonzero for
3448  * error, 0 for success.  */
3449 int
3450 get_server_responses ()
3451 {
3452     struct response *rs;
3453     do
3454     {
3455 	char *cmd;
3456 	int len;
3457 
3458 	len = read_line (&cmd);
3459 	for (rs = responses; rs->name != NULL; ++rs)
3460 	    if (strncmp (cmd, rs->name, strlen (rs->name)) == 0)
3461 	    {
3462 		int cmdlen = strlen (rs->name);
3463 		if (cmd[cmdlen] == '\0')
3464 		    ;
3465 		else if (cmd[cmdlen] == ' ')
3466 		    ++cmdlen;
3467 		else
3468 		    /*
3469 		     * The first len characters match, but it's a different
3470 		     * response.  e.g. the response is "oklahoma" but we
3471 		     * matched "ok".
3472 		     */
3473 		    continue;
3474 		(*rs->func) (cmd + cmdlen, len - cmdlen);
3475 		break;
3476 	    }
3477 	if (rs->name == NULL)
3478 	    /* It's OK to print just to the first '\0'.  */
3479 	    /* We might want to handle control characters and the like
3480 	       in some other way other than just sending them to stdout.
3481 	       One common reason for this error is if people use :ext:
3482 	       with a version of rsh which is doing CRLF translation or
3483 	       something, and so the client gets "ok^M" instead of "ok".
3484 	       Right now that will tend to print part of this error
3485 	       message over the other part of it.  It seems like we could
3486 	       do better (either in general, by quoting or omitting all
3487 	       control characters, and/or specifically, by detecting the CRLF
3488 	       case and printing a specific error message).  */
3489 	    error (0, 0,
3490 		   "warning: unrecognized response `%s' from cvs server",
3491 		   cmd);
3492 	free (cmd);
3493     } while (rs->type == response_type_normal);
3494 
3495     if (updated_fname != NULL)
3496     {
3497 	/* Output the previous message now.  This can happen
3498 	   if there was no Update-existing or other such
3499 	   response, due to the -n global option.  */
3500 	cvs_output ("U ", 0);
3501 	cvs_output (updated_fname, 0);
3502 	cvs_output ("\n", 1);
3503 	free (updated_fname);
3504 	updated_fname = NULL;
3505     }
3506 
3507     if (rs->type == response_type_error)
3508 	return 1;
3509     if (failure_exit)
3510 	return 1;
3511     return 0;
3512 }
3513 
3514 /* Get the responses and then close the connection.  */
3515 int server_fd = -1;
3516 
3517 /*
3518  * Flag var; we'll set it in start_server() and not one of its
3519  * callees, such as start_rsh_server().  This means that there might
3520  * be a small window between the starting of the server and the
3521  * setting of this var, but all the code in that window shouldn't care
3522  * because it's busy checking return values to see if the server got
3523  * started successfully anyway.
3524  */
3525 int server_started = 0;
3526 
3527 int
3528 get_responses_and_close ()
3529 {
3530     int errs = get_server_responses ();
3531     int status;
3532 
3533     if (last_entries != NULL)
3534     {
3535 	Entries_Close (last_entries);
3536 	last_entries = NULL;
3537     }
3538 
3539     do_deferred_progs ();
3540 
3541     if (client_prune_dirs)
3542 	process_prune_candidates ();
3543 
3544     /* The calls to buf_shutdown are currently only meaningful when we
3545        are using compression.  First we shut down TO_SERVER.  That
3546        tells the server that its input is finished.  It then shuts
3547        down the buffer it is sending to us, at which point our shut
3548        down of FROM_SERVER will complete.  */
3549 
3550     status = buf_shutdown (to_server);
3551     if (status != 0)
3552         error (0, status, "shutting down buffer to server");
3553     status = buf_shutdown (from_server);
3554     if (status != 0)
3555 	error (0, status, "shutting down buffer from server");
3556 
3557 #ifdef NO_SOCKET_TO_FD
3558     if (use_socket_style)
3559     {
3560 	if (shutdown (server_sock, 2) < 0)
3561 	    error (1, 0, "shutting down server socket: %s", SOCK_STRERROR (SOCK_ERRNO));
3562     }
3563     else
3564 #endif /* NO_SOCKET_TO_FD */
3565     {
3566 #if defined(HAVE_KERBEROS) || defined(AUTH_CLIENT_SUPPORT)
3567 	if (server_fd != -1)
3568 	{
3569 	    if (shutdown (server_fd, 1) < 0)
3570 		error (1, 0, "shutting down connection to %s: %s",
3571 		       CVSroot_hostname, SOCK_STRERROR (SOCK_ERRNO));
3572             /*
3573              * This test will always be true because we dup the descriptor
3574              */
3575 	    if (fileno (from_server_fp) != fileno (to_server_fp))
3576 	    {
3577 		if (fclose (to_server_fp) != 0)
3578 		    error (1, errno,
3579 			   "closing down connection to %s",
3580 			   CVSroot_hostname);
3581 	    }
3582 	}
3583         else
3584 #endif
3585 
3586 #ifdef SHUTDOWN_SERVER
3587 	    SHUTDOWN_SERVER (fileno (to_server_fp));
3588 #else /* ! SHUTDOWN_SERVER */
3589 	{
3590 
3591 #ifdef START_RSH_WITH_POPEN_RW
3592 	    if (pclose (to_server_fp) == EOF)
3593 #else /* ! START_RSH_WITH_POPEN_RW */
3594 		if (fclose (to_server_fp) == EOF)
3595 #endif /* START_RSH_WITH_POPEN_RW */
3596 		{
3597 		    error (1, errno, "closing connection to %s",
3598 			   CVSroot_hostname);
3599 		}
3600         }
3601 
3602 	if (! buf_empty_p (from_server)
3603 	    || getc (from_server_fp) != EOF)
3604 	    error (0, 0, "dying gasps from %s unexpected", CVSroot_hostname);
3605 	else if (ferror (from_server_fp))
3606 	    error (0, errno, "reading from %s", CVSroot_hostname);
3607 
3608 	fclose (from_server_fp);
3609 #endif /* SHUTDOWN_SERVER */
3610     }
3611 
3612     if (rsh_pid != -1
3613 	&& waitpid (rsh_pid, (int *) 0, 0) == -1)
3614 	error (1, errno, "waiting for process %d", rsh_pid);
3615 
3616     buf_free (to_server);
3617     buf_free (from_server);
3618     server_started = 0;
3619 
3620     /* see if we need to sleep before returning to avoid time-stamp races */
3621     if (last_register_time)
3622     {
3623 	while (time ((time_t *) NULL) == last_register_time)
3624 	    sleep (1);
3625     }
3626 
3627     return errs;
3628 }
3629 
3630 #ifndef NO_EXT_METHOD
3631 static void start_rsh_server PROTO((int *, int *));
3632 #endif
3633 
3634 int
3635 supported_request (name)
3636     char *name;
3637 {
3638     struct request *rq;
3639 
3640     for (rq = requests; rq->name; rq++)
3641 	if (!strcmp (rq->name, name))
3642 	    return (rq->flags & RQ_SUPPORTED) != 0;
3643     error (1, 0, "internal error: testing support for unknown option?");
3644     /* NOTREACHED */
3645     return 0;
3646 }
3647 
3648 
3649 #if defined (AUTH_CLIENT_SUPPORT) || defined (HAVE_KERBEROS)
3650 static struct hostent *init_sockaddr PROTO ((struct sockaddr_in *, char *,
3651 					     unsigned int));
3652 
3653 static struct hostent *
3654 init_sockaddr (name, hostname, port)
3655     struct sockaddr_in *name;
3656     char *hostname;
3657     unsigned int port;
3658 {
3659     struct hostent *hostinfo;
3660     unsigned short shortport = port;
3661 
3662     memset (name, 0, sizeof (*name));
3663     name->sin_family = AF_INET;
3664     name->sin_port = htons (shortport);
3665     hostinfo = gethostbyname (hostname);
3666     if (hostinfo == NULL)
3667     {
3668 	fprintf (stderr, "Unknown host %s.\n", hostname);
3669 	error_exit ();
3670     }
3671     name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
3672     return hostinfo;
3673 }
3674 
3675 #endif /* defined (AUTH_CLIENT_SUPPORT) || defined (HAVE_KERBEROS) */
3676 
3677 #ifdef AUTH_CLIENT_SUPPORT
3678 
3679 static int auth_server_port_number PROTO ((void));
3680 
3681 static int
3682 auth_server_port_number ()
3683 {
3684     struct servent *s = getservbyname ("cvspserver", "tcp");
3685 
3686     if (s)
3687 	return ntohs (s->s_port);
3688     else
3689 	return CVS_AUTH_PORT;
3690 }
3691 
3692 
3693 /* Read a line from socket SOCK.  Result does not include the
3694    terminating linefeed.  This is only used by the authentication
3695    protocol, which we call before we set up all the buffering stuff.
3696    It is possible it should use the buffers too, which would be faster
3697    (unlike the server, there isn't really a security issue in terms of
3698    separating authentication from the rest of the code).
3699 
3700    Space for the result is malloc'd and should be freed by the caller.
3701 
3702    Returns number of bytes read.  */
3703 static int
3704 recv_line (sock, resultp)
3705     int sock;
3706     char **resultp;
3707 {
3708     char *result;
3709     size_t input_index = 0;
3710     size_t result_size = 80;
3711 
3712     result = (char *) xmalloc (result_size);
3713 
3714     while (1)
3715     {
3716 	char ch;
3717 	int n;
3718 	n = recv (sock, &ch, 1, 0);
3719 	if (n <= 0)
3720 	    error (1, 0, "recv() from server %s: %s", CVSroot_hostname,
3721 		   n == 0 ? "EOF" : SOCK_STRERROR (SOCK_ERRNO));
3722 
3723 	if (ch == '\012')
3724 	    break;
3725 
3726 	result[input_index++] = ch;
3727 	while (input_index + 1 >= result_size)
3728 	{
3729 	    result_size *= 2;
3730 	    result = (char *) xrealloc (result, result_size);
3731 	}
3732     }
3733 
3734     if (resultp)
3735 	*resultp = result;
3736 
3737     /* Terminate it just for kicks, but we *can* deal with embedded NULs.  */
3738     result[input_index] = '\0';
3739 
3740     if (resultp == NULL)
3741 	free (result);
3742     return input_index;
3743 }
3744 
3745 /* Connect to a forked server process. */
3746 
3747 void
3748 connect_to_forked_server (tofdp, fromfdp)
3749      int *tofdp, *fromfdp;
3750 {
3751     /* This is pretty simple.  All we need to do is choose the correct
3752        cvs binary and call piped_child. */
3753 
3754     char *command[3];
3755 
3756     command[0] = getenv ("CVS_SERVER");
3757     if (! command[0])
3758 	command[0] = "cvs";
3759 
3760     command[1] = "server";
3761     command[2] = NULL;
3762 
3763     if (! piped_child (command, tofdp, fromfdp))
3764 	error (1, 0, "could not fork server process");
3765 }
3766 
3767 /* Connect to the authenticating server.
3768 
3769    If VERIFY_ONLY is non-zero, then just verify that the password is
3770    correct and then shutdown the connection.
3771 
3772    If VERIFY_ONLY is 0, then really connect to the server.
3773 
3774    If DO_GSSAPI is non-zero, then we use GSSAPI authentication rather
3775    than the pserver password authentication.
3776 
3777    If we fail to connect or if access is denied, then die with fatal
3778    error.  */
3779 void
3780 connect_to_pserver (tofdp, fromfdp, verify_only, do_gssapi)
3781      int *tofdp, *fromfdp;
3782      int verify_only;
3783      int do_gssapi;
3784 {
3785     int sock;
3786 #ifndef NO_SOCKET_TO_FD
3787     int tofd, fromfd;
3788 #endif
3789     int port_number;
3790     struct sockaddr_in client_sai;
3791     struct hostent *hostinfo;
3792     char no_passwd = 0;   /* gets set if no password found */
3793 
3794     sock = socket (AF_INET, SOCK_STREAM, 0);
3795     if (sock == -1)
3796     {
3797 	error (1, 0, "cannot create socket: %s", SOCK_STRERROR (SOCK_ERRNO));
3798     }
3799     port_number = auth_server_port_number ();
3800     hostinfo = init_sockaddr (&client_sai, CVSroot_hostname, port_number);
3801     if (connect (sock, (struct sockaddr *) &client_sai, sizeof (client_sai))
3802 	< 0)
3803 	error (1, 0, "connect to %s:%d failed: %s", CVSroot_hostname,
3804 	       port_number, SOCK_STRERROR (SOCK_ERRNO));
3805 
3806     /* Run the authorization mini-protocol before anything else. */
3807     if (do_gssapi)
3808     {
3809 #ifdef HAVE_GSSAPI
3810 	if (! connect_to_gserver (sock, hostinfo))
3811 	    goto rejected;
3812 #else
3813 	error (1, 0, "This client does not support GSSAPI authentication");
3814 #endif
3815     }
3816     else
3817     {
3818 	char *begin      = NULL;
3819 	char *repository = CVSroot_directory;
3820 	char *username   = CVSroot_username;
3821 	char *password   = NULL;
3822 	char *end        = NULL;
3823 
3824 	if (verify_only)
3825 	{
3826 	    begin = "BEGIN VERIFICATION REQUEST\012";
3827 	    end   = "END VERIFICATION REQUEST\012";
3828 	}
3829 	else
3830 	{
3831 	    begin = "BEGIN AUTH REQUEST\012";
3832 	    end   = "END AUTH REQUEST\012";
3833 	}
3834 
3835 	/* Get the password, probably from ~/.cvspass. */
3836 	password = get_cvs_password ();
3837 
3838         /* Send the empty string by default.  This is so anonymous CVS
3839            access doesn't require client to have done "cvs login". */
3840         if (password == NULL)
3841         {
3842             no_passwd = 1;
3843             password = scramble ("");
3844         }
3845 
3846 	/* Announce that we're starting the authorization protocol. */
3847 	if (send (sock, begin, strlen (begin), 0) < 0)
3848 	    error (1, 0, "cannot send: %s", SOCK_STRERROR (SOCK_ERRNO));
3849 
3850 	/* Send the data the server needs. */
3851 	if (send (sock, repository, strlen (repository), 0) < 0)
3852 	    error (1, 0, "cannot send: %s", SOCK_STRERROR (SOCK_ERRNO));
3853 	if (send (sock, "\012", 1, 0) < 0)
3854 	    error (1, 0, "cannot send: %s", SOCK_STRERROR (SOCK_ERRNO));
3855 	if (send (sock, username, strlen (username), 0) < 0)
3856 	    error (1, 0, "cannot send: %s", SOCK_STRERROR (SOCK_ERRNO));
3857 	if (send (sock, "\012", 1, 0) < 0)
3858 	    error (1, 0, "cannot send: %s", SOCK_STRERROR (SOCK_ERRNO));
3859 	if (send (sock, password, strlen (password), 0) < 0)
3860 	    error (1, 0, "cannot send: %s", SOCK_STRERROR (SOCK_ERRNO));
3861 	if (send (sock, "\012", 1, 0) < 0)
3862 	    error (1, 0, "cannot send: %s", SOCK_STRERROR (SOCK_ERRNO));
3863 
3864 	/* Announce that we're ending the authorization protocol. */
3865 	if (send (sock, end, strlen (end), 0) < 0)
3866 	    error (1, 0, "cannot send: %s", SOCK_STRERROR (SOCK_ERRNO));
3867 
3868         /* Paranoia. */
3869         memset (password, 0, strlen (password));
3870     }
3871 
3872     {
3873 	char *read_buf;
3874 
3875 	/* Loop, getting responses from the server.  */
3876 	while (1)
3877 	{
3878 	    recv_line (sock, &read_buf);
3879 
3880 	    if (strcmp (read_buf, "I HATE YOU") == 0)
3881 	    {
3882 		/* Authorization not granted. */
3883 		goto rejected;
3884 	    }
3885 	    else if (strncmp (read_buf, "E ", 2) == 0)
3886 	    {
3887 		fprintf (stderr, "%s\n", read_buf + 2);
3888 
3889 		/* Continue with the authentication protocol.  */
3890 	    }
3891 	    else if (strncmp (read_buf, "error ", 6) == 0)
3892 	    {
3893 		char *p;
3894 
3895 		/* First skip the code.  */
3896 		p = read_buf + 6;
3897 		while (*p != ' ' && *p != '\0')
3898 		    ++p;
3899 
3900 		/* Skip the space that follows the code.  */
3901 		if (*p == ' ')
3902 		    ++p;
3903 
3904 		/* Now output the text.  */
3905 		fprintf (stderr, "%s\n", p);
3906 		goto rejected;
3907 	    }
3908 	    else if (strcmp (read_buf, "I LOVE YOU") == 0)
3909 	    {
3910 		free (read_buf);
3911 		break;
3912 	    }
3913 	    else
3914 	    {
3915 		/* Unrecognized response from server. */
3916 		if (shutdown (sock, 2) < 0)
3917 		{
3918 		    error (0, 0,
3919 			   "unrecognized auth response from %s: %s",
3920 			   CVSroot_hostname, read_buf);
3921 		    error (1, 0,
3922 			   "shutdown() failed, server %s: %s",
3923 			   CVSroot_hostname,
3924 			   SOCK_STRERROR (SOCK_ERRNO));
3925 		}
3926 		error (1, 0,
3927 		       "unrecognized auth response from %s: %s",
3928 		       CVSroot_hostname, read_buf);
3929 	    }
3930 	    free (read_buf);
3931 	}
3932     }
3933 
3934     if (verify_only)
3935     {
3936 	if (shutdown (sock, 2) < 0)
3937 	    error (0, 0, "shutdown() failed, server %s: %s", CVSroot_hostname,
3938 		   SOCK_STRERROR (SOCK_ERRNO));
3939 	return;
3940     }
3941     else
3942     {
3943 #ifdef NO_SOCKET_TO_FD
3944 	use_socket_style = 1;
3945 	server_sock = sock;
3946 	/* Try to break mistaken callers: */
3947 	*tofdp = 0;
3948 	*fromfdp = 0;
3949 #else /* ! NO_SOCKET_TO_FD */
3950 	server_fd = sock;
3951 	close_on_exec (server_fd);
3952 	tofd = fromfd = sock;
3953 	/* Hand them back to the caller. */
3954 	*tofdp   = tofd;
3955 	*fromfdp = fromfd;
3956 #endif /* NO_SOCKET_TO_FD */
3957     }
3958 
3959     return;
3960 
3961   rejected:
3962     error (0, 0,
3963 	"authorization failed: server %s rejected access to %s for user %s",
3964 	CVSroot_hostname, CVSroot_directory, CVSroot_username);
3965 
3966     /* Output a special error message if authentication was attempted
3967        with no password -- the user should be made aware that they may
3968        have missed a step. */
3969     if (no_passwd)
3970     {
3971         error (0, 0,
3972                "used empty password; try \"cvs login\" with a real password");
3973     }
3974 
3975     if (shutdown (sock, 2) < 0)
3976     {
3977 	error (0, 0,
3978 	       "shutdown() failed (server %s): %s",
3979 	       CVSroot_hostname,
3980 	       SOCK_STRERROR (SOCK_ERRNO));
3981     }
3982 
3983     error_exit();
3984 }
3985 #endif /* AUTH_CLIENT_SUPPORT */
3986 
3987 
3988 #if HAVE_KERBEROS
3989 
3990 /* This function has not been changed to deal with NO_SOCKET_TO_FD
3991    (i.e., systems on which sockets cannot be converted to file
3992    descriptors).  The first person to try building a kerberos client
3993    on such a system (OS/2, Windows 95, and maybe others) will have to
3994    make take care of this.  */
3995 void
3996 start_tcp_server (tofdp, fromfdp)
3997     int *tofdp, *fromfdp;
3998 {
3999     int s;
4000     const char *portenv;
4001     int port;
4002     struct hostent *hp;
4003     struct sockaddr_in sin;
4004     char *hname;
4005 
4006     s = socket (AF_INET, SOCK_STREAM, 0);
4007     if (s < 0)
4008 	error (1, 0, "cannot create socket: %s", SOCK_STRERROR (SOCK_ERRNO));
4009 
4010     /* Get CVS_CLIENT_PORT or look up cvs/tcp with CVS_PORT as default */
4011     portenv = getenv ("CVS_CLIENT_PORT");
4012     if (portenv != NULL)
4013     {
4014 	port = atoi (portenv);
4015 	if (port <= 0)
4016 	{
4017 	    error (0, 0, "CVS_CLIENT_PORT must be a positive number!  If you");
4018 	    error (0, 0, "are trying to force a connection via ssh, please");
4019 	    error (0, 0, "put \":server:\" at the beginning of your CVSROOT");
4020 	    error (1, 0, "variable.");
4021 	}
4022 	if (trace)
4023 	    fprintf(stderr, "Using TCP port %d to contact server.\n", port);
4024     }
4025     else
4026     {
4027 	struct servent *sp;
4028 
4029 	sp = getservbyname ("cvs", "tcp");
4030 	if (sp == NULL)
4031 	    port = CVS_PORT;
4032 	else
4033 	    port = ntohs (sp->s_port);
4034     }
4035 
4036     hp = init_sockaddr (&sin, CVSroot_hostname, port);
4037 
4038     hname = xmalloc (strlen (hp->h_name) + 1);
4039     strcpy (hname, hp->h_name);
4040 
4041     if (connect (s, (struct sockaddr *) &sin, sizeof sin) < 0)
4042 	error (1, 0, "connect to %s:%d failed: %s", CVSroot_hostname,
4043 	       port, SOCK_STRERROR (SOCK_ERRNO));
4044 
4045 #ifdef HAVE_KERBEROS
4046     {
4047 	const char *realm;
4048 	struct sockaddr_in laddr;
4049 	int laddrlen;
4050 	KTEXT_ST ticket;
4051 	MSG_DAT msg_data;
4052 	CREDENTIALS cred;
4053 	int status;
4054 
4055 	realm = krb_realmofhost (hname);
4056 
4057 	laddrlen = sizeof (laddr);
4058 	if (getsockname (s, (struct sockaddr *) &laddr, &laddrlen) < 0)
4059 	    error (1, 0, "getsockname failed: %s", SOCK_STRERROR (SOCK_ERRNO));
4060 
4061 	/* We don't care about the checksum, and pass it as zero.  */
4062 	status = krb_sendauth (KOPT_DO_MUTUAL, s, &ticket, "rcmd",
4063 			       hname, realm, (unsigned long) 0, &msg_data,
4064 			       &cred, sched, &laddr, &sin, "KCVSV1.0");
4065 	if (status != KSUCCESS)
4066 	    error (1, 0, "kerberos authentication failed: %s",
4067 		   krb_get_err_text (status));
4068 	memcpy (kblock, cred.session, sizeof (C_Block));
4069     }
4070 #endif /* HAVE_KERBEROS */
4071 
4072     server_fd = s;
4073     close_on_exec (server_fd);
4074 
4075     free (hname);
4076 
4077     /* Give caller the values it wants. */
4078     *tofdp   = s;
4079     *fromfdp = s;
4080 }
4081 
4082 #endif /* HAVE_KERBEROS */
4083 
4084 #ifdef HAVE_GSSAPI
4085 
4086 /* Receive a given number of bytes.  */
4087 
4088 static void
4089 recv_bytes (sock, buf, need)
4090      int sock;
4091      char *buf;
4092      int need;
4093 {
4094     while (need > 0)
4095     {
4096 	int got;
4097 
4098 	got = recv (sock, buf, need, 0);
4099 	if (got < 0)
4100 	    error (1, 0, "recv() from server %s: %s", CVSroot_hostname,
4101 		   SOCK_STRERROR (SOCK_ERRNO));
4102 	buf += got;
4103 	need -= got;
4104     }
4105 }
4106 
4107 /* Connect to the server using GSSAPI authentication.  */
4108 
4109 static int
4110 connect_to_gserver (sock, hostinfo)
4111      int sock;
4112      struct hostent *hostinfo;
4113 {
4114     char *str;
4115     char buf[1024];
4116     gss_buffer_desc *tok_in_ptr, tok_in, tok_out;
4117     OM_uint32 stat_min, stat_maj;
4118     gss_name_t server_name;
4119 
4120     str = "BEGIN GSSAPI REQUEST\012";
4121 
4122     if (send (sock, str, strlen (str), 0) < 0)
4123 	error (1, 0, "cannot send: %s", SOCK_STRERROR (SOCK_ERRNO));
4124 
4125     sprintf (buf, "cvs@%s", hostinfo->h_name);
4126     tok_in.length = strlen (buf);
4127     tok_in.value = buf;
4128     gss_import_name (&stat_min, &tok_in, GSS_C_NT_HOSTBASED_SERVICE,
4129 		     &server_name);
4130 
4131     tok_in_ptr = GSS_C_NO_BUFFER;
4132     gcontext = GSS_C_NO_CONTEXT;
4133 
4134     do
4135     {
4136 	stat_maj = gss_init_sec_context (&stat_min, GSS_C_NO_CREDENTIAL,
4137 					 &gcontext, server_name,
4138 					 GSS_C_NULL_OID,
4139 					 (GSS_C_MUTUAL_FLAG
4140 					  | GSS_C_REPLAY_FLAG),
4141 					 0, NULL, tok_in_ptr, NULL, &tok_out,
4142 					 NULL, NULL);
4143 	if (stat_maj != GSS_S_COMPLETE && stat_maj != GSS_S_CONTINUE_NEEDED)
4144 	{
4145 	    OM_uint32 message_context;
4146 	    OM_uint32 new_stat_min;
4147 
4148 	    message_context = 0;
4149 	    gss_display_status (&new_stat_min, stat_maj, GSS_C_GSS_CODE,
4150                                 GSS_C_NULL_OID, &message_context, &tok_out);
4151 	    error (0, 0, "GSSAPI authentication failed: %s",
4152 		   (char *) tok_out.value);
4153 
4154 	    message_context = 0;
4155 	    gss_display_status (&new_stat_min, stat_min, GSS_C_MECH_CODE,
4156 				GSS_C_NULL_OID, &message_context, &tok_out);
4157 	    error (1, 0, "GSSAPI authentication failed: %s",
4158 		   (char *) tok_out.value);
4159 	}
4160 
4161 	if (tok_out.length == 0)
4162 	{
4163 	    tok_in.length = 0;
4164 	}
4165 	else
4166 	{
4167 	    char cbuf[2];
4168 	    int need;
4169 
4170 	    cbuf[0] = (tok_out.length >> 8) & 0xff;
4171 	    cbuf[1] = tok_out.length & 0xff;
4172 	    if (send (sock, cbuf, 2, 0) < 0)
4173 		error (1, 0, "cannot send: %s", SOCK_STRERROR (SOCK_ERRNO));
4174 	    if (send (sock, tok_out.value, tok_out.length, 0) < 0)
4175 		error (1, 0, "cannot send: %s", SOCK_STRERROR (SOCK_ERRNO));
4176 
4177 	    recv_bytes (sock, cbuf, 2);
4178 	    need = ((cbuf[0] & 0xff) << 8) | (cbuf[1] & 0xff);
4179 
4180 	    if (need > sizeof buf)
4181 	    {
4182 		int got;
4183 
4184 		/* This usually means that the server sent us an error
4185 		   message.  Read it byte by byte and print it out.
4186 		   FIXME: This is a terrible error handling strategy.
4187 		   However, even if we fix the server, we will still
4188 		   want to do this to work with older servers.  */
4189 		buf[0] = cbuf[0];
4190 		buf[1] = cbuf[1];
4191 		got = recv (sock, buf + 2, sizeof buf - 2, 0);
4192 		if (got < 0)
4193 		    error (1, 0, "recv() from server %s: %s",
4194 			   CVSroot_hostname, SOCK_STRERROR (SOCK_ERRNO));
4195 		buf[got + 2] = '\0';
4196 		if (buf[got + 1] == '\n')
4197 		    buf[got + 1] = '\0';
4198 		error (1, 0, "error from server %s: %s", CVSroot_hostname,
4199 		       buf);
4200 	    }
4201 
4202 	    recv_bytes (sock, buf, need);
4203 	    tok_in.length = need;
4204 	}
4205 
4206 	tok_in.value = buf;
4207 	tok_in_ptr = &tok_in;
4208     }
4209     while (stat_maj == GSS_S_CONTINUE_NEEDED);
4210 
4211     return 1;
4212 }
4213 
4214 #endif /* HAVE_GSSAPI */
4215 
4216 static int send_variable_proc PROTO ((Node *, void *));
4217 
4218 static int
4219 send_variable_proc (node, closure)
4220     Node *node;
4221     void *closure;
4222 {
4223     send_to_server ("Set ", 0);
4224     send_to_server (node->key, 0);
4225     send_to_server ("=", 1);
4226     send_to_server (node->data, 0);
4227     send_to_server ("\012", 1);
4228     return 0;
4229 }
4230 
4231 /* Contact the server.  */
4232 void
4233 start_server ()
4234 {
4235     int tofd, fromfd, rootless;
4236     char *log = getenv ("CVS_CLIENT_LOG");
4237 
4238 
4239     /* Clear our static variables for this invocation. */
4240     if (toplevel_repos != NULL)
4241 	free (toplevel_repos);
4242     toplevel_repos = NULL;
4243 
4244 
4245     /* Note that generally speaking we do *not* fall back to a different
4246        way of connecting if the first one does not work.  This is slow
4247        (*really* slow on a 14.4kbps link); the clean way to have a CVS
4248        which supports several ways of connecting is with access methods.  */
4249 
4250     switch (CVSroot_method)
4251     {
4252 
4253 #ifdef AUTH_CLIENT_SUPPORT
4254 	case pserver_method:
4255 	    /* Toss the return value.  It will die with error if anything
4256 	       goes wrong anyway. */
4257 	    connect_to_pserver (&tofd, &fromfd, 0, 0);
4258 	    break;
4259 #endif
4260 
4261 #if HAVE_KERBEROS
4262 	case kserver_method:
4263 	    start_tcp_server (&tofd, &fromfd);
4264 	    break;
4265 #endif
4266 
4267 #if HAVE_GSSAPI
4268 	case gserver_method:
4269 	    /* GSSAPI authentication is handled by the pserver.  */
4270 	    connect_to_pserver (&tofd, &fromfd, 0, 1);
4271 	    break;
4272 #endif
4273 
4274 	case ext_method:
4275 #if defined (NO_EXT_METHOD)
4276 	    error (0, 0, ":ext: method not supported by this port of CVS");
4277 	    error (1, 0, "try :server: instead");
4278 #else
4279 	    start_rsh_server (&tofd, &fromfd);
4280 #endif
4281 	    break;
4282 
4283 	case server_method:
4284 #if defined(START_SERVER)
4285 	    START_SERVER (&tofd, &fromfd, getcaller (),
4286 			  CVSroot_username, CVSroot_hostname,
4287 			  CVSroot_directory);
4288 #  if defined (START_SERVER_RETURNS_SOCKET) && defined (NO_SOCKET_TO_FD)
4289 	    /* This is a system on which we can only write to a socket
4290 	       using send/recv.  Therefore its START_SERVER needs to
4291 	       return a socket.  */
4292 	    use_socket_style = 1;
4293 	    server_sock = tofd;
4294 #  endif
4295 
4296 #else
4297 	    /* FIXME: It should be possible to implement this portably,
4298 	       like pserver, which would get rid of the duplicated code
4299 	       in {vms,windows-NT,...}/startserver.c.  */
4300 	    error (1, 0, "\
4301 the :server: access method is not supported by this port of CVS");
4302 #endif
4303 	    break;
4304 
4305         case fork_method:
4306 	    connect_to_forked_server (&tofd, &fromfd);
4307 	    break;
4308 
4309 	default:
4310 	    error (1, 0, "\
4311 (start_server internal error): unknown access method");
4312 	    break;
4313     }
4314 
4315     /* "Hi, I'm Darlene and I'll be your server tonight..." */
4316     server_started = 1;
4317 
4318 #ifdef NO_SOCKET_TO_FD
4319     if (use_socket_style)
4320     {
4321 	to_server = socket_buffer_initialize (server_sock, 0,
4322 					      (BUFMEMERRPROC) NULL);
4323 	from_server = socket_buffer_initialize (server_sock, 1,
4324 						(BUFMEMERRPROC) NULL);
4325     }
4326     else
4327 #endif /* NO_SOCKET_TO_FD */
4328     {
4329         /* todo: some OS's don't need these calls... */
4330         close_on_exec (tofd);
4331         close_on_exec (fromfd);
4332 
4333 	/* SCO 3 and AIX have a nasty bug in the I/O libraries which precludes
4334 	   fdopening the same file descriptor twice, so dup it if it is the
4335 	   same.  */
4336 	if (tofd == fromfd)
4337 	{
4338 	    fromfd = dup (tofd);
4339 	    if (fromfd < 0)
4340 		error (1, errno, "cannot dup net connection");
4341 	}
4342 
4343         /* These will use binary mode on systems which have it.  */
4344         to_server_fp = fdopen (tofd, FOPEN_BINARY_WRITE);
4345         if (to_server_fp == NULL)
4346 	    error (1, errno, "cannot fdopen %d for write", tofd);
4347 	to_server = stdio_buffer_initialize (to_server_fp, 0,
4348 					     (BUFMEMERRPROC) NULL);
4349 
4350         from_server_fp = fdopen (fromfd, FOPEN_BINARY_READ);
4351         if (from_server_fp == NULL)
4352 	    error (1, errno, "cannot fdopen %d for read", fromfd);
4353 	from_server = stdio_buffer_initialize (from_server_fp, 1,
4354 					       (BUFMEMERRPROC) NULL);
4355     }
4356 
4357     /* Set up logfiles, if any. */
4358     if (log)
4359     {
4360 	int len = strlen (log);
4361 	char *buf = xmalloc (len + 5);
4362 	char *p;
4363 	FILE *fp;
4364 
4365 	strcpy (buf, log);
4366 	p = buf + len;
4367 
4368 	/* Open logfiles in binary mode so that they reflect
4369 	   exactly what was transmitted and received (that is
4370 	   more important than that they be maximally
4371 	   convenient to view).  */
4372 	/* Note that if we create several connections in a single CVS client
4373 	   (currently used by update.c), then the last set of logfiles will
4374 	   overwrite the others.  There is currently no way around this.  */
4375 	strcpy (p, ".in");
4376 	fp = open_file (buf, "wb");
4377         if (fp == NULL)
4378 	    error (0, errno, "opening to-server logfile %s", buf);
4379 	else
4380 	    to_server = log_buffer_initialize (to_server, fp, 0,
4381 					       (BUFMEMERRPROC) NULL);
4382 
4383 	strcpy (p, ".out");
4384 	fp = open_file (buf, "wb");
4385         if (fp == NULL)
4386 	    error (0, errno, "opening from-server logfile %s", buf);
4387 	else
4388 	    from_server = log_buffer_initialize (from_server, fp, 1,
4389 						 (BUFMEMERRPROC) NULL);
4390 
4391 	free (buf);
4392     }
4393 
4394     /* Clear static variables.  */
4395     if (toplevel_repos != NULL)
4396 	free (toplevel_repos);
4397     toplevel_repos = NULL;
4398     if (last_dir_name != NULL)
4399 	free (last_dir_name);
4400     last_dir_name = NULL;
4401     if (last_repos != NULL)
4402 	free (last_repos);
4403     last_repos = NULL;
4404     if (last_update_dir != NULL)
4405 	free (last_update_dir);
4406     last_update_dir = NULL;
4407     stored_checksum_valid = 0;
4408     if (stored_mode != NULL)
4409     {
4410 	free (stored_mode);
4411 	stored_mode = NULL;
4412     }
4413 
4414     rootless = (strcmp (command_name, "init") == 0);
4415     if (!rootless)
4416     {
4417 	send_to_server ("Root ", 0);
4418 	send_to_server (CVSroot_directory, 0);
4419 	send_to_server ("\012", 1);
4420     }
4421 
4422     {
4423 	struct response *rs;
4424 
4425 	send_to_server ("Valid-responses", 0);
4426 
4427 	for (rs = responses; rs->name != NULL; ++rs)
4428 	{
4429 	    send_to_server (" ", 0);
4430 	    send_to_server (rs->name, 0);
4431 	}
4432 	send_to_server ("\012", 1);
4433     }
4434     send_to_server ("valid-requests\012", 0);
4435 
4436     if (get_server_responses ())
4437 	error_exit ();
4438 
4439     /*
4440      * Now handle global options.
4441      *
4442      * -H, -f, -d, -e should be handled OK locally.
4443      *
4444      * -b we ignore (treating it as a server installation issue).
4445      * FIXME: should be an error message.
4446      *
4447      * -v we print local version info; FIXME: Add a protocol request to get
4448      * the version from the server so we can print that too.
4449      *
4450      * -l -t -r -w -q -n and -Q need to go to the server.
4451      */
4452 
4453     {
4454 	int have_global = supported_request ("Global_option");
4455 
4456 	if (noexec)
4457 	{
4458 	    if (have_global)
4459 	    {
4460 		send_to_server ("Global_option -n\012", 0);
4461 	    }
4462 	    else
4463 		error (1, 0,
4464 		       "This server does not support the global -n option.");
4465 	}
4466 	if (quiet)
4467 	{
4468 	    if (have_global)
4469 	    {
4470 		send_to_server ("Global_option -q\012", 0);
4471 	    }
4472 	    else
4473 		error (1, 0,
4474 		       "This server does not support the global -q option.");
4475 	}
4476 	if (really_quiet)
4477 	{
4478 	    if (have_global)
4479 	    {
4480 		send_to_server ("Global_option -Q\012", 0);
4481 	    }
4482 	    else
4483 		error (1, 0,
4484 		       "This server does not support the global -Q option.");
4485 	}
4486 	if (!cvswrite)
4487 	{
4488 	    if (have_global)
4489 	    {
4490 		send_to_server ("Global_option -r\012", 0);
4491 	    }
4492 	    else
4493 		error (1, 0,
4494 		       "This server does not support the global -r option.");
4495 	}
4496 	if (trace)
4497 	{
4498 	    if (have_global)
4499 	    {
4500 		send_to_server ("Global_option -t\012", 0);
4501 	    }
4502 	    else
4503 		error (1, 0,
4504 		       "This server does not support the global -t option.");
4505 	}
4506 	if (logoff)
4507 	{
4508 	    if (have_global)
4509 	    {
4510 		send_to_server ("Global_option -l\012", 0);
4511 	    }
4512 	    else
4513 		error (1, 0,
4514 		       "This server does not support the global -l option.");
4515 	}
4516     }
4517 
4518     /* Find out about server-side cvswrappers.  An extra network
4519        turnaround for cvs import seems to be unavoidable, unless we
4520        want to add some kind of client-side place to configure which
4521        filenames imply binary.  For cvs add, we could avoid the
4522        problem by keeping a copy of the wrappers in CVSADM (the main
4523        reason to bother would be so we could make add work without
4524        contacting the server, I suspect).  */
4525 
4526     if ((strcmp (command_name, "import") == 0)
4527         || (strcmp (command_name, "add") == 0))
4528     {
4529 	if (supported_request ("wrapper-sendme-rcsOptions"))
4530 	{
4531 	    int err;
4532 	    send_to_server ("wrapper-sendme-rcsOptions\012", 0);
4533 	    err = get_server_responses ();
4534 	    if (err != 0)
4535 		error (err, 0, "error reading from server");
4536 	}
4537     }
4538 
4539     if (cvsencrypt && !rootless)
4540     {
4541 #ifdef ENCRYPTION
4542 	/* Turn on encryption before turning on compression.  We do
4543            not want to try to compress the encrypted stream.  Instead,
4544            we want to encrypt the compressed stream.  If we can't turn
4545            on encryption, bomb out; don't let the user think the data
4546            is being encrypted when it is not.  */
4547 #ifdef HAVE_KERBEROS
4548 	if (CVSroot_method == kserver_method)
4549 	{
4550 	    if (! supported_request ("Kerberos-encrypt"))
4551 		error (1, 0, "This server does not support encryption");
4552 	    send_to_server ("Kerberos-encrypt\012", 0);
4553 	    to_server = krb_encrypt_buffer_initialize (to_server, 0, sched,
4554 						       kblock,
4555 						       (BUFMEMERRPROC) NULL);
4556 	    from_server = krb_encrypt_buffer_initialize (from_server, 1,
4557 							 sched, kblock,
4558 							 (BUFMEMERRPROC) NULL);
4559 	}
4560 	else
4561 #endif /* HAVE_KERBEROS */
4562 #ifdef HAVE_GSSAPI
4563 	if (CVSroot_method == gserver_method)
4564 	{
4565 	    if (! supported_request ("Gssapi-encrypt"))
4566 		error (1, 0, "This server does not support encryption");
4567 	    send_to_server ("Gssapi-encrypt\012", 0);
4568 	    to_server = cvs_gssapi_wrap_buffer_initialize (to_server, 0,
4569 							   gcontext,
4570 							   ((BUFMEMERRPROC)
4571 							    NULL));
4572 	    from_server = cvs_gssapi_wrap_buffer_initialize (from_server, 1,
4573 							     gcontext,
4574 							     ((BUFMEMERRPROC)
4575 							      NULL));
4576 	    cvs_gssapi_encrypt = 1;
4577 	}
4578 	else
4579 #endif /* HAVE_GSSAPI */
4580 	    error (1, 0, "Encryption is only supported when using GSSAPI or Kerberos");
4581 #else /* ! ENCRYPTION */
4582 	error (1, 0, "This client does not support encryption");
4583 #endif /* ! ENCRYPTION */
4584     }
4585 
4586     if (gzip_level && !rootless)
4587     {
4588 	if (supported_request ("Gzip-stream"))
4589 	{
4590 	    char gzip_level_buf[5];
4591 	    send_to_server ("Gzip-stream ", 0);
4592 	    sprintf (gzip_level_buf, "%d", gzip_level);
4593 	    send_to_server (gzip_level_buf, 0);
4594 	    send_to_server ("\012", 1);
4595 
4596 	    /* All further communication with the server will be
4597                compressed.  */
4598 
4599 	    to_server = compress_buffer_initialize (to_server, 0, gzip_level,
4600 						    (BUFMEMERRPROC) NULL);
4601 	    from_server = compress_buffer_initialize (from_server, 1,
4602 						      gzip_level,
4603 						      (BUFMEMERRPROC) NULL);
4604 	}
4605 #ifndef NO_CLIENT_GZIP_PROCESS
4606 	else if (supported_request ("gzip-file-contents"))
4607 	{
4608             char gzip_level_buf[5];
4609 	    send_to_server ("gzip-file-contents ", 0);
4610             sprintf (gzip_level_buf, "%d", gzip_level);
4611 	    send_to_server (gzip_level_buf, 0);
4612 
4613 	    send_to_server ("\012", 1);
4614 
4615 	    file_gzip_level = gzip_level;
4616 	}
4617 #endif
4618 	else
4619 	{
4620 	    fprintf (stderr, "server doesn't support gzip-file-contents\n");
4621 	    /* Setting gzip_level to 0 prevents us from giving the
4622                error twice if update has to contact the server again
4623                to fetch unpatchable files.  */
4624 	    gzip_level = 0;
4625 	}
4626     }
4627 
4628     if (cvsauthenticate && ! cvsencrypt && !rootless)
4629     {
4630 	/* Turn on authentication after turning on compression, so
4631 	   that we can compress the authentication information.  We
4632 	   assume that encrypted data is always authenticated--the
4633 	   ability to decrypt the data stream is itself a form of
4634 	   authentication.  */
4635 #ifdef HAVE_GSSAPI
4636 	if (CVSroot_method == gserver_method)
4637 	{
4638 	    if (! supported_request ("Gssapi-authenticate"))
4639 		error (1, 0,
4640 		       "This server does not support stream authentication");
4641 	    send_to_server ("Gssapi-authenticate\012", 0);
4642 	    to_server = cvs_gssapi_wrap_buffer_initialize (to_server, 0,
4643 							   gcontext,
4644 							   ((BUFMEMERRPROC)
4645 							    NULL));
4646 	    from_server = cvs_gssapi_wrap_buffer_initialize (from_server, 1,
4647 							     gcontext,
4648 							     ((BUFMEMERRPROC)
4649 							      NULL));
4650 	}
4651 	else
4652 	    error (1, 0, "Stream authentication is only supported when using GSSAPI");
4653 #else /* ! HAVE_GSSAPI */
4654 	error (1, 0, "This client does not support stream authentication");
4655 #endif /* ! HAVE_GSSAPI */
4656     }
4657 
4658 #ifdef FILENAMES_CASE_INSENSITIVE
4659     if (supported_request ("Case") && !rootless)
4660 	send_to_server ("Case\012", 0);
4661 #endif
4662 
4663     /* If "Set" is not supported, just silently fail to send the variables.
4664        Users with an old server should get a useful error message when it
4665        fails to recognize the ${=foo} syntax.  This way if someone uses
4666        several servers, some of which are new and some old, they can still
4667        set user variables in their .cvsrc without trouble.  */
4668     if (supported_request ("Set"))
4669 	walklist (variable_list, send_variable_proc, NULL);
4670 }
4671 
4672 #ifndef NO_EXT_METHOD
4673 
4674 /* Contact the server by starting it with rsh.  */
4675 
4676 /* Right now, we have two different definitions for this function,
4677    depending on whether we start the rsh server using popenRW or not.
4678    This isn't ideal, and the best thing would probably be to change
4679    the OS/2 port to be more like the regular Unix client (i.e., by
4680    implementing piped_child)... but I'm doing something else at the
4681    moment, and wish to make only one change at a time.  -Karl */
4682 
4683 #ifdef START_RSH_WITH_POPEN_RW
4684 
4685 /* This is actually a crock -- it's OS/2-specific, for no one else
4686    uses it.  If I get time, I want to make piped_child and all the
4687    other stuff in os2/run.c work right.  In the meantime, this gets us
4688    up and running, and that's most important. */
4689 
4690 static void
4691 start_rsh_server (tofdp, fromfdp)
4692     int *tofdp, *fromfdp;
4693 {
4694     int pipes[2];
4695 
4696     /* If you're working through firewalls, you can set the
4697        CVS_RSH environment variable to a script which uses rsh to
4698        invoke another rsh on a proxy machine.  */
4699     char *cvs_rsh = getenv ("CVS_RSH");
4700     char *cvs_server = getenv ("CVS_SERVER");
4701     int i = 0;
4702     /* This needs to fit "rsh", "-b", "-l", "USER", "host",
4703        "cmd (w/ args)", and NULL.  We leave some room to grow. */
4704     char *rsh_argv[10];
4705 
4706     if (!cvs_rsh)
4707 	/* People sometimes suggest or assume that this should default
4708 	   to "remsh" on systems like HPUX in which that is the
4709 	   system-supplied name for the rsh program.  However, that
4710 	   causes various problems (keep in mind that systems such as
4711 	   HPUX might have non-system-supplied versions of "rsh", like
4712 	   a Kerberized one, which one might want to use).  If we
4713 	   based the name on what is found in the PATH of the person
4714 	   who runs configure, that would make it harder to
4715 	   consistently produce the same result in the face of
4716 	   different people producing binary distributions.  If we
4717 	   based it on "remsh" always being the default for HPUX
4718 	   (e.g. based on uname), that might be slightly better but
4719 	   would require us to keep track of what the defaults are for
4720 	   each system type, and probably would cope poorly if the
4721 	   existence of remsh or rsh varies from OS version to OS
4722 	   version.  Therefore, it seems best to have the default
4723 	   remain "rsh", and tell HPUX users to specify remsh, for
4724 	   example in CVS_RSH or other such mechanisms to be devised,
4725 	   if that is what they want (the manual already tells them
4726 	   that).
4727 	   Nowadays, however, ssh is pretty much everywhere, so we start
4728 	   to default to ssh instead.
4729         */
4730 	cvs_rsh = "ssh";
4731     if (!cvs_server)
4732 	cvs_server = "cvs";
4733 
4734     /* The command line starts out with rsh. */
4735     rsh_argv[i++] = cvs_rsh;
4736 
4737 #ifdef RSH_NEEDS_BINARY_FLAG
4738     /* "-b" for binary, under OS/2. */
4739     rsh_argv[i++] = "-b";
4740 #endif /* RSH_NEEDS_BINARY_FLAG */
4741 
4742     /* Then we strcat more things on the end one by one. */
4743     if (CVSroot_username != NULL)
4744     {
4745 	rsh_argv[i++] = "-l";
4746 	rsh_argv[i++] = CVSroot_username;
4747     }
4748 
4749     rsh_argv[i++] = CVSroot_hostname;
4750     rsh_argv[i++] = cvs_server;
4751     rsh_argv[i++] = "server";
4752 
4753     /* Mark the end of the arg list. */
4754     rsh_argv[i]   = (char *) NULL;
4755 
4756     if (trace)
4757     {
4758 	fprintf (stderr, " -> Starting server: ");
4759 	putc ('\n', stderr);
4760     }
4761 
4762     /* Do the deed. */
4763     rsh_pid = popenRW (rsh_argv, pipes);
4764     if (rsh_pid < 0)
4765 	error (1, errno, "cannot start server via ssh");
4766 
4767     /* Give caller the file descriptors. */
4768     *tofdp   = pipes[0];
4769     *fromfdp = pipes[1];
4770 }
4771 
4772 #else /* ! START_RSH_WITH_POPEN_RW */
4773 
4774 static void
4775 start_rsh_server (tofdp, fromfdp)
4776      int *tofdp;
4777      int *fromfdp;
4778 {
4779     /* If you're working through firewalls, you can set the
4780        CVS_RSH environment variable to a script which uses rsh to
4781        invoke another rsh on a proxy machine.  */
4782     char *cvs_rsh = getenv ("CVS_RSH");
4783     char *cvs_server = getenv ("CVS_SERVER");
4784     char *command;
4785 
4786     if (!cvs_rsh)
4787 	cvs_rsh = "ssh";
4788     if (!cvs_server)
4789 	cvs_server = "cvs";
4790 
4791     /* Pass the command to rsh as a single string.  This shouldn't
4792        affect most rsh servers at all, and will pacify some buggy
4793        versions of rsh that grab switches out of the middle of the
4794        command (they're calling the GNU getopt routines incorrectly).  */
4795     command = xmalloc (strlen (cvs_server)
4796 		       + strlen (CVSroot_directory)
4797 		       + 50);
4798 
4799     /* If you are running a very old (Nov 3, 1994, before 1.5)
4800      * version of the server, you need to make sure that your .bashrc
4801      * on the server machine does not set CVSROOT to something
4802      * containing a colon (or better yet, upgrade the server).  */
4803     sprintf (command, "%s server", cvs_server);
4804 
4805     {
4806         char *argv[10];
4807 	char **p = argv;
4808 
4809 	*p++ = cvs_rsh;
4810 	*p++ = CVSroot_hostname;
4811 
4812 	/* If the login names differ between client and server
4813 	 * pass it on to rsh.
4814 	 */
4815 	if (CVSroot_username != NULL)
4816 	{
4817 	    *p++ = "-l";
4818 	    *p++ = CVSroot_username;
4819 	}
4820 
4821 	*p++ = command;
4822 	*p++ = NULL;
4823 
4824 	if (trace)
4825         {
4826 	    int i;
4827 
4828             fprintf (stderr, " -> Starting server: ");
4829 	    for (i = 0; argv[i]; i++)
4830 	        fprintf (stderr, "%s ", argv[i]);
4831 	    putc ('\n', stderr);
4832 	}
4833 	rsh_pid = piped_child (argv, tofdp, fromfdp);
4834 
4835 	if (rsh_pid < 0)
4836 	    error (1, errno, "cannot start server via ssh");
4837     }
4838     free (command);
4839 }
4840 
4841 #endif /* START_RSH_WITH_POPEN_RW */
4842 
4843 #endif /* NO_EXT_METHOD */
4844 
4845 
4846 
4847 /* Send an argument STRING.  */
4848 void
4849 send_arg (string)
4850     char *string;
4851 {
4852     char buf[1];
4853     char *p = string;
4854 
4855     send_to_server ("Argument ", 0);
4856 
4857     while (*p)
4858     {
4859 	if (*p == '\n')
4860 	{
4861 	    send_to_server ("\012Argumentx ", 0);
4862 	}
4863 	else
4864         {
4865 	    buf[0] = *p;
4866 	    send_to_server (buf, 1);
4867         }
4868 	++p;
4869     }
4870     send_to_server ("\012", 1);
4871 }
4872 
4873 static void send_modified PROTO ((char *, char *, Vers_TS *));
4874 
4875 /* VERS->OPTIONS specifies whether the file is binary or not.  NOTE: BEFORE
4876    using any other fields of the struct vers, we would need to fix
4877    client_process_import_file to set them up.  */
4878 
4879 static void
4880 send_modified (file, short_pathname, vers)
4881     char *file;
4882     char *short_pathname;
4883     Vers_TS *vers;
4884 {
4885     /* File was modified, send it.  */
4886     struct stat sb;
4887     int fd;
4888     char *buf;
4889     char *mode_string;
4890     size_t bufsize;
4891     int bin;
4892 
4893     if (trace)
4894 	(void) fprintf (stderr, " -> Sending file `%s' to server\n", file);
4895 
4896     /* Don't think we can assume fstat exists.  */
4897     if ( CVS_STAT (file, &sb) < 0)
4898 	error (1, errno, "reading %s", short_pathname);
4899 
4900     mode_string = mode_to_string (sb.st_mode);
4901 
4902     /* Beware: on systems using CRLF line termination conventions,
4903        the read and write functions will convert CRLF to LF, so the
4904        number of characters read is not the same as sb.st_size.  Text
4905        files should always be transmitted using the LF convention, so
4906        we don't want to disable this conversion.  */
4907     bufsize = sb.st_size;
4908     buf = xmalloc (bufsize);
4909 
4910     /* Is the file marked as containing binary data by the "-kb" flag?
4911        If so, make sure to open it in binary mode: */
4912 
4913     if (vers && vers->options)
4914       bin = !(strcmp (vers->options, "-kb"));
4915     else
4916       bin = 0;
4917 
4918 #ifdef BROKEN_READWRITE_CONVERSION
4919     if (!bin)
4920     {
4921 	/* If only stdio, not open/write/etc., do text/binary
4922 	   conversion, use convert_file which can compensate
4923 	   (FIXME: we could just use stdio instead which would
4924 	   avoid the whole problem).  */
4925 	char tfile[1024]; strcpy(tfile, file); strcat(tfile, ".CVSBFCTMP");
4926 	convert_file (file, O_RDONLY,
4927 		      tfile, O_WRONLY | O_CREAT | O_TRUNC | OPEN_BINARY);
4928 	fd = CVS_OPEN (tfile, O_RDONLY | OPEN_BINARY);
4929 	if (fd < 0)
4930 	    error (1, errno, "reading %s", short_pathname);
4931     }
4932     else
4933 	fd = CVS_OPEN (file, O_RDONLY | OPEN_BINARY);
4934 #else
4935     fd = CVS_OPEN (file, O_RDONLY | (bin ? OPEN_BINARY : 0));
4936 #endif
4937 
4938     if (fd < 0)
4939 	error (1, errno, "reading %s", short_pathname);
4940 
4941     if (file_gzip_level && sb.st_size > 100)
4942     {
4943 	size_t newsize = 0;
4944 
4945 	if (read_and_gzip (fd, short_pathname, (unsigned char **)&buf,
4946 			   &bufsize, &newsize,
4947 			   file_gzip_level))
4948 	    error (1, 0, "aborting due to compression error");
4949 
4950 	if (close (fd) < 0)
4951 	    error (0, errno, "warning: can't close %s", short_pathname);
4952 
4953         {
4954           char tmp[80];
4955 
4956 	  send_to_server ("Modified ", 0);
4957 	  send_to_server (file, 0);
4958 	  send_to_server ("\012", 1);
4959 	  send_to_server (mode_string, 0);
4960 	  send_to_server ("\012z", 2);
4961 	  sprintf (tmp, "%lu\n", (unsigned long) newsize);
4962 	  send_to_server (tmp, 0);
4963 
4964           send_to_server (buf, newsize);
4965         }
4966     }
4967     else
4968     {
4969     	int newsize;
4970 
4971         {
4972 	    char *bufp = buf;
4973 	    int len;
4974 
4975 	    /* FIXME: This is gross.  It assumes that we might read
4976 	       less than st_size bytes (true on NT), but not more.
4977 	       Instead of this we should just be reading a block of
4978 	       data (e.g. 8192 bytes), writing it to the network, and
4979 	       so on until EOF.  */
4980 	    while ((len = read (fd, bufp, (buf + sb.st_size) - bufp)) > 0)
4981 	        bufp += len;
4982 
4983 	    if (len < 0)
4984 	        error (1, errno, "reading %s", short_pathname);
4985 
4986 	    newsize = bufp - buf;
4987 	}
4988 	if (close (fd) < 0)
4989 	    error (0, errno, "warning: can't close %s", short_pathname);
4990 
4991         {
4992           char tmp[80];
4993 
4994 	  send_to_server ("Modified ", 0);
4995 	  send_to_server (file, 0);
4996 	  send_to_server ("\012", 1);
4997 	  send_to_server (mode_string, 0);
4998 	  send_to_server ("\012", 1);
4999           sprintf (tmp, "%lu\012", (unsigned long) newsize);
5000           send_to_server (tmp, 0);
5001         }
5002 #ifdef BROKEN_READWRITE_CONVERSION
5003 	if (!bin)
5004 	{
5005 	    char tfile[1024]; strcpy(tfile, file); strcat(tfile, ".CVSBFCTMP");
5006 	    if (CVS_UNLINK (tfile) < 0)
5007 		error (0, errno, "warning: can't remove temp file %s", tfile);
5008 	}
5009 #endif
5010 
5011 	/*
5012 	 * Note that this only ends with a newline if the file ended with
5013 	 * one.
5014 	 */
5015 	if (newsize > 0)
5016 	    send_to_server (buf, newsize);
5017     }
5018     free (buf);
5019     free (mode_string);
5020 }
5021 
5022 /* The address of an instance of this structure is passed to
5023    send_fileproc, send_filesdoneproc, and send_direntproc, as the
5024    callerdat parameter.  */
5025 
5026 struct send_data
5027 {
5028     /* Each of the following flags are zero for clear or nonzero for set.  */
5029     int build_dirs;
5030     int force;
5031     int no_contents;
5032     int backup_modified;
5033 };
5034 
5035 static int send_fileproc PROTO ((void *callerdat, struct file_info *finfo));
5036 
5037 /* Deal with one file.  */
5038 static int
5039 send_fileproc (callerdat, finfo)
5040     void *callerdat;
5041     struct file_info *finfo;
5042 {
5043     struct send_data *args = (struct send_data *) callerdat;
5044     Vers_TS *vers;
5045     struct file_info xfinfo;
5046     /* File name to actually use.  Might differ in case from
5047        finfo->file.  */
5048     char *filename;
5049 
5050     send_a_repository ("", finfo->repository, finfo->update_dir);
5051 
5052     xfinfo = *finfo;
5053     xfinfo.repository = NULL;
5054     xfinfo.rcs = NULL;
5055     vers = Version_TS (&xfinfo, NULL, NULL, NULL, 0, 0);
5056 
5057     if (vers->entdata != NULL)
5058 	filename = vers->entdata->user;
5059     else
5060 	filename = finfo->file;
5061 
5062     if (vers->vn_user != NULL)
5063     {
5064 	/* The Entries request.  */
5065 	send_to_server ("Entry /", 0);
5066 	send_to_server (filename, 0);
5067 	send_to_server ("/", 0);
5068 	send_to_server (vers->vn_user, 0);
5069 	send_to_server ("/", 0);
5070 	if (vers->ts_conflict != NULL)
5071 	{
5072 	    if (vers->ts_user != NULL &&
5073 		strcmp (vers->ts_conflict, vers->ts_user) == 0)
5074 		send_to_server ("+=", 0);
5075 	    else
5076 		send_to_server ("+modified", 0);
5077 	}
5078 	send_to_server ("/", 0);
5079 	send_to_server (vers->entdata != NULL
5080 			? vers->entdata->options
5081 			: vers->options,
5082 			0);
5083 	send_to_server ("/", 0);
5084 	if (vers->entdata != NULL && vers->entdata->tag)
5085 	{
5086 	    send_to_server ("T", 0);
5087 	    send_to_server (vers->entdata->tag, 0);
5088 	}
5089 	else if (vers->entdata != NULL && vers->entdata->date)
5090           {
5091 	    send_to_server ("D", 0);
5092 	    send_to_server (vers->entdata->date, 0);
5093           }
5094 	send_to_server ("\012", 1);
5095     }
5096     else
5097     {
5098 	/* It seems a little silly to re-read this on each file, but
5099 	   send_dirent_proc doesn't get called if filenames are specified
5100 	   explicitly on the command line.  */
5101 	wrap_add_file (CVSDOTWRAPPER, 1);
5102 
5103 	if (wrap_name_has (filename, WRAP_RCSOPTION))
5104 	{
5105 	    /* No "Entry", but the wrappers did give us a kopt so we better
5106 	       send it with "Kopt".  As far as I know this only happens
5107 	       for "cvs add".  Question: is there any reason why checking
5108 	       for options from wrappers isn't done in Version_TS?
5109 
5110 	       Note: it might have been better to just remember all the
5111 	       kopts on the client side, rather than send them to the server,
5112 	       and have it send us back the same kopts.  But that seemed like
5113 	       a bigger change than I had in mind making now.  */
5114 
5115 	    if (supported_request ("Kopt"))
5116 	    {
5117 		char *opt;
5118 
5119 		send_to_server ("Kopt ", 0);
5120 		opt = wrap_rcsoption (filename, 1);
5121 		send_to_server (opt, 0);
5122 		send_to_server ("\012", 1);
5123 		free (opt);
5124 	    }
5125 	    else
5126 		error (0, 0,
5127 		       "\
5128 warning: ignoring -k options due to server limitations");
5129 	}
5130     }
5131 
5132     if (vers->ts_user == NULL)
5133     {
5134 	/*
5135 	 * Do we want to print "file was lost" like normal CVS?
5136 	 * Would it always be appropriate?
5137 	 */
5138 	/* File no longer exists.  Don't do anything, missing files
5139 	   just happen.  */
5140     }
5141     else if (vers->ts_rcs == NULL
5142 	     || args->force
5143 	     || strcmp (vers->ts_user, vers->ts_rcs) != 0)
5144     {
5145 	if (args->no_contents
5146 	    && supported_request ("Is-modified"))
5147 	{
5148 	    send_to_server ("Is-modified ", 0);
5149 	    send_to_server (filename, 0);
5150 	    send_to_server ("\012", 1);
5151 	}
5152 	else
5153 	    send_modified (filename, finfo->fullname, vers);
5154 
5155         if (args->backup_modified)
5156         {
5157             char *bakname;
5158             bakname = backup_file (filename, vers->vn_user);
5159             /* This behavior is sufficiently unexpected to
5160                justify overinformativeness, I think. */
5161             if (! really_quiet)
5162                 printf ("(Locally modified %s moved to %s)\n",
5163                         filename, bakname);
5164             free (bakname);
5165         }
5166     }
5167     else
5168     {
5169 	send_to_server ("Unchanged ", 0);
5170 	send_to_server (filename, 0);
5171 	send_to_server ("\012", 1);
5172     }
5173 
5174     /* if this directory has an ignore list, add this file to it */
5175     if (ignlist)
5176     {
5177 	Node *p;
5178 
5179 	p = getnode ();
5180 	p->type = FILES;
5181 	p->key = xstrdup (finfo->file);
5182 	(void) addnode (ignlist, p);
5183     }
5184 
5185     freevers_ts (&vers);
5186     return 0;
5187 }
5188 
5189 static void send_ignproc PROTO ((char *, char *));
5190 
5191 static void
5192 send_ignproc (file, dir)
5193     char *file;
5194     char *dir;
5195 {
5196     if (ign_inhibit_server || !supported_request ("Questionable"))
5197     {
5198 	if (dir[0] != '\0')
5199 	    (void) printf ("? %s/%s\n", dir, file);
5200 	else
5201 	    (void) printf ("? %s\n", file);
5202     }
5203     else
5204     {
5205 	send_to_server ("Questionable ", 0);
5206 	send_to_server (file, 0);
5207 	send_to_server ("\012", 1);
5208     }
5209 }
5210 
5211 static int send_filesdoneproc PROTO ((void *, int, char *, char *, List *));
5212 
5213 static int
5214 send_filesdoneproc (callerdat, err, repository, update_dir, entries)
5215     void *callerdat;
5216     int err;
5217     char *repository;
5218     char *update_dir;
5219     List *entries;
5220 {
5221     /* if this directory has an ignore list, process it then free it */
5222     if (ignlist)
5223     {
5224 	ignore_files (ignlist, entries, update_dir, send_ignproc);
5225 	dellist (&ignlist);
5226     }
5227 
5228     return (err);
5229 }
5230 
5231 static Dtype send_dirent_proc PROTO ((void *, char *, char *, char *, List *));
5232 
5233 /*
5234  * send_dirent_proc () is called back by the recursion processor before a
5235  * sub-directory is processed for update.
5236  * A return code of 0 indicates the directory should be
5237  * processed by the recursion code.  A return of non-zero indicates the
5238  * recursion code should skip this directory.
5239  *
5240  */
5241 static Dtype
5242 send_dirent_proc (callerdat, dir, repository, update_dir, entries)
5243     void *callerdat;
5244     char *dir;
5245     char *repository;
5246     char *update_dir;
5247     List *entries;
5248 {
5249     struct send_data *args = (struct send_data *) callerdat;
5250     int dir_exists;
5251     char *cvsadm_name;
5252 
5253     if (ignore_directory (update_dir))
5254     {
5255 	/* print the warm fuzzy message */
5256 	if (!quiet)
5257 	    error (0, 0, "Ignoring %s", update_dir);
5258         return (R_SKIP_ALL);
5259     }
5260 
5261     /*
5262      * If the directory does not exist yet (e.g. "cvs update -d foo"),
5263      * no need to send any files from it.  If the directory does not
5264      * have a CVS directory, then we pretend that it does not exist.
5265      * Otherwise, we will fail when trying to open the Entries file.
5266      * This case will happen when checking out a module defined as
5267      * ``-a .''.
5268      */
5269     cvsadm_name = xmalloc (strlen (dir) + sizeof (CVSADM) + 10);
5270     sprintf (cvsadm_name, "%s/%s", dir, CVSADM);
5271     dir_exists = isdir (cvsadm_name);
5272     free (cvsadm_name);
5273 
5274     /*
5275      * If there is an empty directory (e.g. we are doing `cvs add' on a
5276      * newly-created directory), the server still needs to know about it.
5277      */
5278 
5279     if (dir_exists)
5280     {
5281 	/*
5282 	 * Get the repository from a CVS/Repository file whenever possible.
5283 	 * The repository variable is wrong if the names in the local
5284 	 * directory don't match the names in the repository.
5285 	 */
5286 	char *repos = Name_Repository (dir, update_dir);
5287 	send_a_repository (dir, repos, update_dir);
5288 	free (repos);
5289 
5290 	/* initialize the ignore list for this directory */
5291 	ignlist = getlist ();
5292     }
5293     else
5294     {
5295 	/* It doesn't make sense to send a non-existent directory,
5296 	   because there is no way to get the correct value for
5297 	   the repository (I suppose maybe via the expand-modules
5298 	   request).  In the case where the "obvious" choice for
5299 	   repository is correct, the server can figure out whether
5300 	   to recreate the directory; in the case where it is wrong
5301 	   (that is, does not match what modules give us), we might as
5302 	   well just fail to recreate it.
5303 
5304 	   Checking for noexec is a kludge for "cvs -n add dir".  */
5305 	/* Don't send a non-existent directory unless we are building
5306            new directories (build_dirs is true).  Otherwise, CVS may
5307            see a D line in an Entries file, and recreate a directory
5308            which the user removed by hand.  */
5309 	if (args->build_dirs && noexec)
5310 	    send_a_repository (dir, repository, update_dir);
5311     }
5312 
5313     return (dir_exists ? R_PROCESS : R_SKIP_ALL);
5314 }
5315 
5316 static int send_dirleave_proc PROTO ((void *, char *, int, char *, List *));
5317 
5318 /*
5319  * send_dirleave_proc () is called back by the recursion code upon leaving
5320  * a directory.  All it does is delete the ignore list if it hasn't already
5321  * been done (by send_filesdone_proc).
5322  */
5323 /* ARGSUSED */
5324 static int
5325 send_dirleave_proc (callerdat, dir, err, update_dir, entries)
5326     void *callerdat;
5327     char *dir;
5328     int err;
5329     char *update_dir;
5330     List *entries;
5331 {
5332 
5333     /* Delete the ignore list if it hasn't already been done.  */
5334     if (ignlist)
5335 	dellist (&ignlist);
5336     return err;
5337 }
5338 
5339 /*
5340  * Send each option in a string to the server, one by one.
5341  * This assumes that the options are separated by spaces, for example
5342  * STRING might be "--foo -C5 -y".
5343  */
5344 
5345 void
5346 send_option_string (string)
5347     char *string;
5348 {
5349     char *copy;
5350     char *p;
5351 
5352     copy = xstrdup (string);
5353     p = copy;
5354     while (1)
5355     {
5356         char *s;
5357 	char l;
5358 
5359 	for (s = p; *s != ' ' && *s != '\0'; s++)
5360 	    ;
5361 	l = *s;
5362 	*s = '\0';
5363 	if (s != p)
5364 	    send_arg (p);
5365 	if (l == '\0')
5366 	    break;
5367 	p = s + 1;
5368     }
5369     free (copy);
5370 }
5371 
5372 
5373 /* Send the names of all the argument files to the server.  */
5374 
5375 void
5376 send_file_names (argc, argv, flags)
5377     int argc;
5378     char **argv;
5379     unsigned int flags;
5380 {
5381     int i;
5382     int level;
5383     int max_level;
5384 
5385     /* The fact that we do this here as well as start_recursion is a bit
5386        of a performance hit.  Perhaps worth cleaning up someday.  */
5387     if (flags & SEND_EXPAND_WILD)
5388 	expand_wild (argc, argv, &argc, &argv);
5389 
5390     /* Send Max-dotdot if needed.  */
5391     max_level = 0;
5392     for (i = 0; i < argc; ++i)
5393     {
5394 	level = pathname_levels (argv[i]);
5395 	if (level > max_level)
5396 	    max_level = level;
5397     }
5398     if (max_level > 0)
5399     {
5400 	if (supported_request ("Max-dotdot"))
5401 	{
5402             char buf[10];
5403             sprintf (buf, "%d", max_level);
5404 
5405 	    send_to_server ("Max-dotdot ", 0);
5406 	    send_to_server (buf, 0);
5407 	    send_to_server ("\012", 1);
5408 	}
5409 	else
5410 	    /*
5411 	     * "leading .." is not strictly correct, as this also includes
5412 	     * cases like "foo/../..".  But trying to explain that in the
5413 	     * error message would probably just confuse users.
5414 	     */
5415 	    error (1, 0,
5416 		   "leading .. not supported by old (pre-Max-dotdot) servers");
5417     }
5418 
5419     for (i = 0; i < argc; ++i)
5420     {
5421 	char buf[1];
5422 	char *p = argv[i];
5423 	char *line = NULL;
5424 
5425 	if (arg_should_not_be_sent_to_server (argv[i]))
5426 	    continue;
5427 
5428 #ifdef FILENAMES_CASE_INSENSITIVE
5429 	/* We want to send the file name as it appears
5430 	   in CVS/Entries.  We put this inside an ifdef
5431 	   to avoid doing all these system calls in
5432 	   cases where fncmp is just strcmp anyway.  */
5433 	/* For now just do this for files in the local
5434 	   directory.  Would be nice to handle the
5435 	   non-local case too, though.  */
5436 	/* The isdir check could more gracefully be replaced
5437 	   with a way of having Entries_Open report back the
5438 	   error to us and letting us ignore existence_error.
5439 	   Or some such.  */
5440 	if (p == last_component (p) && isdir (CVSADM))
5441 	{
5442 	    List *entries;
5443 	    Node *node;
5444 
5445 	    /* If we were doing non-local directory,
5446 	       we would save_cwd, CVS_CHDIR
5447 	       like in update.c:isemptydir.  */
5448 	    /* Note that if we are adding a directory,
5449 	       the following will read the entry
5450 	       that we just wrote there, that is, we
5451 	       will get the case specified on the
5452 	       command line, not the case of the
5453 	       directory in the filesystem.  This
5454 	       is correct behavior.  */
5455 	    entries = Entries_Open (0, NULL);
5456 	    node = findnode_fn (entries, p);
5457 	    if (node != NULL)
5458 	    {
5459 		line = xstrdup (node->key);
5460 		p = line;
5461 		delnode (node);
5462 	    }
5463 	    Entries_Close (entries);
5464 	}
5465 #endif /* FILENAMES_CASE_INSENSITIVE */
5466 
5467 	send_to_server ("Argument ", 0);
5468 
5469 	while (*p)
5470 	{
5471 	    if (*p == '\n')
5472 	    {
5473 		send_to_server ("\012Argumentx ", 0);
5474 	    }
5475 	    else if (ISDIRSEP (*p))
5476 	    {
5477 		buf[0] = '/';
5478 		send_to_server (buf, 1);
5479 	    }
5480 	    else
5481 	    {
5482 		buf[0] = *p;
5483 		send_to_server (buf, 1);
5484 	    }
5485 	    ++p;
5486 	}
5487 	send_to_server ("\012", 1);
5488 	if (line != NULL)
5489 	    free (line);
5490     }
5491 
5492     if (flags & SEND_EXPAND_WILD)
5493     {
5494 	int i;
5495 	for (i = 0; i < argc; ++i)
5496 	    free (argv[i]);
5497 	free (argv);
5498     }
5499 }
5500 
5501 
5502 /* Send Repository, Modified and Entry.  argc and argv contain only
5503   the files to operate on (or empty for everything), not options.
5504   local is nonzero if we should not recurse (-l option).  flags &
5505   SEND_BUILD_DIRS is nonzero if nonexistent directories should be
5506   sent.  flags & SEND_FORCE is nonzero if we should send unmodified
5507   files to the server as though they were modified.  flags &
5508   SEND_NO_CONTENTS means that this command only needs to know
5509   _whether_ a file is modified, not the contents.  Also sends Argument
5510   lines for argc and argv, so should be called after options are sent.  */
5511 void
5512 send_files (argc, argv, local, aflag, flags)
5513     int argc;
5514     char **argv;
5515     int local;
5516     int aflag;
5517     unsigned int flags;
5518 {
5519     struct send_data args;
5520     int err;
5521 
5522     /*
5523      * aflag controls whether the tag/date is copied into the vers_ts.
5524      * But we don't actually use it, so I don't think it matters what we pass
5525      * for aflag here.
5526      */
5527     args.build_dirs = flags & SEND_BUILD_DIRS;
5528     args.force = flags & SEND_FORCE;
5529     args.no_contents = flags & SEND_NO_CONTENTS;
5530     args.backup_modified = flags & BACKUP_MODIFIED_FILES;
5531     err = start_recursion
5532 	(send_fileproc, send_filesdoneproc,
5533 	 send_dirent_proc, send_dirleave_proc, (void *) &args,
5534 	 argc, argv, local, W_LOCAL, aflag, 0, (char *)NULL, 0);
5535     if (err)
5536 	error_exit ();
5537     if (toplevel_repos == NULL)
5538 	/*
5539 	 * This happens if we are not processing any files,
5540 	 * or for checkouts in directories without any existing stuff
5541 	 * checked out.  The following assignment is correct for the
5542 	 * latter case; I don't think toplevel_repos matters for the
5543 	 * former.
5544 	 */
5545 	toplevel_repos = xstrdup (CVSroot_directory);
5546     send_repository ("", toplevel_repos, ".");
5547 }
5548 
5549 void
5550 client_import_setup (repository)
5551     char *repository;
5552 {
5553     if (toplevel_repos == NULL)		/* should always be true */
5554         send_a_repository ("", repository, "");
5555 }
5556 
5557 /*
5558  * Process the argument import file.
5559  */
5560 int
5561 client_process_import_file (message, vfile, vtag, targc, targv, repository,
5562                             all_files_binary, modtime)
5563     char *message;
5564     char *vfile;
5565     char *vtag;
5566     int targc;
5567     char *targv[];
5568     char *repository;
5569     int all_files_binary;
5570 
5571     /* Nonzero for "import -d".  */
5572     int modtime;
5573 {
5574     char *update_dir;
5575     char *fullname;
5576     Vers_TS vers;
5577 
5578     assert (toplevel_repos != NULL);
5579 
5580     if (strncmp (repository, toplevel_repos, strlen (toplevel_repos)) != 0)
5581 	error (1, 0,
5582 	       "internal error: pathname `%s' doesn't specify file in `%s'",
5583 	       repository, toplevel_repos);
5584 
5585     if (strcmp (repository, toplevel_repos) == 0)
5586     {
5587 	update_dir = "";
5588 	fullname = xstrdup (vfile);
5589     }
5590     else
5591     {
5592 	update_dir = repository + strlen (toplevel_repos) + 1;
5593 
5594 	fullname = xmalloc (strlen (vfile) + strlen (update_dir) + 10);
5595 	strcpy (fullname, update_dir);
5596 	strcat (fullname, "/");
5597 	strcat (fullname, vfile);
5598     }
5599 
5600     send_a_repository ("", repository, update_dir);
5601     if (all_files_binary)
5602     {
5603 	vers.options = xmalloc (4); /* strlen("-kb") + 1 */
5604 	strcpy (vers.options, "-kb");
5605     }
5606     else
5607     {
5608 	vers.options = wrap_rcsoption (vfile, 1);
5609     }
5610     if (vers.options != NULL)
5611     {
5612 	if (supported_request ("Kopt"))
5613 	{
5614 	    send_to_server ("Kopt ", 0);
5615 	    send_to_server (vers.options, 0);
5616 	    send_to_server ("\012", 1);
5617 	}
5618 	else
5619 	    error (0, 0,
5620 		   "warning: ignoring -k options due to server limitations");
5621     }
5622     if (modtime)
5623     {
5624 	if (supported_request ("Checkin-time"))
5625 	{
5626 	    struct stat sb;
5627 	    char *rcsdate;
5628 	    char netdate[MAXDATELEN];
5629 
5630 	    if (CVS_STAT (vfile, &sb) < 0)
5631 		error (1, errno, "cannot stat %s", fullname);
5632 	    rcsdate = date_from_time_t (sb.st_mtime);
5633 	    date_to_internet (netdate, rcsdate);
5634 	    free (rcsdate);
5635 
5636 	    send_to_server ("Checkin-time ", 0);
5637 	    send_to_server (netdate, 0);
5638 	    send_to_server ("\012", 1);
5639 	}
5640 	else
5641 	    error (0, 0,
5642 		   "warning: ignoring -d option due to server limitations");
5643     }
5644     send_modified (vfile, fullname, &vers);
5645     if (vers.options != NULL)
5646 	free (vers.options);
5647     free (fullname);
5648     return 0;
5649 }
5650 
5651 void
5652 client_import_done ()
5653 {
5654     if (toplevel_repos == NULL)
5655 	/*
5656 	 * This happens if we are not processing any files,
5657 	 * or for checkouts in directories without any existing stuff
5658 	 * checked out.  The following assignment is correct for the
5659 	 * latter case; I don't think toplevel_repos matters for the
5660 	 * former.
5661 	 */
5662         /* FIXME: "can't happen" now that we call client_import_setup
5663 	   at the beginning.  */
5664 	toplevel_repos = xstrdup (CVSroot_directory);
5665     send_repository ("", toplevel_repos, ".");
5666 }
5667 
5668 static void
5669 notified_a_file (data, ent_list, short_pathname, filename)
5670     char *data;
5671     List *ent_list;
5672     char *short_pathname;
5673     char *filename;
5674 {
5675     FILE *fp;
5676     FILE *newf;
5677     size_t line_len = 8192;
5678     char *line = xmalloc (line_len);
5679     char *cp;
5680     int nread;
5681     int nwritten;
5682     char *p;
5683 
5684     fp = open_file (CVSADM_NOTIFY, "r");
5685     if (getline (&line, &line_len, fp) < 0)
5686     {
5687 	if (feof (fp))
5688 	    error (0, 0, "cannot read %s: end of file", CVSADM_NOTIFY);
5689 	else
5690 	    error (0, errno, "cannot read %s", CVSADM_NOTIFY);
5691 	goto error_exit;
5692     }
5693     cp = strchr (line, '\t');
5694     if (cp == NULL)
5695     {
5696 	error (0, 0, "malformed %s file", CVSADM_NOTIFY);
5697 	goto error_exit;
5698     }
5699     *cp = '\0';
5700     if (strcmp (filename, line + 1) != 0)
5701     {
5702 	error (0, 0, "protocol error: notified %s, expected %s", filename,
5703 	       line + 1);
5704     }
5705 
5706     if (getline (&line, &line_len, fp) < 0)
5707     {
5708 	if (feof (fp))
5709 	{
5710 	    free (line);
5711 	    if (fclose (fp) < 0)
5712 		error (0, errno, "cannot close %s", CVSADM_NOTIFY);
5713 	    if ( CVS_UNLINK (CVSADM_NOTIFY) < 0)
5714 		error (0, errno, "cannot remove %s", CVSADM_NOTIFY);
5715 	    return;
5716 	}
5717 	else
5718 	{
5719 	    error (0, errno, "cannot read %s", CVSADM_NOTIFY);
5720 	    goto error_exit;
5721 	}
5722     }
5723     newf = open_file (CVSADM_NOTIFYTMP, "w");
5724     if (fputs (line, newf) < 0)
5725     {
5726 	error (0, errno, "cannot write %s", CVSADM_NOTIFYTMP);
5727 	goto error2;
5728     }
5729     while ((nread = fread (line, 1, line_len, fp)) > 0)
5730     {
5731 	p = line;
5732 	while ((nwritten = fwrite (p, 1, nread, newf)) > 0)
5733 	{
5734 	    nread -= nwritten;
5735 	    p += nwritten;
5736 	}
5737 	if (ferror (newf))
5738 	{
5739 	    error (0, errno, "cannot write %s", CVSADM_NOTIFYTMP);
5740 	    goto error2;
5741 	}
5742     }
5743     if (ferror (fp))
5744     {
5745 	error (0, errno, "cannot read %s", CVSADM_NOTIFY);
5746 	goto error2;
5747     }
5748     if (fclose (newf) < 0)
5749     {
5750 	error (0, errno, "cannot close %s", CVSADM_NOTIFYTMP);
5751 	goto error_exit;
5752     }
5753     free (line);
5754     if (fclose (fp) < 0)
5755     {
5756 	error (0, errno, "cannot close %s", CVSADM_NOTIFY);
5757 	return;
5758     }
5759 
5760     {
5761         /* In this case, we want rename_file() to ignore noexec. */
5762         int saved_noexec = noexec;
5763         noexec = 0;
5764         rename_file (CVSADM_NOTIFYTMP, CVSADM_NOTIFY);
5765         noexec = saved_noexec;
5766     }
5767 
5768     return;
5769   error2:
5770     (void) fclose (newf);
5771   error_exit:
5772     free (line);
5773     (void) fclose (fp);
5774 }
5775 
5776 static void
5777 handle_notified (args, len)
5778     char *args;
5779     int len;
5780 {
5781     call_in_directory (args, notified_a_file, NULL);
5782 }
5783 
5784 void
5785 client_notify (repository, update_dir, filename, notif_type, val)
5786     char *repository;
5787     char *update_dir;
5788     char *filename;
5789     int notif_type;
5790     char *val;
5791 {
5792     char buf[2];
5793 
5794     send_a_repository ("", repository, update_dir);
5795     send_to_server ("Notify ", 0);
5796     send_to_server (filename, 0);
5797     send_to_server ("\012", 1);
5798     buf[0] = notif_type;
5799     buf[1] = '\0';
5800     send_to_server (buf, 1);
5801     send_to_server ("\t", 1);
5802     send_to_server (val, 0);
5803 }
5804 
5805 /*
5806  * Send an option with an argument, dealing correctly with newlines in
5807  * the argument.  If ARG is NULL, forget the whole thing.
5808  */
5809 void
5810 option_with_arg (option, arg)
5811     char *option;
5812     char *arg;
5813 {
5814     if (arg == NULL)
5815 	return;
5816 
5817     send_to_server ("Argument ", 0);
5818     send_to_server (option, 0);
5819     send_to_server ("\012", 1);
5820 
5821     send_arg (arg);
5822 }
5823 
5824 /* Send a date to the server.  The input DATE is in RCS format.
5825    The time will be GMT.
5826 
5827    We then convert that to the format required in the protocol
5828    (including the "-D" option) and send it.  According to
5829    cvsclient.texi, RFC 822/1123 format is preferred.  */
5830 
5831 void
5832 client_senddate (date)
5833     const char *date;
5834 {
5835     char buf[MAXDATELEN];
5836 
5837     date_to_internet (buf, (char *)date);
5838     option_with_arg ("-D", buf);
5839 }
5840 
5841 void
5842 send_init_command ()
5843 {
5844     /* This is here because we need the CVSroot_directory variable.  */
5845     send_to_server ("init ", 0);
5846     send_to_server (CVSroot_directory, 0);
5847     send_to_server ("\012", 0);
5848 }
5849 
5850 #endif /* CLIENT_SUPPORT */
5851