Phasor 2.2.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
ls.phs
Go to the documentation of this file.
1// Port of 'ls' directory listing program for GNU.
2// 'ls' directory listing program for Phasor.
3// Copyright (C) 2026 Daniel McGuire.
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17
18include_stdfile();
19include_stdsys();
20include_stdstr();
21include_stdio();
22
23var g_show_hidden = false;
24var g_long_format = false;
25var g_path = ".";
26
27fn show_help() -> void {
28 puts("Usage: ls [OPTION]... [FILE]");
29 puts("List information about the FILE (the current directory by default).");
30 puts("");
31 puts("Options:");
32 puts(" -a, --all do not ignore entries starting with .");
33 puts(" -l use a long listing format");
34 puts(" --help display this help and exit");
35 puts(" --version output version information and exit");
36}
37
38fn show_version() -> void {
39 puts("ls (Phasor coreutils) 0.1");
40 puts("License GPLv3+: GNU GPL version 3 or later");
41 puts("This is free software: you are free to change and redistribute it.");
42 puts("There is NO WARRANTY, to the extent permitted by law.");
43}
44
45fn list_directory() -> int {
46 var dir_listing = freaddir(g_path);
47 if (dir_listing == null || dir_listing == "") {
48 puts_error("ls: cannot access '" + g_path + "': No such file or directory");
49 return 1;
50 }
51
52 puts(dir_listing);
53 return 0;
54}
55
56fn starts_with(input: string, prefix: string) -> bool {
57 if (len(input) < len(prefix)) return false;
58 return substr(input, 0, len(prefix)) == prefix;
59}
60
61fn main() -> int {
62 var i = 1;
63 var arg_count = sys_argc();
64
65 while (i < arg_count) {
66 var arg = sys_argv(i);
67
68 if (arg == "--help") {
69 show_help();
70 return 0;
71 } else if (arg == "--version") {
72 show_version();
73 return 0;
74 } else if (arg == "--all") {
75 g_show_hidden = true;
76 } else if (arg == "-a") {
77 g_show_hidden = true;
78 } else if (arg == "-l") {
79 g_long_format = true;
80 } else if (starts_with(arg, "-")) {
81 puts("ls: invalid option");
82 puts("Try 'ls --help' for more information.");
83 return 1;
84 } else if (g_path == "." && i == arg_count - 1) {
85 g_path = arg;
86 }
87
88 i = i + 1;
89 }
90
91 return list_directory();
92}
93
94shutdown(main());