-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNWBACKUP.C
193 lines (165 loc) · 5.56 KB
/
NWBACKUP.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>
#include <malloc.h>
#include <direct.h>
#include <dos.h>
#include "dir.h"
#include "nwbackup.h"
static void print_banner();
static void print_usage();
int main(int argc, char * argv[]) {
short prog_mode = 0;
char * logfile_name = NULL;
unsigned short count = 1;
nwBackupParms parms;
nw_algorithm main_program[] = {do_backup, do_diff, do_restore};
char * mode_string[] = {"FULL", "DIFF", "RESTORE"};
/* To ask... can option arguments use '-' for stdin/stdout processing? */
if(argc < 3) {
print_usage();
return EXIT_FAILURE;
}
/* POSIX option args block */
{
int options_parsing = 1;
while(count < argc && argv[count][0] == '-' && options_parsing) {
unsigned short num_opts_after_hyphen, i;
int backup_mode_implied = 0;
int restore_mode_implied = 0;
num_opts_after_hyphen = strlen(argv[count]) - 1;
/* If option doesn't follow hyphen... */
if(!num_opts_after_hyphen) {
/* This single hyphen is actually an operand, which in POSIX world
represents stdin/stdout or a file called '-'... */
fprintf(stderr, "Single hyphen not supported by this utility.\n");
}
for(i = 0; i < num_opts_after_hyphen; i++) {
switch(argv[count][i + 1]) {
case 'r':
if(backup_mode_implied) {
fprintf(stderr, "Backup and restore options are mutually exclusive.\n");
return EXIT_FAILURE;
}
restore_mode_implied = 1;
prog_mode = 2;
break;
case 'f':
if(restore_mode_implied) {
fprintf(stderr, "Full backup and restore options are mutually exclusive.\n");
return EXIT_FAILURE;
}
else if(backup_mode_implied) {
fprintf(stderr, "A backup mode was supplied twice!\n");
return EXIT_FAILURE;
}
backup_mode_implied = 1;
prog_mode = 0;
break;
case 'd':
if(restore_mode_implied) {
fprintf(stderr, "Differential backup and restore options are mutually exclusive.\n");
return EXIT_FAILURE;
}
else if(backup_mode_implied) {
fprintf(stderr, "A backup mode was supplied twice!\n");
return EXIT_FAILURE;
}
backup_mode_implied = 1;
prog_mode = 1;
break;
case 'h':
print_usage();
return EXIT_FAILURE;
case 'l':
if(i != num_opts_after_hyphen - 1) {
fprintf(stderr, "Option -l requires an argument, but " \
"was specified as part of options which do not " \
"require arguments\n");
return EXIT_FAILURE;
}
count++;
/* Skip the leading space! */
logfile_name = argv[count];
break;
default:
fprintf(stderr, "Invalid option: %c.\n", argv[count][i + 1]);
return EXIT_FAILURE;
}
}
count++;
}
}
if(argc - count < 2) {
fprintf(stderr, "Not enough command line parameters (type -h for help)\n");
return EXIT_FAILURE;
}
/* POSIX operands and main logic block */
{
char * backup_name = NULL;
char * start_dir = NULL;
signed char client_rc;
backup_name = argv[count++];
start_dir = argv[count];
if(logfile_name == NULL) {
logfile_name = malloc(128);
snprintf(logfile_name, 128, "%s.LOG", backup_name);
}
fprintf(stderr, "Backup Params:\n" \
"Mode: %s\nBackup Name: %s\nRoot Dir: %s\nLogfile: %s\n", \
mode_string[prog_mode], backup_name, start_dir, logfile_name);
/* To do: Add NWGET utility for restoring individual files... or add as
third program variant. NWGET restore mode and "fetch from public server" mode?
Re-Look up mutual exclusion between program invocation
types and mutual exclusion within invocation type, as well as whether it is
legal to re-specify command line params.*/
client_rc = main_program[prog_mode](&parms, backup_name, start_dir, logfile_name);
return 0;
}
}
/* Disabled for now... */
signed char do_diff(nwBackupParms * parms, char * remote_name, char * local_dir, char * logfile) {
char ctrl_file[L_tmpnam];
return 0;
}
void print_banner() {
fprintf( stderr, "NetWork BACKUP program for mTCP FTP by W. Jones ([email protected])\n"
"TCP stack by M. Brutman ([email protected]) (C)opyright 2012-2014\n\n" );
}
void print_usage() {
print_banner();
/* Removed: [-s server_name][-u username][-p password] */
fprintf( stderr, \
"Usage: NWBACKUP [-d | -f][-l logfile] [backup_name] [local_dir]\n" \
" NWBACKUP -r[-l logfile] [backup_name] [local_dir] \n" \
" [-d | -f]: Differential or full backup (default full).\n" \
" -r: Perform restore. Does NOT need to be first argument.\n" \
" Default logfile name is [backup_name].LOG.\n");
}
/* Returns non-zero value if heap is corrupted. Declare in the main program,
permit any other module to use it. Todo- place in a "general" functions
source file and include only front end header. */
int check_heap() {
int rc = 0;
#ifdef __DOS__
switch(_heapchk()) {
case _HEAPOK:
printf("OK - heap is good\n");
break;
case _HEAPEMPTY:
printf("OK - heap is empty\n");
break;
case _HEAPBADBEGIN:
printf("ERROR - heap is damaged\n");
rc = -1;
break;
case _HEAPBADNODE:
printf("ERROR - bad node in heap\n");
rc = -1;
break;
}
#endif
return rc;
}