mkmodules.c revision 66528
1/*
2 * Copyright (c) 1992, Brian Berliner and Jeff Polk
3 * Copyright (c) 1989-1992, Brian Berliner
4 *
5 * You may distribute under the terms of the GNU General Public License as
6 * specified in the README file that comes with the CVS kit.
7 *
8 * $FreeBSD: head/contrib/cvs/src/mkmodules.c 66528 2000-10-02 06:43:58Z peter $
9 */
10
11#include "cvs.h"
12#include "savecwd.h"
13#include "getline.h"
14
15#ifndef DBLKSIZ
16#define	DBLKSIZ	4096			/* since GNU ndbm doesn't define it */
17#endif
18
19static int checkout_file PROTO((char *file, char *temp));
20static char *make_tempfile PROTO((void));
21static void rename_rcsfile PROTO((char *temp, char *real));
22
23#ifndef MY_NDBM
24static void rename_dbmfile PROTO((char *temp));
25static void write_dbmfile PROTO((char *temp));
26#endif				/* !MY_NDBM */
27
28/* Structure which describes an administrative file.  */
29struct admin_file {
30   /* Name of the file, within the CVSROOT directory.  */
31   char *filename;
32
33   /* This is a one line description of what the file is for.  It is not
34      currently used, although one wonders whether it should be, somehow.
35      If NULL, then don't process this file in mkmodules (FIXME?: a bit of
36      a kludge; probably should replace this with a flags field).  */
37   char *errormsg;
38
39   /* Contents which the file should have in a new repository.  To avoid
40      problems with brain-dead compilers which choke on long string constants,
41      this is a pointer to an array of char * terminated by NULL--each of
42      the strings is concatenated.
43
44      If this field is NULL, the file is not created in a new
45      repository, but it can be added with "cvs add" (just as if one
46      had created the repository with a version of CVS which didn't
47      know about the file) and the checked-out copy will be updated
48      without having to add it to checkoutlist.  */
49   const char * const *contents;
50};
51
52static const char *const loginfo_contents[] = {
53    "# The \"loginfo\" file controls where \"cvs commit\" log information\n",
54    "# is sent.  The first entry on a line is a regular expression which must match\n",
55    "# the directory that the change is being made to, relative to the\n",
56    "# $CVSROOT.  If a match is found, then the remainder of the line is a filter\n",
57    "# program that should expect log information on its standard input.\n",
58    "#\n",
59    "# If the repository name does not match any of the regular expressions in this\n",
60    "# file, the \"DEFAULT\" line is used, if it is specified.\n",
61    "#\n",
62    "# If the name ALL appears as a regular expression it is always used\n",
63    "# in addition to the first matching regex or DEFAULT.\n",
64    "#\n",
65    "# You may specify a format string as part of the\n",
66    "# filter.  The string is composed of a `%' followed\n",
67    "# by a single format character, or followed by a set of format\n",
68    "# characters surrounded by `{' and `}' as separators.  The format\n",
69    "# characters are:\n",
70    "#\n",
71    "#   s = file name\n",
72    "#   V = old version number (pre-checkin)\n",
73    "#   v = new version number (post-checkin)\n",
74    "#\n",
75    "# For example:\n",
76    "#DEFAULT (echo \"\"; id; echo %s; date; cat) >> $CVSROOT/CVSROOT/commitlog\n",
77    "# or\n",
78    "#DEFAULT (echo \"\"; id; echo %{sVv}; date; cat) >> $CVSROOT/CVSROOT/commitlog\n",
79    NULL
80};
81
82static const char *const rcsinfo_contents[] = {
83    "# The \"rcsinfo\" file is used to control templates with which the editor\n",
84    "# is invoked on commit and import.\n",
85    "#\n",
86    "# The first entry on a line is a regular expression which is tested\n",
87    "# against the directory that the change is being made to, relative to the\n",
88    "# $CVSROOT.  For the first match that is found, then the remainder of the\n",
89    "# line is the name of the file that contains the template.\n",
90    "#\n",
91    "# If the repository name does not match any of the regular expressions in this\n",
92    "# file, the \"DEFAULT\" line is used, if it is specified.\n",
93    "#\n",
94    "# If the name \"ALL\" appears as a regular expression it is always used\n",
95    "# in addition to the first matching regex or \"DEFAULT\".\n",
96    NULL
97};
98
99static const char *const editinfo_contents[] = {
100    "# The \"editinfo\" file is used to allow verification of logging\n",
101    "# information.  It works best when a template (as specified in the\n",
102    "# rcsinfo file) is provided for the logging procedure.  Given a\n",
103    "# template with locations for, a bug-id number, a list of people who\n",
104    "# reviewed the code before it can be checked in, and an external\n",
105    "# process to catalog the differences that were code reviewed, the\n",
106    "# following test can be applied to the code:\n",
107    "#\n",
108    "#   Making sure that the entered bug-id number is correct.\n",
109    "#   Validating that the code that was reviewed is indeed the code being\n",
110    "#       checked in (using the bug-id number or a seperate review\n",
111    "#       number to identify this particular code set.).\n",
112    "#\n",
113    "# If any of the above test failed, then the commit would be aborted.\n",
114    "#\n",
115    "# Actions such as mailing a copy of the report to each reviewer are\n",
116    "# better handled by an entry in the loginfo file.\n",
117    "#\n",
118    "# One thing that should be noted is the the ALL keyword is not\n",
119    "# supported.  There can be only one entry that matches a given\n",
120    "# repository.\n",
121    NULL
122};
123
124static const char *const verifymsg_contents[] = {
125    "# The \"verifymsg\" file is used to allow verification of logging\n",
126    "# information.  It works best when a template (as specified in the\n",
127    "# rcsinfo file) is provided for the logging procedure.  Given a\n",
128    "# template with locations for, a bug-id number, a list of people who\n",
129    "# reviewed the code before it can be checked in, and an external\n",
130    "# process to catalog the differences that were code reviewed, the\n",
131    "# following test can be applied to the code:\n",
132    "#\n",
133    "#   Making sure that the entered bug-id number is correct.\n",
134    "#   Validating that the code that was reviewed is indeed the code being\n",
135    "#       checked in (using the bug-id number or a seperate review\n",
136    "#       number to identify this particular code set.).\n",
137    "#\n",
138    "# If any of the above test failed, then the commit would be aborted.\n",
139    "#\n",
140    "# Actions such as mailing a copy of the report to each reviewer are\n",
141    "# better handled by an entry in the loginfo file.\n",
142    "#\n",
143    "# One thing that should be noted is the the ALL keyword is not\n",
144    "# supported.  There can be only one entry that matches a given\n",
145    "# repository.\n",
146    NULL
147};
148
149static const char *const commitinfo_contents[] = {
150    "# The \"commitinfo\" file is used to control pre-commit checks.\n",
151    "# The filter on the right is invoked with the repository and a list \n",
152    "# of files to check.  A non-zero exit of the filter program will \n",
153    "# cause the commit to be aborted.\n",
154    "#\n",
155    "# The first entry on a line is a regular expression which is tested\n",
156    "# against the directory that the change is being committed to, relative\n",
157    "# to the $CVSROOT.  For the first match that is found, then the remainder\n",
158    "# of the line is the name of the filter to run.\n",
159    "#\n",
160    "# If the repository name does not match any of the regular expressions in this\n",
161    "# file, the \"DEFAULT\" line is used, if it is specified.\n",
162    "#\n",
163    "# If the name \"ALL\" appears as a regular expression it is always used\n",
164    "# in addition to the first matching regex or \"DEFAULT\".\n",
165    NULL
166};
167
168static const char *const taginfo_contents[] = {
169    "# The \"taginfo\" file is used to control pre-tag checks.\n",
170    "# The filter on the right is invoked with the following arguments:\n",
171    "#\n",
172    "# $1 -- tagname\n",
173    "# $2 -- operation \"add\" for tag, \"mov\" for tag -F, and \"del\" for tag -d\n",
174    "# $3 -- repository\n",
175    "# $4->  file revision [file revision ...]\n",
176    "#\n",
177    "# A non-zero exit of the filter program will cause the tag to be aborted.\n",
178    "#\n",
179    "# The first entry on a line is a regular expression which is tested\n",
180    "# against the directory that the change is being committed to, relative\n",
181    "# to the $CVSROOT.  For the first match that is found, then the remainder\n",
182    "# of the line is the name of the filter to run.\n",
183    "#\n",
184    "# If the repository name does not match any of the regular expressions in this\n",
185    "# file, the \"DEFAULT\" line is used, if it is specified.\n",
186    "#\n",
187    "# If the name \"ALL\" appears as a regular expression it is always used\n",
188    "# in addition to the first matching regex or \"DEFAULT\".\n",
189    NULL
190};
191
192static const char *const checkoutlist_contents[] = {
193    "# The \"checkoutlist\" file is used to support additional version controlled\n",
194    "# administrative files in $CVSROOT/CVSROOT, such as template files.\n",
195    "#\n",
196    "# The first entry on a line is a filename which will be checked out from\n",
197    "# the corresponding RCS file in the $CVSROOT/CVSROOT directory.\n",
198    "# The remainder of the line is an error message to use if the file cannot\n",
199    "# be checked out.\n",
200    "#\n",
201    "# File format:\n",
202    "#\n",
203    "#	[<whitespace>]<filename><whitespace><error message><end-of-line>\n",
204    "#\n",
205    "# comment lines begin with '#'\n",
206    NULL
207};
208
209static const char *const cvswrappers_contents[] = {
210    "# This file affects handling of files based on their names.\n",
211    "#\n",
212    "# The -t/-f options allow one to treat directories of files\n",
213    "# as a single file, or to transform a file in other ways on\n",
214    "# its way in and out of CVS.\n",
215    "#\n",
216    "# The -m option specifies whether CVS attempts to merge files.\n",
217    "#\n",
218    "# The -k option specifies keyword expansion (e.g. -kb for binary).\n",
219    "#\n",
220    "# Format of wrapper file ($CVSROOT/CVSROOT/cvswrappers or .cvswrappers)\n",
221    "#\n",
222    "#  wildcard	[option value][option value]...\n",
223    "#\n",
224    "#  where option is one of\n",
225    "#  -f		from cvs filter		value: path to filter\n",
226    "#  -t		to cvs filter		value: path to filter\n",
227    "#  -m		update methodology	value: MERGE or COPY\n",
228    "#  -k		expansion mode		value: b, o, kkv, &c\n",
229    "#\n",
230    "#  and value is a single-quote delimited value.\n",
231    "# For example:\n",
232    "#*.gif -k 'b'\n",
233    NULL
234};
235
236static const char *const notify_contents[] = {
237    "# The \"notify\" file controls where notifications from watches set by\n",
238    "# \"cvs watch add\" or \"cvs edit\" are sent.  The first entry on a line is\n",
239    "# a regular expression which is tested against the directory that the\n",
240    "# change is being made to, relative to the $CVSROOT.  If it matches,\n",
241    "# then the remainder of the line is a filter program that should contain\n",
242    "# one occurrence of %s for the user to notify, and information on its\n",
243    "# standard input.\n",
244    "#\n",
245    "# \"ALL\" or \"DEFAULT\" can be used in place of the regular expression.\n",
246    "#\n",
247    "# For example:\n",
248    "#ALL mail %s -s \"CVS notification\"\n",
249    NULL
250};
251
252static const char *const modules_contents[] = {
253    "# Three different line formats are valid:\n",
254    "#	key	-a    aliases...\n",
255    "#	key [options] directory\n",
256    "#	key [options] directory files...\n",
257    "#\n",
258    "# Where \"options\" are composed of:\n",
259    "#	-i prog		Run \"prog\" on \"cvs commit\" from top-level of module.\n",
260    "#	-o prog		Run \"prog\" on \"cvs checkout\" of module.\n",
261    "#	-e prog		Run \"prog\" on \"cvs export\" of module.\n",
262    "#	-t prog		Run \"prog\" on \"cvs rtag\" of module.\n",
263    "#	-u prog		Run \"prog\" on \"cvs update\" of module.\n",
264    "#	-d dir		Place module in directory \"dir\" instead of module name.\n",
265    "#	-l		Top-level directory only -- do not recurse.\n",
266    "#\n",
267    "# NOTE:  If you change any of the \"Run\" options above, you'll have to\n",
268    "# release and re-checkout any working directories of these modules.\n",
269    "#\n",
270    "# And \"directory\" is a path to a directory relative to $CVSROOT.\n",
271    "#\n",
272    "# The \"-a\" option specifies an alias.  An alias is interpreted as if\n",
273    "# everything on the right of the \"-a\" had been typed on the command line.\n",
274    "#\n",
275    "# You can encode a module within a module by using the special '&'\n",
276    "# character to interpose another module into the current module.  This\n",
277    "# can be useful for creating a module that consists of many directories\n",
278    "# spread out over the entire source repository.\n",
279    NULL
280};
281
282static const char *const config_contents[] = {
283    "# Set this to \"no\" if pserver shouldn't check system users/passwords\n",
284    "#SystemAuth=no\n",
285    "\n",
286    "# Put CVS lock files in this directory rather than directly in the repository.\n",
287    "#LockDir=/var/lock/cvs\n",
288    "\n",
289#ifdef PRESERVE_PERMISSIONS_SUPPORT
290    "# Set `PreservePermissions' to `yes' to save file status information\n",
291    "# in the repository.\n",
292    "#PreservePermissions=no\n",
293    "\n",
294#endif
295    "# Set `TopLevelAdmin' to `yes' to create a CVS directory at the top\n",
296    "# level of the new working directory when using the `cvs checkout'\n",
297    "# command.\n",
298    "#TopLevelAdmin=no\n",
299    "\n",
300    "# Set `LogHistory' to `all' or `TOFEWGCMAR' to log all transactions to the\n",
301    "# history file, or a subset as needed (ie `TMAR' logs all write operations)\n",
302    "#LogHistory=TOFEWGCMAR\n",
303    NULL
304};
305
306static const struct admin_file filelist[] = {
307    {CVSROOTADM_LOGINFO,
308	"no logging of 'cvs commit' messages is done without a %s file",
309	&loginfo_contents[0]},
310    {CVSROOTADM_RCSINFO,
311	"a %s file can be used to configure 'cvs commit' templates",
312	rcsinfo_contents},
313    {CVSROOTADM_EDITINFO,
314	"a %s file can be used to validate log messages",
315	editinfo_contents},
316    {CVSROOTADM_VERIFYMSG,
317	"a %s file can be used to validate log messages",
318	verifymsg_contents},
319    {CVSROOTADM_COMMITINFO,
320	"a %s file can be used to configure 'cvs commit' checking",
321	commitinfo_contents},
322    {CVSROOTADM_TAGINFO,
323	"a %s file can be used to configure 'cvs tag' checking",
324	taginfo_contents},
325    {CVSROOTADM_IGNORE,
326	"a %s file can be used to specify files to ignore",
327	NULL},
328    {CVSROOTADM_CHECKOUTLIST,
329	"a %s file can specify extra CVSROOT files to auto-checkout",
330	checkoutlist_contents},
331    {CVSROOTADM_WRAPPER,
332	"a %s file can be used to specify files to treat as wrappers",
333	cvswrappers_contents},
334    {CVSROOTADM_NOTIFY,
335	"a %s file can be used to specify where notifications go",
336	notify_contents},
337    {CVSROOTADM_MODULES,
338	/* modules is special-cased in mkmodules.  */
339	NULL,
340	modules_contents},
341    {CVSROOTADM_READERS,
342	"a %s file specifies read-only users",
343	NULL},
344    {CVSROOTADM_WRITERS,
345	"a %s file specifies read/write users",
346	NULL},
347
348    /* Some have suggested listing CVSROOTADM_PASSWD here too.  This
349       would mean that CVS commands which operate on the
350       CVSROOTADM_PASSWD file would transmit hashed passwords over the
351       net.  This might seem to be no big deal, as pserver normally
352       transmits cleartext passwords, but the difference is that
353       CVSROOTADM_PASSWD contains *all* passwords, not just the ones
354       currently being used.  For example, it could be too easy to
355       accidentally give someone readonly access to CVSROOTADM_PASSWD
356       (e.g. via anonymous CVS or cvsweb), and then if there are any
357       guessable passwords for read/write access (usually there will be)
358       they get read/write access.
359
360       Another worry is the implications of storing old passwords--if
361       someone used a password in the past they might be using it
362       elsewhere, using a similar password, etc, and so saving old
363       passwords, even hashed, is probably not a good idea.  */
364
365    {CVSROOTADM_CONFIG,
366	 "a %s file configures various behaviors",
367	 config_contents},
368    {NULL, NULL, NULL}
369};
370
371/* Rebuild the checked out administrative files in directory DIR.  */
372int
373mkmodules (dir)
374    char *dir;
375{
376    struct saved_cwd cwd;
377    char *temp;
378    char *cp, *last, *fname;
379#ifdef MY_NDBM
380    DBM *db;
381#endif
382    FILE *fp;
383    char *line = NULL;
384    size_t line_allocated = 0;
385    const struct admin_file *fileptr;
386
387    if (noexec)
388	return 0;
389
390    if (save_cwd (&cwd))
391	error_exit ();
392
393    if ( CVS_CHDIR (dir) < 0)
394	error (1, errno, "cannot chdir to %s", dir);
395
396    /*
397     * First, do the work necessary to update the "modules" database.
398     */
399    temp = make_tempfile ();
400    switch (checkout_file (CVSROOTADM_MODULES, temp))
401    {
402
403	case 0:			/* everything ok */
404#ifdef MY_NDBM
405	    /* open it, to generate any duplicate errors */
406	    if ((db = dbm_open (temp, O_RDONLY, 0666)) != NULL)
407		dbm_close (db);
408#else
409	    write_dbmfile (temp);
410	    rename_dbmfile (temp);
411#endif
412	    rename_rcsfile (temp, CVSROOTADM_MODULES);
413	    break;
414
415	default:
416	    error (0, 0,
417		"'cvs checkout' is less functional without a %s file",
418		CVSROOTADM_MODULES);
419	    break;
420    }					/* switch on checkout_file() */
421
422    if (unlink_file (temp) < 0
423	&& !existence_error (errno))
424	error (0, errno, "cannot remove %s", temp);
425    free (temp);
426
427    /* Checkout the files that need it in CVSROOT dir */
428    for (fileptr = filelist; fileptr && fileptr->filename; fileptr++) {
429	if (fileptr->errormsg == NULL)
430	    continue;
431	temp = make_tempfile ();
432	if (checkout_file (fileptr->filename, temp) == 0)
433	    rename_rcsfile (temp, fileptr->filename);
434#if 0
435	/*
436	 * If there was some problem other than the file not existing,
437	 * checkout_file already printed a real error message.  If the
438	 * file does not exist, it is harmless--it probably just means
439	 * that the repository was created with an old version of CVS
440	 * which didn't have so many files in CVSROOT.
441	 */
442	else if (fileptr->errormsg)
443	    error (0, 0, fileptr->errormsg, fileptr->filename);
444#endif
445	if (unlink_file (temp) < 0
446	    && !existence_error (errno))
447	    error (0, errno, "cannot remove %s", temp);
448	free (temp);
449    }
450
451    fp = CVS_FOPEN (CVSROOTADM_CHECKOUTLIST, "r");
452    if (fp)
453    {
454	/*
455	 * File format:
456	 *  [<whitespace>]<filename><whitespace><error message><end-of-line>
457	 *
458	 * comment lines begin with '#'
459	 */
460	while (getline (&line, &line_allocated, fp) >= 0)
461	{
462	    /* skip lines starting with # */
463	    if (line[0] == '#')
464		continue;
465
466	    if ((last = strrchr (line, '\n')) != NULL)
467		*last = '\0';			/* strip the newline */
468
469	    /* Skip leading white space. */
470	    for (fname = line;
471		 *fname && isspace ((unsigned char) *fname);
472		 fname++)
473		;
474
475	    /* Find end of filename. */
476	    for (cp = fname; *cp && !isspace ((unsigned char) *cp); cp++)
477		;
478	    *cp = '\0';
479
480	    temp = make_tempfile ();
481	    if (checkout_file (fname, temp) == 0)
482	    {
483		rename_rcsfile (temp, fname);
484	    }
485	    else
486	    {
487		for (cp++;
488		     cp < last && *last && isspace ((unsigned char) *last);
489		     cp++)
490		    ;
491		if (cp < last && *cp)
492		    error (0, 0, cp, fname);
493	    }
494	    if (unlink_file (temp) < 0
495		&& !existence_error (errno))
496		error (0, errno, "cannot remove %s", temp);
497	    free (temp);
498	}
499	if (line)
500	    free (line);
501	if (ferror (fp))
502	    error (0, errno, "cannot read %s", CVSROOTADM_CHECKOUTLIST);
503	if (fclose (fp) < 0)
504	    error (0, errno, "cannot close %s", CVSROOTADM_CHECKOUTLIST);
505    }
506    else
507    {
508	/* Error from CVS_FOPEN.  */
509	if (!existence_error (errno))
510	    error (0, errno, "cannot open %s", CVSROOTADM_CHECKOUTLIST);
511    }
512
513    if (restore_cwd (&cwd, NULL))
514	error_exit ();
515    free_cwd (&cwd);
516
517    return (0);
518}
519
520/*
521 * Yeah, I know, there are NFS race conditions here.
522 */
523static char *
524make_tempfile ()
525{
526    static int seed = 0;
527    int fd;
528    char *temp;
529
530    if (seed == 0)
531	seed = getpid ();
532    temp = xmalloc (sizeof (BAKPREFIX) + 40);
533    while (1)
534    {
535	(void) sprintf (temp, "%s%d", BAKPREFIX, seed++);
536	if ((fd = CVS_OPEN (temp, O_CREAT|O_EXCL|O_RDWR, 0666)) != -1)
537	    break;
538	if (errno != EEXIST)
539	    error (1, errno, "cannot create temporary file %s", temp);
540    }
541    if (close(fd) < 0)
542	error(1, errno, "cannot close temporary file %s", temp);
543    return temp;
544}
545
546/* Get a file.  If the file does not exist, return 1 silently.  If
547   there is an error, print a message and return 1 (FIXME: probably
548   not a very clean convention).  On success, return 0.  */
549
550static int
551checkout_file (file, temp)
552    char *file;
553    char *temp;
554{
555    char *rcs;
556    RCSNode *rcsnode;
557    int retcode = 0;
558
559    if (noexec)
560	return 0;
561
562    rcs = xmalloc (strlen (file) + 5);
563    strcpy (rcs, file);
564    strcat (rcs, RCSEXT);
565    if (!isfile (rcs))
566    {
567	free (rcs);
568	return (1);
569    }
570    rcsnode = RCS_parsercsfile (rcs);
571    retcode = RCS_checkout (rcsnode, NULL, NULL, NULL, NULL, temp,
572			    (RCSCHECKOUTPROC) NULL, (void *) NULL);
573    if (retcode != 0)
574    {
575	/* Probably not necessary (?); RCS_checkout already printed a
576	   message.  */
577	error (0, 0, "failed to check out %s file",
578	       file);
579    }
580    freercsnode (&rcsnode);
581    free (rcs);
582    return (retcode);
583}
584
585#ifndef MY_NDBM
586
587static void
588write_dbmfile (temp)
589    char *temp;
590{
591    char line[DBLKSIZ], value[DBLKSIZ];
592    FILE *fp;
593    DBM *db;
594    char *cp, *vp;
595    datum key, val;
596    int len, cont, err = 0;
597
598    fp = open_file (temp, "r");
599    if ((db = dbm_open (temp, O_RDWR | O_CREAT | O_TRUNC, 0666)) == NULL)
600	error (1, errno, "cannot open dbm file %s for creation", temp);
601    for (cont = 0; fgets (line, sizeof (line), fp) != NULL;)
602    {
603	if ((cp = strrchr (line, '\n')) != NULL)
604	    *cp = '\0';			/* strip the newline */
605
606	/*
607	 * Add the line to the value, at the end if this is a continuation
608	 * line; otherwise at the beginning, but only after any trailing
609	 * backslash is removed.
610	 */
611	vp = value;
612	if (cont)
613	    vp += strlen (value);
614
615	/*
616	 * See if the line we read is a continuation line, and strip the
617	 * backslash if so.
618	 */
619	len = strlen (line);
620	if (len > 0)
621	    cp = &line[len - 1];
622	else
623	    cp = line;
624	if (*cp == '\\')
625	{
626	    cont = 1;
627	    *cp = '\0';
628	}
629	else
630	{
631	    cont = 0;
632	}
633	(void) strcpy (vp, line);
634	if (value[0] == '#')
635	    continue;			/* comment line */
636	vp = value;
637	while (*vp && isspace ((unsigned char) *vp))
638	    vp++;
639	if (*vp == '\0')
640	    continue;			/* empty line */
641
642	/*
643	 * If this was not a continuation line, add the entry to the database
644	 */
645	if (!cont)
646	{
647	    key.dptr = vp;
648	    while (*vp && !isspace ((unsigned char) *vp))
649		vp++;
650	    key.dsize = vp - key.dptr;
651	    *vp++ = '\0';		/* NULL terminate the key */
652	    while (*vp && isspace ((unsigned char) *vp))
653		vp++;			/* skip whitespace to value */
654	    if (*vp == '\0')
655	    {
656		error (0, 0, "warning: NULL value for key `%s'", key.dptr);
657		continue;
658	    }
659	    val.dptr = vp;
660	    val.dsize = strlen (vp);
661	    if (dbm_store (db, key, val, DBM_INSERT) == 1)
662	    {
663		error (0, 0, "duplicate key found for `%s'", key.dptr);
664		err++;
665	    }
666	}
667    }
668    dbm_close (db);
669    if (fclose (fp) < 0)
670	error (0, errno, "cannot close %s", temp);
671    if (err)
672    {
673	/* I think that the size of the buffer needed here is
674	   just determined by sizeof (CVSROOTADM_MODULES), the
675	   filenames created by make_tempfile, and other things that won't
676	   overflow.  */
677	char dotdir[50], dotpag[50], dotdb[50];
678
679	(void) sprintf (dotdir, "%s.dir", temp);
680	(void) sprintf (dotpag, "%s.pag", temp);
681	(void) sprintf (dotdb, "%s.db", temp);
682	if (unlink_file (dotdir) < 0
683	    && !existence_error (errno))
684	    error (0, errno, "cannot remove %s", dotdir);
685	if (unlink_file (dotpag) < 0
686	    && !existence_error (errno))
687	    error (0, errno, "cannot remove %s", dotpag);
688	if (unlink_file (dotdb) < 0
689	    && !existence_error (errno))
690	    error (0, errno, "cannot remove %s", dotdb);
691	error (1, 0, "DBM creation failed; correct above errors");
692    }
693}
694
695static void
696rename_dbmfile (temp)
697    char *temp;
698{
699    /* I think that the size of the buffer needed here is
700       just determined by sizeof (CVSROOTADM_MODULES), the
701       filenames created by make_tempfile, and other things that won't
702       overflow.  */
703    char newdir[50], newpag[50], newdb[50];
704    char dotdir[50], dotpag[50], dotdb[50];
705    char bakdir[50], bakpag[50], bakdb[50];
706
707    int dir1_errno = 0, pag1_errno = 0, db1_errno = 0;
708    int dir2_errno = 0, pag2_errno = 0, db2_errno = 0;
709    int dir3_errno = 0, pag3_errno = 0, db3_errno = 0;
710
711    (void) sprintf (dotdir, "%s.dir", CVSROOTADM_MODULES);
712    (void) sprintf (dotpag, "%s.pag", CVSROOTADM_MODULES);
713    (void) sprintf (dotdb, "%s.db", CVSROOTADM_MODULES);
714    (void) sprintf (bakdir, "%s%s.dir", BAKPREFIX, CVSROOTADM_MODULES);
715    (void) sprintf (bakpag, "%s%s.pag", BAKPREFIX, CVSROOTADM_MODULES);
716    (void) sprintf (bakdb, "%s%s.db", BAKPREFIX, CVSROOTADM_MODULES);
717    (void) sprintf (newdir, "%s.dir", temp);
718    (void) sprintf (newpag, "%s.pag", temp);
719    (void) sprintf (newdb, "%s.db", temp);
720
721    (void) chmod (newdir, 0666);
722    (void) chmod (newpag, 0666);
723    (void) chmod (newdb, 0666);
724
725    /* don't mess with me */
726    SIG_beginCrSect ();
727
728    /* rm .#modules.dir .#modules.pag */
729    if (unlink_file (bakdir) < 0)
730	dir1_errno = errno;
731    if (unlink_file (bakpag) < 0)
732	pag1_errno = errno;
733    if (unlink_file (bakdb) < 0)
734	db1_errno = errno;
735
736    /* mv modules.dir .#modules.dir */
737    if (CVS_RENAME (dotdir, bakdir) < 0)
738	dir2_errno = errno;
739    /* mv modules.pag .#modules.pag */
740    if (CVS_RENAME (dotpag, bakpag) < 0)
741	pag2_errno = errno;
742    /* mv modules.db .#modules.db */
743    if (CVS_RENAME (dotdb, bakdb) < 0)
744	db2_errno = errno;
745
746    /* mv "temp".dir modules.dir */
747    if (CVS_RENAME (newdir, dotdir) < 0)
748	dir3_errno = errno;
749    /* mv "temp".pag modules.pag */
750    if (CVS_RENAME (newpag, dotpag) < 0)
751	pag3_errno = errno;
752    /* mv "temp".db modules.db */
753    if (CVS_RENAME (newdb, dotdb) < 0)
754	db3_errno = errno;
755
756    /* OK -- make my day */
757    SIG_endCrSect ();
758
759    /* I didn't want to call error() when we had signals blocked
760       (unnecessary?), but do it now.  */
761    if (dir1_errno && !existence_error (dir1_errno))
762	error (0, dir1_errno, "cannot remove %s", bakdir);
763    if (pag1_errno && !existence_error (pag1_errno))
764	error (0, pag1_errno, "cannot remove %s", bakpag);
765    if (db1_errno && !existence_error (db1_errno))
766	error (0, db1_errno, "cannot remove %s", bakdb);
767
768    if (dir2_errno && !existence_error (dir2_errno))
769	error (0, dir2_errno, "cannot remove %s", bakdir);
770    if (pag2_errno && !existence_error (pag2_errno))
771	error (0, pag2_errno, "cannot remove %s", bakpag);
772    if (db2_errno && !existence_error (db2_errno))
773	error (0, db2_errno, "cannot remove %s", bakdb);
774
775    if (dir3_errno && !existence_error (dir3_errno))
776	error (0, dir3_errno, "cannot remove %s", bakdir);
777    if (pag3_errno && !existence_error (pag3_errno))
778	error (0, pag3_errno, "cannot remove %s", bakpag);
779    if (db3_errno && !existence_error (db3_errno))
780	error (0, db3_errno, "cannot remove %s", bakdb);
781}
782
783#endif				/* !MY_NDBM */
784
785static void
786rename_rcsfile (temp, real)
787    char *temp;
788    char *real;
789{
790    char *bak;
791    struct stat statbuf;
792    char *rcs;
793
794    /* Set "x" bits if set in original. */
795    rcs = xmalloc (strlen (real) + sizeof (RCSEXT) + 10);
796    (void) sprintf (rcs, "%s%s", real, RCSEXT);
797    statbuf.st_mode = 0; /* in case rcs file doesn't exist, but it should... */
798    if (CVS_STAT (rcs, &statbuf) < 0
799	&& !existence_error (errno))
800	error (0, errno, "cannot stat %s", rcs);
801    free (rcs);
802
803    if (chmod (temp, 0444 | (statbuf.st_mode & 0111)) < 0)
804	error (0, errno, "warning: cannot chmod %s", temp);
805    bak = xmalloc (strlen (real) + sizeof (BAKPREFIX) + 10);
806    (void) sprintf (bak, "%s%s", BAKPREFIX, real);
807
808    /* rm .#loginfo */
809    if (unlink_file (bak) < 0
810	&& !existence_error (errno))
811	error (0, errno, "cannot remove %s", bak);
812
813    /* mv loginfo .#loginfo */
814    if (CVS_RENAME (real, bak) < 0
815	&& !existence_error (errno))
816	error (0, errno, "cannot rename %s to %s", real, bak);
817
818    /* mv "temp" loginfo */
819    if (CVS_RENAME (temp, real) < 0
820	&& !existence_error (errno))
821	error (0, errno, "cannot rename %s to %s", temp, real);
822
823    free (bak);
824}
825
826const char *const init_usage[] = {
827    "Usage: %s %s\n",
828    "(Specify the --help global option for a list of other help options)\n",
829    NULL
830};
831
832int
833init (argc, argv)
834    int argc;
835    char **argv;
836{
837    /* Name of CVSROOT directory.  */
838    char *adm;
839    /* Name of this administrative file.  */
840    char *info;
841    /* Name of ,v file for this administrative file.  */
842    char *info_v;
843    /* Exit status.  */
844    int err;
845
846    const struct admin_file *fileptr;
847
848    umask (cvsumask);
849
850    if (argc == -1 || argc > 1)
851	usage (init_usage);
852
853#ifdef CLIENT_SUPPORT
854    if (client_active)
855    {
856	start_server ();
857
858	ign_setup ();
859	send_init_command ();
860	return get_responses_and_close ();
861    }
862#endif /* CLIENT_SUPPORT */
863
864    /* Note: we do *not* create parent directories as needed like the
865       old cvsinit.sh script did.  Few utilities do that, and a
866       non-existent parent directory is as likely to be a typo as something
867       which needs to be created.  */
868    mkdir_if_needed (CVSroot_directory);
869
870    adm = xmalloc (strlen (CVSroot_directory) + sizeof (CVSROOTADM) + 10);
871    strcpy (adm, CVSroot_directory);
872    strcat (adm, "/");
873    strcat (adm, CVSROOTADM);
874    mkdir_if_needed (adm);
875
876    /* This is needed because we pass "fileptr->filename" not "info"
877       to add_rcs_file below.  I think this would be easy to change,
878       thus nuking the need for CVS_CHDIR here, but I haven't looked
879       closely (e.g. see wrappers calls within add_rcs_file).  */
880    if ( CVS_CHDIR (adm) < 0)
881	error (1, errno, "cannot change to directory %s", adm);
882
883    /* Make Emptydir so it's there if we need it */
884    mkdir_if_needed (CVSNULLREPOS);
885
886    /* 80 is long enough for all the administrative file names, plus
887       "/" and so on.  */
888    info = xmalloc (strlen (adm) + 80);
889    info_v = xmalloc (strlen (adm) + 80);
890    for (fileptr = filelist; fileptr && fileptr->filename; ++fileptr)
891    {
892	if (fileptr->contents == NULL)
893	    continue;
894	strcpy (info, adm);
895	strcat (info, "/");
896	strcat (info, fileptr->filename);
897	strcpy (info_v, info);
898	strcat (info_v, RCSEXT);
899	if (isfile (info_v))
900	    /* We will check out this file in the mkmodules step.
901	       Nothing else is required.  */
902	    ;
903	else
904	{
905	    int retcode;
906
907	    if (!isfile (info))
908	    {
909		FILE *fp;
910		const char * const *p;
911
912		fp = open_file (info, "w");
913		for (p = fileptr->contents; *p != NULL; ++p)
914		    if (fputs (*p, fp) < 0)
915			error (1, errno, "cannot write %s", info);
916		if (fclose (fp) < 0)
917		    error (1, errno, "cannot close %s", info);
918	    }
919	    /* The message used to say " of " and fileptr->filename after
920	       "initial checkin" but I fail to see the point as we know what
921	       file it is from the name.  */
922	    retcode = add_rcs_file ("initial checkin", info_v,
923				    fileptr->filename, "1.1", NULL,
924
925				    /* No vendor branch.  */
926				    NULL, NULL, 0, NULL,
927
928				    NULL, 0, NULL);
929	    if (retcode != 0)
930		/* add_rcs_file already printed an error message.  */
931		err = 1;
932	}
933    }
934
935    /* Turn on history logging by default.  The user can remove the file
936       to disable it.  */
937    strcpy (info, adm);
938    strcat (info, "/");
939    strcat (info, CVSROOTADM_HISTORY);
940    if (!isfile (info))
941    {
942	FILE *fp;
943
944	fp = open_file (info, "w");
945	if (fclose (fp) < 0)
946	    error (1, errno, "cannot close %s", info);
947
948        /* Make the new history file world-writeable, since every CVS
949           user will need to be able to write to it.  We use chmod()
950           because xchmod() is too shy. */
951        chmod (info, 0666);
952    }
953
954    /* Make an empty val-tags file to prevent problems creating it later.  */
955    strcpy (info, adm);
956    strcat (info, "/");
957    strcat (info, CVSROOTADM_VALTAGS);
958    if (!isfile (info))
959    {
960	FILE *fp;
961
962	fp = open_file (info, "w");
963	if (fclose (fp) < 0)
964	    error (1, errno, "cannot close %s", info);
965
966        /* Make the new val-tags file world-writeable, since every CVS
967           user will need to be able to write to it.  We use chmod()
968           because xchmod() is too shy. */
969        chmod (info, 0666);
970    }
971
972    free (info);
973    free (info_v);
974
975    mkmodules (adm);
976
977    free (adm);
978    return 0;
979}
980