summaryrefslogtreecommitdiff
path: root/client/walker.c
blob: e764332fada558fc2dd282b7d6722bfe2bdbeb4c (plain)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*       Recursive Walking Backup
 *       Copyright © 2015 Jeffrey Armstrong <jeff@rainbow-100.com>
 *       http://jeff.rainbow-100.com/
 *
 *       This program is free software: you can redistribute it and/or modify
 *       it under the terms of the GNU General Public License as published by
 *       the Free Software Foundation, either version 3 of the License, or
 *       (at your option) any later version.
 *       
 *       This program is distributed in the hope that it will be useful
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU General Public License for more details.
 *       
 *       You should have received a copy of the GNU General Public License
 *       along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* walker.c - This program will walk a directory (by default E:), including
 *            recursively, and transmit every file via FOSSIL to a server
 *            program.  To compile with Borland Turbo C (2.0 or 2.01), be
 *            sure to define TURBOC.
 */

#include <stdio.h>
#include <dos.h>

#include "fossil.h"
#include "f16.h"

#ifdef TURBOC

#include <dir.h>

#define FFNAME(x)       x.ff_name
#define FFATTRIB(x)     x.ff_attrib

#define SEARCH_FLAGS    FA_DIREC

#else

/* MSC defines things a bit differently */

#define findfirst(x, y, z)      _dos_findfirst(x, z, y)
#define findnext(x)             _dos_findnext(x)

#define SEARCH_FLAGS    _A_SUBDIR | _A_NORMAL
#define FA_DIREC        _A_SUBDIR

#define ffblk           find_t

#define FFNAME(x)       x.name
#define FFATTRIB(x)     x.attrib

#endif

static char response[16];

/* The transmission block size. This many bytes are transfered
 * prior to sending the checksum.  Do not increase above 255!
 */
#define BLOCK_SIZE 64

/* Define this constant to prohibit FOSSIL initialization */
/* #define NO_FOS_INIT     1 */

/* Sends a null-terminated string */
void fos_send(const char *text)
{
int i, length, r;

        length = strlen(text);
        for(i=0;i<length;i++) {
                r = F_Send_Char(TRANSMIT, text[i]);
        }
        F_Flush_Out();
}

/* Reads a semicolon-terminated response from the serial port
 * and stores the value in the global array "response."
 */
void read_response()
{
char c;
int i;
        
        while(!F_NBPeek());
        
        memset(response, 0, 16);
        
        c = 13;
        i = 0;
        while(c != ';') {
                while(!F_NBPeek());        
                c = Get_Raw();
                response[i] = c;
                i++;
        }
        /* printf("RESPONSE(%s) .. ", response); */
}

/* Tranmits the passed buffer, measuring "rlen" bytes long, to
 * the server.  The protocol first sends the number of bytes in
 * the buffer, then the buffer itself, and, finally, a two-byte
 * Fletcher 16 checksum for the buffer.
 */
void transmit(unsigned char *buf, int rlen)
{
int i;
unsigned char b1, b2;

        F_Send_Char(TRANSMIT, (unsigned char)rlen);
        for(i=0;i<rlen;i++) {
                F_Send_Char(TRANSMIT, buf[i]);
                if(i % 8 == 0)
                        F_Flush_Out();
        }        
        fletcher16(buf, rlen, &b1, &b2);
        F_Send_Char(TRANSMIT, b1);
        F_Send_Char(TRANSMIT, b2);

        F_Flush_Out();
}

/* Transmits the next block in a file pointer.  The actual sending
 * is handled by the "transmit()" function.  This routine handles
 * loading the block from the file and subsequently dealing with the
 * server response.
 */
int send_block(FILE *fp)
{
int rlen, i;
unsigned char buf[BLOCK_SIZE];
unsigned char b1, b2;

        if(fp == NULL) return -1;
        
        rlen = fread(buf, sizeof(char), BLOCK_SIZE, fp);
        if(rlen > 0) { 
                i = 0;
                do {
                        if(i > 8) {
                            rlen = -1;
                            break;
                        }
                        
                        i++;
                        transmit(buf, rlen);
                        read_response();
                } while(strcmp("OK;", response) != 0);
        }
        return rlen;
}

