]> andersk Git - moira.git/blame - backup/dump_db.pc
in ASCII backups, write `|'s within fields as \174 instead of \| so
[moira.git] / backup / dump_db.pc
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
3d058c8a 35EXEC SQL BEGIN DECLARE SECTION;
36char *db="moira";
37EXEC SQL END DECLARE SECTION;
d77bf7b9 38
39main(argc, argv)
40 int argc;
41 char **argv;
42{
43 char *prefix;
44 register int i;
45
46 if (argc != 2) {
8173ad47 47 fprintf(stderr, "Usage: %s prefix\n", argv[0]);
d77bf7b9 48 exit(1);
49 }
50 prefix = argv[1];
51
3d058c8a 52 EXEC SQL CONNECT :db IDENTIFIED BY :db;
d77bf7b9 53
54 do_backups(prefix);
55
3d058c8a 56 EXEC SQL COMMIT;
d77bf7b9 57 exit(0);
58}
59
60dump_int(f, n)
61 FILE *f;
62 int n;
63{
64 char buf[1024];
65 (void) sprintf(buf, "%d", n);
66 dump_str(f, buf);
67}
68
69wpunt()
70{
71 punt("can't write backup file");
72}
73
74dump_str(f, str)
75 register FILE *f;
76 register char *str;
77{
3d058c8a 78 register char *ibp;
d77bf7b9 79 register int c; /* PCC doesn't put chars in registers.. */
8173ad47 80 register int t;
81
3d058c8a 82 for (ibp = str; c = (unsigned char) *ibp; ibp++) {
c127fcb3 83 if(c<32 || c>126 || c==SEP_CHAR || c=='\\') {
639a8de3 84 if (putc1('\\', f) < 0) wpunt();
85 t = ((c>>6)&7) + '0';
86 if (putc1(t,f) < 0) wpunt();
87 t = ((c>>3)&7) + '0';
88 if (putc1(t,f) < 0) wpunt();
89 t = (c&7) + '0';
90 if (putc1(t,f) < 0) wpunt();
c127fcb3 91 } else {
d77bf7b9 92 if (putc1(c, f) < 0) wpunt();
d77bf7b9 93 }
94 }
95}
96
97safe_close(stream)
98 FILE *stream;
99{
100 if (fflush(stream) == EOF)
101 punt("Unable to fflush");
102 if (fsync(fileno(stream)) != 0)
103 punt("Unable to fsync");
104 (void) fclose(stream);
105}
106
107FILE *open_file(prefix, suffix)
108 char *prefix, *suffix;
109{
110 char name[BUFSIZ];
111 int fd;
112 FILE *f;
113
114 (void) strcpy(name, prefix);
115 (void) strcat(name, suffix);
116
117 fd = open(name, O_CREAT|O_WRONLY|O_EXCL, 0644);
118 if (fd < 0) {
119 punt(name);
120 }
121 f = fdopen(fd, "w");
122 if (f == NULL) {
123 fprintf(stderr, "fdopen of ");
124 punt(name);
125 }
126 fprintf(stderr, "Working on %s\n", name);
127 return(f);
128}
129
130
131/*
132 * Trim whitespace off both ends of a string.
133 */
134char *strtrim(save)
135 register char *save;
136{
137 register char *t, *s;
138
139 s = save;
140 while (isspace(*s)) s++;
141 /* skip to end of string */
142 if (*s == '\0') {
143 *save = '\0';
144 return(save);
145 }
146
147 for (t = s; *t; t++) continue;
148 while (t > s) {
149 --t;
150 if (!isspace(*t)) {
151 t++;
152 break;
153 }
154 }
155 *t = '\0';
156 return s;
157}
This page took 0.281284 seconds and 5 git commands to generate.