Mon Aug 24 20:04:04 EDT 1998 Nathan Bryant <bryant@cs.usm.maine.edu>
[citadel.git] / citadel / control.c
1 /*
2  * control.c
3  *
4  * This module handles states which are global to the entire server.
5  *
6  */
7
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <signal.h>
13 #include <time.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <pthread.h>
18 #include <syslog.h>
19 #include "citadel.h"
20 #include "server.h"
21 #include "control.h"
22 #include "sysdep_decls.h"
23
24 struct CitControl CitControl;
25
26 /*
27  * get_control  -  read the control record into memory.
28  */
29 void get_control(void) {
30         FILE *fp;
31
32         /* Zero it out.  If the control record on disk is missing or short,
33          * the system functions with all control record fields initialized
34          * to zero.
35          */
36         bzero(&CitControl, sizeof(struct CitControl));
37         fp = fopen("citadel.control", "rb");
38         if (fp == NULL) return;
39
40         fread(&CitControl, sizeof(struct CitControl), 1, fp);
41         fclose(fp);
42         }
43
44 /*
45  * put_control  -  write the control record to disk.
46  */
47 void put_control(void) {
48         FILE *fp;
49
50         fp = fopen("citadel.control", "wb");
51         if (fp != NULL) {
52                 fwrite(&CitControl, sizeof(struct CitControl), 1, fp);
53                 fclose(fp);
54                 }
55         }
56
57
58 /*
59  * get_new_message_number()  -  Obtain a new, unique ID to be used for a message.
60  */
61 long get_new_message_number(void) {
62         begin_critical_section(S_CONTROL);
63         get_control();
64         ++CitControl.MMhighest;
65         put_control();
66         end_critical_section(S_CONTROL);
67         return(CitControl.MMhighest);
68         }
69
70
71 /*
72  * get_new_user_number()  -  Obtain a new, unique ID to be used for a user.
73  */
74 long get_new_user_number(void) {
75         begin_critical_section(S_CONTROL);
76         get_control();
77         ++CitControl.MMnextuser;
78         put_control();
79         end_critical_section(S_CONTROL);
80         return(CitControl.MMnextuser);
81         }