/* Sends the filename to the server and reads the server response */
int report_file(const char *fullname)
{
        fos_send(fullname);
        fos_send(";");

        read_response();
        if(strcmp("OK;", response) == 0)
                return 1;
        else
                return 0;

}

/* Wrapper for sending a file, along with all the bookkeeping, to
 * the server software.  This function will open the file and request
 * sending all blocks until the entire file is transmitted.  It also
 * produces minimum reports of progress.
 */
int send_file(const char *fullname)
{
FILE *fp;
int result;
char cmd[512];

        printf("File: %s .. ", fullname);
        
        if(report_file(fullname) == 1)
                printf("SENDING...");
        else {
                printf("ABORT\n");
                return 0;
        }
        fp = fopen(fullname, "rb");
     
        result = 0;
        do {
                /* printf("."); */
                result = send_block(fp);
        } while(result > 0);
        
        /* Send a 0 length flag */
        F_Send_Char(TRANSMIT, '\0');
        F_Flush_Out();

        fclose(fp);
        printf("DONE\n");
        
        return 1 /* result */;
}

/* Walks the directory tree starting at the directory specified, and
 * sends every file.  When a directory is encountered, this function
 * is called recursively.
 */
int list_dir(const char *directory)
{
int fcount;
struct ffblk walker;
char *fname;
char *fullname;
char *path;

        fcount = 0;

        fullname = (char *)malloc((strlen(directory)+16)*sizeof(char));

        printf("Entering: %s\n", directory);

        path = (char *)malloc((strlen(directory)+8)*sizeof(char));
        sprintf(path, "%s\\*.*", directory);

        /* Loop files */
        if(findfirst(path, &walker, SEARCH_FLAGS) == 0) {
                do {
                        /* Populate fname */
                        fname = FFNAME(walker);
                        if(fname[0] != '.') {
                                sprintf(fullname, "%s\\%s", directory, fname);
                                
                                if(FFATTRIB(walker) & FA_DIREC) {
                                
                                        fcount += list_dir(fullname);
                                                               
                                } else if(send_file(fullname) != 1) {
                                
                                        break;
                                
                                } else {
                                
                                        fcount++;
                                        
                                }
                        }
                } while(findnext(&walker) == 0);
        } 

        free(fullname);
        free(path);
        
        return fcount;
}

/* Initializes the COM port and FOSSIL driver */
int start_comm(long fspeed)
{
#ifndef NO_FOS_INIT                

        printf("Initializing FOSSIL      ");
        if(!F_Init()) {
                printf("FAIL\n");
                return 0;
        } else
                printf("OK\n");

        printf("Configuring FOSSIL       ");
        if (!F_Set_Baud(fspeed)) {
                printf("FAIL (speed)\n");
                return 0;
        }

        Fl_Ctrl(TXONXOFF|RXONXOFF);     /* CTSRTS); */
        printf("OK\n");
        
        printf("Raising DTR              ");
        F_Dtr(TRUE);
        printf("OK\n");
#endif
        
        return 1;
}

int main(int argc, char *argv[])
{
int fcount;
char *directory;
long fspeed;
char c;

        if(argc < 3) {
                printf("Usage: %s port speed [directory (optional)]\n", argv[0]);
                return 0;
        }
        
        port = atoi(argv[1]);
        fspeed = atol(argv[2]);
        printf("Configuring Port %d for %d BPS...\n", port, fspeed);

        dcd_ignore = TRUE;
 
        if(argc == 3) {
                directory = "E:";
        } else {
                directory = argv[3];
        }

        if(start_comm(fspeed) != 1) {
                return 0;
        }
                
        printf("Sending test string...\n");
        fos_send("ONLINE");
        printf("Press Q to quit or anything else to continue...\n");
        scanf("%c", &c);
        if(c == 'q' || c == 'Q') return 0;

        printf("Starting in %s\n\n", directory);

        fcount = list_dir(directory);
        
        printf("TOTAL: %d\n\n", fcount);
        
        F_De_Init();

        return fcount;
}