File: local_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 (509 lines) | stat: -rw-r--r-- 11,313 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
/*
 * 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: local_dir.c,v 1.2 1994/05/20 20:16:10 jones Exp $\n";
static char rcspath[] =
"$Source: /mintaka/u0/xx/ss/jones/jones.old/mftp.src/moxftp-2.0/RCS/local_dir.c,v $\n";
#endif

/* $Log: local_dir.c,v $
 * Revision 1.2  1994/05/20  20:16:10  jones
 * Use Push_Files and Restore_Dir_Old in
 * Update_Local_Dir.
 *
 * Revision 1.1  1994/03/14  18:55:53  jones
 * Initial revision
 *
 */

#include <machine.h>
#include "defs.h"
#include "proto/local_dir.h"

#if defined(USE_DIRENT)
#include <dirent.h>
#else
#include <sys/dir.h>
#define dirent direct
#endif

#include <pwd.h>
#include <grp.h>
#include <sys/stat.h>

#if defined(S_IREAD)
#if !defined(S_IRUSR)
#define S_IRUSR S_IREAD
#endif
#if !defined(S_IRGRP)
#define S_IRGRP (S_IREAD>3)
#endif
#if !defined(S_IROTH)
#define S_IROTH (S_IREAD>6)
#endif
#endif

#if defined(S_IWRITE)
#if !defined(S_IWUSR)
#define S_IWUSR S_IWRITE
#endif
#if !defined(S_IWGRP)
#define S_IWGRP (S_IWRITE>3)
#endif
#if !defined(S_IWOTH)
#define S_IWOTH (S_IWRITE>6)
#endif
#endif

#if defined(S_IEXEC)
#if !defined(S_IXUSR)
#define S_IXUSR S_IEXEC
#endif
#if !defined(S_IXGRP)
#define S_IXGRP (S_IEXEC>3)
#endif
#if !defined(S_IXOTH)
#define S_IXOTH (S_IEXEC>6)
#endif
#endif

#if !defined(_IFLNK)
#if defined(_S_IFLNK)
#define _IFLNK _S_IFLNK
#endif
#endif

#if !defined(_IFMT)
#if defined(_S_IFMT)
#define _IFMT _S_IFMT
#else
#if defined(S_IFMT)
#define _IFMT S_IFMT
#endif
#endif
#endif

#if !defined(S_ISDIR) && defined(S_IFDIR)
#define S_ISDIR(x) (((x) & _IFMT) == S_IFDIR)
#endif

#if !defined(S_ISBLK) && defined(S_IFBLK)
#define S_ISBLK(x) (((x) & _IFMT) == S_IFBLK)
#endif

#if !defined(S_ISCHR) && defined(S_IFCHR)
#define S_ISCHR(x) (((x) & _IFMT) == S_IFCHR)
#endif

#if !defined(S_ISREG) && defined(S_IFREG)
#define S_ISREG(x) (((x) & _IFMT) == S_IFREG)
#endif

#if defined(S_IFOFL) && !defined(S_ISOFL)
#define S_ISOFL(x) (((x) & _IFMT) == S_IFOFL)
#endif

extern struct _dirs *Lastdir;

void 
Start_Local_Dir(echo, flag, callback, data, cb)
int    echo;
int    flag;
void (*callback)();
DATA   data;
CB    *cb;
{
    struct _dirs *dir;
    CB  *cbp;
 
    dir = Find_Dir(local_dir, localhost, 0);

    if (flag || dir == NULL || (dir && dir->n < 0) || (dir && dir->update)) {
        Push_Files(dir);
        Update_Screen();
	ls();
        dir = Find_Dir(local_dir, localhost, 0);
        Restore_Dir_Old(dir);
	if(Remote_local == LOCAL) 
		Display_Files(local_dir, localhost, 0);
	if (dir) dir->update = 0;
	if (dir) dir->time = 0;
        Update_Stat_Time(dir);
    }

    dir = Find_Dir(local_dir, localhost, 0);
    if (dir && Lastdir && Lastdir->link_up)  {
         dir->up = Lastdir;
         Lastdir->link_up = 0;
    }
    Set_Status(" ");
    cbp = link_callback(NULL, callback, data, cb);
    do_callbacks(1, cbp);

}

void
Update_Local_Dir()
{
    struct _dirs  *dir;
    struct _dirs  dirold;
    struct _files *files = NULL;
    int            n, i, j, update = 0;
    int            highlight = -1;
    extern int Remote_local;

    ZERO((char *)&dirold,  sizeof(struct _dirs));
    dir = Find_Dir(local_dir, localhost, 0);

    if (!dir) return;

    for (j=0; j<dir->n; j++) {
	if(dir->files[j].mode&MODE_TYPE_DO) return;
    }

    if (Lastdir == dir) update++;
    Push_Files(dir);
    ls();
    dir->update = 0;
    Restore_Dir_Old(dir);
    if (update) Draw_Files(NULL);
}

