ls/ls.c

148 lines
2.7 KiB
C
Raw Permalink Normal View History

2024-10-21 01:27:53 +00:00
#define _POSIX_C_SOURCE 200809L
2024-10-07 01:04:39 +00:00
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <limits.h>
#include <stdarg.h>
2024-10-21 01:27:53 +00:00
#include <stdbool.h>
2024-10-07 01:04:39 +00:00
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
2024-10-21 01:27:53 +00:00
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
2024-10-07 01:04:39 +00:00
void ls_ent(const char *path);
void do_regfile(const char *path, const struct stat *sb);
void do_dir(const char *path, const struct stat *sb);
void do_symlink(const char *path, const struct stat *sb);
void do_blockdev(const char *path, const struct stat *sb);
void do_char(const char *path, const struct stat *sb);
void do_fifo(const char *path, const struct stat *sb);
void do_socket(const char *path, const struct stat *sb);
void do_unknown(const char *path, const struct stat *sb);
int
main(int argc, char *argv[])
{
int i;
if (argc == 1) {
ls_ent(".");
} else {
for (i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
/* argparsing */
} else {
ls_ent(argv[i]);
}
}
}
return 0;
}
void
ls_ent(const char *path)
{
struct stat sb;
if (lstat(path, &sb) == -1) {
err(1, "Failed to stat %s", path);
}
2024-10-21 01:27:53 +00:00
if (S_ISREG(sb.st_mode)) {
2024-10-07 01:04:39 +00:00
do_regfile(path, &sb);
2024-10-21 01:27:53 +00:00
} else if (S_ISDIR(sb.st_mode)) {
2024-10-07 01:04:39 +00:00
do_dir(path, &sb);
2024-10-21 01:27:53 +00:00
} else if (S_ISLNK(sb.st_mode)) {
2024-10-07 01:04:39 +00:00
do_symlink(path, &sb);
2024-10-21 01:27:53 +00:00
} else if (S_ISCHR(sb.st_mode)) {
2024-10-07 01:04:39 +00:00
do_char(path, &sb);
2024-10-21 01:27:53 +00:00
} else if (S_ISFIFO(sb.st_mode)) {
2024-10-07 01:04:39 +00:00
do_fifo(path, &sb);
2024-10-21 01:27:53 +00:00
} else if (S_ISSOCK(sb.st_mode)) {
2024-10-07 01:04:39 +00:00
do_socket(path, &sb);
2024-10-21 01:27:53 +00:00
} else {
2024-10-07 01:04:39 +00:00
do_unknown(path, &sb);
}
}
void
do_regfile(const char *path, const struct stat *sb)
{
(void)sb;
printf("reg: %s\n", path);
}
void
do_dir(const char *path, const struct stat *sb)
{
DIR *d;
struct dirent *dir;
char full_path[PATH_MAX];
(void)sb;
d = opendir(path);
if (!d) {
err(1, "Failed to open dir: %s", path);
}
printf("dir: %s\n", path);
while ((dir = readdir(d))) {
/* avoids infinite recursion :( */
if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
continue;
}
snprintf(full_path, sizeof(full_path), "%s/%s", path, dir->d_name);
ls_ent(full_path);
}
closedir(d);
}
void
do_symlink(const char *path, const struct stat *sb)
{
(void)sb;
printf("sym: %s\n", path);
}
void
do_blockdev(const char *path, const struct stat *sb)
{
(void)sb;
printf("blk: %s\n", path);
}
void
do_char(const char *path, const struct stat *sb)
{
(void)sb;
printf("char: %s\n", path);
}
void
do_fifo(const char *path, const struct stat *sb)
{
(void)sb;
printf("fifo: %s\n", path);
}
void
do_socket(const char *path, const struct stat *sb)
{
(void)sb;
printf("sock: %s\n", path);
}
void
do_unknown(const char *path, const struct stat *sb)
{
(void)sb;
printf("wtf: %s\n", path);
}