ScrapExplorer - ls.c
Home / usr / light_builtins Lines: 4 | Size: 1599 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* Light builtins's (C version) 2 * ls - list directory contents 3 * A lightweight repository of useful shell commands 4 * GitHub: https://www.github.com/0x4248/light_builtins 5 * Licence: GNU General Public License v3.0 6 * By: 0x4248 7 */ 8 9#include <dirent.h> 10#include <stdio.h> 11#include <stdlib.h> 12#include <string.h> 13#include <sys/stat.h> 14#include <sys/types.h> 15 16#include "config.h" 17 18int main(int argc, char *argv[]) { 19 if (argc >= 2 && 20 (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0)) { 21 printf("Light Builtins (C) %d.%d.%d-%s\n", VERSION_MAJOR, VERSION_MINOR, 22 VERSION_PATCH, EXTRA_VERSION); 23 return 0; 24 } 25 DIR *dir; 26 struct dirent *ent; 27 char *path = argv[1]; 28 if (argc == 1) { 29 path = "."; 30 } 31 if ((dir = opendir(path)) != NULL) { 32 while ((ent = readdir(dir)) != NULL) { 33 if (ent->d_name[0] != '.') { 34 struct stat st; 35 char *fullpath = malloc(strlen(path) + strlen(ent->d_name) + 2); 36 strcpy(fullpath, path); 37 strcat(fullpath, "/"); 38 strcat(fullpath, ent->d_name); 39 stat(fullpath, &st); 40 if (S_ISDIR(st.st_mode)) { 41 printf("/%s ", ent->d_name); 42 } else { 43 printf("%s ", ent->d_name); 44 } 45 } 46 } 47 printf("\n"); 48 closedir(dir); 49 } else { 50 printf("ls: cannot access '%s': No such file or directory\n", path); 51 } 52 return 0; 53} 54[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.