This commit is contained in:
Your Name 2023-10-30 18:45:24 +03:00
parent d3fc1c1f88
commit 5eda08233d

View File

@ -3,6 +3,7 @@
#include <time.h> #include <time.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <unistd.h> #include <unistd.h>
@ -13,6 +14,27 @@
unsigned int a_flag; unsigned int a_flag;
unsigned int l_flag; unsigned int l_flag;
int get_stat(const char *path, struct stat *stat_path) {
if (stat(path, stat_path)) {
fprintf(stderr, "rm: unable to stat %s: %s\n", path, strerror(errno));
return 1;
}
return 0;
}
char *make_path(const char *src, const char *dst) {
size_t len = strlen(src) + strlen(dst) + 2;
char *full_path = malloc(len + 1);
if (full_path == NULL) {
fprintf(stderr, "cp: malloc() returned NULL\n");
return NULL;
}
snprintf(full_path, len, "%s/%s", src, dst);
return full_path;
}
void PrintPerm(struct stat sb) { void PrintPerm(struct stat sb) {
printf("%c", (S_ISDIR(sb.st_mode)) ? 'd' : '-'); 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_IRUSR) ? 'r' : '-', (sb.st_mode & S_IWUSR) ? 'w' : '-', (sb.st_mode & S_IXUSR) ? 'x' : '-');
@ -22,10 +44,8 @@ void PrintPerm(struct stat sb) {
char *fileflag(const char *path) { char *fileflag(const char *path) {
struct stat sb; struct stat sb;
if (stat(path, &sb)) { if (get_stat(path, &sb))
fprintf(stderr, "ls: unable to stat %s: %s\n", path, strerror(errno));
return " "; return " ";
}
if (S_ISDIR(sb.st_mode)) if (S_ISDIR(sb.st_mode))
return "/"; return "/";
@ -60,7 +80,7 @@ void PrintInfo(struct stat sb, const char *filename) {
int list(const char *path, int label) { int list(const char *path, int label) {
struct stat sb; struct stat sb;
if (lstat(path, &sb)) { if (lstat(path, &sb)) {
fprintf(stderr, "ls: unable to stat %s: %s\n", path, strerror(errno)); fprintf(stderr, "ls: unable to lstat %s: %s\n", path, strerror(errno));
return 1; return 1;
} }
@ -79,32 +99,33 @@ int list(const char *path, int label) {
if (label) if (label)
printf("\n%s: \n", path); printf("\n%s: \n", path);
if (chdir(path)) { /* Open and print dir */
DIR *dp = opendir(path);
if (dp == NULL) {
fprintf(stderr, "ls: %s: %s\n", path, strerror(errno)); fprintf(stderr, "ls: %s: %s\n", path, strerror(errno));
return 1; return 1;
} }
/* Open and print dir */
DIR *dp = opendir(".");
if (dp == NULL)
return 1;
struct dirent *ep; struct dirent *ep;
while ((ep = readdir(dp)) != NULL) { while ((ep = readdir(dp)) != NULL) {
if (ep->d_name[0] == '.' && !a_flag) if (ep->d_name[0] == '.' && !a_flag)
continue; continue;
if (l_flag) { char *full_path = make_path(path, ep->d_name);
if (stat(ep->d_name, &sb) == -1) { if (full_path == NULL)
fprintf(stderr, "ls: lstat()\n");
return 1; return 1;
}
PrintInfo(sb, ep->d_name); if (l_flag) {
if (get_stat(full_path, &sb))
return 1;
PrintInfo(sb, full_path);
} }
else else
printf("%s%s ", ep->d_name, fileflag(ep->d_name)); printf("%s%s\n", ep->d_name, fileflag(full_path));
free(full_path);
} }
@ -140,7 +161,7 @@ int main(const int argc, const char **argv) {
return list(argv[i], 0); return list(argv[i], 0);
else else
for (int i = 1; i < argc; i++) for (; i < argc; i++)
if (list(argv[i], 1)) if (list(argv[i], 1))
return 1; return 1;