static void
ls()
{
    DIR *dirp;
    struct dirent *dp;
    int files = 0;
    char *cp = NULL;
    struct _dirs *dir;
    int pass;


restart:

    pass = 0;

    cp = concat(cp, "Getting a directory listing of local dir ");
    cp = concat(cp, local_dir);
    Set_Status(cp);
    XtFree(cp);

    dirp = opendir(local_dir);
    if (dirp == NULL) {
        dir = Find_Dir(local_dir, localhost, 1);
	dir->n = 0;
	Set_Status("Could not open local dir"); 
        return;
    }
    for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
	files++;
    }
    rewinddir(dirp);
    if (files == 0) {
        dir = Find_Dir(local_dir, localhost, 1);
	dir->n = 0;
	closedir(dirp);
    	return;
    }
    dir = Find_Dir(local_dir, localhost, 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_LOCAL;
    files =  0;
    for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
	struct stat statbuf;
	char *path = NULL;
        char *ctime();
        char *t;
	char  month[4], day[3], time_year[6], types[11];
        int n;
	int k;

        if(pass > 20) {
	    pass = 0;
	    Update_Screen();
	}
	pass++;
	path = concatdir(path, local_dir);
	path = concat(path, dp->d_name);
#if defined(_IFLNK)||defined(IFLNK)
	if ( k = lstat(path, &statbuf) != 0){ XtFree(path); continue;}
#else
	if ( stat(path, &statbuf) != 0) {XtFree(path); continue;}
#endif
	dir->files[files].owner = XtNewString(getowner(statbuf.st_uid));
        dir->files[files].group = XtNewString(getgroup(statbuf.st_gid));
	dir->files[files].link	    = statbuf.st_nlink;
	dir->files[files].size      = statbuf.st_size;
        t = XtNewString(ctime(&statbuf.st_mtime));
        strncpy(month, (char *)&t[4], 3);
        month[3] = '\0';
	strncpy(day, (char *)&t[8], 2);
	day[2] = '\0';
	strncpy(time_year, (char *)&t[20], 4);
	time_year[4] = '\0';
	dir->files[files].month     = XtNewString(month);
	dir->files[files].day       = XtNewString(day);
	dir->files[files].time_year = XtNewString(time_year);
	dir->files[files].modes     = XtNewString("-----------");
#if defined(CRAY)
	dir->files[files].mode      = set_unix_mode(statbuf.st_dm_mode,
					            dir->files[files].modes);
#else
	dir->files[files].mode      = set_unix_mode(statbuf.st_mode,
					            dir->files[files].modes);
#endif
        dir->files[files].linkname = NULL;
#if defined(S_ISLNK)||defined(_IFLNK)
	if ( dir->files[files].mode & MODE_TYPE_LINK)  {
	    char linkname[2024+1];
	    int nc;

	    nc = readlink(path, linkname, sizeof(linkname)-1);
	    if (nc > 0) { 
		linkname[nc] = '\0';
	    	dir->files[files].linkname = XtNewString(linkname);
	    }
	} 
#endif
	dir->files[files].name      = XtNewString(dp->d_name);
	dir->files[files].local     = dir->files[files].name;
	dir->files[files].remote    = Tran_Remote_File(dp->d_name, 
						       &dir->files[files].mode);
	if (dir->files[files].mode & MODE_TYPE_DIR ||
	    dir->files[files].mode & MODE_TYPE_LINK) 
	    dir->files[files].dir_name  = dir->files[files].name;

	if (path) XtFree(path);
        files++;
	if (files > dir->n) {
	     closedir(dirp);
	     Clear_Files(dir);
	     goto restart;
	}
    }
    dir->n = files;
    closedir(dirp);
}


