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