]> andersk Git - moira.git/blame - backup/rest_db.qc
Throw away rest of field if too long
[moira.git] / backup / rest_db.qc
CommitLineData
dec49b94 1/*
2 * $Source$
3 * $Author$
4 * $Header$
5 *
6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
7 *
8 * $Log$
9cf69cfc 9 * Revision 1.2 1988-05-10 11:34:37 mar
10 * Throw away rest of field if too long
dec49b94 11 *
9cf69cfc 12 * Revision 1.1 87/07/13 03:50:02 wesommer
13 * Initial revision
14 *
dec49b94 15 */
16
17#ifndef lint
18static char *rcsid_rest_db_qc = "$Header$";
19#endif lint
20
21#include <sys/file.h>
22#include <stdio.h>
23#include <ctype.h>
24
25/*ARGSUSED*/
26main(argc, argv)
27 int argc;
28 char **argv;
29{
30 char buf[BUFSIZ];
31 char *prefix;
32
33 if (!yes_or_no("Do you *REALLY* want to wipe the SMS database?")) {
34 printf("I didn't think so\n");
35 exit(1);
36 }
37 if (!yes_or_no("Have you initialized an empty database named smstemp?")) {
38 printf("You should have\n");
39 exit(1);
40 }
41
42 printf("Opening database: ");
43 (void) fflush(stdout);
44## ingres smstemp
45
46 printf(" done\n");
47
48 printf("Prefix of backup to restore: ");
49 (void) fflush(stdout);
50 if (gets(buf) == NULL) {
51 return 1;
52 }
53 prefix = buf;
54
55 if (!yes_or_no("Are you SURE?")) {
56 printf("I didn't think so\n");
57 exit(1);
58 }
59 do_restores(prefix);
60 printf("Restore complete\n");
61## exit
62 exit(0);
63 /*NOTREACHED*/
64}
65
66yes_or_no(prompt)
67 char *prompt;
68{
69 char buf[BUFSIZ];
70 int ret;
71
72 int tt = open("/dev/tty", O_RDWR, 0);
73 FILE *o, *i;
74
75 register char *cp;
76
77 if (tt < 0) return 0;
78
79 (void) fflush(stdout);
80 (void) fflush(stderr);
81 o = fdopen(dup(tt), "w");
82 i = fdopen(dup(tt), "r");
83 (void) close(tt);
84
85 for (;;) {
86 fprintf(o, "%s (yes or no): ", prompt);
87 (void) fflush(o);
88 if (fgets(buf, BUFSIZ, i) == NULL) goto err;
89 for (cp = buf; *cp; cp++) {
90 if (isupper(*cp)) *cp=tolower(*cp);
91 }
92 if (strcmp(buf, "yes\n") == 0) {
93 ret = 1; goto out;
94 }
95 if (strcmp(buf, "no\n") == 0) {
96 ret = 0; goto out;
97 }
98 }
99
100err:
101 ret = 0;
102
103out:
104 (void) fclose(o);
105 (void) fclose(i);
106 return ret;
107}
108
109int parse_int(f)
110 register FILE *f;
111{
112 register int c;
113 register int val = 0;
114 register int sign = 1;
115 while ((c = getc(f)) != EOF && c != ':' && c != '\n') {
116 if (c == '-') sign = -1;
117 else if (isdigit(c)) {
118 val *= 10;
119 val += (c - '0');
120 } else (void) fprintf(stderr,"non-digit in numeric field\n");
121 }
122 (void) ungetc(c, f);
123 return(val * sign);
124}
125
126void parse_str(f, buf, len)
127 register FILE *f;
128 register char *buf;
129 register int len; /* incl trailing NULL */
130{
131 register int c;
132
133 while ((c = getc(f)) != EOF && c != ':' && c != '\n' && len > 0) {
134 if (c == '\\') {
135 c = getc(f);
136 if (isdigit(c)) {
137 /* Expect three-digit octal number.. */
138 register int c1, c2;
139 c1 = getc(f);
140 c2 = getc(f);
141 if (!isdigit(c1) || !isdigit(c2))
142 punt("Broken \\###");
143 /* Convert to ASCII code: */
144 *buf++ = (((c-'0')<<6) + ((c1-'0')<<3) + c2-'0');
145 } else if (c == '\\' || c == ':') {
146 *buf++ = c;
147 --len;
148 } else punt ("Broken '\\'");
149 } else {
150 *buf++ = c;
151 --len;
152 }
153 }
154 if (c == EOF)
155 return;
156
9cf69cfc 157 if (c != EOF && c != ':' && c != '\n') {
dec49b94 158 fprintf(stderr, "Field too wide, truncated\n");
9cf69cfc 159 while ((c = getc(f)) != EOF && c != ':' && c != '\n');
160 (void) ungetc(c, f);
161 } else {
dec49b94 162 *buf++ = 0;
163 (void) ungetc(c, f);
164 }
165}
166
167void parse_sep(f)
168 FILE *f;
169{
170 if (getc(f) != ':') punt("Expected colon");
171}
172void parse_nl(f)
173 FILE *f;
174{
175 if (getc(f) != '\n') punt("Expected newline");
176}
177
178
179FILE *open_file(prefix, suffix)
180 char *prefix, *suffix;
181{
182 char name[BUFSIZ];
183 int fd;
184 FILE *f;
185
186 (void) strcpy(name, prefix);
187 (void) strcat(name, suffix);
188
189 fd = open(name, O_RDONLY, 0);
190 if (fd < 0) {
191 punt(name);
192 }
193 f = fdopen(fd, "r");
194 if (f == NULL) {
195 fprintf(stderr, "fdopen of ");
196 punt(name);
197 }
198 fprintf(stderr, "Working on %s\n", name);
199 return(f);
200}
201
202/*
203 * Local Variables:
204 * mode: c
205 * c-indent-level: 4
206 * c-continued-statement-offset: 4
207 * c-brace-offset: -4
208 * c-argdecl-indent: 4
209 * c-label-offset: -4
210 * End:
211 */
This page took 0.074277 seconds and 5 git commands to generate.