1/*
2 * Copyright (C) 1986-2005 The Free Software Foundation, Inc.
3 *
4 * Portions Copyright (C) 1998-2005 Derek Price, Ximbiot <http://ximbiot.com>,
5 *                                  and others.
6 *
7 * Portions Copyright (C) 1992, Brian Berliner and Jeff Polk
8 * Portions Copyright (C) 1989-1992, Brian Berliner
9 *
10 * You may distribute under the terms of the GNU General Public License as
11 * specified in the README file that comes with the CVS kit.  */
12
13#include "cvs.h"
14#include "getline.h"
15#include "history.h"
16#include "save-cwd.h"
17
18#ifndef DBLKSIZ
19#define	DBLKSIZ	4096			/* since GNU ndbm doesn't define it */
20#endif
21
22static int checkout_file (char *file, char *temp);
23static char *make_tempfile (void);
24static void rename_rcsfile (char *temp, char *real);
25
26#ifndef MY_NDBM
27static void rename_dbmfile (char *temp);
28static void write_dbmfile (char *temp);
29#endif				/* !MY_NDBM */
30
31/* Structure which describes an administrative file.  */
32struct admin_file {
33   /* Name of the file, within the CVSROOT directory.  */
34   char *filename;
35
36   /* This is a one line description of what the file is for.  It is not
37      currently used, although one wonders whether it should be, somehow.
38      If NULL, then don't process this file in mkmodules (FIXME?: a bit of
39      a kludge; probably should replace this with a flags field).  */
40   char *errormsg;
41
42   /* Contents which the file should have in a new repository.  To avoid
43      problems with brain-dead compilers which choke on long string constants,
44      this is a pointer to an array of char * terminated by NULL--each of
45      the strings is concatenated.
46
47      If this field is NULL, the file is not created in a new
48      repository, but it can be added with "cvs add" (just as if one
49      had created the repository with a version of CVS which didn't
50      know about the file) and the checked-out copy will be updated
51      without having to add it to checkoutlist.  */
52   const char * const *contents;
53};
54
55static const char *const loginfo_contents[] = {
56    "# The \"loginfo\" file controls where \"cvs commit\" log information is\n",
57    "# sent. The first entry on a line is a regular expression which must\n",
58    "# match the directory that the change is being made to, relative to the\n",
59    "# $CVSROOT.  If a match is found, then the remainder of the line is a\n",
60    "# filter program that should expect log information on its standard input.\n",
61    "#\n",
62    "# If the repository name does not match any of the regular expressions in this\n",
63    "# file, the \"DEFAULT\" line is used, if it is specified.\n",
64    "#\n",
65    "# If the name ALL appears as a regular expression it is always used\n",
66    "# in addition to the first matching regex or DEFAULT.\n",
67    "#\n",
68    "# If any format strings are present in the filter, they will be replaced\n",
69    "# as follows:\n",
70    "#    %c = canonical name of the command being executed\n",
71#ifdef PROXY_SUPPORT
72    "#    %R = the name of the referrer, if any, otherwise the value NONE\n",
73#endif
74    "#    %p = path relative to repository\n",
75    "#    %r = repository (path portion of $CVSROOT)\n",
76    "#    %{sVv} = attribute list = file name, old version number (pre-checkin),\n",
77    "#           new version number (post-checkin).  When either old or new revision\n",
78    "#           is unknown, doesn't exist, or isn't applicable, the string \"NONE\"\n",
79    "#           will be placed on the command line instead.\n",
80    "#\n",
81    "# Note that %{sVv} is a list operator and not all elements are necessary.\n",
82    "# Thus %{sv} is a legal format string, but will only be replaced with\n",
83    "# file name and new revision.\n",
84    "# It also generates multiple arguments for each file being operated upon.\n",
85    "# That is, if two files, file1 & file2, are being commited from 1.1 to\n",
86    "# version 1.1.2.1 and from 1.1.2.2 to 1.1.2.3, respectively, %{sVv} will\n",
87    "# generate the following six arguments in this order:\n",
88    "# file1, 1.1, 1.1.2.1, file2, 1.1.2.2, 1.1.2.3.\n",
89    "#\n",
90    "# For example:\n",
91    "#DEFAULT (echo \"\"; id; echo %s; date; cat) >> $CVSROOT/CVSROOT/commitlog\n",
92    "# or\n",
93    "#DEFAULT (echo \"\"; id; echo %{sVv}; date; cat) >> $CVSROOT/CVSROOT/commitlog\n",
94    NULL
95};
96
97static const char *const rcsinfo_contents[] = {
98    "# The \"rcsinfo\" file is used to control templates with which the editor\n",
99    "# is invoked on commit and import.\n",
100    "#\n",
101    "# The first entry on a line is a regular expression which is tested\n",
102    "# against the directory that the change is being made to, relative to the\n",
103    "# $CVSROOT.  For the first match that is found, then the remainder of the\n",
104    "# line is the name of the file that contains the template.\n",
105    "#\n",
106    "# If the repository name does not match any of the regular expressions in this\n",
107    "# file, the \"DEFAULT\" line is used, if it is specified.\n",
108    "#\n",
109    "# If the name \"ALL\" appears as a regular expression it is always used\n",
110    "# in addition to the first matching regex or \"DEFAULT\".\n",
111    NULL
112};
113
114
115
116static const char *const verifymsg_contents[] = {
117    "# The \"verifymsg\" file is used to allow verification of logging\n",
118    "# information.  It works best when a template (as specified in the\n",
119    "# rcsinfo file) is provided for the logging procedure.  Given a\n",
120    "# template with locations for, a bug-id number, a list of people who\n",
121    "# reviewed the code before it can be checked in, and an external\n",
122    "# process to catalog the differences that were code reviewed, the\n",
123    "# following test can be applied to the code:\n",
124    "#\n",
125    "#   Making sure that the entered bug-id number is correct.\n",
126    "#   Validating that the code that was reviewed is indeed the code being\n",
127    "#       checked in (using the bug-id number or a seperate review\n",
128    "#       number to identify this particular code set.).\n",
129    "#\n",
130    "# If any of the above test failed, then the commit would be aborted.\n",
131    "#\n",
132    "# Format strings present in the filter will be replaced as follows:\n",
133    "#    %c = canonical name of the command being executed\n",
134#ifdef PROXY_SUPPORT
135    "#    %R = the name of the referrer, if any, otherwise the value NONE\n",
136#endif
137    "#    %p = path relative to repository\n",
138    "#    %r = repository (path portion of $CVSROOT)\n",
139    "#    %l = name of log file to be verified.\n",
140    "#\n",
141    "# If no format strings are present in the filter, a default \" %l\" will\n",
142    "# be appended to the filter, but this usage is deprecated.\n",
143    "#\n",
144    "# Actions such as mailing a copy of the report to each reviewer are\n",
145    "# better handled by an entry in the loginfo file.\n",
146    "#\n",
147    "# One thing that should be noted is the the ALL keyword is not\n",
148    "# supported.  There can be only one entry that matches a given\n",
149    "# repository.\n",
150    NULL
151};
152
153static const char *const commitinfo_contents[] = {
154    "# The \"commitinfo\" file is used to control pre-commit checks.\n",
155    "# The filter on the right is invoked with the repository and a list \n",
156    "# of files to check.  A non-zero exit of the filter program will \n",
157    "# cause the commit to be aborted.\n",
158    "#\n",
159    "# The first entry on a line is a regular expression which is tested\n",
160    "# against the directory that the change is being committed to, relative\n",
161    "# to the $CVSROOT.  For the first match that is found, then the remainder\n",
162    "# of the line is the name of the filter to run.\n",
163    "#\n",
164    "# Format strings present in the filter will be replaced as follows:\n",
165    "#    %c = canonical name of the command being executed\n",
166#ifdef PROXY_SUPPORT
167    "#    %R = the name of the referrer, if any, otherwise the value NONE\n",
168#endif
169    "#    %p = path relative to repository\n",
170    "#    %r = repository (path portion of $CVSROOT)\n",
171    "#    %{s} = file name, file name, ...\n",
172    "#\n",
173    "# If no format strings are present in the filter string, a default of\n",
174    "# \" %r %s\" will be appended to the filter string, but this usage is\n",
175    "# deprecated.\n",
176    "#\n",
177    "# If the repository name does not match any of the regular expressions in this\n",
178    "# file, the \"DEFAULT\" line is used, if it is specified.\n",
179    "#\n",
180    "# If the name \"ALL\" appears as a regular expression it is always used\n",
181    "# in addition to the first matching regex or \"DEFAULT\".\n",
182    NULL
183};
184
185static const char *const taginfo_contents[] = {
186    "# The \"taginfo\" file is used to control pre-tag checks.\n",
187    "# The filter on the right is invoked with the following arguments\n",
188    "# if no format strings are present:\n",
189    "#\n",
190    "# $1 -- tagname\n",
191    "# $2 -- operation \"add\" for tag, \"mov\" for tag -F, and \"del\" for tag -d\n",
192    "# $3 -- tagtype \"?\" on delete, \"T\" for branch, \"N\" for static\n",
193    "# $4 -- repository\n",
194    "# $5->  file revision [file revision ...]\n",
195    "#\n",
196    "# If any format strings are present in the filter, they will be replaced\n",
197    "# as follows:\n",
198    "#    %b = branch mode = \"?\" (delete ops - unknown) | \"T\" (branch)\n",
199    "#                     | \"N\" (not branch)\n",
200    "#    %o = operation = \"add\" | \"mov\" | \"del\"\n",
201    "#    %c = canonical name of the command being executed\n",
202#ifdef PROXY_SUPPORT
203    "#    %R = the name of the referrer, if any, otherwise the value NONE\n",
204#endif
205    "#    %p = path relative to repository\n",
206    "#    %r = repository (path portion of $CVSROOT)\n",
207    "#    %t = tagname\n",
208    "#    %{sVv} = attribute list = file name, old version tag will be deleted\n",
209    "#             from, new version tag will be added to (or deleted from, but\n",
210    "#             this feature is deprecated.  When either old or new revision is\n",
211    "#             unknown, doesn't exist, or isn't applicable, the string \"NONE\"\n",
212    "#             will be placed on the command line.\n",
213    "#\n",
214    "# Note that %{sVv} is a list operator and not all elements are necessary.\n",
215    "# Thus %{sV} is a legal format string, but will only be replaced with file\n",
216    "# name and old revision. it also generates multiple arguments for each file\n",
217    "# being operated upon.  i.e. if two files, file1 & file2, are having a tag\n",
218    "# moved from version 1.1 to version 1.1.2.9, %{sVv} will generate the\n",
219    "# following six arguments in this order:\n",
220    "# file1, 1.1, 1.1.2.9, file2, 1.1, 1.1.2.9.\n",
221    "#\n",
222    "# A non-zero exit of the filter program will cause the tag to be aborted.\n",
223    "#\n",
224    "# The first entry on a line is a regular expression which is tested\n",
225    "# against the directory that the change is being committed to, relative\n",
226    "# to the $CVSROOT.  For the first match that is found, then the remainder\n",
227    "# of the line is the name of the filter to run.\n",
228    "#\n",
229    "# If the repository name does not match any of the regular expressions in this\n",
230    "# file, the \"DEFAULT\" line is used, if it is specified.\n",
231    "#\n",
232    "# If the name \"ALL\" appears as a regular expression it is always used\n",
233    "# in addition to the first matching regex or \"DEFAULT\".\n",
234    NULL
235};
236
237static const char *const preproxy_contents[] = {
238    "# The \"preproxy\" file is called form the secondary server as soon as\n",
239    "# the secondary server determines that it will be proxying a write\n",
240    "# command to a primary server and immediately before it opens a\n",
241    "# connection to the primary server.  This script might, for example, be\n",
242    "# used to launch a dial up or VPN connection to the primary server's\n",
243    "# network.\n",
244    "#\n",
245    "# If any format strings are present in the filter, they will be replaced\n",
246    "# as follows:\n",
247    "#    %c = canonical name of the command being executed\n",
248#ifdef PROXY_SUPPORT
249    "#    %R = the name of the referrer, if any, otherwise the value NONE\n",
250#endif
251    "#    %p = path relative to repository (currently always \".\")\n",
252    "#    %r = repository (path portion of $CVSROOT)\n",
253    "#\n",
254    "# The first entry on a line is a regular expression which is tested\n",
255    "# against the directory that the change is being committed to, relative\n",
256    "# to the $CVSROOT.  For the first match that is found, then the remainder\n",
257    "# of the line is the name of the filter to run.\n",
258    "#\n",
259    "# If the repository name does not match any of the regular expressions in this\n",
260    "# file, the \"DEFAULT\" line is used, if it is specified.\n",
261    "#\n",
262    "# If the name \"ALL\" appears as a regular expression it is always used\n",
263    "# in addition to the first matching regex or \"DEFAULT\".\n",
264    NULL
265};
266
267static const char *const postadmin_contents[] = {
268    "# The \"postadmin\" file is called after the \"admin\" command finishes\n",
269    "# processing a directory.\n",
270    "#\n",
271    "# If any format strings are present in the filter, they will be replaced\n",
272    "# as follows:\n",
273    "#    %c = canonical name of the command being executed\n",
274#ifdef PROXY_SUPPORT
275    "#    %R = the name of the referrer, if any, otherwise the value NONE\n",
276#endif
277    "#    %p = path relative to repository\n",
278    "#    %r = repository (path portion of $CVSROOT)\n",
279    "#\n",
280    "# The first entry on a line is a regular expression which is tested\n",
281    "# against the directory that the change is being committed to, relative\n",
282    "# to the $CVSROOT.  For the first match that is found, then the remainder\n",
283    "# of the line is the name of the filter to run.\n",
284    "#\n",
285    "# If the repository name does not match any of the regular expressions in this\n",
286    "# file, the \"DEFAULT\" line is used, if it is specified.\n",
287    "#\n",
288    "# If the name \"ALL\" appears as a regular expression it is always used\n",
289    "# in addition to the first matching regex or \"DEFAULT\".\n",
290    NULL
291};
292
293static const char *const postproxy_contents[] = {
294    "# The \"postproxy\" file is called from a secondary server as soon as\n",
295    "# the secondary server closes its connection to the primary server.\n",
296    "# This script might, for example, be used to shut down a dial up\n",
297    "# or VPN connection to the primary server's network.\n",
298    "#\n",
299    "# If any format strings are present in the filter, they will be replaced\n",
300    "# as follows:\n",
301    "#    %c = canonical name of the command being executed\n",
302#ifdef PROXY_SUPPORT
303    "#    %R = the name of the referrer, if any, otherwise the value NONE\n",
304#endif
305    "#    %p = path relative to repository (currently always \".\")\n",
306    "#    %r = repository (path portion of $CVSROOT)\n",
307    "#\n",
308    "# The first entry on a line is a regular expression which is tested\n",
309    "# against the directory that the change is being committed to, relative\n",
310    "# to the $CVSROOT.  For the first match that is found, then the remainder\n",
311    "# of the line is the name of the filter to run.\n",
312    "#\n",
313    "# If the repository name does not match any of the regular expressions in this\n",
314    "# file, the \"DEFAULT\" line is used, if it is specified.\n",
315    "#\n",
316    "# If the name \"ALL\" appears as a regular expression it is always used\n",
317    "# in addition to the first matching regex or \"DEFAULT\".\n",
318    NULL
319};
320
321static const char *const posttag_contents[] = {
322    "# The \"posttag\" file is called after the \"tag\" command finishes\n",
323    "# processing a directory.\n",
324    "#\n",
325    "# If any format strings are present in the filter, they will be replaced\n",
326    "# as follows:\n",
327    "#    %b = branch mode = \"?\" (delete ops - unknown) | \"T\" (branch)\n",
328    "#                     | \"N\" (not branch)\n",
329    "#    %o = operation = \"add\" | \"mov\" | \"del\"\n",
330    "#    %c = canonical name of the command being executed\n",
331#ifdef PROXY_SUPPORT
332    "#    %R = the name of the referrer, if any, otherwise the value NONE\n",
333#endif
334    "#    %p = path relative to repository\n",
335    "#    %r = repository (path portion of $CVSROOT)\n",
336    "#    %t = tagname\n",
337    "#    %{sVv} = attribute list = file name, old version tag will be deleted\n",
338    "#             from, new version tag will be added to (or deleted from, but\n",
339    "#             this feature is deprecated.  When either old or new revision is\n",
340    "#             unknown, doesn't exist, or isn't applicable, the string \"NONE\"\n",
341    "#             will be placed on the command line.\n",
342    "#\n",
343    "# Note that %{sVv} is a list operator and not all elements are necessary.\n",
344    "# Thus %{sV} is a legal format string, but will only be replaced with file\n",
345    "# name and old revision. it also generates multiple arguments for each file\n",
346    "# being operated upon.  i.e. if two files, file1 & file2, are having a tag\n",
347    "# moved from version 1.1 to version 1.1.2.9, %{sVv} will generate the\n",
348    "# following six arguments in this order:\n",
349    "# file1, 1.1, 1.1.2.9, file2, 1.1, 1.1.2.9.\n",
350    "#\n",
351    "# The first entry on a line is a regular expression which is tested\n",
352    "# against the directory that the change is being committed to, relative\n",
353    "# to the $CVSROOT.  For the first match that is found, then the remainder\n",
354    "# of the line is the name of the filter to run.\n",
355    "#\n",
356    "# If the repository name does not match any of the regular expressions in this\n",
357    "# file, the \"DEFAULT\" line is used, if it is specified.\n",
358    "#\n",
359    "# If the name \"ALL\" appears as a regular expression it is always used\n",
360    "# in addition to the first matching regex or \"DEFAULT\".\n",
361    NULL
362};
363
364static const char *const postwatch_contents[] = {
365    "# The \"postwatch\" file is called after any command finishes writing new\n",
366    "# file attibute (watch/edit) information in a directory.\n",
367    "#\n",
368    "# If any format strings are present in the filter, they will be replaced\n",
369    "# as follows:\n",
370    "#    %c = canonical name of the command being executed\n",
371#ifdef PROXY_SUPPORT
372    "#    %R = the name of the referrer, if any, otherwise the value NONE\n",
373#endif
374    "#    %p = path relative to repository\n",
375    "#    %r = repository (path portion of $CVSROOT)\n",
376    "#\n",
377    "# The first entry on a line is a regular expression which is tested\n",
378    "# against the directory that the change is being committed to, relative\n",
379    "# to the $CVSROOT.  For the first match that is found, then the remainder\n",
380    "# of the line is the name of the filter to run.\n",
381    "#\n",
382    "# If the repository name does not match any of the regular expressions in this\n",
383    "# file, the \"DEFAULT\" line is used, if it is specified.\n",
384    "#\n",
385    "# If the name \"ALL\" appears as a regular expression it is always used\n",
386    "# in addition to the first matching regex or \"DEFAULT\".\n",
387    NULL
388};
389
390static const char *const checkoutlist_contents[] = {
391    "# The \"checkoutlist\" file is used to support additional version controlled\n",
392    "# administrative files in $CVSROOT/CVSROOT, such as template files.\n",
393    "#\n",
394    "# The first entry on a line is a filename which will be checked out from\n",
395    "# the corresponding RCS file in the $CVSROOT/CVSROOT directory.\n",
396    "# The remainder of the line is an error message to use if the file cannot\n",
397    "# be checked out.\n",
398    "#\n",
399    "# File format:\n",
400    "#\n",
401    "#	[<whitespace>]<filename>[<whitespace><error message>]<end-of-line>\n",
402    "#\n",
403    "# comment lines begin with '#'\n",
404    NULL
405};
406
407static const char *const cvswrappers_contents[] = {
408    "# This file affects handling of files based on their names.\n",
409    "#\n",
410#if 0    /* see comments in wrap_add in wrapper.c */
411    "# The -t/-f options allow one to treat directories of files\n",
412    "# as a single file, or to transform a file in other ways on\n",
413    "# its way in and out of CVS.\n",
414    "#\n",
415#endif
416    "# The -m option specifies whether CVS attempts to merge files.\n",
417    "#\n",
418    "# The -k option specifies keyword expansion (e.g. -kb for binary).\n",
419    "#\n",
420    "# Format of wrapper file ($CVSROOT/CVSROOT/cvswrappers or .cvswrappers)\n",
421    "#\n",
422    "#  wildcard	[option value][option value]...\n",
423    "#\n",
424    "#  where option is one of\n",
425    "#  -f		from cvs filter		value: path to filter\n",
426    "#  -t		to cvs filter		value: path to filter\n",
427    "#  -m		update methodology	value: MERGE or COPY\n",
428    "#  -k		expansion mode		value: b, o, kkv, &c\n",
429    "#\n",
430    "#  and value is a single-quote delimited value.\n",
431    "# For example:\n",
432    "#*.gif -k 'b'\n",
433    NULL
434};
435
436static const char *const notify_contents[] = {
437    "# The \"notify\" file controls where notifications from watches set by\n",
438    "# \"cvs watch add\" or \"cvs edit\" are sent.  The first entry on a line is\n",
439    "# a regular expression which is tested against the directory that the\n",
440    "# change is being made to, relative to the $CVSROOT.  If it matches,\n",
441    "# then the remainder of the line is a filter program that should contain\n",
442    "# one occurrence of %s for the user to notify, and information on its\n",
443    "# standard input.\n",
444    "#\n",
445    "# \"ALL\" or \"DEFAULT\" can be used in place of the regular expression.\n",
446    "#\n",
447    "# format strings are replaceed as follows:\n",
448    "#    %c = canonical name of the command being executed\n",
449#ifdef PROXY_SUPPORT
450    "#    %R = the name of the referrer, if any, otherwise the value NONE\n",
451#endif
452    "#    %p = path relative to repository\n",
453    "#    %r = repository (path portion of $CVSROOT)\n",
454    "#    %s = user to notify\n",
455    "#\n",
456    "# For example:\n",
457    "#ALL (echo Committed to %r/%p; cat) |mail %s -s \"CVS notification\"\n",
458    NULL
459};
460
461static const char *const modules_contents[] = {
462    "# Three different line formats are valid:\n",
463    "#	key	-a    aliases...\n",
464    "#	key [options] directory\n",
465    "#	key [options] directory files...\n",
466    "#\n",
467    "# Where \"options\" are composed of:\n",
468    "#	-i prog		Run \"prog\" on \"cvs commit\" from top-level of module.\n",
469    "#	-o prog		Run \"prog\" on \"cvs checkout\" of module.\n",
470    "#	-e prog		Run \"prog\" on \"cvs export\" of module.\n",
471    "#	-t prog		Run \"prog\" on \"cvs rtag\" of module.\n",
472    "#	-u prog		Run \"prog\" on \"cvs update\" of module.\n",
473    "#	-d dir		Place module in directory \"dir\" instead of module name.\n",
474    "#	-l		Top-level directory only -- do not recurse.\n",
475    "#\n",
476    "# NOTE:  If you change any of the \"Run\" options above, you'll have to\n",
477    "# release and re-checkout any working directories of these modules.\n",
478    "#\n",
479    "# And \"directory\" is a path to a directory relative to $CVSROOT.\n",
480    "#\n",
481    "# The \"-a\" option specifies an alias.  An alias is interpreted as if\n",
482    "# everything on the right of the \"-a\" had been typed on the command line.\n",
483    "#\n",
484    "# You can encode a module within a module by using the special '&'\n",
485    "# character to interpose another module into the current module.  This\n",
486    "# can be useful for creating a module that consists of many directories\n",
487    "# spread out over the entire source repository.\n",
488    NULL
489};
490
491static const char *const config_contents[] = {
492    "# Set `SystemAuth' to `no' if pserver shouldn't check system users/passwords.\n",
493    "#SystemAuth=no\n",
494    "\n",
495    "# Set `LocalKeyword' to specify a local alias for a standard keyword.\n",
496    "#LocalKeyword=MYCVS=CVSHeader\n",
497    "\n",
498    "# Set `KeywordExpand' to `i' followed by a list of keywords to expand or\n",
499    "# `e' followed by a list of keywords to not expand.\n"
500    "#KeywordExpand=iMYCVS,Name,Date\n",
501    "#KeywordExpand=eCVSHeader\n",
502    "\n",
503#ifdef PRESERVE_PERMISSIONS_SUPPORT
504    "# Set `PreservePermissions' to `yes' to save file status information\n",
505    "# in the repository.\n",
506    "#PreservePermissions=no\n",
507    "\n",
508#endif
509    "# Set `TopLevelAdmin' to `yes' to create a CVS directory at the top\n",
510    "# level of the new working directory when using the `cvs checkout'\n",
511    "# command.\n",
512    "#TopLevelAdmin=no\n",
513    "\n",
514    "# Put CVS lock files in this directory rather than directly in the repository.\n",
515    "#LockDir=/var/lock/cvs\n",
516    "\n",
517    "# Set `LogHistory' to `all' or `" ALL_HISTORY_REC_TYPES "' to log all transactions to the\n",
518    "# history file, or a subset as needed (ie `TMAR' logs all write operations)\n",
519    "#LogHistory=" ALL_HISTORY_REC_TYPES "\n",
520    "\n",
521    "# Set `RereadLogAfterVerify' to `always' (the default) to allow the verifymsg\n",
522    "# script to change the log message.  Set it to `stat' to force CVS to verify\n",
523    "# that the file has changed before reading it (this can take up to an extra\n",
524    "# second per directory being committed, so it is not recommended for large\n",
525    "# repositories.  Set it to `never' (the previous CVS behavior) to prevent\n",
526    "# verifymsg scripts from changing the log message.\n",
527    "#RereadLogAfterVerify=always\n",
528    "\n",
529    "# Set `UserAdminOptions' to the list of `cvs admin' commands (options)\n",
530    "# that users not in the `cvsadmin' group are allowed to run.  This\n",
531    "# defaults to `k', or only allowing the changing of the default\n",
532    "# keyword expansion mode for files for users not in the `cvsadmin' group.\n",
533    "# This value is ignored if the `cvsadmin' group does not exist.\n",
534    "#\n",
535    "# The following string would enable all `cvs admin' commands for all\n",
536    "# users:\n",
537    "#AdminGroup=wheel\n",
538    "#AdminOptions=aAbceIklLmnNostuU\n",
539#ifdef SUPPORT_OLD_INFO_FMT_STRINGS
540    "\n",
541    "# Set `UseNewInfoFmtStrings' to `no' if you must support a legacy system by\n",
542    "# enabling the deprecated old style info file command line format strings.\n",
543    "# Be warned that these strings could be disabled in any new version of CVS.\n",
544    "UseNewInfoFmtStrings=yes\n",
545#endif /* SUPPORT_OLD_INFO_FMT_STRINGS */
546    "\n",
547    "# Set `ImportNewFilesToVendorBranchOnly' to `yes' if you wish to force\n",
548    "# every `cvs import' command to behave as if the `-X' flag was\n",
549    "# specified.\n",
550    "#ImportNewFilesToVendorBranchOnly=no\n",
551#ifdef PROXY_SUPPORT
552    "\n",
553    "# Set `PrimaryServer' to the CVSROOT to the primary, or write, server when\n",
554    "# establishing one or more read-only mirrors which serve as proxies for\n",
555    "# the write server in write mode or redirect the client to the primary for\n",
556    "# write requests.\n",
557    "#\n",
558    "# For example:\n",
559    "#\n",
560    "#   PrimaryServer=:fork:localhost/cvsroot\n",
561    "\n",
562    "# Set `MaxProxyBufferSize' to the the maximum allowable secondary\n",
563    "# buffer memory cache size before the buffer begins being stored to disk, in\n",
564    "# bytes.  Must be a positive integer but may end in `k', `M', `G', or `T' (for\n",
565    "# kiilo, mega, giga, & tera, respectively).  If an otherwise valid number you\n",
566    "# specify is greater than the SIZE_MAX defined by your system's C compiler,\n",
567    "# then it will be resolved to SIZE_MAX without a warning.  Defaults to 8M (8\n",
568    "# megabytes).\n",
569    "#\n",
570    "# High values for MaxProxyBufferSize may speed up a secondary server\n",
571    "# with old hardware and a lot of available memory but can actually slow a\n",
572    "# modern system down slightly.\n",
573    "#\n",
574    "# For example:\n",
575    "#\n",
576    "#   MaxProxyBufferSize=1G\n",
577#endif /* PROXY_SUPPORT */
578    "\n",
579    "# Set `MaxCommentLeaderLength' to the maximum length permitted for the\n",
580    "# automagically determined comment leader used when expanding the Log\n",
581    "# keyword, in bytes.  CVS's behavior when the automagically determined\n",
582    "# comment leader exceeds this length is dependant on the value of\n",
583    "# `UseArchiveCommentLeader' set in this file.  `unlimited' is a valid\n",
584    "# setting for this value.  Defaults to 20 bytes.\n",
585    "#\n",
586    "# For example:\n",
587    "#\n",
588    "#   MaxCommentLeaderLength=20\n",
589    "\n",
590    "# Set `UseArchiveCommentLeader' to `yes' to cause CVS to fall back on\n",
591    "# the comment leader set in the RCS archive file, if any, when the\n",
592    "# automagically determined comment leader exceeds `MaxCommentLeaderLength'\n",
593    "# bytes.  If `UseArchiveCommentLeader' is not set and a comment leader\n",
594    "# greater than `MaxCommentLeaderLength' is calculated, the Log keyword\n",
595    "# being examined will not be expanded.  Defaults to `no'.\n",
596    "#\n",
597    "# For example:\n",
598    "#\n",
599    "#   UseArchiveCommentLeader=no\n",
600    "#\n",
601    "# Set this to the name of a local tag to use in addition to Id\n",
602    "#tag=OurTag\n",
603    NULL
604};
605
606static const struct admin_file filelist[] = {
607    {CVSROOTADM_CHECKOUTLIST,
608	"a %s file can specify extra CVSROOT files to auto-checkout",
609	checkoutlist_contents},
610    {CVSROOTADM_COMMITINFO,
611	"a %s file can be used to configure 'cvs commit' checking",
612	commitinfo_contents},
613    {CVSROOTADM_IGNORE,
614	"a %s file can be used to specify files to ignore",
615	NULL},
616    {CVSROOTADM_LOGINFO,
617	"no logging of 'cvs commit' messages is done without a %s file",
618	&loginfo_contents[0]},
619    {CVSROOTADM_MODULES,
620	/* modules is special-cased in mkmodules.  */
621	NULL,
622	modules_contents},
623    {CVSROOTADM_NOTIFY,
624	"a %s file can be used to specify where notifications go",
625	notify_contents},
626    {CVSROOTADM_POSTADMIN,
627	"a %s file can be used to configure 'cvs admin' logging",
628	postadmin_contents},
629    {CVSROOTADM_POSTPROXY,
630	"a %s file can be used to close or log connections to a primary server",
631	postproxy_contents},
632    {CVSROOTADM_POSTTAG,
633	"a %s file can be used to configure 'cvs tag' logging",
634	posttag_contents},
635    {CVSROOTADM_POSTWATCH,
636	"a %s file can be used to configure 'cvs watch' logging",
637	postwatch_contents},
638    {CVSROOTADM_PREPROXY,
639	"a %s file can be used to open or log connections to a primary server",
640	preproxy_contents},
641    {CVSROOTADM_RCSINFO,
642	"a %s file can be used to configure 'cvs commit' templates",
643	rcsinfo_contents},
644    {CVSROOTADM_READERS,
645	"a %s file specifies read-only users",
646	NULL},
647    {CVSROOTADM_TAGINFO,
648	"a %s file can be used to configure 'cvs tag' checking",
649	taginfo_contents},
650    {CVSROOTADM_VERIFYMSG,
651	"a %s file can be used to validate log messages",
652	verifymsg_contents},
653    {CVSROOTADM_WRAPPER,
654	"a %s file can be used to specify files to treat as wrappers",
655	cvswrappers_contents},
656    {CVSROOTADM_WRITERS,
657	"a %s file specifies read/write users",
658	NULL},
659
660    /* Some have suggested listing CVSROOTADM_PASSWD here too.  This
661       would mean that CVS commands which operate on the
662       CVSROOTADM_PASSWD file would transmit hashed passwords over the
663       net.  This might seem to be no big deal, as pserver normally
664       transmits cleartext passwords, but the difference is that
665       CVSROOTADM_PASSWD contains *all* passwords, not just the ones
666       currently being used.  For example, it could be too easy to
667       accidentally give someone readonly access to CVSROOTADM_PASSWD
668       (e.g. via anonymous CVS or cvsweb), and then if there are any
669       guessable passwords for read/write access (usually there will be)
670       they get read/write access.
671
672       Another worry is the implications of storing old passwords--if
673       someone used a password in the past they might be using it
674       elsewhere, using a similar password, etc, and so saving old
675       passwords, even hashed, is probably not a good idea.  */
676
677    {CVSROOTADM_CONFIG,
678	 "a %s file configures various behaviors",
679	 config_contents},
680    {NULL, NULL, NULL}
681};
682
683/* Rebuild the checked out administrative files in directory DIR.  */
684int
685mkmodules (char *dir)
686{
687    struct saved_cwd cwd;
688    char *temp;
689    char *cp, *last, *fname;
690#ifdef MY_NDBM
691    DBM *db;
692#endif
693    FILE *fp;
694    char *line = NULL;
695    size_t line_allocated = 0;
696    const struct admin_file *fileptr;
697
698    if (noexec)
699	return 0;
700
701    if (save_cwd (&cwd))
702	error (1, errno, "Failed to save current directory.");
703
704    if (CVS_CHDIR (dir) < 0)
705	error (1, errno, "cannot chdir to %s", dir);
706
707    /*
708     * First, do the work necessary to update the "modules" database.
709     */
710    temp = make_tempfile ();
711    switch (checkout_file (CVSROOTADM_MODULES, temp))
712    {
713
714	case 0:			/* everything ok */
715#ifdef MY_NDBM
716	    /* open it, to generate any duplicate errors */
717	    if ((db = dbm_open (temp, O_RDONLY, 0666)) != NULL)
718		dbm_close (db);
719#else
720	    write_dbmfile (temp);
721	    rename_dbmfile (temp);
722#endif
723	    rename_rcsfile (temp, CVSROOTADM_MODULES);
724	    break;
725
726	default:
727	    error (0, 0,
728		"'cvs checkout' is less functional without a %s file",
729		CVSROOTADM_MODULES);
730	    break;
731    }					/* switch on checkout_file() */
732
733    if (unlink_file (temp) < 0
734	&& !existence_error (errno))
735	error (0, errno, "cannot remove %s", temp);
736    free (temp);
737
738    /* Checkout the files that need it in CVSROOT dir */
739    for (fileptr = filelist; fileptr && fileptr->filename; fileptr++) {
740	if (fileptr->errormsg == NULL)
741	    continue;
742	temp = make_tempfile ();
743	if (checkout_file (fileptr->filename, temp) == 0)
744	    rename_rcsfile (temp, fileptr->filename);
745	/* else
746	 *   If there was some problem other than the file not existing,
747	 *   checkout_file already printed a real error message.  If the
748	 *   file does not exist, it is harmless--it probably just means
749	 *   that the repository was created with an old version of CVS
750	 *   which didn't have so many files in CVSROOT.
751	 */
752
753	if (unlink_file (temp) < 0
754	    && !existence_error (errno))
755	    error (0, errno, "cannot remove %s", temp);
756	free (temp);
757    }
758
759    fp = CVS_FOPEN (CVSROOTADM_CHECKOUTLIST, "r");
760    if (fp)
761    {
762	/*
763	 * File format:
764	 *  [<whitespace>]<filename>[<whitespace><error message>]<end-of-line>
765	 *
766	 * comment lines begin with '#'
767	 */
768	while (getline (&line, &line_allocated, fp) >= 0)
769	{
770	    /* skip lines starting with # */
771	    if (line[0] == '#')
772		continue;
773
774	    if ((last = strrchr (line, '\n')) != NULL)
775		*last = '\0';			/* strip the newline */
776
777	    /* Skip leading white space. */
778	    for (fname = line;
779		 *fname && isspace ((unsigned char) *fname);
780		 fname++)
781		;
782
783	    /* Find end of filename. */
784	    for (cp = fname; *cp && !isspace ((unsigned char) *cp); cp++)
785		;
786	    *cp = '\0';
787
788	    temp = make_tempfile ();
789	    if (checkout_file (fname, temp) == 0)
790	    {
791		rename_rcsfile (temp, fname);
792	    }
793	    else
794	    {
795		/* Skip leading white space before the error message.  */
796		for (cp++;
797		     cp < last && *cp && isspace ((unsigned char) *cp);
798		     cp++)
799		    ;
800		if (cp < last && *cp)
801		    error (0, 0, "%s", cp);
802	    }
803	    if (unlink_file (temp) < 0
804		&& !existence_error (errno))
805		error (0, errno, "cannot remove %s", temp);
806	    free (temp);
807	}
808	if (line)
809	    free (line);
810	if (ferror (fp))
811	    error (0, errno, "cannot read %s", CVSROOTADM_CHECKOUTLIST);
812	if (fclose (fp) < 0)
813	    error (0, errno, "cannot close %s", CVSROOTADM_CHECKOUTLIST);
814    }
815    else
816    {
817	/* Error from CVS_FOPEN.  */
818	if (!existence_error (errno))
819	    error (0, errno, "cannot open %s", CVSROOTADM_CHECKOUTLIST);
820    }
821
822    if (restore_cwd (&cwd))
823	error (1, errno, "Failed to restore current directory, `%s'.",
824	       cwd.name);
825    free_cwd (&cwd);
826
827    return 0;
828}
829
830
831
832/*
833 * Yeah, I know, there are NFS race conditions here.
834 */
835static char *
836make_tempfile (void)
837{
838    static int seed = 0;
839    int fd;
840    char *temp;
841
842    if (seed == 0)
843	seed = getpid ();
844    temp = xmalloc (sizeof (BAKPREFIX) + 40);
845    while (1)
846    {
847	(void) sprintf (temp, "%s%d", BAKPREFIX, seed++);
848	if ((fd = CVS_OPEN (temp, O_CREAT|O_EXCL|O_RDWR, 0666)) != -1)
849	    break;
850	if (errno != EEXIST)
851	    error (1, errno, "cannot create temporary file %s", temp);
852    }
853    if (close(fd) < 0)
854	error(1, errno, "cannot close temporary file %s", temp);
855    return temp;
856}
857
858
859
860/* Get a file.  If the file does not exist, return 1 silently.  If
861   there is an error, print a message and return 1 (FIXME: probably
862   not a very clean convention).  On success, return 0.  */
863static int
864checkout_file (char *file, char *temp)
865{
866    char *rcs;
867    RCSNode *rcsnode;
868    int retcode = 0;
869
870    if (noexec)
871	return 0;
872
873    rcs = Xasprintf ("%s%s", file, RCSEXT);
874    if (!isfile (rcs))
875    {
876	free (rcs);
877	return 1;
878    }
879
880    rcsnode = RCS_parsercsfile (rcs);
881    if (!rcsnode)
882    {
883	/* Probably not necessary (?); RCS_parsercsfile already printed a
884	   message.  */
885	error (0, 0, "Failed to parse `%s'.", rcs);
886	free (rcs);
887	return 1;
888    }
889
890    retcode = RCS_checkout (rcsnode, NULL, NULL, NULL, NULL, temp, NULL, NULL);
891    if (retcode != 0)
892    {
893	/* Probably not necessary (?); RCS_checkout already printed a
894	   message.  */
895	error (0, 0, "failed to check out %s file",
896	       file);
897    }
898    freercsnode (&rcsnode);
899    free (rcs);
900    return retcode;
901}
902
903
904
905#ifndef MY_NDBM
906
907static void
908write_dbmfile( char *temp )
909{
910    char line[DBLKSIZ], value[DBLKSIZ];
911    FILE *fp;
912    DBM *db;
913    char *cp, *vp;
914    datum key, val;
915    int len, cont, err = 0;
916
917    fp = xfopen (temp, "r");
918    if ((db = dbm_open (temp, O_RDWR | O_CREAT | O_TRUNC, 0666)) == NULL)
919	error (1, errno, "cannot open dbm file %s for creation", temp);
920    for (cont = 0; fgets (line, sizeof (line), fp) != NULL;)
921    {
922	if ((cp = strrchr (line, '\n')) != NULL)
923	    *cp = '\0';			/* strip the newline */
924
925	/*
926	 * Add the line to the value, at the end if this is a continuation
927	 * line; otherwise at the beginning, but only after any trailing
928	 * backslash is removed.
929	 */
930	vp = value;
931	if (cont)
932	    vp += strlen (value);
933
934	/*
935	 * See if the line we read is a continuation line, and strip the
936	 * backslash if so.
937	 */
938	len = strlen (line);
939	if (len > 0)
940	    cp = &line[len - 1];
941	else
942	    cp = line;
943	if (*cp == '\\')
944	{
945	    cont = 1;
946	    *cp = '\0';
947	}
948	else
949	{
950	    cont = 0;
951	}
952	(void) strcpy (vp, line);
953	if (value[0] == '#')
954	    continue;			/* comment line */
955	vp = value;
956	while (*vp && isspace ((unsigned char) *vp))
957	    vp++;
958	if (*vp == '\0')
959	    continue;			/* empty line */
960
961	/*
962	 * If this was not a continuation line, add the entry to the database
963	 */
964	if (!cont)
965	{
966	    key.dptr = vp;
967	    while (*vp && !isspace ((unsigned char) *vp))
968		vp++;
969	    key.dsize = vp - key.dptr;
970	    *vp++ = '\0';		/* NULL terminate the key */
971	    while (*vp && isspace ((unsigned char) *vp))
972		vp++;			/* skip whitespace to value */
973	    if (*vp == '\0')
974	    {
975		error (0, 0, "warning: NULL value for key `%s'", key.dptr);
976		continue;
977	    }
978	    val.dptr = vp;
979	    val.dsize = strlen (vp);
980	    if (dbm_store (db, key, val, DBM_INSERT) == 1)
981	    {
982		error (0, 0, "duplicate key found for `%s'", key.dptr);
983		err++;
984	    }
985	}
986    }
987    dbm_close (db);
988    if (fclose (fp) < 0)
989	error (0, errno, "cannot close %s", temp);
990    if (err)
991    {
992	/* I think that the size of the buffer needed here is
993	   just determined by sizeof (CVSROOTADM_MODULES), the
994	   filenames created by make_tempfile, and other things that won't
995	   overflow.  */
996	char dotdir[50], dotpag[50], dotdb[50];
997
998	(void) sprintf (dotdir, "%s.dir", temp);
999	(void) sprintf (dotpag, "%s.pag", temp);
1000	(void) sprintf (dotdb, "%s.db", temp);
1001	if (unlink_file (dotdir) < 0
1002	    && !existence_error (errno))
1003	    error (0, errno, "cannot remove %s", dotdir);
1004	if (unlink_file (dotpag) < 0
1005	    && !existence_error (errno))
1006	    error (0, errno, "cannot remove %s", dotpag);
1007	if (unlink_file (dotdb) < 0
1008	    && !existence_error (errno))
1009	    error (0, errno, "cannot remove %s", dotdb);
1010	error (1, 0, "DBM creation failed; correct above errors");
1011    }
1012}
1013
1014static void
1015rename_dbmfile( char *temp )
1016{
1017    /* I think that the size of the buffer needed here is
1018       just determined by sizeof (CVSROOTADM_MODULES), the
1019       filenames created by make_tempfile, and other things that won't
1020       overflow.  */
1021    char newdir[50], newpag[50], newdb[50];
1022    char dotdir[50], dotpag[50], dotdb[50];
1023    char bakdir[50], bakpag[50], bakdb[50];
1024
1025    int dir1_errno = 0, pag1_errno = 0, db1_errno = 0;
1026    int dir2_errno = 0, pag2_errno = 0, db2_errno = 0;
1027    int dir3_errno = 0, pag3_errno = 0, db3_errno = 0;
1028
1029    (void) sprintf (dotdir, "%s.dir", CVSROOTADM_MODULES);
1030    (void) sprintf (dotpag, "%s.pag", CVSROOTADM_MODULES);
1031    (void) sprintf (dotdb, "%s.db", CVSROOTADM_MODULES);
1032    (void) sprintf (bakdir, "%s%s.dir", BAKPREFIX, CVSROOTADM_MODULES);
1033    (void) sprintf (bakpag, "%s%s.pag", BAKPREFIX, CVSROOTADM_MODULES);
1034    (void) sprintf (bakdb, "%s%s.db", BAKPREFIX, CVSROOTADM_MODULES);
1035    (void) sprintf (newdir, "%s.dir", temp);
1036    (void) sprintf (newpag, "%s.pag", temp);
1037    (void) sprintf (newdb, "%s.db", temp);
1038
1039    (void) chmod (newdir, 0666);
1040    (void) chmod (newpag, 0666);
1041    (void) chmod (newdb, 0666);
1042
1043    /* don't mess with me */
1044    SIG_beginCrSect ();
1045
1046    /* rm .#modules.dir .#modules.pag */
1047    if (unlink_file (bakdir) < 0)
1048	dir1_errno = errno;
1049    if (unlink_file (bakpag) < 0)
1050	pag1_errno = errno;
1051    if (unlink_file (bakdb) < 0)
1052	db1_errno = errno;
1053
1054    /* mv modules.dir .#modules.dir */
1055    if (CVS_RENAME (dotdir, bakdir) < 0)
1056	dir2_errno = errno;
1057    /* mv modules.pag .#modules.pag */
1058    if (CVS_RENAME (dotpag, bakpag) < 0)
1059	pag2_errno = errno;
1060    /* mv modules.db .#modules.db */
1061    if (CVS_RENAME (dotdb, bakdb) < 0)
1062	db2_errno = errno;
1063
1064    /* mv "temp".dir modules.dir */
1065    if (CVS_RENAME (newdir, dotdir) < 0)
1066	dir3_errno = errno;
1067    /* mv "temp".pag modules.pag */
1068    if (CVS_RENAME (newpag, dotpag) < 0)
1069	pag3_errno = errno;
1070    /* mv "temp".db modules.db */
1071    if (CVS_RENAME (newdb, dotdb) < 0)
1072	db3_errno = errno;
1073
1074    /* OK -- make my day */
1075    SIG_endCrSect ();
1076
1077    /* I didn't want to call error() when we had signals blocked
1078       (unnecessary?), but do it now.  */
1079    if (dir1_errno && !existence_error (dir1_errno))
1080	error (0, dir1_errno, "cannot remove %s", bakdir);
1081    if (pag1_errno && !existence_error (pag1_errno))
1082	error (0, pag1_errno, "cannot remove %s", bakpag);
1083    if (db1_errno && !existence_error (db1_errno))
1084	error (0, db1_errno, "cannot remove %s", bakdb);
1085
1086    if (dir2_errno && !existence_error (dir2_errno))
1087	error (0, dir2_errno, "cannot remove %s", bakdir);
1088    if (pag2_errno && !existence_error (pag2_errno))
1089	error (0, pag2_errno, "cannot remove %s", bakpag);
1090    if (db2_errno && !existence_error (db2_errno))
1091	error (0, db2_errno, "cannot remove %s", bakdb);
1092
1093    if (dir3_errno && !existence_error (dir3_errno))
1094	error (0, dir3_errno, "cannot remove %s", bakdir);
1095    if (pag3_errno && !existence_error (pag3_errno))
1096	error (0, pag3_errno, "cannot remove %s", bakpag);
1097    if (db3_errno && !existence_error (db3_errno))
1098	error (0, db3_errno, "cannot remove %s", bakdb);
1099}
1100
1101#endif				/* !MY_NDBM */
1102
1103static void
1104rename_rcsfile (char *temp, char *real)
1105{
1106    char *bak;
1107    struct stat statbuf;
1108    char *rcs;
1109
1110    /* Set "x" bits if set in original. */
1111    rcs = Xasprintf ("%s%s", real, RCSEXT);
1112    statbuf.st_mode = 0; /* in case rcs file doesn't exist, but it should... */
1113    if (stat (rcs, &statbuf) < 0
1114	&& !existence_error (errno))
1115	error (0, errno, "cannot stat %s", rcs);
1116    free (rcs);
1117
1118    if (chmod (temp, 0444 | (statbuf.st_mode & 0111)) < 0)
1119	error (0, errno, "warning: cannot chmod %s", temp);
1120    bak = Xasprintf ("%s%s", BAKPREFIX, real);
1121
1122    /* rm .#loginfo */
1123    if (unlink_file (bak) < 0
1124	&& !existence_error (errno))
1125	error (0, errno, "cannot remove %s", bak);
1126
1127    /* mv loginfo .#loginfo */
1128    if (CVS_RENAME (real, bak) < 0
1129	&& !existence_error (errno))
1130	error (0, errno, "cannot rename %s to %s", real, bak);
1131
1132    /* mv "temp" loginfo */
1133    if (CVS_RENAME (temp, real) < 0
1134	&& !existence_error (errno))
1135	error (0, errno, "cannot rename %s to %s", temp, real);
1136
1137    free (bak);
1138}
1139
1140const char *const init_usage[] = {
1141    "Usage: %s %s\n",
1142    "(Specify the --help global option for a list of other help options)\n",
1143    NULL
1144};
1145
1146int
1147init (int argc, char **argv)
1148{
1149    /* Name of CVSROOT directory.  */
1150    char *adm;
1151    /* Name of this administrative file.  */
1152    char *info;
1153    /* Name of ,v file for this administrative file.  */
1154    char *info_v;
1155    /* Exit status.  */
1156    int err = 0;
1157    struct stat st;
1158
1159    const struct admin_file *fileptr;
1160
1161    umask (cvsumask);
1162
1163    if (argc == -1 || argc > 1)
1164	usage (init_usage);
1165
1166#ifdef CLIENT_SUPPORT
1167    if (current_parsed_root->isremote)
1168    {
1169	start_server ();
1170
1171	ign_setup ();
1172	send_init_command ();
1173	return get_responses_and_close ();
1174    }
1175#endif /* CLIENT_SUPPORT */
1176
1177    if (stat (current_parsed_root->directory, &st) != -1)
1178	if (!admin_group_member ())
1179	    error (1, 0, "init to an existing repository is restricted to"
1180		" members of the group %s", config->UserAdminGroup);
1181
1182    /* Note: we do *not* create parent directories as needed like the
1183       old cvsinit.sh script did.  Few utilities do that, and a
1184       non-existent parent directory is as likely to be a typo as something
1185       which needs to be created.  */
1186    mkdir_if_needed (current_parsed_root->directory);
1187
1188    adm = Xasprintf ("%s/%s", current_parsed_root->directory, CVSROOTADM);
1189    mkdir_if_needed (adm);
1190
1191    /* This is needed because we pass "fileptr->filename" not "info"
1192       to add_rcs_file below.  I think this would be easy to change,
1193       thus nuking the need for CVS_CHDIR here, but I haven't looked
1194       closely (e.g. see wrappers calls within add_rcs_file).  */
1195    if ( CVS_CHDIR (adm) < 0)
1196	error (1, errno, "cannot change to directory %s", adm);
1197
1198    /* Make Emptydir so it's there if we need it */
1199    mkdir_if_needed (CVSNULLREPOS);
1200
1201    /* 80 is long enough for all the administrative file names, plus
1202       "/" and so on.  */
1203    info = xmalloc (strlen (adm) + 80);
1204    info_v = xmalloc (strlen (adm) + 80);
1205    for (fileptr = filelist; fileptr && fileptr->filename; ++fileptr)
1206    {
1207	if (fileptr->contents == NULL)
1208	    continue;
1209	strcpy (info, adm);
1210	strcat (info, "/");
1211	strcat (info, fileptr->filename);
1212	strcpy (info_v, info);
1213	strcat (info_v, RCSEXT);
1214	if (isfile (info_v))
1215	    /* We will check out this file in the mkmodules step.
1216	       Nothing else is required.  */
1217	    ;
1218	else
1219	{
1220	    int retcode;
1221
1222	    if (!isfile (info))
1223	    {
1224		FILE *fp;
1225		const char * const *p;
1226
1227		fp = xfopen (info, "w");
1228		for (p = fileptr->contents; *p != NULL; ++p)
1229		    if (fputs (*p, fp) < 0)
1230			error (1, errno, "cannot write %s", info);
1231		if (fclose (fp) < 0)
1232		    error (1, errno, "cannot close %s", info);
1233	    }
1234	    /* The message used to say " of " and fileptr->filename after
1235	       "initial checkin" but I fail to see the point as we know what
1236	       file it is from the name.  */
1237	    retcode = add_rcs_file ("initial checkin", info_v,
1238				    fileptr->filename, "1.1", NULL,
1239
1240				    /* No vendor branch.  */
1241				    NULL, NULL, 0, NULL,
1242
1243				    NULL, 0, NULL, 0);
1244	    if (retcode != 0)
1245		/* add_rcs_file already printed an error message.  */
1246		err = 1;
1247	}
1248    }
1249
1250    /* Turn on history logging by default.  The user can remove the file
1251       to disable it.  */
1252    strcpy (info, adm);
1253    strcat (info, "/");
1254    strcat (info, CVSROOTADM_HISTORY);
1255    if (!isfile (info))
1256    {
1257	FILE *fp;
1258
1259	fp = xfopen (info, "w");
1260	if (fclose (fp) < 0)
1261	    error (1, errno, "cannot close %s", info);
1262
1263        /* Make the new history file world-writeable, since every CVS
1264           user will need to be able to write to it.  We use chmod()
1265           because xchmod() is too shy. */
1266        chmod (info, 0666);
1267    }
1268
1269    /* Make an empty val-tags file to prevent problems creating it later.  */
1270    strcpy (info, adm);
1271    strcat (info, "/");
1272    strcat (info, CVSROOTADM_VALTAGS);
1273    if (!isfile (info))
1274    {
1275	FILE *fp;
1276
1277	fp = xfopen (info, "w");
1278	if (fclose (fp) < 0)
1279	    error (1, errno, "cannot close %s", info);
1280
1281        /* Make the new val-tags file world-writeable, since every CVS
1282           user will need to be able to write to it.  We use chmod()
1283           because xchmod() is too shy. */
1284        chmod (info, 0666);
1285    }
1286
1287    free (info);
1288    free (info_v);
1289
1290    mkmodules (adm);
1291
1292    free (adm);
1293    return err;
1294}
1295