Open MessiBest07 opened 4 months ago
I can code review this one
I think a useful addition would be adding a flag to display file data in either bytes, kilobytes, or megabytes.
Example:
du cat -b
du cat -kb
du cat -mb
You could even add du cat -gb
but that might be too big for this project haha.
You could also create a flag that prints the data in bytes, kilobytes, and megabytes all at once. du cat -all
A helper flag with test cases or java documentation would help as well
Currently "du ." does not work as it says the path is too long but it is a simple fix in order to print sizes of each file in the directory
if (strlen(path) + 1 + DIRSIZ + 1 > sizeof path) {
printf("du: path too long\n");
break;
}
char buf[MAX];
strcpy(buf, path);
char *p = buf + strlen(buf);
*p++ = '/';
Fix:
char buf[MAX], *p;
if (strlen(path) + 1 + DIRSIZ + 1 > sizeof buf) {
printf("du: path too long\n");
break;
}
//char buf[MAX];
strcpy(buf, path);
p = buf + strlen(buf);
*p++ = '/';
This should fix the error
I also tried du console
and it printed out nothing. Since console does not have any size, you could print out something like console is empty
or constant 0KB
.
I also tried du cat snowman sdioadjsaodj
and it prints cat 34 KB
I think it would be useful to say
cat 34KB
du: cannot access snowman
du: cannot access sdioadjsaodj
if du can not find files such as snowman in the directory
I think a useful addition would be able to print out multiple sizes of files at once.
As of the code currently, if I enter du zombie cat
, it prints zombie 32 KB
I think it would be useful if you could loop through each argument and print each file size so it could have an output like.
zombie 32KB
cat 34KB
if you enter du zombie cat
I think the project has made really good progress so far and just needs a couple fixes for the project to improve. I'll add more code review when have a second look
I will code review this one too
Use Constants for Readability and Maintenance: The use of magic numbers (e.g., 1024, 1024*1024) directly in the code can hinder readability and maintainability. Defining constants for these values at the beginning of your file can make the code cleaner and easier to understand or modify.
#define KB 1024
#define MB (KB * KB)
Refactoring print_size Function: Consider using a loop or a more scalable approach for size conversion. As your project grows, you might want to support gigabytes, terabytes, etc., without having a long chain of if-else statements.
Error Handling Enhancement: Improve error messages by specifying the type of error (e.g., permission denied, file not found) using strerror(errno) for more descriptive error reporting.
Your "fileviewtool" project is great, and with these suggested improvements, it can become even more versatile and user-friendly. Focus on gradually implementing these enhancements while maintaining code quality and usability.
Adding to the code review above:
Notes:
-b -kb -mb
print_size(int size)
you can do:
#define KB 1024
or you can define both byte sizes instead of (1024 * 1024) for MB:
#define KB 1024
#define MB 1048576
void print_size(int size) {
if (size < KB) {
printf("%d B\n", size);
} else if (size < MB) {
printf("%d KB\n", size / KB);
} else {
printf("%d MB\n", size / MB);
}
}
du /sampledir -w
Prints size of whole directory
du /sampledir -s
output:
file1 21KB
file2 33KB
file3 45KB
Overall: Great start, maybe add a few more functions and maybe some documentation for each function would work. A few more flags would improve this program slightly. Great job!
Code review feedback is excellent and should be implemented. As it stands now, this is only ls
with features removed. du
should be recursive, and this is not.
-s
(summary) flag: total up the size for each parameter like standard unix du
does.
You can use "du" to check the size of file $ du README.md