]> andersk Git - moira.git/blob - backup/dump_db.dc
In the past year, added: /mit/info/public-mailing-lists maintained
[moira.git] / backup / dump_db.dc
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      (c) Copyright 1988 by the Massachusetts Institute of Technology.
7  *      For copying and distribution information, please see the file
8  *      <mit-copyright.h>.
9  *
10  *      This program dumps the SMS database to a series of output files
11  * which can be later read back into SMS in the event of a crash.
12  * 
13  */
14
15 #ifndef lint
16 static char *rcsid_dump_db_c = "$Header$";
17 #endif lint
18
19 #include <stdio.h>
20 #include <sys/file.h>
21 #include <ctype.h>
22 #include <mit-copyright.h>
23 EXEC SQL INCLUDE sqlca;
24 #include "dump_db.h"
25
26 /* putc without the line buffer hair */
27
28 #define putc1(x, p)     (--(p)->_cnt >= 0 ?\
29         (int)(*(unsigned char *)(p)->_ptr++ = (x)) :\
30                 _flsbuf((unsigned char)(x), p))
31
32 FILE *open_file();
33
34 char act[257];
35
36 main(argc, argv)
37     int argc;
38     char **argv;
39 {
40     char *prefix;
41     register int i;
42     
43     if (argc != 2) {
44         fprintf(stderr, "Usage: %s prefix\n", argv[0]);
45         exit(1);
46     }
47     prefix = argv[1];
48
49     bzero(act, 256);
50
51     for (i=0; i<' '; i++) act[i]=2;
52     for (i=128; i<256; i++) act[i]=2;
53     act[SEP_CHAR]=1;
54     act['\\']=1;
55     act[127]=2;
56     
57     EXEC SQL CONNECT moira;
58     EXEC SQL set lockmode session where level = table;
59
60     do_backups(prefix);
61
62     EXEC SQL DISCONNECT;
63     exit(0);
64 }
65
66 dump_int(f, n)
67     FILE *f;
68     int n;
69 {
70     char buf[1024];
71     (void) sprintf(buf, "%d", n);
72     dump_str(f, buf);
73 }
74
75 wpunt()
76 {
77     punt("can't write backup file");
78 }
79
80 dump_str(f, str)
81     register FILE *f;
82     register char *str;
83 {
84     char  *strtrim();
85     register char *ibp = strtrim(str);
86     register int c;             /* PCC doesn't put chars in registers.. */
87     register int t;
88
89     for (; c = (unsigned char) *ibp; ibp++) {
90         switch(act[c]) {
91         case 1:
92             if (putc1('\\', f) < 0) wpunt();
93             /* fall thru.. */
94         case 0:
95             if (putc1(c, f) < 0) wpunt();
96             
97             break;
98         case 2:
99             if (putc1('\\', f) < 0) wpunt();        
100             t = ((c>>6)&7) + '0';
101             if (putc1(t,f) < 0) wpunt();
102             t = ((c>>3)&7) + '0';
103             if (putc1(t,f) < 0) wpunt();
104             t = (c&7) + '0';
105             if (putc1(t,f) < 0) wpunt();
106             break;
107             
108         default:
109             punt("Can't get here");
110         }
111     }
112 }
113
114 dump_bin(f, str)
115     register FILE *f;
116     register char *str;
117 {
118     register char *ibp = str;
119     register int c;             /* PCC doesn't put chars in registers.. */
120     register int t;
121
122     for (; c = (unsigned char) *ibp; ibp++) {
123         switch(act[c]) {
124         case 1:
125             if (putc1('\\', f) < 0) wpunt();
126             /* fall thru.. */
127         case 0:
128             if (putc1(c, f) < 0) wpunt();
129             
130             break;
131         case 2:
132             if (putc1('\\', f) < 0) wpunt();        
133             t = ((c>>6)&7) + '0';
134             if (putc1(t,f) < 0) wpunt();
135             t = ((c>>3)&7) + '0';
136             if (putc1(t,f) < 0) wpunt();
137             t = (c&7) + '0';
138             if (putc1(t,f) < 0) wpunt();
139             break;
140             
141         default:
142             punt("Can't get here");
143         }
144     }
145 }
146
147 safe_close(stream)
148         FILE *stream;
149 {
150         if (fflush(stream) == EOF)
151                 punt("Unable to fflush");
152         if (fsync(fileno(stream)) != 0) 
153                 punt("Unable to fsync");
154         (void) fclose(stream);
155 }       
156
157 FILE *open_file(prefix, suffix)
158     char *prefix, *suffix;
159 {
160     char name[BUFSIZ];
161     int fd;
162     FILE *f;
163     
164     (void) strcpy(name, prefix);
165     (void) strcat(name, suffix);
166
167     fd = open(name, O_CREAT|O_WRONLY|O_EXCL, 0644);
168     if (fd < 0) {
169         punt(name);
170     }
171     f = fdopen(fd, "w");
172     if (f == NULL) {
173         fprintf(stderr, "fdopen of ");
174         punt(name);
175     }
176     fprintf(stderr, "Working on %s\n", name);
177     return(f);
178 }
179
180
181 /*
182  * Trim whitespace off both ends of a string.
183  */
184 char *strtrim(save)
185     register char *save;
186 {
187     register char *t, *s;
188
189     s = save;
190     while (isspace(*s)) s++;
191     /* skip to end of string */
192     if (*s == '\0') {
193         *save = '\0';
194         return(save);
195     }
196
197     for (t = s; *t; t++) continue; 
198     while (t > s) {
199         --t;
200         if (!isspace(*t)) {
201             t++;
202             break;
203         }
204     }
205     *t = '\0';
206     return s;
207 }
This page took 1.06779 seconds and 5 git commands to generate.