2023-10-20 22:24:35 +03:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
2023-10-20 00:19:46 +03:00
|
|
|
#include <time.h>
|
2023-10-03 23:09:17 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2023-10-20 16:59:07 +03:00
|
|
|
#include <stdint.h>
|
2023-10-20 17:32:50 +03:00
|
|
|
#include <unistd.h>
|
2023-10-03 23:09:17 +03:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2023-10-20 22:24:35 +03:00
|
|
|
|
2023-10-20 00:19:46 +03:00
|
|
|
unsigned int a_flag;
|
|
|
|
unsigned int l_flag;
|
|
|
|
|
|
|
|
void PrintPerm(struct stat sb) {
|
|
|
|
printf("%c", (S_ISDIR(sb.st_mode)) ? 'd' : '-');
|
|
|
|
printf("%c%c%c", (sb.st_mode & S_IRUSR) ? 'r' : '-', (sb.st_mode & S_IWUSR) ? 'w' : '-', (sb.st_mode & S_IXUSR) ? 'x' : '-');
|
|
|
|
printf("%c%c%c", (sb.st_mode & S_IRGRP) ? 'r' : '-', (sb.st_mode & S_IWGRP) ? 'w' : '-', (sb.st_mode & S_IXGRP) ? 'x' : '-');
|
|
|
|
printf("%c%c%c", (sb.st_mode & S_IROTH) ? 'r' : '-', (sb.st_mode & S_IWOTH) ? 'w' : '-', (sb.st_mode & S_IXOTH) ? 'x' : '-');
|
|
|
|
}
|
2023-10-03 23:09:17 +03:00
|
|
|
|
2023-10-30 18:09:33 +03:00
|
|
|
char *fileflag(const char *path) {
|
|
|
|
struct stat sb;
|
|
|
|
if (stat(path, &sb)) {
|
|
|
|
fprintf(stderr, "ls: unable to stat %s: %s\n", path, strerror(errno));
|
|
|
|
return " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISDIR(sb.st_mode))
|
|
|
|
return "/";
|
|
|
|
|
|
|
|
else if ((sb.st_mode & S_IXUSR) || (sb.st_mode & S_IXGRP) || (sb.st_mode & S_IXOTH))
|
|
|
|
return "*";
|
2023-10-21 17:18:07 +03:00
|
|
|
|
2023-10-30 18:09:33 +03:00
|
|
|
else
|
|
|
|
return " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintInfo(struct stat sb, const char *filename) {
|
2023-10-21 17:18:07 +03:00
|
|
|
/* Permissions */
|
|
|
|
PrintPerm(sb);
|
|
|
|
|
|
|
|
/* Date */
|
|
|
|
struct tm *tm = localtime(&sb.st_mtime);
|
|
|
|
|
|
|
|
char date[14];
|
|
|
|
if (strftime(date, sizeof(date), "%b %d %H:%M", tm) == 0) {
|
|
|
|
fprintf(stderr, "ls: strftime()\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Group and user name */
|
|
|
|
struct passwd *pw = getpwuid(sb.st_uid);
|
|
|
|
struct group *gr = getgrgid(sb.st_gid);
|
|
|
|
|
2023-10-30 18:09:33 +03:00
|
|
|
printf(" %s %s %jd %s %s%s\n", (pw != 0) ? pw->pw_name : "nobody", (gr != 0) ? gr->gr_name : "nobody", (uintmax_t)sb.st_size, date, filename, fileflag(filename));
|
2023-10-21 17:18:07 +03:00
|
|
|
}
|
|
|
|
|
2023-10-20 00:19:46 +03:00
|
|
|
int list(const char *path, int label) {
|
2023-10-20 22:24:35 +03:00
|
|
|
struct stat sb;
|
2023-10-30 18:09:33 +03:00
|
|
|
if (lstat(path, &sb)) {
|
2023-10-04 20:00:21 +03:00
|
|
|
fprintf(stderr, "ls: unable to stat %s: %s\n", path, strerror(errno));
|
2023-10-03 23:09:17 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If its file */
|
2023-10-20 22:24:35 +03:00
|
|
|
if (!S_ISDIR(sb.st_mode)) {
|
2023-10-21 17:18:07 +03:00
|
|
|
if (l_flag)
|
|
|
|
PrintInfo(sb, path);
|
|
|
|
|
|
|
|
else
|
|
|
|
puts(path);
|
|
|
|
|
2023-10-03 23:09:17 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make label */
|
|
|
|
if (label)
|
|
|
|
printf("\n%s: \n", path);
|
|
|
|
|
2023-10-20 17:32:50 +03:00
|
|
|
if (chdir(path)) {
|
|
|
|
fprintf(stderr, "ls: %s: %s\n", path, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-10-03 23:09:17 +03:00
|
|
|
/* Open and print dir */
|
2023-10-20 17:32:50 +03:00
|
|
|
DIR *dp = opendir(".");
|
2023-10-20 22:24:35 +03:00
|
|
|
if (dp == NULL)
|
2023-10-03 23:09:17 +03:00
|
|
|
return 1;
|
|
|
|
|
2023-10-20 00:19:46 +03:00
|
|
|
struct dirent *ep;
|
2023-10-03 23:09:17 +03:00
|
|
|
while ((ep = readdir(dp)) != NULL) {
|
2023-10-20 00:19:46 +03:00
|
|
|
if (ep->d_name[0] == '.' && !a_flag)
|
2023-10-03 23:09:17 +03:00
|
|
|
continue;
|
|
|
|
|
2023-10-20 00:19:46 +03:00
|
|
|
if (l_flag) {
|
2023-10-30 18:09:33 +03:00
|
|
|
if (stat(ep->d_name, &sb) == -1) {
|
2023-10-20 00:19:46 +03:00
|
|
|
fprintf(stderr, "ls: lstat()\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-10-21 17:18:07 +03:00
|
|
|
PrintInfo(sb, ep->d_name);
|
2023-10-20 00:19:46 +03:00
|
|
|
}
|
|
|
|
|
2023-10-20 17:32:50 +03:00
|
|
|
else
|
2023-10-30 18:09:33 +03:00
|
|
|
printf("%s%s ", ep->d_name, fileflag(ep->d_name));
|
|
|
|
|
2023-10-03 23:09:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dp);
|
2023-10-20 17:32:50 +03:00
|
|
|
printf("\n");
|
2023-10-03 23:09:17 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(const int argc, const char **argv) {
|
|
|
|
int i;
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
if (argv[i][0] != '-')
|
|
|
|
break;
|
|
|
|
|
|
|
|
else if (!strcmp(argv[i], "-a"))
|
2023-10-20 00:19:46 +03:00
|
|
|
a_flag = 1;
|
|
|
|
|
|
|
|
else if (!strcmp(argv[i], "-l"))
|
|
|
|
l_flag = 1;
|
2023-10-03 23:09:17 +03:00
|
|
|
|
2023-10-21 15:05:49 +03:00
|
|
|
else if (!strcmp(argv[i], "--help")) {
|
2023-10-30 18:09:33 +03:00
|
|
|
printf("ls [-a show hidden files] [-l use a long listing format] [Path]\n");
|
2023-10-03 23:09:17 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == argc)
|
2023-10-20 00:19:46 +03:00
|
|
|
return list(".", 0);
|
2023-10-03 23:09:17 +03:00
|
|
|
|
|
|
|
if (i == argc - 1)
|
2023-10-20 00:19:46 +03:00
|
|
|
return list(argv[i], 0);
|
2023-10-03 23:09:17 +03:00
|
|
|
|
|
|
|
else
|
|
|
|
for (int i = 1; i < argc; i++)
|
2023-10-20 00:19:46 +03:00
|
|
|
if (list(argv[i], 1))
|
2023-10-03 23:09:17 +03:00
|
|
|
return 1;
|
2023-10-20 00:19:46 +03:00
|
|
|
|
2023-10-03 23:09:17 +03:00
|
|
|
return 0;
|
|
|
|
}
|