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