]>
Commit | Line | Data |
---|---|---|
6fb7536b | 1 | /* |
2 | * $Source$ | |
3 | * $Author$ | |
4 | * $Header$ | |
5 | * | |
f0a492af | 6 | * (c) Copyright 1988 by the Massachusetts Institute of Technology. |
7 | * For copying and distribution information, please see the file | |
8 | * <mit-copyright.h>. | |
6fb7536b | 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 | * | |
6fb7536b | 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> | |
d55654f4 | 21 | #include <ctype.h> |
f0a492af | 22 | #include <mit-copyright.h> |
d55654f4 | 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)) | |
6fb7536b | 29 | |
30 | FILE *open_file(); | |
31 | ||
d55654f4 | 32 | char act[128]; |
33 | ||
6fb7536b | 34 | main(argc, argv) |
35 | int argc; | |
36 | char **argv; | |
37 | { | |
38 | char *prefix; | |
d55654f4 | 39 | register int i; |
6fb7536b | 40 | |
41 | if (argc != 2) { | |
42 | fprintf(stderr, "Usage: smsbackup prefix\n"); | |
43 | exit(1); | |
44 | } | |
45 | prefix = argv[1]; | |
46 | ||
d55654f4 | 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 | ||
6fb7536b | 54 | ## ingres sms |
55 | ||
56 | do_backups(prefix); | |
57 | ||
58 | ## exit | |
59 | exit(0); | |
60 | } | |
61 | ||
6fb7536b | 62 | dump_int(f, n) |
63 | FILE *f; | |
64 | int n; | |
65 | { | |
66 | char buf[1024]; | |
d55654f4 | 67 | (void) sprintf(buf, "%d", n); |
6fb7536b | 68 | dump_str(f, buf); |
69 | } | |
70 | ||
d55654f4 | 71 | wpunt() |
6fb7536b | 72 | { |
d55654f4 | 73 | punt("can't write backup file"); |
6fb7536b | 74 | } |
75 | ||
6fb7536b | 76 | dump_str(f, str) |
d55654f4 | 77 | register FILE *f; |
78 | register char *str; | |
6fb7536b | 79 | { |
d6224b2e | 80 | char *strtrim(); |
81 | register char *ibp = strtrim(str); | |
d55654f4 | 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 | } | |
6fb7536b | 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"); | |
d55654f4 | 118 | (void) fclose(stream); |
6fb7536b | 119 | } |
120 | ||
d55654f4 | 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 | ||
d6224b2e | 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 | ||
6fb7536b | 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 | */ |