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