See ChangeLog for 1999-03-0[45] for details.
[citadel.git] / citadel / config.c
1 /*
2  * This function reads the citadel.config file.  It should be called at
3  * the beginning of EVERY Citadel program.
4  *
5  * $Id$
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <errno.h>
13 #include <string.h>
14 #include <limits.h>
15 #include "citadel.h"
16 #include "config.h"
17
18 struct config config;
19 char bbs_home_directory[PATH_MAX];
20 int home_specified = 0;
21
22 void get_config(void) {
23         FILE *cfp;
24
25         if (chdir( home_specified ? bbs_home_directory : BBSDIR ) != 0) {
26                 fprintf(stderr, "Cannot start.\nThere is no Citadel installation in %s\n%s\n",
27                         (home_specified ? bbs_home_directory : BBSDIR),
28                         strerror(errno));
29                 exit(errno);
30                 }
31         cfp=fopen("citadel.config","rb");
32         if (cfp==NULL) {
33                 fprintf(stderr, "Cannot start.\n");
34                 fprintf(stderr, "There is no citadel.config in %s\n%s\n",
35                         (home_specified ? bbs_home_directory : BBSDIR),
36                         strerror(errno));
37                 exit(errno);
38                 }
39         fread((char *)&config,sizeof(struct config),1,cfp);
40         fclose(cfp);
41         if ( (config.c_setup_level / 10) != (REV_LEVEL/10) ) {
42                 fprintf(stderr, "config: Your data files are out of date.  ");
43                 fprintf(stderr, "Run setup to update them.\n");
44                 fprintf(stderr,
45                         "        This program requires level %d.%02d\n",
46                                 (REV_LEVEL / 100), (REV_LEVEL % 100) );
47                 fprintf(stderr,
48                         "        Data files are currently at %d.%02d\n",
49                                 (config.c_setup_level / 100),
50                                 (config.c_setup_level % 100) );
51                 exit(1);
52                 }
53         }
54
55
56 /*
57  * Occasionally, we will need to write the config file, because some operations
58  * change site-wide parameters.
59  */
60 void put_config(void) {
61         FILE *cfp;
62         
63         if ((cfp = fopen("citadel.config", "rb+")) == NULL)
64                 perror("citadel.config");
65         else {
66                 fwrite((char *)&config, sizeof(struct config), 1, cfp);
67                 fclose(cfp);
68                 }
69         }