]> andersk Git - moira.git/blob - backup/dump_db.pc
eliminate use of the `register' keyword: let the compiler decide
[moira.git] / backup / dump_db.pc
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 <fcntl.h>
22 #include <ctype.h>
23 #include <mit-copyright.h>
24 EXEC SQL INCLUDE sqlca;
25 #include "dump_db.h"
26
27 /* putc without the line buffer hair */
28
29 #define putc1(x, p)     (--(p)->_cnt >= 0 ?\
30         (int)(*(unsigned char *)(p)->_ptr++ = (x)) :\
31                 _flsbuf((unsigned char)(x), p))
32
33 FILE *open_file();
34
35 EXEC SQL BEGIN DECLARE SECTION;
36 char *db = "moira";
37 EXEC SQL END DECLARE SECTION;
38
39 int main(int argc, char **argv)
40 {
41   char *prefix;
42   int i;
43
44   if (argc != 2)
45     {
46       fprintf(stderr, "Usage: %s prefix\n", argv[0]);
47       exit(1);
48     }
49   prefix = argv[1];
50
51   EXEC SQL CONNECT :db IDENTIFIED BY :db;
52
53   do_backups(prefix);
54
55   EXEC SQL COMMIT;
56   exit(0);
57 }
58
59 int dump_int(FILE *f, int n)
60 {
61   char buf[1024];
62   sprintf(buf, "%d", n);
63   dump_str(f, buf);
64 }
65
66 int wpunt(void)
67 {
68   punt("can't write backup file");
69 }
70
71 int dump_str(FILE *f, char *str)
72 {
73   char *ibp, c;
74   int t;
75
76   for (ibp = str; c = (unsigned char) *ibp; ibp++)
77     {
78       if (c < 32 || c > 126 || c == SEP_CHAR || c == '\\')
79         {
80           if (putc1('\\', f) < 0)
81             wpunt();
82           t = ((c >> 6) & 7) + '0';
83           if (putc1(t, f) < 0)
84             wpunt();
85           t = ((c >> 3) & 7) + '0';
86           if (putc1(t, f) < 0)
87             wpunt();
88           t = (c & 7) + '0';
89           if (putc1(t, f) < 0)
90             wpunt();
91         }
92       else
93         {
94           if (putc1(c, f) < 0)
95             wpunt();
96         }
97     }
98 }
99
100 int safe_close(FILE *stream)
101 {
102   if (fflush(stream) == EOF)
103     punt("Unable to fflush");
104   if (fsync(fileno(stream)) != 0)
105     punt("Unable to fsync");
106   fclose(stream);
107 }
108
109 FILE *open_file(char *prefix, char *suffix)
110 {
111   char name[BUFSIZ];
112   int fd;
113   FILE *f;
114
115   strcpy(name, prefix);
116   strcat(name, suffix);
117
118   fd = open(name, O_CREAT|O_WRONLY|O_EXCL, 0644);
119   if (fd < 0)
120     punt(name);
121   f = fdopen(fd, "w");
122   if (!f)
123     {
124       fprintf(stderr, "fdopen of ");
125       punt(name);
126     }
127   fprintf(stderr, "Working on %s\n", name);
128   return f;
129 }
130
131
132 /*
133  * Trim whitespace off both ends of a string.
134  */
135 char *strtrim(char *save)
136 {
137   char *t, *s;
138
139   s = save;
140   while (isspace(*s))
141     s++;
142   /* skip to end of string */
143   if (*s == '\0')
144     {
145       *save = '\0';
146       return save;
147     }
148
149   for (t = s; *t; t++)
150     continue;
151   while (t > s)
152     {
153       --t;
154       if (!isspace(*t))
155         {
156           t++;
157           break;
158         }
159     }
160   *t = '\0';
161   return s;
162 }
This page took 0.134973 seconds and 5 git commands to generate.