static int
set_unix_mode(unix_mode, status)
int unix_mode;
char *status;
{
    int mode = 0;

    if (S_ISDIR(unix_mode)) {
	mode  |= MODE_TYPE_DIR;
	status[0] = 'd';
    }
    if (S_ISBLK(unix_mode)) {
	mode  |= MODE_TYPE_BLOCK;
	status[0] = 'b';
    }
    if (S_ISCHR(unix_mode)) {
	mode  |= MODE_TYPE_CHAR;
	status[0] = 'c';
    }
    if (S_ISREG(unix_mode)) {
	mode  |= MODE_TYPE_FILE;
	status[0] = '-';
    }

#if defined(S_ISLNK)
    if (S_ISLNK(unix_mode)) {
	mode  |= MODE_TYPE_LINK;
	status[0] = 'l';
    }
#else
#if defined(_IFLNK)
    if (((unix_mode)&_IFMT) == _IFLNK) {
	mode  |= MODE_TYPE_LINK;
	status[0] = 'l';
    }
#endif
#endif

#if defined(S_IFOFL)
    if (S_ISOFL(unix_mode)) {
	mode  |=  MODE_TYPE_OFFLINE|MODE_TYPE_FILE;
	status[0] = 'm';
    }
#endif

#if defined(S_ISSOCK)
    if (S_ISSOCK(unix_mode)) {
	mode  |= MODE_TYPE_SOCK;
	status[0] = 's';
    }
#else
#if defined(_IFSOC)
    if (((unix_mode)&_IFMT) == _IFSOC) {
	 mode  |= MODE_TYPE_SOCK;
	status[0] = 's';
    }
#endif
#endif

    if (unix_mode & S_IRUSR) {
	mode  |= MODE_OWNER_READ;
	status[1] = 'r';
    }
    if (unix_mode & S_IWUSR) {
	mode  |= MODE_OWNER_WRITE;
	status[2] = 'w';
    }
    if (unix_mode & S_IXUSR) {
	mode  |= MODE_OWNER_EXECUTE;
	status[3] = 'x';
    }
    if (unix_mode & S_IRGRP) {
	mode  |= MODE_GROUP_READ;
	status[4] = 'r';
    }
    if (unix_mode & S_IWGRP) {
	mode  |= MODE_GROUP_WRITE;
	status[5] = 'w';
    }
    if (unix_mode & S_IXGRP) {
	mode  |= MODE_GROUP_EXECUTE;
	status[6] = 'x';
    }
    if (unix_mode & S_IROTH) {
	mode  |= MODE_OTHER_READ;
	status[7] = 'r';
    }
    if (unix_mode & S_IWOTH) {
	mode  |= MODE_OTHER_WRITE;
	status[8] = 'w';
    }
    if (unix_mode & S_IXOTH) {
	mode  |= MODE_OTHER_EXECUTE;
	status[9] = 'x';
    }
    return mode;
}


#define MAX_SAVE_UID  1000
static int  n_saved_uid = 0;
static struct _saved_uid {
	int uid;
	char *owen;
} saved_uid[MAX_SAVE_UID];

static char *
getowner(uid)
int uid;
{
    struct passwd *pw;
    int i;
    char *owen;
    static char temp[10];

    if (n_saved_uid) {
	for (i=0; i<n_saved_uid; i++) {
	    if (saved_uid[i].uid == uid) return saved_uid[i].owen;
	}
    }

    pw = getpwuid(uid);
    if (pw) {
	owen  = pw->pw_name;
    } else {
	sprintf(temp, "%6d",uid);
	owen = temp;
    }

    if (n_saved_uid < MAX_SAVE_UID) {
	owen = XtNewString(owen);
	saved_uid[n_saved_uid].uid = uid;
	saved_uid[n_saved_uid].owen = owen;
	n_saved_uid++;
    }
    return owen;
}

#define MAX_SAVE_GID  1000
static int  n_saved_gid = 0;
static struct _saved_gid {
	int gid;
	char *group;
} saved_gid[MAX_SAVE_GID];

static char *
getgroup(gid)
int gid;
{
    struct group *grp;
    int i;
    char *group;
    static char temp[10];

    if (n_saved_gid) {
	for (i=0; i<n_saved_gid; i++) {
	    if (saved_gid[i].gid== gid) return saved_gid[i].group;
	}
    }

    grp = getgrgid(gid);
    if (grp) {
	group = grp->gr_name;
    } else {
	sprintf(temp, "%6d",gid);
        group = temp;
    }

    if (n_saved_gid < MAX_SAVE_GID) {
	group = XtNewString(group);
	saved_gid[n_saved_gid].gid = gid;
	saved_gid[n_saved_gid].group = group;
	n_saved_gid++;
    }

    return group;
}

void
Update_Stat_Time(dir)
struct _dirs *dir;
{
    struct stat statbuff;
    extern int mult_file;

    /*
     * Some one else is worring direcotry updates.
     */
    if (mult_file) return;
    if (dir == NULL) return;
    if (stat(".", &statbuff) == 0) {
	if (dir->time != 0 && 
             dir->time != statbuff.st_mtime) dir->update++;
	dir->time = statbuff.st_mtime;
    }
}