Fix permission checks in EXT2.

This commit is contained in:
Enrico Fraccaroli
2021-12-29 13:16:02 +01:00
parent 1ebc74f31f
commit 81a0abed20
7 changed files with 99 additions and 22 deletions
+39 -17
View File
@@ -9,32 +9,54 @@
#include <string.h>
#include <sys/unistd.h>
#include <strerror.h>
#include <sys/stat.h>
int main(int argc, char **argv)
{
if (argc != 2) {
printf("%s: missing operand.\n", argv[0]);
printf("Try '%s --help' for more information.\n\n", argv[0]);
if (argc < 2) {
printf("cat: missing operand.\n");
printf("Try 'cat --help' for more information.\n\n");
return 1;
}
if (strcmp(argv[1], "--help") == 0) {
printf("Prints the content of the given file.\n");
printf("Usage:\n");
printf(" %s <file>\n\n", argv[0]);
return 0;
}
int fd = open(argv[1], O_RDONLY, 42);
if (fd < 0) {
printf("%s: %s: %s\n\n", argv[0], strerror(errno), argv[1]);
return 1;
// Check if `--help` is provided.
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "--help") == 0) {
printf("Prints the content of the given file.\n");
printf("Usage:\n");
printf(" cat <file>\n\n");
return 0;
}
}
// Prepare the buffer for reading.
char buffer[BUFSIZ];
// Put on the standard output the characters.
while (read(fd, buffer, BUFSIZ) > 0) {
puts(buffer);
// Iterate the arguments.
for (int i = 1; i < argc; ++i) {
int fd = open(argv[i], O_RDONLY, 42);
if (fd < 0) {
printf("cat: %s: %s\n\n", argv[i], strerror(errno));
continue;
}
stat_t statbuf;
if (fstat(fd, &statbuf) == -1) {
printf("cat: %s: %s\n\n", argv[i], strerror(errno));
// Close the file descriptor.
close(fd);
continue;
}
if (S_ISDIR(statbuf.st_mode)) {
printf("cat: %s: %s\n\n", argv[i], strerror(EISDIR));
// Close the file descriptor.
close(fd);
continue;
}
// Put on the standard output the characters.
while (read(fd, buffer, BUFSIZ) > 0) {
puts(buffer);
}
// Close the file descriptor.
close(fd);
}
putchar('\n');
putchar('\n');
close(fd);
return 0;
}
+2 -2
View File
@@ -145,7 +145,7 @@ int main(int argc, char *argv[])
no_directory = false;
int fd = open(argv[i], O_RDONLY | O_DIRECTORY, 0);
if (fd == -1) {
printf("%s: cannot access '%s': %s\n\n", argv[0], argv[i], strerror(errno));
printf("ls: cannot access '%s': %s\n\n", argv[i], strerror(errno));
} else {
printf("%s:\n", argv[i]);
print_ls(fd, argv[i], flags);
@@ -157,7 +157,7 @@ int main(int argc, char *argv[])
getcwd(cwd, PATH_MAX);
int fd = open(cwd, O_RDONLY | O_DIRECTORY, 0);
if (fd == -1) {
printf("%s: cannot access '%s': %s\n\n", argv[0], cwd, strerror(errno));
printf("ls: cannot access '%s': %s\n\n", cwd, strerror(errno));
} else {
print_ls(fd, cwd, flags);
close(fd);