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