Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/wait.h>
- #include <dirent.h>
- #include <errno.h>
- #include <ftw.h>
- #include <ctype.h>
- #include <sys/mman.h>
- #include <locale.h>
- #include <errno.h>
- #define MAX_PATH_LEN 2048
- unsigned long total_words = 0UL;
- unsigned long total_dirs = 0UL;
- unsigned long total_files = 0UL;
- // Just proves counting number of file in a folder
- int countInEveryFolder(const char *dir)
- {
- struct stat stDirInfo;
- struct dirent * stFiles;
- DIR * stDirIn;
- char szFullName[MAX_PATH_LEN];
- char szDirectory[MAX_PATH_LEN];
- struct stat stFileInfo;
- int numOfFile = 0;
- strncpy( szDirectory, dir, MAX_PATH_LEN - 1 );
- if (lstat( szDirectory, &stDirInfo) < 0)
- {
- perror (szDirectory);
- return 0;
- }
- if (!S_ISDIR(stDirInfo.st_mode))
- return 0;
- if ((stDirIn = opendir( szDirectory)) == NULL)
- {
- perror( szDirectory );
- return 0;
- }
- while (( stFiles = readdir(stDirIn)) != NULL)
- {
- if (!strcmp(stFiles->d_name, ".") || !strcmp(stFiles->d_name, ".."))
- continue;
- sprintf(szFullName, "%s/%s", szDirectory, stFiles -> d_name );
- if (lstat(szFullName, &stFileInfo) < 0)
- perror ( szFullName );
- /* is the file a directory? */
- if (S_ISREG(stFileInfo.st_mode))
- {
- printf( "Filename: %s\n", szFullName );
- numOfFile++;
- }
- } // end while
- closedir(stDirIn);
- return numOfFile;
- }
- // Count words in files.
- unsigned long count_words_in_file(const char *const filename)
- {
- printf("counting\n");
- unsigned long count = 0UL;
- int errnum = 0;
- int c;
- FILE *in;
- in = fopen(filename, "rt");
- if (in == NULL)
- {
- errnum = errno;
- fprintf(stderr, "%s: %s.\n", filename, strerror(errnum));
- errno = errnum;
- return 0UL;
- }
- /* Skip leading whitespace. */
- do
- {
- c = getc(in);
- }
- while (isspace(c));
- /* Token loop. */
- while (c != EOF)
- {
- /* This token is a word, if it starts with a letter. */
- if (isalpha(c))
- count++;
- /* Skip the rest of this token. */
- while (!isspace(c) && c != EOF)
- c = getc(in);
- /* Skip the trailing whitespace. */
- while (isspace(c))
- c = getc(in);
- }
- /* Paranoid checking for I/O errors. */
- if (!feof(in) || ferror(in))
- {
- fclose(in);
- fprintf(stderr, "Warning: %s: %s.\n", filename, strerror(EIO));
- errnum = EIO;
- }
- else if (fclose(in))
- {
- fprintf(stderr, "Warning: %s: %s.\n", filename, strerror(EIO));
- errnum = EIO;
- }
- errno = errnum;
- return count;
- }
- int fun(const char *filepath){
- int t=0;
- t= fork();
- if(t>0)exit(1);
- else
- printf("%lutot1\n",total_words);
- total_words += count_words_in_file(filepath);
- printf("%lutot2\n",total_words);
- }
- // Recursively go in folders
- int nftw_callback(const char *filepath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
- {
- // Directory
- if (typeflag == FTW_DP || typeflag == FTW_D)
- {
- total_dirs++;
- printf("%*s%s\n", ftwbuf->level * 4, "", filepath);
- countInEveryFolder(filepath);
- }
- // Folder
- else if (typeflag == FTW_F)
- {
- total_files++;
- fun(filepath);
- printf("%*s%s\n", ftwbuf->level * 4, "", filepath);
- }
- else
- printf("unknown%d\n",typeflag);
- return 0;
- }
- /* Error message */
- void err_sys(const char *msg)
- {
- perror(msg);
- fflush(stdout);
- exit(EXIT_FAILURE);
- }
- int main(int argc, char *argv[])
- {
- total_files = total_dirs = total_words = 0UL;
- if (nftw(argv[1], nftw_callback, 15, FTW_PHYS) == 0)
- {
- /* Success! */
- printf("%s: %lu files, %lu directories, %lu words total.\n",
- argv[1], total_files, total_dirs, total_words);
- }
- else
- {
- /* Failed... */
- err_sys("ntfw");
- }
- putchar('\n');
- //printf( "\nTotal words = %d\n\n", *wordCount);
- //printf( "\nTotal folders = %d\n\n", *folderCount);
- //printf( "\nTotal childs = %d\n\n", *childCount); //fork()
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement