Rev 7883 | Rev 7942 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 7883 | Rev 7927 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | Author: JohnXenox. |
2 | Author: JohnXenox. |
3 | Description: The program for backing up a file. |
3 | Description: The program for backing up a file. |
4 | 4 | ||
5 | Works from command line, only! |
5 | Works from command line, only! |
6 | */ |
6 | */ |
7 | 7 | ||
8 | #define CREATION_DATE "2020.05.07" |
8 | #define CREATION_DATE "2020.05.07" |
9 | 9 | ||
10 | #include |
10 | #include |
11 | #include |
11 | #include |
12 | #include |
12 | #include |
13 | 13 | ||
14 | #include "Backy-lib.h" |
14 | #include "Backy-lib.h" |
15 | 15 | ||
16 | int date = 0; |
16 | int date = 0; |
17 | int time = 0; |
17 | int time = 0; |
18 | 18 | ||
19 | char years = 0; |
19 | char years = 0; |
20 | char months = 0; |
20 | char months = 0; |
21 | char days = 0; |
21 | char days = 0; |
22 | 22 | ||
23 | char hours = 0; |
23 | char hours = 0; |
24 | char minutes = 0; |
24 | char minutes = 0; |
25 | char seconds = 0; |
25 | char seconds = 0; |
26 | 26 | ||
27 | char *data = 0; |
27 | char *data = 0; |
28 | int length = 0; |
28 | int length = 0; |
29 | 29 | ||
30 | char path_in[4096] = {0}; |
30 | char path_in[4096] = {0}; |
31 | char path_out[4096] = {0}; |
31 | char path_out[4096] = {0}; |
32 | 32 | ||
33 | char num[3] = {0}; |
33 | char num[3] = {0}; |
34 | 34 | ||
35 | char full_date[25] = {0}; |
35 | char full_date[25] = {0}; |
36 | char ext[] = ".bak"; |
36 | char ext[] = ".bak"; |
37 | 37 | ||
38 | char flag = 0; |
38 | char flag = 0; |
39 | 39 | ||
40 | 40 | ||
41 | 41 | ||
42 | int main(int argc, char** argv) |
42 | int main(int argc, char** argv) |
43 | { |
43 | { |
44 | 44 | ||
45 | // ============================================================ // |
45 | // ============================================================ // |
46 | // preprocessing arguments from the command line. ============= // |
46 | // preprocessing arguments from the command line. ============= // |
47 | // |
47 | // |
48 | // 0 argument - name of the programme. |
48 | // 0 argument - name of the programme. |
49 | // 1 argument - path to the file with name that need to be backup. |
49 | // 1 argument - path to the file with name that need to be backup. |
50 | // 2 argument - the key (-o). |
50 | // 2 argument - the key (-o). |
51 | // 3 argument - path to the output directory without the name of the file. |
51 | // 3 argument - path to the output directory without the name of the file. |
52 | 52 | ||
53 | // printf("Number of args: %d\n", argc); |
53 | // printf("Number of args: %d\n", argc); |
54 | // printf("Argv 0: %s\n", argv[0]); |
54 | // printf("Argv 0: %s\n", argv[0]); |
55 | // sprintf("Argv 1: %s\n\n", argv[1]); |
55 | // sprintf("Argv 1: %s\n\n", argv[1]); |
56 | 56 | ||
57 | 57 | ||
58 | // ============================================================ // |
58 | // ============================================================ // |
59 | // preprocess the command line arguments. ===================== // |
59 | // preprocess the command line arguments. ===================== // |
60 | 60 | ||
61 | if (argc > 1) |
61 | if (argc > 1) |
62 | { |
62 | { |
63 | for (int i = 1; i < argc; i++) |
63 | for (int i = 1; i < argc; i++) |
64 | { |
64 | { |
65 | // if found the key "-o", then copy output path into the array "path_out". |
65 | // if found the key "-o", then copy output path into the array "path_out". |
66 | if (*argv[i] == '-') // && (*(argv[i] + 1) == 'o')) |
66 | if (*argv[i] == '-') // && (*(argv[i] + 1) == 'o')) |
67 | { |
67 | { |
68 | // printf("Key -o is found!\n"); |
68 | // printf("Key -o is found!\n"); |
69 | 69 | ||
70 | i++; |
70 | i++; |
71 | 71 | ||
72 | flag = 1; |
72 | flag = 1; |
73 | 73 | ||
74 | if (i <= argc) |
74 | if (i <= argc) |
75 | { |
75 | { |
76 | // copying of a current path into the array "path_out". |
76 | // copying of a current path into the array "path_out". |
77 | strcpy(path_out, argv[i]); |
77 | strcpy(path_out, argv[i]); |
78 | 78 | ||
79 | // printf("Path output is copyed!\n"); |
79 | // printf("Path output is copyed!\n"); |
80 | 80 | ||
81 | i++; |
81 | i++; |
82 | 82 | ||
83 | break; |
83 | break; |
84 | } |
84 | } |
85 | } |
85 | } |
86 | 86 | ||
87 | // if inut path is found, then copy it into the array "path_in". |
87 | // if inut path is found, then copy it into the array "path_in". |
88 | if (*argv[i] == '/') |
88 | if (*argv[i] == '/') |
89 | { |
89 | { |
90 | flag = 2; |
90 | flag = 2; |
91 | 91 | ||
92 | // copying of a current path into the buffer. |
92 | // copying of a current path into the buffer. |
93 | strcpy(path_in, argv[i]); |
93 | strcpy(path_in, argv[i]); |
94 | 94 | ||
95 | if (flag != 1) |
95 | if (flag != 1) |
96 | { |
96 | { |
97 | int idx = strlen(path_in); |
97 | int idx = strlen(path_in); |
98 | while (path_in[idx] != '/') |
98 | while (path_in[idx] != '/') |
99 | { |
99 | { |
100 | idx--; |
100 | idx--; |
101 | } |
101 | } |
102 | 102 | ||
103 | strncpy(path_out, path_in, idx); |
103 | strncpy(path_out, path_in, idx); |
104 | } |
104 | } |
105 | 105 | ||
106 | // printf("Path input is copyed!\n"); |
106 | // printf("Path input is copyed!\n"); |
107 | } |
107 | } |
108 | 108 | ||
109 | // if found characters. |
109 | // if found characters. |
110 | if ( (*argv[i] > '0') && (*argv[i] < '9') || \ |
110 | if ( (*argv[i] > '0') && (*argv[i] < '9') || \ |
111 | (*argv[i] > 'A') && (*argv[i] < 'Z') || \ |
111 | (*argv[i] > 'A') && (*argv[i] < 'Z') || \ |
112 | (*argv[i] > 'a') && (*argv[i] < 'z') ) |
112 | (*argv[i] > 'a') && (*argv[i] < 'z') ) |
113 | { |
113 | { |
114 | flag = 3; |
114 | flag = 3; |
115 | 115 | ||
116 | strcpy(path_in, argv[0]); |
116 | strcpy(path_in, argv[0]); |
117 | // printf("Arg 0 is copyed!\n"); |
117 | // printf("Arg 0 is copyed!\n"); |
118 | 118 | ||
119 | int idx = strlen(path_in); |
119 | int idx = strlen(path_in); |
120 | 120 | ||
121 | while (path_in[idx] != '/') |
121 | while (path_in[idx] != '/') |
122 | { |
122 | { |
123 | path_in[idx] = 0; |
123 | path_in[idx] = 0; |
124 | idx--; |
124 | idx--; |
125 | } |
125 | } |
126 | 126 | ||
127 | idx++; |
127 | idx++; |
128 | strcpy(path_out, path_in); |
128 | strcpy(path_out, path_in); |
129 | strcpy(&path_in[idx], argv[1]); |
129 | strcpy(&path_in[idx], argv[1]); |
130 | // printf("Arg 1 is added!\n"); |
130 | // printf("Arg 1 is added!\n"); |
131 | 131 | ||
132 | } |
132 | } |
133 | } |
133 | } |
134 | 134 | ||
135 | // if not found the flag, then copy path from "path_in" into "path_out". |
135 | // if not found the flag, then copy path from "path_in" into "path_out". |
136 | if ((flag == 0) && (flag != 2) && (flag != 3)) |
136 | if ((flag == 0) && (flag != 2) && (flag != 3)) |
137 | { |
137 | { |
138 | // copying the input path into the output path, |
138 | // copying the input path into the output path, |
139 | strcpy(path_out, path_in); |
139 | strcpy(path_out, path_in); |
140 | //printf("Path input is copyed into the path output!\n"); |
140 | //printf("Path input is copyed into the path output!\n"); |
141 | } |
141 | } |
142 | } |
142 | } |
143 | else |
143 | else |
144 | { |
144 | { |
145 | //con_init_console_dll_param(-1, 23, -1, 23, "Backy"); |
145 | //con_init_console_dll_param(-1, 23, -1, 23, "Backy"); |
146 | 146 | ||
147 | printf("\n Name: Backy"); |
147 | printf("\n Name: Backy"); |
148 | printf("\n Date: %s", CREATION_DATE); |
148 | printf("\n Date: %s", CREATION_DATE); |
149 | printf("\n Description: The programme for backing up a file.\n"); |
149 | printf("\n Description: The programme for backing up a file.\n"); |
150 | printf("\n Author: JohnXenox\n"); |
150 | printf("\n Author: JohnXenox\n"); |
151 | 151 | ||
152 | printf("\n Usage: backy |
152 | printf("\n Usage: backy |
153 | printf(" path1 - path to a file to be backuped.\n"); |
153 | printf(" path1 - path to a file to be backuped.\n"); |
154 | printf(" -o path2 - path to the output directory without the name of a file.\n\n"); |
154 | printf(" -o path2 - path to the output directory without the name of a file.\n\n"); |
155 | 155 | ||
156 | printf(" Examples:\n"); |
156 | printf(" Examples:\n"); |
157 | printf(" backy test.c\n"); |
157 | printf(" backy test.c\n"); |
158 | printf(" backy test.c -o /tmp0/1/\n"); |
158 | printf(" backy test.c -o /tmp0/1/\n"); |
159 | printf(" backy /hd0/1/test.c\n"); |
159 | printf(" backy /hd0/1/test.c\n"); |
160 | printf(" backy /hd0/1/test.c -o /tmp0/1/\n"); |
160 | printf(" backy /hd0/1/test.c -o /tmp0/1/\n"); |
161 | 161 | ||
162 | exit(0); |
162 | exit(0); |
163 | return 1; |
163 | return 1; |
164 | } |
164 | } |
165 | 165 | ||
166 | //printf("Path_in: %s\n", path_in); |
166 | //printf("Path_in: %s\n", path_in); |
167 | //printf("Path_out: %s\n", path_out); |
167 | //printf("Path_out: %s\n", path_out); |
168 | 168 | ||
169 | 169 | ||
170 | // ============================================================ // |
170 | // ============================================================ // |
171 | // getting the time in BCD. =================================== // |
171 | // getting the time in BCD. =================================== // |
172 | 172 | ||
173 | time = getTime(); // time = 0x00SSMMHH. |
173 | time = getTime(); // time = 0x00SSMMHH. |
174 | 174 | ||
175 | hours = (char)time; |
175 | hours = (char)time; |
176 | minutes = (char)(time >> 8); |
176 | minutes = (char)(time >> 8); |
177 | seconds = (char)(time >> 16); |
177 | seconds = (char)(time >> 16); |
178 | 178 | ||
179 | // ============================================================ // |
179 | // ============================================================ // |
180 | // getting the date in BCD. =================================== // |
180 | // getting the date in BCD. =================================== // |
181 | 181 | ||
182 | date = getDate(); // date = 0x00DDMMYY. |
182 | date = getDate(); // date = 0x00DDMMYY. |
183 | 183 | ||
184 | years = (char)date; |
184 | years = (char)date; |
185 | months = (char)(date >> 8); |
185 | months = (char)(date >> 8); |
186 | days = (char)(date >> 16); |
186 | days = (char)(date >> 16); |
187 | 187 | ||
188 | // ============================================================ // |
188 | // ============================================================ // |
189 | // fills the array with the date in ASCII. ==================== // |
189 | // fills the array with the date in ASCII. ==================== // |
190 | 190 | ||
191 | char ofs = 0; |
191 | char ofs = 0; |
192 | char *dta = 0; |
192 | char *dta = 0; |
193 | 193 | ||
194 | for (char i = 0; i < 3; i++) |
194 | for (char i = 0; i < 3; i++) |
195 | { |
195 | { |
196 | if (i == 0) |
196 | if (i == 0) |
197 | { |
197 | { |
198 | dta = &years; |
198 | dta = &years; |
199 | full_date[ofs] = '2'; |
199 | full_date[ofs] = '2'; |
200 | ofs++; |
200 | ofs++; |
201 | full_date[ofs] = '0'; |
201 | full_date[ofs] = '0'; |
202 | ofs++; |
202 | ofs++; |
203 | } |
203 | } |
204 | if (i == 1) |
204 | if (i == 1) |
205 | { |
205 | { |
206 | dta = &months; |
206 | dta = &months; |
207 | } |
207 | } |
208 | if (i == 2) |
208 | if (i == 2) |
209 | { |
209 | { |
210 | dta = &days; |
210 | dta = &days; |
211 | } |
211 | } |
212 | 212 | ||
213 | 213 | ||
214 | itoab(*dta, num, 16); |
214 | itoa(*dta, num, 16); |
215 | 215 | ||
216 | if (num[1] == 0) |
216 | if (num[1] == 0) |
217 | { |
217 | { |
218 | full_date[ofs] = '0'; |
218 | full_date[ofs] = '0'; |
219 | ofs++; |
219 | ofs++; |
220 | full_date[ofs] = num[0]; |
220 | full_date[ofs] = num[0]; |
221 | } |
221 | } |
222 | else |
222 | else |
223 | { |
223 | { |
224 | full_date[ofs] = num[0]; |
224 | full_date[ofs] = num[0]; |
225 | ofs++; |
225 | ofs++; |
226 | full_date[ofs] = num[1]; |
226 | full_date[ofs] = num[1]; |
227 | } |
227 | } |
228 | 228 | ||
229 | ofs++; |
229 | ofs++; |
230 | 230 | ||
231 | if (i != 2) |
231 | if (i != 2) |
232 | { |
232 | { |
233 | full_date[ofs] = '.'; |
233 | full_date[ofs] = '.'; |
234 | ofs++; |
234 | ofs++; |
235 | } |
235 | } |
236 | } |
236 | } |
237 | 237 | ||
238 | full_date[ofs] = '_'; |
238 | full_date[ofs] = '_'; |
239 | ofs++; |
239 | ofs++; |
240 | 240 | ||
241 | // ============================================================ // |
241 | // ============================================================ // |
242 | // fills the array with the time in ASCII. ==================== // |
242 | // fills the array with the time in ASCII. ==================== // |
243 | 243 | ||
244 | ofs = 11; |
244 | ofs = 11; |
245 | dta = 0; |
245 | dta = 0; |
246 | 246 | ||
247 | for (char i = 0; i < 3; i++) |
247 | for (char i = 0; i < 3; i++) |
248 | { |
248 | { |
249 | if (i == 0) |
249 | if (i == 0) |
250 | dta = &hours; |
250 | dta = &hours; |
251 | if (i == 1) |
251 | if (i == 1) |
252 | dta = &minutes; |
252 | dta = &minutes; |
253 | if (i == 2) |
253 | if (i == 2) |
254 | dta = &seconds; |
254 | dta = &seconds; |
255 | 255 | ||
256 | itoab(*dta, num, 16); |
256 | itoa(*dta, num, 16); |
257 | 257 | ||
258 | if (num[1] == 0) |
258 | if (num[1] == 0) |
259 | { |
259 | { |
260 | full_date[ofs] = '0'; |
260 | full_date[ofs] = '0'; |
261 | ofs++; |
261 | ofs++; |
262 | full_date[ofs] = num[0]; |
262 | full_date[ofs] = num[0]; |
263 | } |
263 | } |
264 | else |
264 | else |
265 | { |
265 | { |
266 | full_date[ofs] = num[0]; |
266 | full_date[ofs] = num[0]; |
267 | ofs++; |
267 | ofs++; |
268 | full_date[ofs] = num[1]; |
268 | full_date[ofs] = num[1]; |
269 | } |
269 | } |
270 | 270 | ||
271 | ofs++; |
271 | ofs++; |
272 | 272 | ||
273 | if (i < 2) |
273 | if (i < 2) |
274 | { |
274 | { |
275 | full_date[ofs] = '.'; |
275 | full_date[ofs] = '.'; |
276 | } |
276 | } |
277 | //else |
277 | //else |
278 | //{ |
278 | //{ |
279 | // full_date[ofs] = '_'; |
279 | // full_date[ofs] = '_'; |
280 | //} |
280 | //} |
281 | 281 | ||
282 | ofs++; |
282 | ofs++; |
283 | } |
283 | } |
284 | 284 | ||
285 | // ============================================================ // |
285 | // ============================================================ // |
286 | // adding the name of the input file to the output path. ====== // |
286 | // adding the name of the input file to the output path. ====== // |
287 | 287 | ||
288 | // searching the end of the path string. |
288 | // searching the end of the path string. |
289 | int i = 0; |
289 | int i = 0; |
290 | int y = 0; |
290 | int y = 0; |
291 | 291 | ||
292 | // searching for zero terminator in the input path. |
292 | // searching for zero terminator in the input path. |
293 | while (path_in[i] != 0) |
293 | while (path_in[i] != 0) |
294 | { |
294 | { |
295 | i++; |
295 | i++; |
296 | } |
296 | } |
297 | 297 | ||
298 | // searching for slash in the input path. |
298 | // searching for slash in the input path. |
299 | while (path_in[i] != '/') |
299 | while (path_in[i] != '/') |
300 | { |
300 | { |
301 | i--; |
301 | i--; |
302 | } |
302 | } |
303 | 303 | ||
304 | // searching for zero terminator in the output path. |
304 | // searching for zero terminator in the output path. |
305 | while (path_out[y] != 0) |
305 | while (path_out[y] != 0) |
306 | { |
306 | { |
307 | y++; |
307 | y++; |
308 | } |
308 | } |
309 | 309 | ||
310 | // searching for slash in the output path. |
310 | // searching for slash in the output path. |
311 | if (path_out[y - 1] == '/') |
311 | if (path_out[y - 1] == '/') |
312 | { |
312 | { |
313 | y--; |
313 | y--; |
314 | } |
314 | } |
315 | 315 | ||
316 | // copying the input name of the file into the output path, |
316 | // copying the input name of the file into the output path, |
317 | strcpy(&path_out[y], &path_in[i]); |
317 | strcpy(&path_out[y], &path_in[i]); |
318 | 318 | ||
319 | // ============================================================ // |
319 | // ============================================================ // |
320 | // adding the extension and full date to the path. ============ // |
320 | // adding the extension and full date to the path. ============ // |
321 | 321 | ||
322 | i = 0; |
322 | i = 0; |
323 | 323 | ||
324 | // searching for zero terminator in the output path. |
324 | // searching for zero terminator in the output path. |
325 | while (path_out[i] != 0) |
325 | while (path_out[i] != 0) |
326 | { |
326 | { |
327 | i++; |
327 | i++; |
328 | } |
328 | } |
329 | 329 | ||
330 | path_out[i] = '_'; |
330 | path_out[i] = '_'; |
331 | i++; |
331 | i++; |
332 | 332 | ||
333 | // adding full date. |
333 | // adding full date. |
334 | strcpy(&path_out[i], full_date); |
334 | strcpy(&path_out[i], full_date); |
335 | 335 | ||
336 | i += strlen(full_date); |
336 | i += strlen(full_date); |
337 | 337 | ||
338 | // adding the extension to path. |
338 | // adding the extension to path. |
339 | strncpy(&path_out[i], ext, 4); |
339 | strncpy(&path_out[i], ext, 4); |
340 | 340 | ||
341 | //printf("Path_in: %s\n", path_in); |
341 | //printf("Path_in: %s\n", path_in); |
342 | //printf("Path_out: %s\n", path_out); |
342 | //printf("Path_out: %s\n", path_out); |
343 | 343 | ||
344 | data = openFile(&length, path_in); |
344 | data = openFile(&length, path_in); |
345 | 345 | ||
346 | saveFile(length, data, 0, path_out); |
346 | saveFile(length, data, 0, path_out); |
347 | 347 | ||
348 | 348 | ||
349 | return 0; |
349 | return 0; |
350 | }>>>-o>>>>=>> |
350 | }>>>-o>>>>=>> |