]> andersk Git - moira.git/blob - backup/dump_db.pc
Bomb out with an error message if something goes wrong. Duh!
[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 EXEC SQL BEGIN DECLARE SECTION;
36 char *db="moira";
37 EXEC SQL END DECLARE SECTION;
38
39 main(argc, argv)
40     int argc;
41     char **argv;
42 {
43     char *prefix;
44     register int i;
45     
46     if (argc != 2) {
47         fprintf(stderr, "Usage: %s prefix\n", argv[0]);
48         exit(1);
49     }
50     prefix = argv[1];
51
52     EXEC SQL CONNECT :db IDENTIFIED BY :db;
53
54     do_backups(prefix);
55
56     EXEC SQL COMMIT;
57     exit(0);
58 }
59
60 dump_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
69 wpunt()
70 {
71     punt("can't write backup file");
72 }
73
74 dump_str(f, str)
75     register FILE *f;
76     register char *str;
77 {
78     register char *ibp;
79     register int c;             /* PCC doesn't put chars in registers.. */
80     register int t;
81
82     for (ibp = str; c = (unsigned char) *ibp; ibp++) {
83         if(c<32 || c>126 || c==SEP_CHAR || c=='\\') {
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();
91         } else {
92             if (putc1(c, f) < 0) wpunt();
93         }
94     }
95 }
96
97 safe_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
107 FILE *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  */
134 char *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.124661 seconds and 5 git commands to generate.