I tried my new LS program on a Mac and it compiled straight away. And it runs just fine. But this is to be expected. I am not using anything unexpected such as GTK. This is just a simple console app.
()deusexmachina.local-_ (04:34 pm)New LS $> make mkdir -p build gcc -Wall -Wextra -std=gnu99 -lm -c src/my_ls.c -o build/my_ls.o clang: warning: -lm: 'linker' input unused [-Wunused-command-line-argument] In file included from src/my_ls.c:37: src/my_ls.h:7:9: warning: 'PATH_MAX' macro redefined [-Wmacro-redefined] #define PATH_MAX 4096 /* # chars in a path name including nul */ ^ /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/syslimits.h:99:9: note: previous definition is here #define PATH_MAX 1024 /* max bytes in pathname */ ^ In file included from src/my_ls.c:37: src/my_ls.h:92:26: warning: format specifies type 'long' but the argument has type 'nlink_t' (aka 'unsigned short') [-Wformat] printf(" %-4ld", file_stat.st_nlink); /* Print file hardlinks. */ ~~~~~ ^~~~~~~~~~~~~~~~~~ %-4hu src/my_ls.h:112:53: warning: format specifies type 'long' but the argument has type 'off_t' (aka 'long long') [-Wformat] printf("\x1b[35m %-6ld B \x1b[0m", file_stat.st_size); ~~~~~ ^~~~~~~~~~~~~~~~~ %-6lld 3 warnings generated. gcc -Wall -Wextra -std=gnu99 -lm build/my_ls.o -o build/my_program |
But it works just fine and displays all files in a folder.
()deusexmachina.local-_ (04:38 pm)New LS $> ./build/my_program /System/Library/Kernels/ /System/Library/Kernels/drwxr-xr-x. 142 root wheel 4.00 KB 1693811729 .. -rwxr-xr-x. 1 root wheel 14.00 MB 1693811729 kernel.release.t6000 -rwxr-xr-x. 1 root wheel 13.00 MB 1693811729 kernel.release.vmapple -rwxr-xr-x. 1 root wheel 14.00 MB 1693811729 kernel.release.t8110 -rwxr-xr-x. 1 root wheel 14.00 MB 1693811729 kernel.release.t8101 -rwxr-xr-x. 1 root wheel 14.00 MB 1693811729 kernel.release.t8020 -rwxr-xr-x. 1 root wheel 17.00 MB 1693811729 kernel End of listing. |
There are so many considerations when coding a simple LS clone. But this turned out very well indeed.
Here is a sample of the code that I wanted to show, this is calling another function to actually list files.
int main(int argc, char *argv[]) { #define BUF 0x05 if (argc == 2 && strcmp(argv[1], "--about") == 0) { // Print the about information here print_about(); // Define this function to contain your about content return 0; // Exit the program after printing about } if (argc == 1) { list_files("."); } else { printf("%s", argv[1]); /** * Calls the function list_files with the provided command line argument. * * @param argv[1] The command line argument representing the directory path. */ list_files(argv[1]); } printf("End of listing.\n"); /** * This code snippet invokes the `syscall` function to execute the `exit` system call. * The `exit` system call terminates the program and returns the exit status to the operating system. * The `syscall` function takes three arguments: the system call number, and two additional arguments * that are specific to the system call being invoked. * In this case, the system call number 60 corresponds to the `exit` system call. * The two additional arguments are set to 0, indicating that no additional information is passed to * the `exit` system call. */ __asm__("mov $0, %eax"); } |
This shows how to get a parameter passed to the program and actually use it to get information.