Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/stat.h>
- #include <time.h>
- #include <dirent.h>
- #include <errno.h>
- #include <unistd.h>
- #include <libgen.h>
- #define false 0
- #define true 1
- char *ProgramName;
- unsigned long long sizeFile = 0;
- unsigned long long sizeAllFiles = 0;
- char *pathfile = NULL;
- char *Concat(char *s1, char *s2)
- {
- size_t len1 = strlen(s1);
- size_t len2 = strlen(s2);
- char *result = malloc(len1 + len2 + 1);
- if (!result)
- {
- fprintf(stderr, "malloc() failed: insufficient memory!\n");
- return NULL;
- }
- memcpy(result, s1, len1);
- memcpy(result + len1, s2, len2 + 1);
- return result;
- }
- void ProceedErrors(char *Message, char *FilePath, char *LinkPath)
- {
- fprintf(stderr, "%s: %s %s %s\n", basename(ProgramName), Message, FilePath, LinkPath);
- }
- bool ParametersCountCheck(int Count)
- {
- if (Count == 2)
- return true;
- ProceedErrors("Error! Wrong console parameters count! Need 1 parameters.", "", "");
- return false;
- }
- bool SelfOrParentDirectoryCheck(char *ContentName)
- {
- return ((strcmp(ContentName, ".") != 0) && (strcmp(ContentName, "..") != 0));
- }
- bool NotCatalogCheck(struct dirent *DirectoryContent)
- {
- return ((*DirectoryContent).d_type != 4);
- }
- char *GetNextDirectoryPath(char *DirectoryPath, char *ContentName)
- {
- return Concat(Concat(DirectoryPath, "/"), ContentName);
- }
- void AnalyzeDirectoryContent(char *DirectoryPath)
- {
- DIR *Directory;
- struct dirent *DirectoryContent;
- if (Directory = opendir(DirectoryPath))
- {
- errno = 0;
- DirectoryContent = readdir(Directory);
- while (DirectoryContent != NULL)
- {
- char *ContentName = (*DirectoryContent).d_name;
- if (SelfOrParentDirectoryCheck(ContentName))
- if (NotCatalogCheck(DirectoryContent))
- {
- char *pathByFile = Concat(DirectoryPath, "/");
- pathByFile = Concat(pathByFile, ContentName);
- struct stat st1;
- stat(pathByFile, &st1);
- sizeAllFiles += st1.st_size;
- if (st1.st_size > sizeFile)
- {
- sizeFile = st1.st_size;
- pathfile = pathByFile;
- }
- }
- else
- {
- char *NextDirectory = GetNextDirectoryPath(DirectoryPath, ContentName);
- AnalyzeDirectoryContent(NextDirectory);
- }
- errno = 0;
- DirectoryContent = readdir(Directory);
- }
- }
- }
- int main(int argc, char *argv[], char *envp[])
- {
- if (ParametersCountCheck(argc))
- {
- ProgramName = argv[0];
- char *SourceDirectoryPath = argv[1];
- AnalyzeDirectoryContent(SourceDirectoryPath);
- printf("Max file size: %llu bytes\nDir:%s\nSum all files: %llu bytes\n", sizeFile, pathfile, sizeAllFiles);
- return true;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement