]> andersk Git - moira.git/blob - backup/dump_db.dc
Works.
[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 safe_close(stream)
115         FILE *stream;
116 {
117         if (fflush(stream) == EOF)
118                 punt("Unable to fflush");
119         if (fsync(fileno(stream)) != 0) 
120                 punt("Unable to fsync");
121         (void) fclose(stream);
122 }       
123
124 FILE *open_file(prefix, suffix)
125     char *prefix, *suffix;
126 {
127     char name[BUFSIZ];
128     int fd;
129     FILE *f;
130     
131     (void) strcpy(name, prefix);
132     (void) strcat(name, suffix);
133
134     fd = open(name, O_CREAT|O_WRONLY|O_EXCL, 0644);
135     if (fd < 0) {
136         punt(name);
137     }
138     f = fdopen(fd, "w");
139     if (f == NULL) {
140         fprintf(stderr, "fdopen of ");
141         punt(name);
142     }
143     fprintf(stderr, "Working on %s\n", name);
144     return(f);
145 }
146
147
148 /*
149  * Trim whitespace off both ends of a string.
150  */
151 char *strtrim(save)
152     register char *save;
153 {
154     register char *t, *s;
155
156     s = save;
157     while (isspace(*s)) s++;
158     /* skip to end of string */
159     if (*s == '\0') {
160         *save = '\0';
161         return(save);
162     }
163
164     for (t = s; *t; t++) continue; 
165     while (t > s) {
166         --t;
167         if (!isspace(*t)) {
168             t++;
169             break;
170         }
171     }
172     *t = '\0';
173     return s;
174 }
This page took 0.047607 seconds and 5 git commands to generate.