1// Port of 'ls' directory listing program for GNU.
2// 'ls' directory listing program for Phasor.
3// Copyright (C) 2026 Daniel McGuire.
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.
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.
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/>.
23var g_show_hidden = false;
24var g_long_format = false;
27fn show_help() -> void {
28 puts("Usage: ls [OPTION]... [FILE]");
29 puts("List information about the FILE (the current directory by default).");
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");
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.");
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");
56fn starts_with(input: string, prefix: string) -> bool {
57 if (len(input) < len(prefix)) return false;
58 return substr(input, 0, len(prefix)) == prefix;
63 var arg_count = sys_argc();
65 while (i < arg_count) {
66 var arg = sys_argv(i);
68 if (arg == "--help") {
71 } else if (arg == "--version") {
74 } else if (arg == "--all") {
76 } else if (arg == "-a") {
78 } else if (arg == "-l") {
80 } else if (starts_with(arg, "-")) {
81 puts("ls: invalid option");
82 puts("Try 'ls --help' for more information.");
84 } else if (g_path == "." && i == arg_count - 1) {
91 return list_directory();