File: remote_dir.c

package info (click to toggle)
moxftp 2.2-18.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,160 kB
  • ctags: 3,837
  • sloc: ansic: 43,420; makefile: 353; perl: 262; sh: 192
file content (541 lines) | stat: -rw-r--r-- 14,139 bytes parent folder | download | duplicates (4)
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
 * Copyright (c) 1992 The Regents of the University of Texas System.
 * Copyright (c) 1993 The Regents of the University of Texas System.
 * Copyright (c) 1994 The Regents of the University of Texas System.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted provided
 * that the above copyright notice and this paragraph are duplicated in all
 * such forms and that any documentation, advertising materials,  and other
 * materials  related to such  distribution  and use  acknowledge  that the
 * software  was  developed  by the  University of Texas.  The  name of the
 * University may not be  used to endorse or promote  products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1993 The Regents of the University of Texas System.\n\
 All rights reserved.\n";
static char rcsid[] =
"$Id: remote_dir.c,v 1.1 1994/03/14 18:55:53 jones Exp $\n";
static char rcspath[] =
"$Source: /mintaka/u0/xx/ss/jones/jones.old/mftp.src/mftpnew/RCS/remote_dir.c,v $\n";
#endif

/* $Log: remote_dir.c,v $
 * Revision 1.1  1994/03/14  18:55:53  jones
 * Initial revision
 *
 */
#include "machine.h"
#include "defs.h"
#include "proto/remote_dir.h"

static XtIntervalId timeout;

extern int  ibm_rt;
extern int  appolo;
extern int  guessed;
extern char *system_name;
extern char *system_type;

#define MAXSTRING 2000


void 
Start_Remote_Dir(echo, flag, callback, data, cb)
int    echo;
int    flag;
void (*callback)();
DATA   data;
CB    *cb;
{
    struct _dirs *dir = NULL;
    char *s;
    CB *cbp;
 
    dir = Find_Dir(remote_dir, hostname, 1);
    Update_Remote_Time(dir);

    cbp = link_callback(NULL, callback, data, cb);
    if (flag || dir == NULL || (dir && dir->n < 0) || (dir && dir->update)) {
	Push_Files(dir);
        if ((guessed == 1) || (appolo == 2)) {
	    s = XtNewString("LIST -l");
	} else {
	    s = XtNewString("LIST");
	}
	Start_Listen(s, status, finish_remote_dir, 0, echo, 
		     flag, cbp, NULL, 0);
	XtFree(s);
        Set_Busy(BUSY_DIR);
        Set_Noop(NOOP_DIR);
        return;
    } else {
	do_callbacks(1, cbp);
    }
}

void
Update_Remote_Time(dir)
struct _dirs *dir;
{
    time_t now;
    time_t w;
    extern int mult_file;

    /*
     * Some one else is worring direcotry updates.
     */
    if (mult_file) return;

    if (dir == NULL) return;
    time(&now);
    if (dir->time != 0) {
	w = now - dir->time;
	w = w/(10*60);
	if (w) {
	    dir->update++;
	    dir->time = now;
	}
    } else {
	dir->time = now;
    }
}

static void 
finish_remote_dir(output, tag,  data)
struct _output *output;
int  tag;
DATA data;
{
    int code;
    struct _dirs *dir;

    if(timeout) {
        if (timeout) XtRemoveTimeOut(timeout);
        timeout = 0;
        Update_Percent(0, 0);
    }

    Unset_Busy();
    Update_Percent(0, 0);
    Clear_Noop(NOOP_DIR);
    code =  ftp_status(output);
    if (ftp_response[code].status & FTP_ERROR) {
	char *cp = NULL;
	cp = concat(cp, "Remote dir failed: ");
	cp = concat(cp, last_response);
	Set_Status(cp);
        dir = Find_Dir(remote_dir, hostname, 0);
	Pop_Files(dir);
        do_callbacks(code, (CB *)data);
	return;
    }

    switch (remote_type) {
      case REMOTE_SYSTEM_UNIX:
	parse_unix(output);
	break;
      case  REMOTE_SYSTEM_OTHER:
        parse_other(output);
	break;
      default:
	fprintf(stderr,"Unknown remote type %d\n",remote_type);
    }

    dir = Find_Dir(remote_dir, hostname, 0);
    Restore_Dir_Old(dir);
    if (dir) dir->update = 0;
    if(Remote_local == REMOTE) Display_Files(remote_dir, hostname,  0);
    Set_Status(" ");
    do_callbacks(code, (CB *)data);
}

