]> andersk Git - moira.git/blame - backup/dump_db.dc
Oracle and Solaris/POSIX changes
[moira.git] / backup / dump_db.dc
CommitLineData
d77bf7b9 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
16static char *rcsid_dump_db_c = "$Header$";
17#endif lint
18
19#include <stdio.h>
20#include <sys/file.h>
3d058c8a 21#include <fcntl.h>
d77bf7b9 22#include <ctype.h>
23#include <mit-copyright.h>
0f9132cd 24EXEC SQL INCLUDE sqlca;
d77bf7b9 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
33FILE *open_file();
34
8173ad47 35char act[257];
3d058c8a 36EXEC SQL BEGIN DECLARE SECTION;
37char *db="moira";
38EXEC SQL END DECLARE SECTION;
d77bf7b9 39
40main(argc, argv)
41 int argc;
42 char **argv;
43{
44 char *prefix;
45 register int i;
46
47 if (argc != 2) {
8173ad47 48 fprintf(stderr, "Usage: %s prefix\n", argv[0]);
d77bf7b9 49 exit(1);
50 }
51 prefix = argv[1];
52
3d058c8a 53 memset(act, 0, 256);
d77bf7b9 54
8173ad47 55 for (i=0; i<' '; i++) act[i]=2;
56 for (i=128; i<256; i++) act[i]=2;
d77bf7b9 57 act[SEP_CHAR]=1;
58 act['\\']=1;
59 act[127]=2;
d77bf7b9 60
3d058c8a 61 EXEC SQL CONNECT :db IDENTIFIED BY :db;
d77bf7b9 62
63 do_backups(prefix);
64
3d058c8a 65 EXEC SQL COMMIT;
d77bf7b9 66 exit(0);
67}
68
69dump_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
78wpunt()
79{
80 punt("can't write backup file");
81}
82
83dump_str(f, str)
84 register FILE *f;
85 register char *str;
86{
3d058c8a 87 register char *ibp;
d77bf7b9 88 register int c; /* PCC doesn't put chars in registers.. */
8173ad47 89 register int t;
90
3d058c8a 91 for (ibp = str; c = (unsigned char) *ibp; ibp++) {
639a8de3 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
116dump_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
8173ad47 124 for (; c = (unsigned char) *ibp; ibp++) {
d77bf7b9 125 switch(act[c]) {
d77bf7b9 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();
d77bf7b9 141 break;
142
143 default:
144 punt("Can't get here");
145 }
146 }
147}
148
149safe_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
159FILE *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 */
186char *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.232144 seconds and 5 git commands to generate.