-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdired.c
1500 lines (1319 loc) · 44.1 KB
/
dired.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
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Directory editor mode for QEmacs.
*
* Copyright (c) 2001-2002 Fabrice Bellard.
* Copyright (c) 2002-2017 Charlie Gordon.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "qe.h"
#include "variables.h"
#include <grp.h>
#include <pwd.h>
enum {
DIRED_STYLE_NORMAL = QE_STYLE_DEFAULT,
DIRED_STYLE_HEADER = QE_STYLE_STRING,
DIRED_STYLE_DIRECTORY = QE_STYLE_COMMENT,
DIRED_STYLE_FILENAME = QE_STYLE_FUNCTION,
};
enum { DIRED_HEADER = 2 };
enum {
DIRED_SORT_NAME = 1,
DIRED_SORT_EXTENSION = 2,
DIRED_SORT_SIZE = 4,
DIRED_SORT_DATE = 8,
DIRED_SORT_MASK = 1+2+4+8,
DIRED_SORT_GROUP = 16,
DIRED_SORT_DESCENDING = 32,
};
enum {
DIRED_UPDATE_SORT = 1,
DIRED_UPDATE_FILTER = 2,
DIRED_UPDATE_COLUMNS = 4,
DIRED_UPDATE_REBUILD = 8,
DIRED_UPDATE_ALL = 15,
};
enum time_format {
TF_COMPACT,
TF_DOS,
TF_DOS_LONG,
TF_TOUCH,
TF_TOUCH_LONG,
TF_FULL,
TF_SECONDS,
TF_MAX = TF_SECONDS,
};
typedef struct DiredState DiredState;
typedef struct DiredItem DiredItem;
struct DiredState {
QEModeData base; /* derived from QEModeData */
StringArray items; /* XXX: should just use a generic array */
enum time_format time_format;
int show_dot_files;
int show_ds_store;
int hflag, nflag;
int sort_mode;
DiredItem *last_cur;
long long total_bytes;
int ndirs, nfiles, ndirs_hidden, nfiles_hidden;
int blocksize;
int last_width;
int no_blocks, no_mode, no_link, no_uid, no_gid, no_size, no_date;
int blockslen, modelen, linklen, uidlen, gidlen, sizelen, datelen, namelen;
int fnamecol;
char path[MAX_FILENAME_SIZE]; /* current path */
};
/* opaque structure for sorting DiredState.items StringArray */
struct DiredItem {
mode_t mode; /* inode protection mode */
nlink_t nlink; /* number of hard links to the file */
uid_t uid; /* user-id of owner */
gid_t gid; /* group-id of owner */
dev_t rdev; /* device type, for special file inode */
time_t mtime;
off_t size;
int offset;
char hidden;
char mark;
char name[1];
};
static ModeDef dired_mode;
static time_t dired_curtime;
static enum time_format dired_time_format;
static int dired_show_dot_files = 1;
static int dired_show_ds_store = 0;
static int dired_nflag = 0; /* 0=name, 1=numeric, 2=hidden */
static int dired_hflag = 0; /* 0=exact, 1=human-decimal, 2=human-binary */
static int dired_sort_mode = DIRED_SORT_GROUP | DIRED_SORT_NAME;
static QVarType dired_sort_mode_set_value(EditState *s, VarDef *vp,
void *ptr, const char *str, int sort_mode);
static QVarType dired_time_format_set_value(EditState *s, VarDef *vp,
void *ptr, const char *str, int format);
static VarDef dired_variables[] = {
G_VAR_F( "dired-sort-mode", dired_sort_mode, VAR_NUMBER, VAR_RW_SAVE,
dired_sort_mode_set_value )
G_VAR_F( "dired-time-format", dired_time_format, VAR_NUMBER, VAR_RW_SAVE,
dired_time_format_set_value )
G_VAR( "dired-show-dot-files", dired_show_dot_files, VAR_NUMBER, VAR_RW_SAVE )
G_VAR( "dired-show-ds-store", dired_show_ds_store, VAR_NUMBER, VAR_RW_SAVE )
};
static inline DiredState *dired_get_state(EditState *e, int status)
{
return qe_get_buffer_mode_data(e->b, &dired_mode, status ? e : NULL);
}
static DiredItem *dired_get_cur_item(DiredState *ds, EditState *s) {
int i, index = list_get_pos(s) - DIRED_HEADER;
if (index >= 0) {
for (i = 0; i < ds->items.nb_items; i++) {
DiredItem *dip = ds->items.items[i]->opaque;
if (!dip->hidden && index-- == 0)
return dip;
}
}
return NULL;
}
static void dired_free(DiredState *ds)
{
if (ds) {
int i;
for (i = 0; i < ds->items.nb_items; i++) {
qe_free(&ds->items.items[i]->opaque);
}
free_strings(&ds->items);
ds->last_cur = NULL;
}
}
static char *dired_get_filename(DiredState *ds, const DiredItem *dip,
char *buf, int buf_size)
{
if (buf_size > 0)
buf[0] = '\0';
if (!dip)
return NULL;
/* build filename */
/* CG: Should canonicalize path */
if (is_directory(ds->path)) {
return makepath(buf, buf_size, ds->path, dip->name);
} else {
get_dirname(buf, buf_size, ds->path);
append_slash(buf, buf_size);
return pstrcat(buf, buf_size, dip->name);
}
}
static int dired_find_target(DiredState *ds, const char *target)
{
char filename[MAX_FILENAME_SIZE];
int i, row;
if (target) {
row = DIRED_HEADER;
for (i = 0; i < ds->items.nb_items; i++) {
DiredItem *dip = ds->items.items[i]->opaque;
if (dired_get_filename(ds, dip, filename, sizeof(filename))
&& strequal(filename, target)) {
return row;
}
if (!dip->hidden)
row++;
}
}
return DIRED_HEADER;
}
/* sort alphabetically with directories first */
static int dired_sort_func(void *opaque, const void *p1, const void *p2)
{
const StringItem *item1 = *(const StringItem **)p1;
const StringItem *item2 = *(const StringItem **)p2;
const DiredItem *dip1 = item1->opaque;
const DiredItem *dip2 = item2->opaque;
DiredState *ds = opaque;
int sort_mode = ds->sort_mode, res;
int is_dir1, is_dir2;
if (sort_mode & DIRED_SORT_GROUP) {
is_dir1 = !!S_ISDIR(dip1->mode);
is_dir2 = !!S_ISDIR(dip2->mode);
if (is_dir1 != is_dir2)
return is_dir2 - is_dir1;
}
for (;;) {
if (sort_mode & DIRED_SORT_DATE) {
if (dip1->mtime != dip2->mtime) {
res = (dip1->mtime < dip2->mtime) ? -1 : 1;
break;
}
}
if (sort_mode & DIRED_SORT_SIZE) {
if (dip1->size != dip2->size) {
res = (dip1->size < dip2->size) ? -1 : 1;
break;
}
}
if (sort_mode & DIRED_SORT_EXTENSION) {
res = qe_strcollate(get_extension(dip1->name),
get_extension(dip2->name));
if (res)
break;
}
res = qe_strcollate(dip1->name, dip2->name);
break;
}
return (sort_mode & DIRED_SORT_DESCENDING) ? -res : res;
}
static int format_number(char *buf, int size, int human, off_t number)
{
if (human == 0) {
return snprintf(buf, size, "%lld", (long long)number);
}
if (human > 1) {
const char *suffix = "BkMGTPEZY";
/* metric version, powers of 1000 */
while (suffix[1] && number >= 1000) {
if (number < 10000) {
buf[0] = '0' + (number / 1000);
buf[1] = '.';
buf[2] = '0' + ((number / 100) % 10);
buf[3] = suffix[1];
buf[4] = '\0';
return 4;
}
number /= 1000;
suffix++;
}
return snprintf(buf, size, "%d%c", (int)number, *suffix);
} else {
const char *suffix = "BKMGTPEZY";
/* geek version, powers of 1024 */
while (suffix[1] && number >= 1000) {
if (number < 10200) {
buf[0] = '0' + (number / 1020);
buf[1] = '.';
buf[2] = '0' + ((number / 102) % 10);
buf[3] = suffix[1];
buf[4] = '\0';
return 4;
}
number >>= 10;
suffix++;
}
return snprintf(buf, size, "%d%c", (int)number, *suffix);
}
}
static int format_gid(char *buf, int size, int nflag, gid_t gid)
{
// group_from_gid ?
struct group *grp;
if (!nflag && (grp = getgrgid(gid)) != NULL && grp->gr_name)
return snprintf(buf, size, "%s", grp->gr_name);
else
return snprintf(buf, size, "%d", (int)gid);
}
static int format_uid(char *buf, int size, int nflag, uid_t uid)
{
// user_from_uid ?
struct passwd *pwp;
if (!nflag && (pwp = getpwuid(uid)) != NULL && pwp->pw_name)
return snprintf(buf, size, "%s", pwp->pw_name);
else
return snprintf(buf, size, "%d", (int)uid);
}
static int format_size(char *buf, int size, int human, const DiredItem *fp)
{
if (S_ISCHR(fp->mode) || S_ISBLK(fp->mode)) {
int major = fp->rdev >> ((sizeof(dev_t) == 2) ? 8 : 24);
int minor = fp->rdev & ((sizeof(dev_t) == 2) ? 0xff : 0xffffff);
return snprintf(buf, size, "%3d, %3d", major, minor);
} else {
return format_number(buf, size, human, fp->size);
}
}
static int format_date(char *dest, int size,
const time_t systime,
enum time_format time_format)
{
buf_t outbuf, *out;
struct tm systm;
int fmonth;
static const char * const month[] = {
"***",
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
};
/* Should test for valid conversion */
/* Should support extra precision on enabled systems */
systm = *localtime(&systime);
fmonth = systm.tm_mon + 1;
if (fmonth <= 0 || fmonth > 12)
fmonth = 0;
out = buf_init(&outbuf, dest, size);
switch (time_format) {
case TF_TOUCH:
case TF_TOUCH_LONG:
buf_printf(out, "%02d%02d%02d%02d%02d",
systm.tm_year % 100, /* year */
fmonth, /* month */
systm.tm_mday, /* day */
systm.tm_hour, /* hours */
systm.tm_min); /* minutes */
if (time_format == TF_TOUCH_LONG) {
buf_printf(out, ".%02d", systm.tm_sec); /* seconds */
}
break;
case TF_DOS:
case TF_DOS_LONG:
buf_printf(out, "%s %2d %4d %2d:%02d",
month[fmonth], /* month */
systm.tm_mday, /* day */
systm.tm_year + 1900, /* year */
systm.tm_hour, /* hours */
systm.tm_min); /* minutes */
if (time_format == TF_DOS_LONG) {
buf_printf(out, ":%02d", systm.tm_sec); /* seconds */
}
break;
case TF_FULL:
buf_printf(out, "%s %2d %02d:%02d:%02d %4d",
month[fmonth], /* month */
systm.tm_mday, /* day */
systm.tm_hour, /* hours */
systm.tm_min, /* minutes */
systm.tm_sec, /* seconds */
systm.tm_year + 1900); /* year */
break;
case TF_SECONDS:
buf_printf(out, "%10lu", systime); /* seconds */
break;
default:
case TF_COMPACT:
if (systime > dired_curtime - 182 * 86400
&& systime < dired_curtime + 182 * 86400) {
buf_printf(out, "%s %2d %02d:%02d",
month[fmonth], /* month */
systm.tm_mday, /* day */
systm.tm_hour, /* hours */
systm.tm_min); /* minutes */
} else {
buf_printf(out, "%s %2d %4d",
month[fmonth], /* month */
systm.tm_mday, /* day */
systm.tm_year + 1900); /* year */
}
break;
}
if (!fmonth) {
memset(dest, ' ', out->len);
}
return out->pos;
}
static int get_trailchar(mode_t mode)
{
int trailchar = 0;
if (mode & S_IEXEC) {
trailchar = '*';
}
if (S_ISDIR(mode)) {
trailchar = '/';
}
if (S_ISLNK(mode)) {
trailchar = '@';
}
#ifdef S_ISSOCK
if (S_ISSOCK(mode))
trailchar = '=';
#endif
#ifdef S_ISWHT
if (S_ISWHT(mode))
trailchar = '%';
#endif
#ifdef S_ISFIFO
if (S_ISFIFO(mode))
trailchar = '|';
#endif
return trailchar;
}
static char *getentryslink(char *path, int size,
const char *dir, const char *name)
{
char filename[MAX_FILENAME_SIZE + 1];
int len;
snprintf(filename, sizeof(filename), "%s/%s", dir, name);
len = readlink(filename, path, size - 1);
if (len < 0)
len = 0;
path[len] = '\0';
if (len)
return path;
else
return NULL;
}
static char *compute_attr(char *atts, mode_t mode)
{
strcpy(atts, "----------");
/* File type */
if (!S_ISREG(mode)) {
if (S_ISDIR(mode)) /* directory */
atts[0] = 'd';
#ifdef S_ISBLK
if (S_ISBLK(mode)) /* block special */
atts[0] = 'b';
#endif
#ifdef S_ISCHR
if (S_ISCHR(mode)) /* char special */
atts[0] = 'c';
#endif
#ifdef S_ISFIFO
if (S_ISFIFO(mode)) /* fifo or socket */
atts[0] = 'p';
#endif
#ifdef S_ISSOCK
if (S_ISSOCK(mode) /* socket */)
atts[0] = 's';
#endif
if (S_ISLNK(mode)) /* symbolic link */
atts[0] = 'l'; /* overrides directory */
}
/* File mode */
/* Read, write, execute/search by owner */
if (mode & S_IRUSR) /* [XSI] R for owner */
atts[1] = 'r';
if (mode & S_IWUSR) /* [XSI] W for owner */
atts[2] = 'w';
if (mode & S_IXUSR) /* [XSI] X for owner */
atts[3] = 'x';
#ifdef S_ISUID
if (mode & S_ISUID) /* [XSI] set user id on execution */
atts[3] = (mode & S_IXUSR) ? 's' : 'S';
#endif
/* Read, write, execute/search by group */
if (mode & S_IRGRP) /* [XSI] R for group */
atts[4] = 'r';
if (mode & S_IWGRP) /* [XSI] W for group */
atts[5] = 'w';
if (mode & S_IXGRP) /* [XSI] X for group */
atts[6] = 'x';
#ifdef S_ISGID
if (mode & S_ISGID) /* [XSI] set group id on execution */
atts[6] = (mode & S_IXGRP) ? 's' : 'S';
#endif
/* Read, write, execute/search by others */
if (mode & S_IROTH)
atts[7] = 'r'; /* [XSI] R for other */
if (mode & S_IWOTH)
atts[8] = 'w'; /* [XSI] W for other */
if (mode & S_IXOTH)
atts[9] = 'x'; /* [XSI] X for other */
#ifdef S_ISVTX
if (mode & S_ISVTX) /* [XSI] directory restrcted delete */
atts[6] = (mode & S_IXOTH) ? 't' : 'T';
#endif
return atts;
}
static void dired_filter_files(DiredState *ds)
{
int i;
ds->show_dot_files = dired_show_dot_files;
ds->show_ds_store = dired_show_ds_store;
ds->total_bytes = 0;
ds->ndirs = ds->nfiles = 0;
ds->ndirs_hidden = ds->nfiles_hidden = 0;
for (i = 0; i < ds->items.nb_items; i++) {
DiredItem *dip = ds->items.items[i]->opaque;
const char *p = ds->items.items[i]->str;
int hidden = 0;
if (*p == '.') {
if ((!dired_show_dot_files)
|| (!dired_show_ds_store && strequal(p, ".DS_Store")))
hidden = 1;
}
/* XXX: should apply other filters? */
dip->hidden = hidden;
if (hidden) {
if (S_ISDIR(dip->mode)) {
ds->ndirs_hidden++;
} else {
ds->nfiles_hidden++;
}
} else {
if (S_ISDIR(dip->mode)) {
ds->ndirs++;
} else {
ds->nfiles++;
ds->total_bytes += dip->size;
}
}
}
}
static void dired_compute_columns(DiredState *ds)
{
char buf[32];
int i, len;
dired_curtime = time(NULL);
ds->time_format = dired_time_format;
ds->hflag = dired_hflag;
ds->nflag = dired_nflag;
ds->blockslen = ds->modelen = ds->linklen = 0;
ds->uidlen = ds->gidlen = 0;
ds->sizelen = ds->datelen = ds->namelen = 0;
for (i = 0; i < ds->items.nb_items; i++) {
DiredItem *dip = ds->items.items[i]->opaque;
len = strlen(dip->name);
if (ds->namelen < len)
ds->namelen = len;
len = snprintf(buf, sizeof(buf), "%ld",
(long)(((long long)dip->size + ds->blocksize - 1) /
ds->blocksize));
if (ds->blockslen < len)
ds->blockslen = len;
ds->modelen = 10;
len = snprintf(buf, sizeof(buf), "%d", (int)dip->nlink);
if (ds->linklen < len)
ds->linklen = len;
len = format_uid(buf, sizeof(buf), ds->nflag, dip->uid);
if (ds->uidlen < len)
ds->uidlen = len;
len = format_gid(buf, sizeof(buf), ds->nflag, dip->gid);
if (ds->gidlen < len)
ds->gidlen = len;
len = format_size(buf, sizeof(buf), ds->hflag, dip);
if (ds->sizelen < len)
ds->sizelen = len;
len = format_date(buf, sizeof(buf), dip->mtime, dired_time_format);
if (ds->datelen < len)
ds->datelen = len;
}
}
#define inflect(n, singular, plural) ((n) == 1 ? (singular) : (plural))
/* `b` is valid, `ds` and `s` may be NULL */
static void dired_update_buffer(DiredState *ds, EditBuffer *b, EditState *s,
int flags)
{
char buf[MAX_FILENAME_SIZE];
DiredItem *dip, *cur_item;
int i, col, w, width, window_width, top_line;
if (!ds)
return;
/* Try and preserve scroll position */
if (s) {
w = max(1, get_glyph_width(s->screen, s, QE_STYLE_DEFAULT, '0'));
window_width = s->width;
width = window_width / w;
eb_get_pos(s->b, &top_line, &col, s->offset_top);
/* XXX: should use dip->offset and delay to rebuild phase */
cur_item = dired_get_cur_item(ds, s);
} else {
width = window_width = 80;
col = top_line = 0;
cur_item = NULL;
}
if (ds->sort_mode != dired_sort_mode)
flags |= DIRED_UPDATE_SORT;
if (flags & DIRED_UPDATE_SORT) {
flags |= DIRED_UPDATE_REBUILD;
ds->sort_mode = dired_sort_mode;
qe_qsort_r(ds->items.items, ds->items.nb_items,
sizeof(StringItem *), ds, dired_sort_func);
}
if (ds->show_dot_files != dired_show_dot_files
|| ds->show_ds_store != dired_show_ds_store) {
flags |= DIRED_UPDATE_FILTER;
}
if (flags & DIRED_UPDATE_FILTER) {
flags |= DIRED_UPDATE_REBUILD;
dired_filter_files(ds);
}
if (ds->time_format != dired_time_format
|| ds->nflag != dired_nflag
|| ds->hflag != dired_hflag) {
flags |= DIRED_UPDATE_COLUMNS;
}
if (flags & DIRED_UPDATE_COLUMNS) {
flags |= DIRED_UPDATE_REBUILD;
dired_compute_columns(ds);
}
if (!(flags & DIRED_UPDATE_REBUILD))
return;
ds->last_width = window_width;
ds->last_cur = NULL;
width -= clamp(ds->namelen, 16, 40);
ds->no_size = ((width -= ds->sizelen + 2) < 0);
ds->no_date = ((width -= ds->datelen + 2) < 0);
ds->no_mode = ((width -= ds->modelen + 1) < 0);
ds->no_uid = (ds->nflag == 2) || ((width -= ds->uidlen + 1) < 0);
ds->no_gid = (ds->nflag == 2) || ((width -= ds->gidlen + 1) < 0);
ds->no_link = ((width -= ds->linklen + 1) < 0);
ds->no_blocks = ((width -= ds->blockslen + 1) < 0);
ds->no_blocks = 1; // disable blocks display to avoid confusing output
/* construct list buffer */
/* deleting buffer contents resets s->offset and s->offset_top */
eb_clear(b);
if (DIRED_HEADER) {
int seq = ' ';
b->cur_style = DIRED_STYLE_HEADER;
eb_printf(b, " Directory of ");
b->cur_style = DIRED_STYLE_DIRECTORY;
eb_printf(b, "%s", ds->path);
b->cur_style = DIRED_STYLE_HEADER;
eb_printf(b, "\n ");
if (ds->ndirs) {
eb_printf(b, "%c %d %s", seq, ds->ndirs,
inflect(ds->ndirs, "directory", "directories"));
seq = ',';
}
if (ds->ndirs_hidden) {
eb_printf(b, "%c %d %s", seq, ds->ndirs_hidden,
inflect(ds->ndirs_hidden, "hidden directory", "hidden directories"));
seq = ',';
}
if (ds->nfiles) {
eb_printf(b, "%c %d %s", seq, ds->nfiles,
inflect(ds->nfiles, "file", "files"));
seq = ',';
}
if (ds->nfiles_hidden) {
eb_printf(b, "%c %d %s", seq, ds->nfiles_hidden,
inflect(ds->nfiles_hidden, "hidden file", "hidden files"));
seq = ',';
}
if (ds->total_bytes) {
format_number(buf, sizeof(buf), ds->hflag, ds->total_bytes);
eb_printf(b, "%c %s %s", seq, buf,
inflect(ds->total_bytes, "byte", "bytes"));
seq = ',';
}
if (ds->ndirs + ds->ndirs_hidden + ds->nfiles + ds->nfiles_hidden == 0) {
eb_printf(b, "%c empty", seq);
}
eb_printf(b, "\n");
}
b->cur_style = DIRED_STYLE_NORMAL;
for (i = 0; i < ds->items.nb_items; i++) {
dip = ds->items.items[i]->opaque;
dip->offset = b->total_size;
if (dip == cur_item) {
ds->last_cur = dip;
if (s)
s->offset = b->total_size;
}
if (dip->hidden)
continue;
col = eb_printf(b, "%c ", dip->mark);
if (!ds->no_blocks) {
col += eb_printf(b, "%*ld ", ds->blockslen,
(long)(((long long)dip->size + ds->blocksize - 1) /
ds->blocksize));
}
if (!ds->no_mode) {
compute_attr(buf, dip->mode);
col += eb_printf(b, "%s ", buf);
}
if (!ds->no_link) {
col += eb_printf(b, "%*d ", ds->linklen, (int)dip->nlink);
}
if (!ds->no_uid) {
format_uid(buf, sizeof(buf), ds->nflag, dip->uid);
col += eb_printf(b, "%-*s ", ds->uidlen, buf);
}
if (!ds->no_gid) {
format_gid(buf, sizeof(buf), ds->nflag, dip->gid);
col += eb_printf(b, "%-*s ", ds->gidlen, buf);
}
if (!ds->no_size) {
format_size(buf, sizeof(buf), ds->hflag, dip);
col += eb_printf(b, " %*s ", ds->sizelen, buf);
}
if (!ds->no_date) {
format_date(buf, sizeof(buf), dip->mtime, dired_time_format);
col += eb_printf(b, "%s ", buf);
}
ds->fnamecol = col - 1;
if (S_ISDIR(dip->mode))
b->cur_style = DIRED_STYLE_DIRECTORY;
else
b->cur_style = DIRED_STYLE_FILENAME;
eb_printf(b, "%s", dip->name);
if (1) {
int trailchar = get_trailchar(dip->mode);
if (trailchar) {
eb_printf(b, "%c", trailchar);
}
}
if (S_ISLNK(dip->mode)
&& getentryslink(buf, sizeof(buf), ds->path, dip->name)) {
eb_printf(b, " -> %s", buf);
}
b->cur_style = DIRED_STYLE_NORMAL;
eb_printf(b, "\n");
}
b->modified = 0;
b->flags |= BF_READONLY;
if (s) {
s->offset_top = eb_goto_pos(b, top_line, 0);
}
}
/* dired-mode commands */
static void dired_up_down(EditState *s, int dir)
{
DiredState *ds;
int line, col;
if (!(ds = dired_get_state(s, 1)))
return;
if (dir) {
text_move_up_down(s, dir);
}
if (s->offset && s->offset == s->b->total_size)
text_move_up_down(s, -1);
eb_get_pos(s->b, &line, &col, s->offset);
s->offset = eb_goto_pos(s->b, line, ds->fnamecol);
}
static void dired_mark(EditState *s, int mark)
{
DiredState *ds;
DiredItem *dip;
int ch, dir = 1, flags;
if (!(ds = dired_get_state(s, 1)))
return;
if (mark < 0) {
dir = -1;
mark = ' ';
}
if (dir < 0)
dired_up_down(s, -1);
dip = dired_get_cur_item(ds, s);
if (dip) {
ch = dip->mark = mark;
do_bol(s);
flags = s->b->flags & BF_READONLY;
s->b->flags ^= flags;
eb_replace_uchar(s->b, s->offset, ch);
s->b->flags ^= flags;
}
if (dir > 0)
dired_up_down(s, 1);
}
static QVarType dired_sort_mode_set_value(EditState *s, VarDef *vp,
void *ptr, const char *str, int sort_mode)
{
const char *p;
for (p = str; p && *p; p++) {
switch (qe_tolower((unsigned char)*p)) {
case 'n': /* name */
sort_mode &= ~DIRED_SORT_MASK;
sort_mode |= DIRED_SORT_NAME;
break;
case 'e': /* extension */
sort_mode &= ~DIRED_SORT_MASK;
sort_mode |= DIRED_SORT_EXTENSION;
break;
case 's': /* size */
sort_mode &= ~DIRED_SORT_MASK;
sort_mode |= DIRED_SORT_SIZE;
break;
case 'd': /* date */
sort_mode &= ~DIRED_SORT_MASK;
sort_mode |= DIRED_SORT_DATE;
break;
case 'g': /* group */
sort_mode |= DIRED_SORT_GROUP;
break;
case 'u': /* ungroup */
sort_mode &= ~DIRED_SORT_GROUP;
break;
case 'r': /* reverse */
sort_mode ^= DIRED_SORT_DESCENDING;
break;
case '+': /* ascending */
sort_mode &= ~DIRED_SORT_DESCENDING;
break;
case '-': /* descending */
sort_mode |= DIRED_SORT_DESCENDING;
break;
}
}
if (dired_sort_mode != sort_mode) {
dired_sort_mode = sort_mode;
vp->modified = 1;
}
return VAR_NUMBER;
}
static void dired_sort(EditState *s, const char *sort_order)
{
int sort_mode = dired_sort_mode;
dired_sort_mode_set_value(s, &dired_variables[0], &dired_sort_mode,
sort_order, sort_mode);
if (sort_mode != dired_sort_mode)
dired_update_buffer(dired_get_state(s, 0), s->b, s, DIRED_UPDATE_SORT);
}
static QVarType dired_time_format_set_value(EditState *s, VarDef *vp,
void *ptr, const char *str, int format)
{
if (str) {
if (!strxcmp(str, "default")) format = TF_COMPACT; else
if (!strxcmp(str, "compact")) format = TF_COMPACT; else
if (!strxcmp(str, "dos")) format = TF_DOS; else
if (!strxcmp(str, "dos-long")) format = TF_DOS_LONG; else
if (!strxcmp(str, "touch")) format = TF_TOUCH; else
if (!strxcmp(str, "touch-long")) format = TF_TOUCH_LONG; else
if (!strxcmp(str, "full")) format = TF_FULL; else
if (!strxcmp(str, "seconds")) format = TF_SECONDS; else
return VAR_UNKNOWN;
}
if (format < 0 || format > TF_MAX)
return VAR_UNKNOWN;
if (dired_time_format != (enum time_format)format) {
dired_time_format = format;
vp->modified = 1;
}
return VAR_NUMBER;
}
static void dired_set_time_format(EditState *s, int format)
{
dired_time_format_set_value(s, &dired_variables[1], &dired_time_format,
NULL, format);
}
/* `ds` and `b` are valid, `s` and `target` may be NULL */
static void dired_build_list(DiredState *ds, const char *path,
const char *target, EditBuffer *b, EditState *s)
{
FindFileState *ffst;
char filename[MAX_FILENAME_SIZE];
char dir[MAX_FILENAME_SIZE];
char line[1024];
const char *p;
const char *pattern;
struct stat st;
StringItem *item;
/* free previous list, if any */
dired_free(ds);
ds->blocksize = 1024;
ds->last_width = 0;
/* CG: should make absolute ? */
canonicalize_path(ds->path, sizeof(ds->path), path);
eb_set_filename(b, ds->path);
b->flags |= BF_DIRED;
eb_clear(b);
pstrcpy(dir, sizeof(dir), ds->path);
pattern = "*";
if (!is_directory(dir)) {
get_dirname(dir, sizeof(dir), ds->path);
pattern = get_basename(ds->path);
}
/* XXX: should scan directory for subdirectories and filter with
* pattern only for regular files.
* XXX: should handle generalized file patterns.
* XXX: should use a separate thread to make the scan asynchronous.
* XXX: should compute recursive size data.
* XXX: should track file creation, deletion and modifications.
*/
ffst = find_file_open(dir, pattern);
/* Should scan directory/filespec before computing lines to adjust
* filename gutter width.
*/
while (!find_file_next(ffst, filename, sizeof(filename))) {
if (lstat(filename, &st) < 0)
continue;
p = get_basename(filename);
#if 1 /* CG: bad idea, but causes spurious bugs */
/* exclude redundant '.' and '..' */
if (strequal(p, ".") || strequal(p, ".."))
continue;
#endif
pstrcpy(line, sizeof(line), p);
item = add_string(&ds->items, line, 0);
if (item) {
DiredItem *dip;
int plen = strlen(p);
dip = qe_malloc_hack(DiredItem, plen);
dip->mode = st.st_mode;
dip->nlink = st.st_nlink;
dip->uid = st.st_uid;
dip->gid = st.st_gid;
dip->rdev = st.st_rdev;
dip->mtime = st.st_mtime;
dip->size = st.st_size;
dip->hidden = 0;
dip->mark = ' ';
memcpy(dip->name, p, plen + 1);
item->opaque = dip;
}
}