This commit is contained in:
Your Name 2023-10-20 22:24:35 +03:00
parent ecb6f39d33
commit d5c55abd21

View File

@ -1,3 +1,5 @@
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <stdio.h>
#include <errno.h>
@ -7,6 +9,7 @@
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
unsigned int a_flag;
unsigned int l_flag;
@ -18,14 +21,14 @@ void PrintPerm(struct stat sb) {
}
int list(const char *path, int label) {
struct stat statbuf;
if (stat(path, &statbuf)) {
struct stat sb;
if (stat(path, &sb)) {
fprintf(stderr, "ls: unable to stat %s: %s\n", path, strerror(errno));
return 1;
}
/* If its file */
if (S_ISDIR(statbuf.st_mode) == 0) {
if (!S_ISDIR(sb.st_mode)) {
puts(path);
return 0;
}
@ -41,10 +44,8 @@ int list(const char *path, int label) {
/* Open and print dir */
DIR *dp = opendir(".");
if (dp == NULL) {
fprintf(stderr, "ls: %s: %s\n", path, strerror(errno));
if (dp == NULL)
return 1;
}
struct dirent *ep;
while ((ep = readdir(dp)) != NULL) {
@ -52,7 +53,6 @@ int list(const char *path, int label) {
continue;
if (l_flag) {
struct stat sb;
if (lstat(ep->d_name, &sb) == -1) {
fprintf(stderr, "ls: lstat()\n");
return 1;
@ -61,7 +61,7 @@ int list(const char *path, int label) {
/* Permissions */
PrintPerm(sb);
/* Date and other info */
/* Date */
struct tm *tm = localtime(&sb.st_mtime);
char date[14];
@ -70,7 +70,11 @@ int list(const char *path, int label) {
return 1;
}
printf(" %jd %jd %s %s\n", (uintmax_t)sb.st_mode, (uintmax_t)sb.st_size, date, ep->d_name);
/* Group and user name */
struct passwd *pw = getpwuid(sb.st_uid);
struct group *gr = getgrgid(sb.st_gid);
printf(" %s %s %jd %s %s\n", (pw != 0) ? pw->pw_name : "nobody", (gr != 0) ? gr->gr_name : "nobody", (uintmax_t)sb.st_size, date, ep->d_name);
}
else