static void
init_output(output)
struct _output *output;
{
    if (output) {
	output->current = 0;
	output->temp = output;
    }
}

static char *
next_output(output, peek)
struct _output *output;
int peek;
{
    struct _output *next;
    int i;

    if (!output) return NULL;
    next = output->temp;
    while(next) {
        i = next->current;
        if (next->current < next->lines ) {
	    if (!peek) next->current++;
            return (next->output[i]);
        }
        next = next->next;
	if (!peek) output->temp = next;
	if (next == NULL) return NULL;
	if (peek) {
            if (0 < next->lines ) {
            	return (next->output[0]);
            } else {
	 	return NULL;
	    }
	}
	
        next->current = 0;
    }
    return NULL;
}

static void
parse_other(output)
struct _output *output;
{
    struct _dirs *dir;
    int files;
     
    if (output == NULL) return;

    /*
     * Compute number of files.
     */
    files = scan_other(output, NULL);
    if (!files) return;
    /*
     * Set up dirtory.
     */
    if (remote_dir == NULL) return;
    dir = Find_Dir(remote_dir, hostname, 1);
    dir->files = (struct _files *)XtMalloc(files*sizeof(struct _files));
    ZERO((char *)dir->files, files*sizeof(struct _files));
    dir->n     = files;
    dir->type  = REMOTE_SYSTEM_OTHER;
    /*
     * Scan files.
     */
    files = scan_other(output, dir);
}

static void
parse_unix(output)
struct _output *output;
{
    struct _dirs *dir;
    int files;
     
    if (output == NULL) return;

    /*
     * Compute number of files.
     */
    files = scan_unix(output, NULL);
    if (!files) {
        dir = Find_Dir(remote_dir, hostname, 1);
	dir->n = 0;
	return;
    };
    /*
     * Set up dirtory.
     */
    dir = Find_Dir(remote_dir, hostname, 1);
    dir->files = (struct _files *)XtMalloc(files*sizeof(struct _files));
    ZERO((char *)dir->files, files*sizeof(struct _files));
    dir->n     = files;
    dir->type  = REMOTE_SYSTEM_UNIX;
    /*
     * Scan files.
     */
    files = scan_unix(output, dir);
    if (files > dir->n) fprintf(stderr,"Too many files %d %d\n",files, dir->n);
}

static void
percent(client_data, id)
caddr_t client_data;
XtIntervalId *id;
{
    int size;
   
    size = Get_Chars();
    if (size) Update_Percent(0, size);
    timeout =  XtAppAddTimeOut(App_context, (unsigned long)1000,
                               (XtTimerCallbackProc)percent,
                               (XtPointer)NULL);
}

static int
scan_other(output, dir)
struct _output *output;
struct _dirs *dir;
{
    char *cp, *nl;
    int  n = 0;
    int  files = 0;
    char *line;

    init_output(output);
    while(cp = next_output(output,0)) {
        n = strlen(cp);
	if (n >= MAXSTRING) continue;
	line = concat(NULL, cp);
	if (s_tran->indent && (cp = next_output(output, 1)) && *cp == ' ') {
	    n = strlen(cp);
	    if (((int)strlen(line)+n) >= MAXSTRING) continue;
 	    cp = next_output(output, 0);
	    /*
	     * Squache new line.
	     */
	    line[(int)strlen(line) -1] = '\0';
	    line = concat(line, cp);
	}
        if(Tran_Scan(line, dir, files)) continue;
	XtFree(line);
	files++;
	if (dir && files > dir->n) {
	    fprintf(stderr, "Busted %d %d\n", files, dir->n);
	}
    }
    return files;
}

