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