]> andersk Git - moira.git/blob - backup/dump_db.qc
Fixed makefile; now has install step.
[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.2  1987-07-13 03:52:53  wesommer
13  *      Modified to pass lint, do better backups.
14  *
15 Revision 1.1  87/07/11  19:51:05  wesommer
16 Initial revision
17
18  *
19  */
20
21 #ifndef lint
22 static char *rcsid_dump_db_c = "$Header$";
23 #endif lint
24
25 #include <stdio.h>
26 #include <sys/file.h>
27 #include <ctype.h>
28
29 /* putc without the line buffer hair */
30
31 #define putc1(x, p)     (--(p)->_cnt >= 0 ?\
32         (int)(*(unsigned char *)(p)->_ptr++ = (x)) :\
33                 _flsbuf((unsigned char)(x), p))
34
35 FILE *open_file();
36
37 char act[128];
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: smsbackup prefix\n");
48         exit(1);
49     }
50     prefix = argv[1];
51
52     bzero(act, 128);
53
54     act[':']=1;
55     act['\\']=1;
56     act[127]=2;
57     for (i=0; i<' '; i++) act[i]=2;
58     
59 ##  ingres sms 
60     
61     do_backups(prefix);
62     
63 ##  exit
64     exit(0);
65 }
66
67 dump_int(f, n)
68     FILE *f;
69     int n;
70 {
71     char buf[1024];
72     (void) sprintf(buf, "%d", n);
73     dump_str(f, buf);
74 }
75
76 wpunt()
77 {
78     punt("can't write backup file");
79 }
80
81 dump_str(f, str)
82     register FILE *f;
83     register char *str;
84 {
85     register char *ibp = str;
86     register int c;             /* PCC doesn't put chars in registers.. */
87     for (; c = *ibp; ibp++) {
88         c = toascii(c);         /* punt 8th bit */
89         switch(act[c]) {
90             register int t;
91         case 1:
92             if (putc1('\\', f) < 0) wpunt();
93             /* fall thru.. */
94         case 0:
95             if (putc1(c, f) < 0) wpunt();
96             
97             break;
98         case 2:
99             if (putc1('\\', f) < 0) wpunt();        
100             t = ((c>>6)&7) + '0';
101             if (putc1(t,f) < 0) wpunt();
102             t = ((c>>3)&7) + '0';
103             if (putc1(t,f) < 0) wpunt();
104             t = (c&7) + '0';
105             if (putc1(t,f) < 0) wpunt();
106             fprintf(stderr, "control character \\%03o\n", c);
107             break;
108             
109         default:
110             punt("Can't get here");
111         }
112     }
113 }
114
115 safe_close(stream)
116         FILE *stream;
117 {
118         if (fflush(stream) == EOF)
119                 punt("Unable to fflush");
120         if (fsync(fileno(stream)) != 0) 
121                 punt("Unable to fsync");
122         (void) fclose(stream);
123 }       
124
125 FILE *open_file(prefix, suffix)
126     char *prefix, *suffix;
127 {
128     char name[BUFSIZ];
129     int fd;
130     FILE *f;
131     
132     (void) strcpy(name, prefix);
133     (void) strcat(name, suffix);
134
135     fd = open(name, O_CREAT|O_WRONLY|O_EXCL, 0644);
136     if (fd < 0) {
137         punt(name);
138     }
139     f = fdopen(fd, "w");
140     if (f == NULL) {
141         fprintf(stderr, "fdopen of ");
142         punt(name);
143     }
144     fprintf(stderr, "Working on %s\n", name);
145     return(f);
146 }
147
148 /*
149  * Local Variables:
150  * mode: c
151  * c-indent-level: 4
152  * c-continued-statement-offset: 4
153  * c-brace-offset: -4
154  * c-argdecl-indent: 4
155  * c-label-offset: -4
156  * End:
157  */
This page took 0.056071 seconds and 5 git commands to generate.