static int
scan_unix(output, dir)
struct _output *output;
struct _dirs *dir;
{
    char *cp, *nl;
    int  n = 0;
    int  mode;
    int  files = 0;
    char type[MAXSTRING+1];
    char owner[MAXSTRING+1];
    char group[MAXSTRING+1];
    int  size;
    int  links;
    char line[MAXSTRING+1];
    char month[MAXSTRING+1];
    char day[MAXSTRING+1];
    char time_year[MAXSTRING+1];
    char file[MAXSTRING+1];
    char linkname[MAXSTRING+1];
    char junk[MAXSTRING+1];
    char junk1[MAXSTRING+1];
    int  pass = 0;

    init_output(output);
    while(cp = next_output(output,0)) {
        if(pass > 20) {
            pass = 0;
            Update_Screen();
        }
        pass++;
        n = strlen(cp);
	if (n >= MAXSTRING) continue;
        if ((guessed == 1) && (appolo == 0)) {
	    if (cp[10] == '+') {
		appolo = 1;
	        n = sscanf(cp, "%11s %[^\n]", type , line);
                n = sscanf(line, "%s %s %s %s %s %s %s %s %s %s",
		                 owner, group, junk1, month, day, time_year, 
		       	       file, junk, linkname);
	        if(n == 5) appolo = 2;
                if (system_name) XtFree(system_name);
                if (system_type) XtFree(system_type);
                system_name = XtNewString("Appolo");
                system_type = XtNewString("Appolo");
		Set_Type_Name();
	        guessed = 3;
	    }
	}
	if (!appolo) {
	    n = sscanf(cp, "%10s%d %[^\n]", type, &links, line);
	    if (n != 3) continue;
	} else {
	    if (appolo == 1) {
	        n = sscanf(cp, "%11s%d %[^\n]",
		     type , &links, line);
	        if (n != 3) continue;
	    } if (appolo == 2) {
	        n = sscanf(cp, "%11s %[^\n]",
		     type, line);
		links = 0;
	        if (n != 2) continue;
	    }
	}
        if ((guessed < 3) && (ibm_rt == 0)) {
            n = sscanf(line, "%s %s %s %s %s %s %s %s %s %s",
		       owner, group, junk1, month, day, time_year, 
		       file, junk, linkname);
	    if (verify_mode(type) > 0) {
	        if (n == 6) ibm_rt++;
		if (n == 8 && strcmp(file, "->") == 0) ibm_rt++; 
	    }
	    if (ibm_rt) {
		if (guessed) {
                    if (system_name) XtFree(system_name);
                    if (system_type) XtFree(system_type);
                    system_name = XtNewString("IBM RT");
                    system_type = XtNewString("IBM RT");
		    Set_Type_Name();
		}
	    }
	    guessed = 3;
	}
	if (ibm_rt) {
            n = sscanf(line, "%s %d %s %s %s %s %s %s %s",
		       owner, &size, month, day, time_year, 
		       file, junk, linkname);
	    group[0] = '\0';
            if (n == 6) {
		linkname[0] = '\0';
	    } else {
	        if (type[0] != 'l' && type[0] != 'L') continue;
	        if (strcmp(junk, "->") != 0) continue;
	    }
	    if (n < 6) {
	        fprintf(stderr, "Could not parse ibm rt file entry %s\n", cp);
	        continue;
	    }
	} else if (appolo == 2) {
            n = sscanf(line, "%d %s %s %s %s %s %s %s",
		       &size, month, day, time_year, 
		       file, junk, linkname);
	    group[0] = '\0';
	    owner[0] = '\0';
            if (n == 5) {
		linkname[0] = '\0';
	    } else {
	        if (type[0] != 'l' && type[0] != 'L') continue;
	        if (strcmp(junk, "->") != 0) continue;
	    }
	    if (n < 5) {
	        fprintf(stderr, "Could not parse appolo file entry %s\n", cp);
	        continue;
	    }
	} else {
            n = sscanf(line, "%s %s %d %s %s %s %s %s %s",
		       owner, group, &size, month, day, time_year, 
		       file, junk, linkname);
            if (n == 7) {
		linkname[0] = '\0';
	    } else {
	        if (!((type[0] == 'l' || type[0] == 'L') &&
	             (strcmp(junk, "->") == 0))) {
                    n = sscanf(line, "%s %s %d %s %s %s %[^\n]",
		       owner, group, &size, month, day, time_year, 
		       file);
		    if (n != 7)  {
			continue;
		    }
		    linkname[0] = '\0';
		}
	    }
	    if (n < 6) continue;
	}

	if ((mode = verify_mode(type)) < 0) {
		fprintf(stderr, "Don't mode mode %s %s\n",type, file);
	        continue;
	}

	if (dir) {
	    dir->files[files].owner     = XtNewString(owner);
	    dir->files[files].group     = XtNewString(group);
	    dir->files[files].link      = links;
	    dir->files[files].size      = size;
	    dir->files[files].month     = XtNewString(month);
	    dir->files[files].day       = XtNewString(day);
	    dir->files[files].time_year = XtNewString(time_year);
	    dir->files[files].name      = XtNewString(file);
	    dir->files[files].linkname  = XtNewString(linkname);
	    dir->files[files].modes     = XtNewString(type);
	    dir->files[files].mode      = mode;
	    dir->files[files].local     = dir->files[files].name;
	    dir->files[files].remote    = dir->files[files].name;    
	    dir->files[files].time      = get_unix_time(day, time_year, month);
	    if ((mode & MODE_TYPE_DIR) || (mode & MODE_TYPE_LINK))
	      dir->files[files].dir_name  = dir->files[files].name;
	}
	files++;
	if (dir && files > dir->n) {
	    fprintf(stderr, "Busted %d %d\n", files, dir->n);
	}
    }
    return files;
}

