forked from cy384/ssheven
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathshell.c
More file actions
10697 lines (9407 loc) · 254 KB
/
Copy pathshell.c
File metadata and controls
10697 lines (9407 loc) · 254 KB
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
/*
* SevenTTY - local shell command interpreter
*
* Maps Unix-like commands to classic Mac OS Toolbox equivalents.
* Runs inside a vterm session tab with no network connection.
*
* Copyright (c) 2020 by cy384 <cy384@cy384.com>
* See LICENSE file for details
*/
#include "app.h"
#include "shell.h"
#include "console.h"
#include "debug.h"
#include "net.h"
#include "telnet.h"
#include <Files.h>
#include <Folders.h>
#include <Devices.h>
#include <Gestalt.h>
#include <DateTimeUtils.h>
#include <MacMemory.h>
#include <Processes.h>
#include <TextUtils.h>
#include <Threads.h>
#include <Aliases.h>
#include <mbedtls/md5.h>
#include <mbedtls/sha1.h>
#include <mbedtls/sha256.h>
#include <mbedtls/sha512.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
/* timeout notifier for blocking OT calls in main thread (ping, host) */
#define OT_TIMEOUT_TICKS 600 /* 10 seconds at 60 ticks/sec */
static unsigned long ot_timeout_deadline = 0;
static ProviderRef ot_timeout_provider = nil;
pascal void shell_ot_timeout_notifier(void* context, OTEventCode event,
OTResult result, void* cookie)
{
(void)context;
(void)result;
(void)cookie;
if (event == kOTSyncIdleEvent)
{
YieldToAnyThread();
if (ot_timeout_deadline > 0 &&
TickCount() > ot_timeout_deadline &&
ot_timeout_provider != nil)
{
OTCancelSynchronousCalls(ot_timeout_provider, kOTCanceledErr);
}
}
}
/* max arguments for a command */
#define MAX_ARGS 32
/* forward declarations */
void shell_prompt(int idx);
static void shell_execute(int idx, char* line);
static void shell_complete(int idx);
static void ls_show_file(int idx, FSSpec* tspec, int long_fmt);
static void ls_show_dir(int idx, short vRef, long dID, int long_fmt, int show_all);
static int local_shell_worker_active(const struct session* s);
/* ------------------------------------------------------------------ */
/* utility: write a C string into the session's vterm */
/* ------------------------------------------------------------------ */
/* Mac Roman bytes 0x80-0xFF to Unicode codepoints.
Used to convert local shell output to UTF-8 for vterm. */
static const unsigned short mac_roman_to_unicode[128] = {
0x00C4, 0x00C5, 0x00C7, 0x00C9, 0x00D1, 0x00D6, 0x00DC, 0x00E1,
0x00E0, 0x00E2, 0x00E4, 0x00E3, 0x00E5, 0x00E7, 0x00E9, 0x00E8,
0x00EA, 0x00EB, 0x00ED, 0x00EC, 0x00EE, 0x00EF, 0x00F1, 0x00F3,
0x00F2, 0x00F4, 0x00F6, 0x00F5, 0x00FA, 0x00F9, 0x00FB, 0x00FC,
0x2020, 0x00B0, 0x00A2, 0x00A3, 0x00A7, 0x2022, 0x00B6, 0x00DF,
0x00AE, 0x00A9, 0x2122, 0x00B4, 0x00A8, 0x2260, 0x00C6, 0x00D8,
0x221E, 0x00B1, 0x2264, 0x2265, 0x00A5, 0x00B5, 0x2202, 0x2211,
0x220F, 0x03C0, 0x222B, 0x00AA, 0x00BA, 0x2126, 0x00E6, 0x00F8,
0x00BF, 0x00A1, 0x00AC, 0x221A, 0x0192, 0x2248, 0x2206, 0x00AB,
0x00BB, 0x2026, 0x00A0, 0x00C0, 0x00C3, 0x00D5, 0x0152, 0x0153,
0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x25CA,
0x00FF, 0x0178, 0x2044, 0x20AC, 0x2039, 0x203A, 0xFB01, 0xFB02,
0x2021, 0x00B7, 0x201A, 0x201E, 0x2030, 0x00C2, 0x00CA, 0x00C1,
0x00CB, 0x00C8, 0x00CD, 0x00CE, 0x00CF, 0x00CC, 0x00D3, 0x00D4,
0xF8FF, 0x00D2, 0x00DA, 0x00DB, 0x00D9, 0x0131, 0x02C6, 0x02DC,
0x00AF, 0x02D8, 0x02D9, 0x02DA, 0x00B8, 0x02DD, 0x02DB, 0x02C7
};
/* Write Mac Roman data to vterm as UTF-8. Bytes 0x00-0x7F pass through
unchanged (ASCII = UTF-8). Bytes 0x80-0xFF are looked up in the Mac
Roman table and encoded as multi-byte UTF-8. */
static void vt_write_mr(int idx, const char* s, size_t len)
{
char buf[512];
int bi = 0;
size_t i;
const unsigned char* p = (const unsigned char*)s;
VTerm* vt = sessions[idx].vterm;
if (!vt) return;
for (i = 0; i < len; i++)
{
unsigned char ch = p[i];
if (ch < 0x80)
{
buf[bi++] = ch;
}
else
{
unsigned short cp = mac_roman_to_unicode[ch - 0x80];
if (cp < 0x80)
{
buf[bi++] = (char)cp;
}
else if (cp < 0x800)
{
buf[bi++] = 0xC0 | (cp >> 6);
buf[bi++] = 0x80 | (cp & 0x3F);
}
else
{
buf[bi++] = 0xE0 | (cp >> 12);
buf[bi++] = 0x80 | ((cp >> 6) & 0x3F);
buf[bi++] = 0x80 | (cp & 0x3F);
}
}
if (bi >= 508)
{
vterm_input_write(vt, buf, bi);
bi = 0;
}
}
if (bi > 0)
vterm_input_write(vt, buf, bi);
}
static void redir_write(int idx, const char* s, size_t len)
{
short ref = sessions[idx].redir_refnum;
if (ref != 0)
{
long count = (long)len;
FSWrite(ref, &count, s);
}
}
static void vt_write(int idx, const char* s)
{
size_t len = strlen(s);
redir_write(idx, s, len);
if (!sessions[idx].redir_quiet && sessions[idx].vterm)
vt_write_mr(idx, s, len);
}
static void copy_cstr_trunc(char* dst, size_t dst_size, const char* src)
{
size_t n;
if (dst_size == 0) return;
if (src == NULL) { dst[0] = '\0'; return; }
n = strlen(src);
if (n >= dst_size) n = dst_size - 1;
if (n > 0) memcpy(dst, src, n);
dst[n] = '\0';
}
static void vt_write_n(int idx, const char* s, size_t n)
{
redir_write(idx, s, n);
if (!sessions[idx].redir_quiet && sessions[idx].vterm)
vt_write_mr(idx, s, n);
}
static void vt_char(int idx, char c)
{
unsigned char uc = (unsigned char)c;
redir_write(idx, &c, 1);
if (sessions[idx].redir_quiet) return;
if (!sessions[idx].vterm) return;
if (uc < 0x80)
{
vterm_input_write(sessions[idx].vterm, &c, 1);
}
else
{
char buf[4];
unsigned short cp = mac_roman_to_unicode[uc - 0x80];
int len = 0;
if (cp < 0x80)
{
buf[0] = (char)cp;
len = 1;
}
else if (cp < 0x800)
{
buf[0] = 0xC0 | (cp >> 6);
buf[1] = 0x80 | (cp & 0x3F);
len = 2;
}
else
{
buf[0] = 0xE0 | (cp >> 12);
buf[1] = 0x80 | ((cp >> 6) & 0x3F);
buf[2] = 0x80 | (cp & 0x3F);
len = 3;
}
vterm_input_write(sessions[idx].vterm, buf, len);
}
}
/* simple integer-to-string for printf_s %d (which exists but let's
have our own for formatting with commas etc.) */
static void vt_print_long(int idx, long val)
{
char buf[32];
int neg = 0;
int i = 30;
buf[31] = '\0';
if (val == 0) { vt_write(idx, "0"); return; }
if (val < 0) { neg = 1; val = -val; }
while (val > 0 && i > 0)
{
buf[i--] = '0' + (val % 10);
val /= 10;
}
if (neg) buf[i--] = '-';
vt_write(idx, buf + i + 1);
}
/* ------------------------------------------------------------------ */
/* path helpers */
/* ------------------------------------------------------------------ */
/* get the name of the current directory as a C string */
static void get_dir_name(short vRefNum, long dirID, char* out, int maxlen)
{
CInfoPBRec pb;
Str255 name;
memset(&pb, 0, sizeof(pb));
name[0] = 0;
pb.dirInfo.ioNamePtr = name;
pb.dirInfo.ioVRefNum = vRefNum;
pb.dirInfo.ioDrDirID = dirID;
pb.dirInfo.ioFDirIndex = -1;
if (PBGetCatInfoSync(&pb) == noErr)
{
int len = name[0];
if (len > maxlen - 1) len = maxlen - 1;
memcpy(out, name + 1, len);
out[len] = '\0';
}
else
{
strncpy(out, "???", maxlen);
}
}
/* build a full HFS path for the current directory */
static void get_full_path(short vRefNum, long dirID, char* out, int maxlen)
{
/* walk parent chain and build path */
char parts[32][64];
int depth = 0;
CInfoPBRec pb;
Str255 name;
long curDir = dirID;
OSErr err;
while (depth < 32)
{
memset(&pb, 0, sizeof(pb));
name[0] = 0;
pb.dirInfo.ioNamePtr = name;
pb.dirInfo.ioVRefNum = vRefNum;
pb.dirInfo.ioDrDirID = curDir;
pb.dirInfo.ioFDirIndex = -1;
err = PBGetCatInfoSync(&pb);
if (err != noErr) break;
{
int len = name[0];
if (len > 63) len = 63;
memcpy(parts[depth], name + 1, len);
parts[depth][len] = '\0';
}
depth++;
if (pb.dirInfo.ioDrDirID == fsRtDirID) break;
curDir = pb.dirInfo.ioDrParID;
}
/* assemble in reverse */
out[0] = '\0';
{
int i;
for (i = depth - 1; i >= 0; i--)
{
if (strlen(out) + strlen(parts[i]) + 2 >= (size_t)maxlen) break;
strcat(out, parts[i]);
strcat(out, ":");
}
}
}
/* resolve a user-typed path (may use / as separator) to vRefNum+dirID+name.
returns noErr on success, populates spec. */
static OSErr resolve_path(int idx, const char* path, FSSpec* spec)
{
struct session* s = &sessions[idx];
Str255 pname;
char hfs_path[512];
int i, j;
if (path == NULL || path[0] == '\0')
return fnfErr;
/* strip leading "./" — HFS has no "." for current directory */
while (path[0] == '.' && path[1] == '/')
path += 2;
/* bare "." means current directory */
if (path[0] == '.' && path[1] == '\0')
return FSMakeFSSpec(s->shell_vRefNum, s->shell_dirID, "\p", spec);
/* convert / to : for HFS, handle leading / as volume root */
j = 0;
for (i = 0; path[i] != '\0' && j < 510; i++)
{
if (path[i] == '/')
hfs_path[j++] = ':';
else
hfs_path[j++] = path[i];
}
hfs_path[j] = '\0';
/* handle ".." -> parent */
if (strcmp(hfs_path, "..") == 0)
strcpy(hfs_path, "::");
/* strip trailing : unless the path IS just ":" or "::" */
{
int plen = strlen(hfs_path);
if (plen > 1 && hfs_path[plen - 1] == ':' && strcmp(hfs_path, "::") != 0)
hfs_path[--plen] = '\0';
}
/* if the path contains a colon but doesn't start with one, it would be
treated by FSMakeFSSpec as a FULL path (volume:folder:file) instead of
relative. prepend ':' to make it a relative partial path. */
if (hfs_path[0] != ':' && strchr(hfs_path, ':') != NULL)
{
int plen = strlen(hfs_path);
if (plen < 510)
{
memmove(hfs_path + 1, hfs_path, plen + 1);
hfs_path[0] = ':';
}
}
/* convert to pascal string */
{
int len = strlen(hfs_path);
if (len > 255) len = 255;
pname[0] = len;
memcpy(pname + 1, hfs_path, len);
}
return FSMakeFSSpec(s->shell_vRefNum, s->shell_dirID, pname, spec);
}
/* Resolve path and follow Finder aliases. Like resolve_path but also
resolves aliases to their targets. No-op if file is not an alias. */
static OSErr resolve_path_alias(int idx, const char* path, FSSpec* spec)
{
OSErr err;
Boolean isFolder, wasAlias;
err = resolve_path(idx, path, spec);
if (err != noErr) return err;
err = ResolveAliasFile(spec, true, &isFolder, &wasAlias);
return err;
}
/* check if an FSSpec points to a directory */
static int is_directory(FSSpec* spec)
{
CInfoPBRec pb;
memset(&pb, 0, sizeof(pb));
pb.hFileInfo.ioNamePtr = spec->name;
pb.hFileInfo.ioVRefNum = spec->vRefNum;
pb.hFileInfo.ioDirID = spec->parID;
pb.hFileInfo.ioFDirIndex = 0;
if (PBGetCatInfoSync(&pb) != noErr) return 0;
return (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0;
}
/* get the dirID for a directory FSSpec */
static long get_dir_id(FSSpec* spec)
{
CInfoPBRec pb;
memset(&pb, 0, sizeof(pb));
pb.dirInfo.ioNamePtr = spec->name;
pb.dirInfo.ioVRefNum = spec->vRefNum;
pb.dirInfo.ioDrDirID = spec->parID;
pb.dirInfo.ioFDirIndex = 0;
if (PBGetCatInfoSync(&pb) != noErr) return 0;
return pb.dirInfo.ioDrDirID;
}
/* ------------------------------------------------------------------ */
/* argument parsing */
/* ------------------------------------------------------------------ */
/* parse a command line into argc/argv, handling quoting and backslash escapes.
modifies line in-place to collapse escape sequences. */
static int parse_args(char* line, char* argv[], int max_args)
{
int argc = 0;
char* p = line;
while (*p && argc < max_args)
{
/* skip whitespace */
while (*p == ' ' || *p == '\t') p++;
if (*p == '\0') break;
if (*p == '"')
{
/* quoted arg */
p++;
argv[argc++] = p;
while (*p && *p != '"') p++;
if (*p == '"') *p++ = '\0';
}
else if (*p == '\'')
{
p++;
argv[argc++] = p;
while (*p && *p != '\'') p++;
if (*p == '\'') *p++ = '\0';
}
else
{
/* unquoted arg: handle backslash-escaped spaces */
char* dst = p;
argv[argc++] = dst;
while (*p)
{
if (*p == '\\' && *(p+1) == ' ')
{
/* escaped space: collapse to literal space */
*dst++ = ' ';
p += 2;
}
else if (*p == ' ' || *p == '\t')
{
break; /* unescaped whitespace = end of arg */
}
else
{
*dst++ = *p++;
}
}
if (*p) { *dst = '\0'; p++; }
else { *dst = '\0'; }
}
}
return argc;
}
/* ------------------------------------------------------------------ */
/* date formatting helper */
/* ------------------------------------------------------------------ */
static void format_date(unsigned long secs, char* out, int maxlen)
{
DateTimeRec dt;
static const char* months[] = {
"Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec"
};
SecondsToDate(secs, &dt);
if (dt.month < 1 || dt.month > 12) dt.month = 1;
snprintf(out, maxlen, "%s %2d %02d:%02d %d",
months[dt.month - 1], dt.day,
dt.hour, dt.minute, dt.year);
}
/* ------------------------------------------------------------------ */
/* type/creator helper */
/* ------------------------------------------------------------------ */
static int lookup_ext_type(const char* filename, OSType* type, OSType* creator);
static void ostype_to_str(OSType t, char* out)
{
out[0] = (t >> 24) & 0xFF;
out[1] = (t >> 16) & 0xFF;
out[2] = (t >> 8) & 0xFF;
out[3] = t & 0xFF;
out[4] = '\0';
/* replace non-printable with '?' */
{
int i;
for (i = 0; i < 4; i++)
if (out[i] < 32 || out[i] > 126) out[i] = '?';
}
}
static OSType str_to_ostype(const char* s)
{
char buf[4] = { ' ', ' ', ' ', ' ' };
int i;
for (i = 0; i < 4 && s[i]; i++) buf[i] = s[i];
return ((OSType)buf[0] << 24) | ((OSType)buf[1] << 16) |
((OSType)buf[2] << 8) | (OSType)buf[3];
}
/* ------------------------------------------------------------------ */
/* commands */
/* ------------------------------------------------------------------ */
/* return ANSI color escape for a file entry */
static const char* ls_color(int is_dir, int is_locked, int is_invis, OSType ftype)
{
if (is_dir) return "\033[1;34m"; /* bold blue */
if (is_invis) return "\033[2m"; /* dim */
if (is_locked) return "\033[1;31m"; /* bold red */
if (ftype == 0x4150504C) return "\033[1;32m"; /* APPL: bold green */
return "";
}
/* simple glob match: supports * and ? only, case-insensitive */
static int glob_match(const char* pattern, const char* str)
{
while (*pattern)
{
if (*pattern == '*')
{
pattern++;
if (!*pattern) return 1; /* trailing * matches everything */
while (*str)
{
if (glob_match(pattern, str)) return 1;
str++;
}
return 0;
}
else if (*pattern == '?')
{
if (!*str) return 0;
pattern++;
str++;
}
else
{
if (tolower((unsigned char)*pattern) != tolower((unsigned char)*str))
return 0;
pattern++;
str++;
}
}
return *str == '\0';
}
/* ls with glob pattern: enumerate directory, filter by pattern */
/* resolve glob base directory + pattern; returns 0 on success */
static int glob_resolve_dir(int idx, const char* target,
short* out_vRef, long* out_dID, const char** out_pattern)
{
struct session* s = &sessions[idx];
const char* last_sep = strrchr(target, ':');
if (last_sep)
{
char dir_path[256];
int dlen = (int)(last_sep - target);
if (dlen > 255) dlen = 255;
memcpy(dir_path, target, dlen);
dir_path[dlen] = '\0';
FSSpec dir_spec;
OSErr e = resolve_path_alias(idx, dir_path, &dir_spec);
if (e != noErr || !is_directory(&dir_spec)) return -1;
*out_vRef = dir_spec.vRefNum;
*out_dID = get_dir_id(&dir_spec);
*out_pattern = last_sep + 1;
}
else
{
*out_vRef = s->shell_vRefNum;
*out_dID = s->shell_dirID;
*out_pattern = target;
}
return 0;
}
/* ls glob: mode 0 = files only, mode 1 = expand dirs with headers */
static void cmd_ls_glob(int idx, const char* target, int long_fmt,
int show_all, int mode)
{
short vRef;
long dID;
const char* pattern;
if (glob_resolve_dir(idx, target, &vRef, &dID, &pattern) != 0)
{
if (mode == 0)
{
vt_write(idx, "ls: cannot access '");
vt_write(idx, target);
vt_write(idx, "': not found\r\n");
}
return;
}
CInfoPBRec pb;
Str255 name;
short index;
for (index = 1; ; index++)
{
memset(&pb, 0, sizeof(pb));
name[0] = 0;
pb.hFileInfo.ioNamePtr = name;
pb.hFileInfo.ioVRefNum = vRef;
pb.hFileInfo.ioDirID = dID;
pb.hFileInfo.ioFDirIndex = index;
if (PBGetCatInfoSync(&pb) != noErr) break;
char name_c[256];
{
int nl = name[0];
if (nl > 255) nl = 255;
memcpy(name_c, name + 1, nl);
name_c[nl] = '\0';
}
int is_dir = (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0;
int is_invis = 0;
if (is_dir)
is_invis = (pb.dirInfo.ioDrUsrWds.frFlags & kIsInvisible) != 0;
else
is_invis = (pb.hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible) != 0;
if (!show_all && (is_invis || name_c[0] == '.')) continue;
if (!glob_match(pattern, name_c)) continue;
if (mode == 0 && is_dir) continue; /* files pass: skip dirs */
if (mode == 1 && !is_dir) continue; /* dirs pass: skip files */
if (mode == 0)
{
/* show as file entry with parent path prefix */
FSSpec fspec;
fspec.vRefNum = vRef;
fspec.parID = dID;
fspec.name[0] = name[0];
memcpy(fspec.name + 1, name + 1, name[0]);
if (long_fmt)
{
/* long format: show metadata then prefixed name */
ls_show_file(idx, &fspec, long_fmt);
}
else
{
/* short format: prefix with parent path */
const char* last_sep = strrchr(target, ':');
if (last_sep)
{
char prefix[256];
int plen = (int)(last_sep - target) + 1;
if (plen > 255) plen = 255;
memcpy(prefix, target, plen);
prefix[plen] = '\0';
int is_locked2 = (pb.hFileInfo.ioFlAttrib & 0x01) != 0;
OSType ftype2 = pb.hFileInfo.ioFlFndrInfo.fdType;
const char* color = ls_color(0, is_locked2, is_invis, ftype2);
if (*color) vt_write(idx, color);
vt_write(idx, prefix);
vt_write(idx, name_c);
if (*color) vt_write(idx, "\033[0m");
vt_write(idx, "\r\n");
}
else
{
ls_show_file(idx, &fspec, 0);
}
}
}
else
{
/* mode 1: expand directory with full path header */
long sub_dID = pb.dirInfo.ioDrDirID;
vt_write(idx, "\r\n");
/* include parent path prefix from glob target */
{
const char* last_sep = strrchr(target, ':');
if (last_sep)
{
char prefix[256];
int plen = (int)(last_sep - target) + 1; /* include the : */
if (plen > 255) plen = 255;
memcpy(prefix, target, plen);
prefix[plen] = '\0';
vt_write(idx, prefix);
}
}
vt_write(idx, name_c);
vt_write(idx, ":\r\n");
ls_show_dir(idx, vRef, sub_dID, long_fmt, show_all);
}
}
}
/* check if a glob has any matches of a given type (0=files, 1=dirs) */
static int glob_has_type(int idx, const char* target, int show_all, int want_dir)
{
short vRef;
long dID;
const char* pattern;
if (glob_resolve_dir(idx, target, &vRef, &dID, &pattern) != 0)
return 0;
CInfoPBRec pb;
Str255 name;
short index;
for (index = 1; ; index++)
{
memset(&pb, 0, sizeof(pb));
name[0] = 0;
pb.hFileInfo.ioNamePtr = name;
pb.hFileInfo.ioVRefNum = vRef;
pb.hFileInfo.ioDirID = dID;
pb.hFileInfo.ioFDirIndex = index;
if (PBGetCatInfoSync(&pb) != noErr) break;
char name_c[256];
{
int nl = name[0];
if (nl > 255) nl = 255;
memcpy(name_c, name + 1, nl);
name_c[nl] = '\0';
}
int is_dir = (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0;
int is_invis = 0;
if (is_dir)
is_invis = (pb.dirInfo.ioDrUsrWds.frFlags & kIsInvisible) != 0;
else
is_invis = (pb.hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible) != 0;
if (!show_all && (is_invis || name_c[0] == '.')) continue;
if (!glob_match(pattern, name_c)) continue;
if (want_dir && is_dir) return 1;
if (!want_dir && !is_dir) return 1;
}
return 0;
}
/* list a single file entry with optional long format */
static void ls_show_file(int idx, FSSpec* tspec, int long_fmt)
{
CInfoPBRec pb;
memset(&pb, 0, sizeof(pb));
pb.hFileInfo.ioNamePtr = tspec->name;
pb.hFileInfo.ioVRefNum = tspec->vRefNum;
pb.hFileInfo.ioDirID = tspec->parID;
pb.hFileInfo.ioFDirIndex = 0;
if (PBGetCatInfoSync(&pb) != noErr) return;
int is_dir = (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0;
int is_invis = 0;
int is_locked = 0;
OSType ftype = 0;
if (is_dir)
is_invis = (pb.dirInfo.ioDrUsrWds.frFlags & kIsInvisible) != 0;
else
{
is_invis = (pb.hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible) != 0;
is_locked = (pb.hFileInfo.ioFlAttrib & 0x01) != 0;
ftype = pb.hFileInfo.ioFlFndrInfo.fdType;
}
if (long_fmt)
{
char perm[5], type_s[5], crea_s[5], date_s[32];
char sizebuf[24];
if (is_dir)
{
strcpy(perm, "drw-");
strcpy(type_s, "fold");
strcpy(crea_s, "MACS");
format_date(pb.dirInfo.ioDrMdDat, date_s, sizeof(date_s));
snprintf(sizebuf, sizeof(sizebuf), "%7d", (int)pb.dirInfo.ioDrNmFls);
}
else
{
perm[0] = '-';
perm[1] = 'r';
perm[2] = is_locked ? '-' : 'w';
perm[3] = is_invis ? 'i' : '-';
perm[4] = '\0';
ostype_to_str(ftype, type_s);
ostype_to_str(pb.hFileInfo.ioFlFndrInfo.fdCreator, crea_s);
format_date(pb.hFileInfo.ioFlMdDat, date_s, sizeof(date_s));
snprintf(sizebuf, sizeof(sizebuf), "%7ld",
pb.hFileInfo.ioFlLgLen + pb.hFileInfo.ioFlRLgLen);
}
vt_write(idx, perm);
vt_write(idx, " ");
vt_write(idx, type_s);
vt_write(idx, "/");
vt_write(idx, crea_s);
vt_write(idx, " ");
vt_write(idx, sizebuf);
vt_write(idx, " ");
vt_write(idx, date_s);
vt_write(idx, " ");
}
{
char name_c[64];
int len = tspec->name[0];
if (len > 63) len = 63;
memcpy(name_c, tspec->name + 1, len);
name_c[len] = '\0';
const char* color = ls_color(is_dir, is_locked, is_invis, ftype);
if (*color) vt_write(idx, color);
vt_write(idx, name_c);
if (is_dir) vt_write(idx, "/");
if (*color) vt_write(idx, "\033[0m");
}
vt_write(idx, "\r\n");
}
/* list contents of a directory */
static void ls_show_dir(int idx, short vRef, long dID, int long_fmt, int show_all)
{
CInfoPBRec pb;
Str255 name;
short index;
int size_width = 7; /* minimum width */
/* first pass for long format: find max size to auto-size column */
if (long_fmt)
{
for (index = 1; ; index++)
{
memset(&pb, 0, sizeof(pb));
name[0] = 0;
pb.hFileInfo.ioNamePtr = name;
pb.hFileInfo.ioVRefNum = vRef;
pb.hFileInfo.ioDirID = dID;
pb.hFileInfo.ioFDirIndex = index;
if (PBGetCatInfoSync(&pb) != noErr) break;
int is_dir = (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0;
int is_invis = 0;
if (is_dir)
is_invis = (pb.dirInfo.ioDrUsrWds.frFlags & kIsInvisible) != 0;
else
is_invis = (pb.hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible) != 0;
if (!show_all && (is_invis || name[1] == '.')) continue;
long sz;
if (is_dir)
sz = (long)pb.dirInfo.ioDrNmFls;
else
sz = pb.hFileInfo.ioFlLgLen + pb.hFileInfo.ioFlRLgLen;
/* count digits */
{
long tmp = sz;
int digits = 1;
while (tmp >= 10) { tmp /= 10; digits++; }
if (digits > size_width) size_width = digits;
}
}
}
/* main pass: display entries */
for (index = 1; ; index++)
{
memset(&pb, 0, sizeof(pb));
name[0] = 0;
pb.hFileInfo.ioNamePtr = name;
pb.hFileInfo.ioVRefNum = vRef;
pb.hFileInfo.ioDirID = dID;
pb.hFileInfo.ioFDirIndex = index;
if (PBGetCatInfoSync(&pb) != noErr) break;
char name_c[256];
{
int len = name[0];
if (len > 255) len = 255;
memcpy(name_c, name + 1, len);
name_c[len] = '\0';
}
int is_dir = (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0;
int is_invis = 0;
if (is_dir)
is_invis = (pb.dirInfo.ioDrUsrWds.frFlags & kIsInvisible) != 0;
else
is_invis = (pb.hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible) != 0;
if (!show_all && (is_invis || name_c[0] == '.')) continue;
if (long_fmt)
{
char perm[5];
char type_s[5], crea_s[5], date_s[32];
char sizebuf[24];
char fmt[16];
snprintf(fmt, sizeof(fmt), "%%%dld", size_width);
if (is_dir)
{
strcpy(perm, "drw-");
strcpy(type_s, "fold");
strcpy(crea_s, "MACS");
format_date(pb.dirInfo.ioDrMdDat, date_s, sizeof(date_s));
snprintf(sizebuf, sizeof(sizebuf), fmt, (long)pb.dirInfo.ioDrNmFls);
}
else
{
perm[0] = '-';
perm[1] = 'r';
perm[2] = (pb.hFileInfo.ioFlAttrib & 0x01) ? '-' : 'w';
perm[3] = is_invis ? 'i' : '-';
perm[4] = '\0';
ostype_to_str(pb.hFileInfo.ioFlFndrInfo.fdType, type_s);
ostype_to_str(pb.hFileInfo.ioFlFndrInfo.fdCreator, crea_s);
format_date(pb.hFileInfo.ioFlMdDat, date_s, sizeof(date_s));
snprintf(sizebuf, sizeof(sizebuf), fmt,
pb.hFileInfo.ioFlLgLen + pb.hFileInfo.ioFlRLgLen);
}
vt_write(idx, perm);
vt_write(idx, " ");
vt_write(idx, type_s);
vt_write(idx, "/");
vt_write(idx, crea_s);
vt_write(idx, " ");
vt_write(idx, sizebuf);
vt_write(idx, " ");
vt_write(idx, date_s);
vt_write(idx, " ");
}
{
int is_locked = !is_dir && (pb.hFileInfo.ioFlAttrib & 0x01);
OSType ftype = is_dir ? 0 : pb.hFileInfo.ioFlFndrInfo.fdType;
const char* color = ls_color(is_dir, is_locked, is_invis, ftype);
if (color[0]) vt_write(idx, color);
vt_write(idx, name_c);
if (is_dir) vt_write(idx, "/");
if (color[0]) vt_write(idx, "\033[0m");
}