OpenDNSSEC-signer  1.3.16
backup.c
Go to the documentation of this file.
1 /*
2  * $Id: tools.c 3817 2010-08-27 08:43:00Z matthijs $
3  *
4  * Copyright (c) 2006-2010 NLNet Labs. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
34 #include "config.h"
35 #include "shared/duration.h"
36 #include "shared/file.h"
37 #include "shared/log.h"
38 #include "signer/backup.h"
39 
40 #include <ldns/ldns.h>
41 
42 static const char* backup_str = "backup";
43 
44 
49 char*
51 {
52  static char buf[4000];
53  buf[sizeof(buf)-1]=0;
54 
55  while (1) {
56  if (fscanf(in, "%3990s", buf) != 1) {
57  return 0;
58  }
59  if (buf[0] != '#') {
60  return buf;
61  }
62  if (!fgets(buf, sizeof(buf), in)) {
63  return 0;
64  }
65  }
66  return 0;
67 }
68 
73 int
74 backup_read_check_str(FILE* in, const char* str)
75 {
76  char *p = backup_read_token(in);
77  if (!p) {
78  ods_log_debug("[%s] cannot read check string \'%s\'", backup_str, str);
79  return 0;
80  }
81  if (ods_strcmp(p, str) != 0) {
82  ods_log_debug("[%s] \'%s\' does not match \'%s\'", backup_str, p, str);
83  return 0;
84  }
85  return 1;
86 }
87 
88 
93 int
94 backup_read_str(FILE* in, const char** str)
95 {
96  char *p = backup_read_token(in);
97  if (!p) {
98  ods_log_debug("[%s] cannot read string", backup_str);
99  return 0;
100  }
101  *str = strdup(p);
102  return 1;
103 }
104 
105 
110 int
111 backup_read_time_t(FILE* in, time_t* v)
112 {
113  char* p = backup_read_token(in);
114  if (!p) {
115  ods_log_debug("[%s] cannot read time", backup_str);
116  return 0;
117  }
118  *v=atol(p);
119  return 1;
120 }
121 
122 
127 int
129 {
130  char* p = backup_read_token(in);
131  if (!p) {
132  ods_log_debug("[%s] cannot read duration", backup_str);
133  return 0;
134  }
135  *v=duration_create_from_string((const char*) p);
136  return 1;
137 }
138 
139 
144 int
145 backup_read_rr_type(FILE* in, ldns_rr_type* v)
146 {
147  char* p = backup_read_token(in);
148  if (!p) {
149  ods_log_debug("[%s] cannot read rr type", backup_str);
150  return 0;
151  }
152  *v=(ldns_rr_type) atoi(p);
153  return 1;
154 }
155 
156 
161 int
162 backup_read_int(FILE* in, int* v)
163 {
164  char* p = backup_read_token(in);
165  if (!p) {
166  ods_log_debug("[%s] cannot read integer", backup_str);
167  return 0;
168  }
169  *v=atoi(p);
170  return 1;
171 }
172 
173 
178 int
179 backup_read_size_t(FILE* in, size_t* v)
180 {
181  char* p = backup_read_token(in);
182  if (!p) {
183  ods_log_debug("[%s] cannot read size_t", backup_str);
184  return 0;
185  }
186  *v=(size_t)atoi(p);
187  return 1;
188 }
189 
190 
195 int
196 backup_read_uint8_t(FILE* in, uint8_t* v)
197 {
198  char* p = backup_read_token(in);
199  if (!p) {
200  ods_log_debug("[%s] cannot read uint8_t", backup_str);
201  return 0;
202  }
203  *v= (uint8_t)atoi(p);
204  return 1;
205 }
206 
207 
212 int
213 backup_read_uint16_t(FILE* in, uint16_t* v)
214 {
215  char* p = backup_read_token(in);
216  if (!p) {
217  ods_log_debug("[%s] cannot read uint16_t", backup_str);
218  return 0;
219  }
220  *v= (uint16_t)atoi(p);
221  return 1;
222 }
223 
224 
229 int
230 backup_read_uint32_t(FILE* in, uint32_t* v)
231 {
232  char* p = backup_read_token(in);
233  if (!p) {
234  ods_log_debug("[%s] cannot read uint32_t", backup_str);
235  return 0;
236  }
237  *v= (uint32_t)atol(p);
238  return 1;
239 }
int backup_read_str(FILE *in, const char **str)
Definition: backup.c:94
char * backup_read_token(FILE *in)
Definition: backup.c:50
int backup_read_uint8_t(FILE *in, uint8_t *v)
Definition: backup.c:196
void ods_log_debug(const char *format,...)
Definition: log.c:285
int backup_read_duration(FILE *in, duration_type **v)
Definition: backup.c:128
int backup_read_rr_type(FILE *in, ldns_rr_type *v)
Definition: backup.c:145
int backup_read_uint16_t(FILE *in, uint16_t *v)
Definition: backup.c:213
int backup_read_time_t(FILE *in, time_t *v)
Definition: backup.c:111
int ods_strcmp(const char *s1, const char *s2)
Definition: file.c:317
int backup_read_int(FILE *in, int *v)
Definition: backup.c:162
int backup_read_size_t(FILE *in, size_t *v)
Definition: backup.c:179
int backup_read_check_str(FILE *in, const char *str)
Definition: backup.c:74
duration_type * duration_create_from_string(const char *str)
Definition: duration.c:125
int backup_read_uint32_t(FILE *in, uint32_t *v)
Definition: backup.c:230