static void
status(command, tag)
char *command;
int   tag;
{
    char *cp = NULL;

    Animate(Top);
    percent(NULL, NULL);
    cp = concat(cp,"Getting a directory list of ");
    cp = concat(cp, remote_dir);
    Set_Status(cp);
    XtFree(cp);
}

static int 
verify_mode(mode)
char *mode;
{
    int type = 0;

    if ((int)strlen(mode) < 10) return -1;

         if (mode[0] == 'd') type |= MODE_TYPE_DIR;
    else if (mode[0] == 'D') type |= MODE_TYPE_DIR;
    else if (mode[0] == '-') type |= MODE_TYPE_FILE;
    else if (mode[0] == 'F') type |= MODE_TYPE_FILE;
    else if (mode[0] == 'm') type |= MODE_TYPE_OFFLINE|MODE_TYPE_FILE;
    else if (mode[0] == 'l') type |= MODE_TYPE_LINK;
    else if (mode[0] == 'L') type |= MODE_TYPE_LINK;
    else if (mode[0] == 'c') type |= MODE_TYPE_CHAR;
    else if (mode[0] == 'C') type |= MODE_TYPE_CHAR;
    else if (mode[0] == 's') type |= MODE_TYPE_SOCK;
    else if (mode[0] == 'S') type |= MODE_TYPE_SOCK;
    else if (mode[0] == 'b') type |= MODE_TYPE_BLOCK;
    else if (mode[0] == 'B') type |= MODE_TYPE_BLOCK;

         if (mode[1] == 'r') type |= MODE_OWNER_READ;

         if (mode[2] == 'w') type |= MODE_OWNER_WRITE;
    else if (mode[2] == 's') type |= MODE_OWNER_WRITE;
    else if (mode[2] == 'S') type |= MODE_OWNER_WRITE;

         if (mode[3] == 'x') type |= MODE_OWNER_EXECUTE;

         if (mode[4] == 'r') type |= MODE_GROUP_READ;

         if (mode[5] == 'w') type |= MODE_GROUP_WRITE;
    else if (mode[5] == 's') type |= MODE_GROUP_WRITE;
    else if (mode[5] == 'S') type |= MODE_GROUP_WRITE;
         if (mode[6] == 'x') type |= MODE_GROUP_EXECUTE;

         if (mode[7] == 'r') type |= MODE_OTHER_READ;

         if (mode[8] == 'w') type |= MODE_OTHER_WRITE;
    else if (mode[8] == 's') type |= MODE_OTHER_WRITE;
    else if (mode[8] == 'S') type |= MODE_OTHER_WRITE;

         if (mode[9] == 'x') type |= MODE_OTHER_EXECUTE;
    else if (mode[9] == 't') type |= MODE_OTHER_EXECUTE;

    return type;
}