xref: /openbsd-src/gnu/usr.bin/cvs/src/mkmodules.c (revision a4afd6dad3fba28f80e70208181c06c482259988)
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 #include "cvs.h"
9 #include "savecwd.h"
10 
11 #ifndef DBLKSIZ
12 #define	DBLKSIZ	4096			/* since GNU ndbm doesn't define it */
13 #endif
14 
15 static int checkout_file PROTO((char *file, char *temp));
16 static void make_tempfile PROTO((char *temp));
17 static void rename_rcsfile PROTO((char *temp, char *real));
18 
19 #ifndef MY_NDBM
20 static void rename_dbmfile PROTO((char *temp));
21 static void write_dbmfile PROTO((char *temp));
22 #endif				/* !MY_NDBM */
23 
24 /* Structure which describes an administrative file.  */
25 struct admin_file {
26    /* Name of the file, within the CVSROOT directory.  */
27    char *filename;
28 
29    /* This is a one line description of what the file is for.  It is not
30       currently used, although one wonders whether it should be, somehow.
31       If NULL, then don't process this file in mkmodules (FIXME: a bit of
32       a kludge; probably should replace this with a flags field).  */
33    char *errormsg;
34 
35    /* Contents which the file should have in a new repository.  To avoid
36       problems with brain-dead compilers which choke on long string constants,
37       this is a pointer to an array of char * terminated by NULL--each of
38       the strings is concatenated.  */
39    const char * const *contents;
40 };
41 
42 static const char *const loginfo_contents[] = {
43     "# The \"loginfo\" file is used to control where \"cvs commit\" log information is\n",
44     "# sent.  The first entry on a line is a regular expression which is tested\n",
45     "# against the directory that the change is being made to, relative to the\n",
46     "# $CVSROOT.  For the first match that is found, the remainder of the line is a\n",
47     "# filter program that should expect log information on its standard input\n",
48     "#\n",
49     "# If the repository name does not match any of the regular expressions in the\n",
50     "# first field of this file, the \"DEFAULT\" line is used, if it is specified.\n",
51     "#\n",
52     "# If the name \"ALL\" appears as a regular expression it is always used\n",
53     "# in addition to the first matching regex or \"DEFAULT\".\n",
54     "#\n",
55     "# The filter program may use one and only one \"%s\" modifier (ala printf).  If\n",
56     "# such a \"%s\" is specified in the filter program, a brief title is included\n",
57     "# (as one argument, enclosed in single quotes) showing the relative directory\n",
58     "# name and listing the modified file names.\n",
59     "#\n",
60     "# For example:\n",
61     "#DEFAULT		(echo \"\"; who am i; echo %s; date; cat) >> $CVSROOT/CVSROOT/commitlog\n",
62     NULL
63 };
64 
65 static const char *const rcsinfo_contents[] = {
66     "# The \"rcsinfo\" file is used to control templates with which the editor\n",
67     "# is invoked on commit and import.\n",
68     "#\n",
69     "# The first entry on a line is a regular expression which is tested\n",
70     "# against the directory that the change is being made to, relative to the\n",
71     "# $CVSROOT.  For the first match that is found, then the remainder of the\n",
72     "# line is the name of the file that contains the template.\n",
73     "#\n",
74     "# If the repository name does not match any of the regular expressions in this\n",
75     "# file, the \"DEFAULT\" line is used, if it is specified.\n",
76     "#\n",
77     "# If the name \"ALL\" appears as a regular expression it is always used\n",
78     "# in addition to the first matching regex or \"DEFAULT\".\n",
79     NULL
80 };
81 
82 static const char *const editinfo_contents[] = {
83     "# The \"editinfo\" file is used to allow verification of logging\n",
84     "# information.  It works best when a template (as specified in the\n",
85     "# rcsinfo file) is provided for the logging procedure.  Given a\n",
86     "# template with locations for, a bug-id number, a list of people who\n",
87     "# reviewed the code before it can be checked in, and an external\n",
88     "# process to catalog the differences that were code reviewed, the\n",
89     "# following test can be applied to the code:\n",
90     "#\n",
91     "#   Making sure that the entered bug-id number is correct.\n",
92     "#   Validating that the code that was reviewed is indeed the code being\n",
93     "#       checked in (using the bug-id number or a seperate review\n",
94     "#       number to identify this particular code set.).\n",
95     "#\n",
96     "# If any of the above test failed, then the commit would be aborted.\n",
97     "#\n",
98     "# Actions such as mailing a copy of the report to each reviewer are\n",
99     "# better handled by an entry in the loginfo file.\n",
100     "#\n",
101     "# One thing that should be noted is the the ALL keyword is not\n",
102     "# supported.  There can be only one entry that matches a given\n",
103     "# repository.\n",
104     NULL
105 };
106 
107 static const char *const commitinfo_contents[] = {
108     "# The \"commitinfo\" file is used to control pre-commit checks.\n",
109     "# The filter on the right is invoked with the repository and a list \n",
110     "# of files to check.  A non-zero exit of the filter program will \n",
111     "# cause the commit to be aborted.\n",
112     "#\n",
113     "# The first entry on a line is a regular expression which is tested\n",
114     "# against the directory that the change is being committed to, relative\n",
115     "# to the $CVSROOT.  For the first match that is found, then the remainder\n",
116     "# of the line is the name of the filter to run.\n",
117     "#\n",
118     "# If the repository name does not match any of the regular expressions in this\n",
119     "# file, the \"DEFAULT\" line is used, if it is specified.\n",
120     "#\n",
121     "# If the name \"ALL\" appears as a regular expression it is always used\n",
122     "# in addition to the first matching regex or \"DEFAULT\".\n",
123     NULL
124 };
125 
126 static const char *const taginfo_contents[] = {
127     "# The \"taginfo\" file is used to control pre-tag checks.\n",
128     "# The filter on the right is invoked with the following arguments:\n",
129     "#\n",
130     "# $1 -- tagname\n",
131     "# $2 -- operation \"add\" for tag, \"mov\" for tag -F, and \"del\" for tag -d\n",
132     "# $3 -- repository\n",
133     "# $4->  file revision [file revision ...]\n",
134     "#\n",
135     "# A non-zero exit of the filter program will cause the tag to be aborted.\n",
136     "#\n",
137     "# The first entry on a line is a regular expression which is tested\n",
138     "# against the directory that the change is being committed to, relative\n",
139     "# to the $CVSROOT.  For the first match that is found, then the remainder\n",
140     "# of the line is the name of the filter to run.\n",
141     "#\n",
142     "# If the repository name does not match any of the regular expressions in this\n",
143     "# file, the \"DEFAULT\" line is used, if it is specified.\n",
144     "#\n",
145     "# If the name \"ALL\" appears as a regular expression it is always used\n",
146     "# in addition to the first matching regex or \"DEFAULT\".\n",
147     NULL
148 };
149 
150 static const char *const checkoutlist_contents[] = {
151     "# The \"checkoutlist\" file is used to support additional version controlled\n",
152     "# administrative files in $CVSROOT/CVSROOT, such as template files.\n",
153     "#\n",
154     "# The first entry on a line is a filename which will be checked out from\n",
155     "# the corresponding RCS file in the $CVSROOT/CVSROOT directory.\n",
156     "# The remainder of the line is an error message to use if the file cannot\n",
157     "# be checked out.\n",
158     "#\n",
159     "# File format:\n",
160     "#\n",
161     "#	[<whitespace>]<filename><whitespace><error message><end-of-line>\n",
162     "#\n",
163     "# comment lines begin with '#'\n",
164     NULL
165 };
166 
167 static const char *const cvswrappers_contents[] = {
168     "# This file describes wrappers and other binary files to CVS.\n",
169     "#\n",
170     "# Wrappers are the concept where directories of files are to be\n",
171     "# treated as a single file.  The intended use is to wrap up a wrapper\n",
172     "# into a single tar such that the tar archive can be treated as a\n",
173     "# single binary file in CVS.\n",
174     "#\n",
175     "# To solve the problem effectively, it was also necessary to be able to\n",
176     "# prevent rcsmerge from merging these files.\n",
177     "#\n",
178     "# Format of wrapper file ($CVSROOT/CVSROOT/cvswrappers or .cvswrappers)\n",
179     "#\n",
180     "#  wildcard	[option value][option value]...\n",
181     "#\n",
182     "#  where option is one of\n",
183     "#  -f		from cvs filter		value: path to filter\n",
184     "#  -t		to cvs filter		value: path to filter\n",
185     "#  -m		update methodology	value: MERGE or COPY\n",
186     "#\n",
187     "#  and value is a single-quote delimited value.\n",
188     "#\n",
189     "# For example:\n",
190     NULL
191 };
192 
193 static const char *const notify_contents[] = {
194     "# The \"notify\" file controls where notifications from watches set by\n",
195     "# \"cvs watch add\" or \"cvs edit\" are sent.  The first entry on a line is\n",
196     "# a regular expression which is tested against the directory that the\n",
197     "# change is being made to, relative to the $CVSROOT.  If it matches,\n",
198     "# then the remainder of the line is a filter program that should contain\n",
199     "# one occurrence of %s for the user to notify, and information on its\n",
200     "# standard input.\n",
201     "#\n",
202     "# \"ALL\" or \"DEFAULT\" can be used in place of the regular expression.\n",
203     "#\n",
204     "# For example:\n",
205     "#ALL mail %s -s \"CVS notification\"\n",
206     NULL
207 };
208 
209 static const char *const modules_contents[] = {
210     "# Three different line formats are valid:\n",
211     "#	key	-a    aliases...\n",
212     "#	key [options] directory\n",
213     "#	key [options] directory files...\n",
214     "#\n",
215     "# Where \"options\" are composed of:\n",
216     "#	-i prog		Run \"prog\" on \"cvs commit\" from top-level of module.\n",
217     "#	-o prog		Run \"prog\" on \"cvs checkout\" of module.\n",
218     "#	-e prog		Run \"prog\" on \"cvs export\" of module.\n",
219     "#	-t prog		Run \"prog\" on \"cvs rtag\" of module.\n",
220     "#	-u prog		Run \"prog\" on \"cvs update\" of module.\n",
221     "#	-d dir		Place module in directory \"dir\" instead of module name.\n",
222     "#	-l		Top-level directory only -- do not recurse.\n",
223     "#\n",
224     "# NOTE:  If you change any of the \"Run\" options above, you'll have to\n",
225     "# release and re-checkout any working directories of these modules.\n",
226     "#\n",
227     "# And \"directory\" is a path to a directory relative to $CVSROOT.\n",
228     "#\n",
229     "# The \"-a\" option specifies an alias.  An alias is interpreted as if\n",
230     "# everything on the right of the \"-a\" had been typed on the command line.\n",
231     "#\n",
232     "# You can encode a module within a module by using the special '&'\n",
233     "# character to interpose another module into the current module.  This\n",
234     "# can be useful for creating a module that consists of many directories\n",
235     "# spread out over the entire source repository.\n",
236     NULL
237 };
238 
239 static const struct admin_file filelist[] = {
240     {CVSROOTADM_LOGINFO,
241 	"no logging of 'cvs commit' messages is done without a %s file",
242 	&loginfo_contents[0]},
243     {CVSROOTADM_RCSINFO,
244 	"a %s file can be used to configure 'cvs commit' templates",
245 	rcsinfo_contents},
246     {CVSROOTADM_EDITINFO,
247 	"a %s file can be used to validate log messages",
248 	editinfo_contents},
249     {CVSROOTADM_COMMITINFO,
250 	"a %s file can be used to configure 'cvs commit' checking",
251 	commitinfo_contents},
252     {CVSROOTADM_TAGINFO,
253 	"a %s file can be used to configure 'cvs tag' checking",
254 	taginfo_contents},
255     {CVSROOTADM_IGNORE,
256 	"a %s file can be used to specify files to ignore",
257 	NULL},
258     {CVSROOTADM_CHECKOUTLIST,
259 	"a %s file can specify extra CVSROOT files to auto-checkout",
260 	checkoutlist_contents},
261     {CVSROOTADM_WRAPPER,
262 	"a %s file can be used to specify files to treat as wrappers",
263 	cvswrappers_contents},
264     {CVSROOTADM_NOTIFY,
265 	"a %s file can be used to specify where notifications go",
266 	notify_contents},
267     {CVSROOTADM_MODULES,
268 	/* modules is special-cased in mkmodules.  */
269 	NULL,
270 	modules_contents},
271     {NULL, NULL}
272 };
273 
274 /* Rebuild the checked out administrative files in directory DIR.  */
275 int
276 mkmodules (dir)
277     char *dir;
278 {
279     struct saved_cwd cwd;
280     /* FIXME: arbitrary limit */
281     char temp[PATH_MAX];
282     char *cp, *last, *fname;
283 #ifdef MY_NDBM
284     DBM *db;
285 #endif
286     FILE *fp;
287     /* FIXME: arbitrary limit */
288     char line[512];
289     const struct admin_file *fileptr;
290 
291     if (save_cwd (&cwd))
292 	exit (EXIT_FAILURE);
293 
294     if ( CVS_CHDIR (dir) < 0)
295 	error (1, errno, "cannot chdir to %s", dir);
296 
297     /*
298      * First, do the work necessary to update the "modules" database.
299      */
300     make_tempfile (temp);
301     switch (checkout_file (CVSROOTADM_MODULES, temp))
302     {
303 
304 	case 0:			/* everything ok */
305 #ifdef MY_NDBM
306 	    /* open it, to generate any duplicate errors */
307 	    if ((db = dbm_open (temp, O_RDONLY, 0666)) != NULL)
308 		dbm_close (db);
309 #else
310 	    write_dbmfile (temp);
311 	    rename_dbmfile (temp);
312 #endif
313 	    rename_rcsfile (temp, CVSROOTADM_MODULES);
314 	    break;
315 
316 	case -1:			/* fork failed */
317 	    (void) unlink_file (temp);
318 	    exit (EXIT_FAILURE);
319 	    /* NOTREACHED */
320 
321 	default:
322 	    error (0, 0,
323 		"'cvs checkout' is less functional without a %s file",
324 		CVSROOTADM_MODULES);
325 	    break;
326     }					/* switch on checkout_file() */
327 
328     (void) unlink_file (temp);
329 
330     /* Checkout the files that need it in CVSROOT dir */
331     for (fileptr = filelist; fileptr && fileptr->filename; fileptr++) {
332 	if (fileptr->errormsg == NULL)
333 	    continue;
334 	make_tempfile (temp);
335 	if (checkout_file (fileptr->filename, temp) == 0)
336 	    rename_rcsfile (temp, fileptr->filename);
337 #if 0
338 	/*
339 	 * If there was some problem other than the file not existing,
340 	 * checkout_file already printed a real error message.  If the
341 	 * file does not exist, it is harmless--it probably just means
342 	 * that the repository was created with an old version of CVS
343 	 * which didn't have so many files in CVSROOT.
344 	 */
345 	else if (fileptr->errormsg)
346 	    error (0, 0, fileptr->errormsg, fileptr->filename);
347 #endif
348 	(void) unlink_file (temp);
349     }
350 
351     /* Use 'fopen' instead of 'open_file' because we want to ignore error */
352     fp = CVS_FOPEN (CVSROOTADM_CHECKOUTLIST, "r");
353     if (fp)
354     {
355 	/*
356 	 * File format:
357 	 *  [<whitespace>]<filename><whitespace><error message><end-of-line>
358 	 *
359 	 * comment lines begin with '#'
360 	 */
361 	while (fgets (line, sizeof (line), fp) != NULL)
362 	{
363 	    /* skip lines starting with # */
364 	    if (line[0] == '#')
365 		continue;
366 
367 	    if ((last = strrchr (line, '\n')) != NULL)
368 		*last = '\0';			/* strip the newline */
369 
370 	    /* Skip leading white space. */
371 	    for (fname = line; *fname && isspace(*fname); fname++)
372 		;
373 
374 	    /* Find end of filename. */
375 	    for (cp = fname; *cp && !isspace(*cp); cp++)
376 		;
377 	    *cp = '\0';
378 
379 	    make_tempfile (temp);
380 	    if (checkout_file (fname, temp) == 0)
381 	    {
382 		rename_rcsfile (temp, fname);
383 	    }
384 	    else
385 	    {
386 		for (cp++; cp < last && *last && isspace(*last); cp++)
387 		    ;
388 		if (cp < last && *cp)
389 		    error (0, 0, cp, fname);
390 	    }
391 	}
392 	(void) fclose (fp);
393     }
394 
395     if (restore_cwd (&cwd, NULL))
396 	exit (EXIT_FAILURE);
397     free_cwd (&cwd);
398 
399     return (0);
400 }
401 
402 /*
403  * Yeah, I know, there are NFS race conditions here.
404  */
405 static void
406 make_tempfile (temp)
407     char *temp;
408 {
409     static int seed = 0;
410     int fd;
411 
412     if (seed == 0)
413 	seed = getpid ();
414     while (1)
415     {
416 	(void) sprintf (temp, "%s%d", BAKPREFIX, seed++);
417 	if ((fd = CVS_OPEN (temp, O_CREAT|O_EXCL|O_RDWR, 0666)) != -1)
418 	    break;
419 	if (errno != EEXIST)
420 	    error (1, errno, "cannot create temporary file %s", temp);
421     }
422     if (close(fd) < 0)
423 	error(1, errno, "cannot close temporary file %s", temp);
424 }
425 
426 static int
427 checkout_file (file, temp)
428     char *file;
429     char *temp;
430 {
431     char rcs[PATH_MAX];
432     int retcode = 0;
433 
434     (void) sprintf (rcs, "%s%s", file, RCSEXT);
435     if (!isfile (rcs))
436 	return (1);
437     run_setup ("%s%s -x,v/ -q -p", Rcsbin, RCS_CO);
438     run_arg (rcs);
439     if ((retcode = run_exec (RUN_TTY, temp, RUN_TTY, RUN_NORMAL)) != 0)
440     {
441 	error (0, retcode == -1 ? errno : 0, "failed to check out %s file", file);
442     }
443     return (retcode);
444 }
445 
446 #ifndef MY_NDBM
447 
448 static void
449 write_dbmfile (temp)
450     char *temp;
451 {
452     char line[DBLKSIZ], value[DBLKSIZ];
453     FILE *fp;
454     DBM *db;
455     char *cp, *vp;
456     datum key, val;
457     int len, cont, err = 0;
458 
459     fp = open_file (temp, "r");
460     if ((db = dbm_open (temp, O_RDWR | O_CREAT | O_TRUNC, 0666)) == NULL)
461 	error (1, errno, "cannot open dbm file %s for creation", temp);
462     for (cont = 0; fgets (line, sizeof (line), fp) != NULL;)
463     {
464 	if ((cp = strrchr (line, '\n')) != NULL)
465 	    *cp = '\0';			/* strip the newline */
466 
467 	/*
468 	 * Add the line to the value, at the end if this is a continuation
469 	 * line; otherwise at the beginning, but only after any trailing
470 	 * backslash is removed.
471 	 */
472 	vp = value;
473 	if (cont)
474 	    vp += strlen (value);
475 
476 	/*
477 	 * See if the line we read is a continuation line, and strip the
478 	 * backslash if so.
479 	 */
480 	len = strlen (line);
481 	if (len > 0)
482 	    cp = &line[len - 1];
483 	else
484 	    cp = line;
485 	if (*cp == '\\')
486 	{
487 	    cont = 1;
488 	    *cp = '\0';
489 	}
490 	else
491 	{
492 	    cont = 0;
493 	}
494 	(void) strcpy (vp, line);
495 	if (value[0] == '#')
496 	    continue;			/* comment line */
497 	vp = value;
498 	while (*vp && isspace (*vp))
499 	    vp++;
500 	if (*vp == '\0')
501 	    continue;			/* empty line */
502 
503 	/*
504 	 * If this was not a continuation line, add the entry to the database
505 	 */
506 	if (!cont)
507 	{
508 	    key.dptr = vp;
509 	    while (*vp && !isspace (*vp))
510 		vp++;
511 	    key.dsize = vp - key.dptr;
512 	    *vp++ = '\0';		/* NULL terminate the key */
513 	    while (*vp && isspace (*vp))
514 		vp++;			/* skip whitespace to value */
515 	    if (*vp == '\0')
516 	    {
517 		error (0, 0, "warning: NULL value for key `%s'", key.dptr);
518 		continue;
519 	    }
520 	    val.dptr = vp;
521 	    val.dsize = strlen (vp);
522 	    if (dbm_store (db, key, val, DBM_INSERT) == 1)
523 	    {
524 		error (0, 0, "duplicate key found for `%s'", key.dptr);
525 		err++;
526 	    }
527 	}
528     }
529     dbm_close (db);
530     (void) fclose (fp);
531     if (err)
532     {
533 	char dotdir[50], dotpag[50], dotdb[50];
534 
535 	(void) sprintf (dotdir, "%s.dir", temp);
536 	(void) sprintf (dotpag, "%s.pag", temp);
537 	(void) sprintf (dotdb, "%s.db", temp);
538 	(void) unlink_file (dotdir);
539 	(void) unlink_file (dotpag);
540 	(void) unlink_file (dotdb);
541 	error (1, 0, "DBM creation failed; correct above errors");
542     }
543 }
544 
545 static void
546 rename_dbmfile (temp)
547     char *temp;
548 {
549     char newdir[50], newpag[50], newdb[50];
550     char dotdir[50], dotpag[50], dotdb[50];
551     char bakdir[50], bakpag[50], bakdb[50];
552 
553     (void) sprintf (dotdir, "%s.dir", CVSROOTADM_MODULES);
554     (void) sprintf (dotpag, "%s.pag", CVSROOTADM_MODULES);
555     (void) sprintf (dotdb, "%s.db", CVSROOTADM_MODULES);
556     (void) sprintf (bakdir, "%s%s.dir", BAKPREFIX, CVSROOTADM_MODULES);
557     (void) sprintf (bakpag, "%s%s.pag", BAKPREFIX, CVSROOTADM_MODULES);
558     (void) sprintf (bakdb, "%s%s.db", BAKPREFIX, CVSROOTADM_MODULES);
559     (void) sprintf (newdir, "%s.dir", temp);
560     (void) sprintf (newpag, "%s.pag", temp);
561     (void) sprintf (newdb, "%s.db", temp);
562 
563     (void) chmod (newdir, 0666);
564     (void) chmod (newpag, 0666);
565     (void) chmod (newdb, 0666);
566 
567     /* don't mess with me */
568     SIG_beginCrSect ();
569 
570     (void) unlink_file (bakdir);	/* rm .#modules.dir .#modules.pag */
571     (void) unlink_file (bakpag);
572     (void) unlink_file (bakdb);
573     (void) CVS_RENAME (dotdir, bakdir);	/* mv modules.dir .#modules.dir */
574     (void) CVS_RENAME (dotpag, bakpag);	/* mv modules.pag .#modules.pag */
575     (void) CVS_RENAME (dotdb, bakdb);	/* mv modules.db .#modules.db */
576     (void) CVS_RENAME (newdir, dotdir);	/* mv "temp".dir modules.dir */
577     (void) CVS_RENAME (newpag, dotpag);	/* mv "temp".pag modules.pag */
578     (void) CVS_RENAME (newdb, dotdb);	/* mv "temp".db modules.db */
579 
580     /* OK -- make my day */
581     SIG_endCrSect ();
582 }
583 
584 #endif				/* !MY_NDBM */
585 
586 static void
587 rename_rcsfile (temp, real)
588     char *temp;
589     char *real;
590 {
591     char bak[50];
592     struct stat statbuf;
593     char rcs[PATH_MAX];
594 
595     /* Set "x" bits if set in original. */
596     (void) sprintf (rcs, "%s%s", real, RCSEXT);
597     statbuf.st_mode = 0; /* in case rcs file doesn't exist, but it should... */
598     (void) CVS_STAT (rcs, &statbuf);
599 
600     if (chmod (temp, 0444 | (statbuf.st_mode & 0111)) < 0)
601 	error (0, errno, "warning: cannot chmod %s", temp);
602     (void) sprintf (bak, "%s%s", BAKPREFIX, real);
603     (void) unlink_file (bak);		/* rm .#loginfo */
604     (void) CVS_RENAME (real, bak);		/* mv loginfo .#loginfo */
605     (void) CVS_RENAME (temp, real);		/* mv "temp" loginfo */
606 }
607 
608 const char *const init_usage[] = {
609     "Usage: %s %s\n",
610     NULL
611 };
612 
613 int
614 init (argc, argv)
615     int argc;
616     char **argv;
617 {
618     /* Name of CVSROOT directory.  */
619     char *adm;
620     /* Name of this administrative file.  */
621     char *info;
622     /* Name of ,v file for this administrative file.  */
623     char *info_v;
624 
625     const struct admin_file *fileptr;
626 
627     umask (cvsumask);
628 
629     if (argc == -1 || argc > 1)
630 	usage (init_usage);
631 
632 #ifdef CLIENT_SUPPORT
633     if (client_active)
634     {
635 	start_server ();
636 
637 	ign_setup ();
638 	send_init_command ();
639 	return get_responses_and_close ();
640     }
641 #endif /* CLIENT_SUPPORT */
642 
643     /* Note: we do *not* create parent directories as needed like the
644        old cvsinit.sh script did.  Few utilities do that, and a
645        non-existent parent directory is as likely to be a typo as something
646        which needs to be created.  */
647     mkdir_if_needed (CVSroot_directory);
648 
649     adm = xmalloc (strlen (CVSroot_directory) + sizeof (CVSROOTADM) + 10);
650     strcpy (adm, CVSroot_directory);
651     strcat (adm, "/");
652     strcat (adm, CVSROOTADM);
653     mkdir_if_needed (adm);
654 
655     /* This is needed by the call to "ci" below.  */
656     if ( CVS_CHDIR (adm) < 0)
657 	error (1, errno, "cannot change to directory %s", adm);
658 
659     /* 80 is long enough for all the administrative file names, plus
660        "/" and so on.  */
661     info = xmalloc (strlen (adm) + 80);
662     info_v = xmalloc (strlen (adm) + 80);
663     for (fileptr = filelist; fileptr && fileptr->filename; ++fileptr)
664     {
665 	if (fileptr->contents == NULL)
666 	    continue;
667 	strcpy (info, adm);
668 	strcat (info, "/");
669 	strcat (info, fileptr->filename);
670 	strcpy (info_v, info);
671 	strcat (info_v, RCSEXT);
672 	if (isfile (info_v))
673 	    /* We will check out this file in the mkmodules step.
674 	       Nothing else is required.  */
675 	    ;
676 	else
677 	{
678 	    int retcode;
679 
680 	    if (!isfile (info))
681 	    {
682 		FILE *fp;
683 		const char * const *p;
684 
685 		fp = open_file (info, "w");
686 		for (p = fileptr->contents; *p != NULL; ++p)
687 		    if (fputs (*p, fp) < 0)
688 			error (1, errno, "cannot write %s", info);
689 		if (fclose (fp) < 0)
690 		    error (1, errno, "cannot close %s", info);
691 	    }
692 	    /* Now check the file in.  FIXME: we could be using
693 	       add_rcs_file from import.c which is faster (if it were
694 	       tweaked slightly).  */
695 	    run_setup ("%s%s -x,v/ -q -u -t-", Rcsbin, RCS_CI);
696 	    run_args ("-minitial checkin of %s", fileptr->filename);
697 	    run_arg (fileptr->filename);
698 	    retcode = run_exec (RUN_TTY, RUN_TTY, RUN_TTY, RUN_NORMAL);
699 	    if (retcode != 0)
700 		error (1, retcode == -1 ? errno : 0,
701 		       "failed to check in %s", info);
702 	}
703     }
704 
705     /* Turn on history logging by default.  The user can remove the file
706        to disable it.  */
707     strcpy (info, adm);
708     strcat (info, "/");
709     strcat (info, CVSROOTADM_HISTORY);
710     if (!isfile (info))
711     {
712 	FILE *fp;
713 
714 	fp = open_file (info, "w");
715 	if (fclose (fp) < 0)
716 	    error (1, errno, "cannot close %s", info);
717     }
718 
719     free (info);
720     free (info_v);
721 
722     mkmodules (adm);
723 
724     free (adm);
725     return 0;
726 }
727