Advertisement
Guest User

stack

a guest
Apr 8th, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <sys/wait.h>
  8. #include <dirent.h>
  9. #include <errno.h>
  10. #include <ftw.h>
  11. #include <ctype.h>
  12. #include <sys/mman.h>
  13. #include <locale.h>
  14. #include <errno.h>
  15. #define MAX_PATH_LEN        2048
  16. unsigned long total_words = 0UL;
  17. unsigned long total_dirs = 0UL;
  18. unsigned long total_files = 0UL;
  19.  
  20.  
  21. // Just proves counting number of file in a folder
  22. int countInEveryFolder(const char *dir)
  23. {
  24.     struct stat stDirInfo;
  25.     struct dirent * stFiles;
  26.     DIR * stDirIn;
  27.     char szFullName[MAX_PATH_LEN];
  28.     char szDirectory[MAX_PATH_LEN];
  29.     struct stat stFileInfo;
  30.  
  31.     int numOfFile = 0;
  32.  
  33.     strncpy( szDirectory, dir, MAX_PATH_LEN - 1 );
  34.  
  35.  
  36.     if (lstat( szDirectory, &stDirInfo) < 0)
  37.     {
  38.         perror (szDirectory);
  39.         return 0;
  40.     }
  41.     if (!S_ISDIR(stDirInfo.st_mode))
  42.         return 0;
  43.     if ((stDirIn = opendir( szDirectory)) == NULL)
  44.     {
  45.         perror( szDirectory );
  46.         return 0;
  47.     }
  48.     while (( stFiles = readdir(stDirIn)) != NULL)
  49.     {
  50.         if (!strcmp(stFiles->d_name, ".") || !strcmp(stFiles->d_name, ".."))
  51.             continue;
  52.         sprintf(szFullName, "%s/%s", szDirectory, stFiles -> d_name );
  53.  
  54.         if (lstat(szFullName, &stFileInfo) < 0)
  55.             perror ( szFullName );
  56.  
  57.         /* is the file a directory? */
  58.         if (S_ISREG(stFileInfo.st_mode))
  59.         {
  60.             printf( "Filename: %s\n", szFullName );
  61.             numOfFile++;
  62.         }
  63.  
  64.     }  // end while
  65.     closedir(stDirIn);
  66.     return numOfFile;
  67. }
  68.  
  69.  
  70.  
  71.  
  72. // Count words in files.
  73. unsigned long count_words_in_file(const char *const filename)
  74. {
  75.     printf("counting\n");
  76.     unsigned long count = 0UL;
  77.     int errnum = 0;
  78.     int c;
  79.     FILE *in;
  80.  
  81.     in = fopen(filename, "rt");
  82.     if (in == NULL)
  83.     {
  84.         errnum = errno;
  85.         fprintf(stderr, "%s: %s.\n", filename, strerror(errnum));
  86.         errno = errnum;
  87.         return 0UL;
  88.     }
  89.  
  90.     /* Skip leading whitespace. */
  91.     do
  92.     {
  93.         c = getc(in);
  94.     }
  95.     while (isspace(c));
  96.  
  97.     /* Token loop. */
  98.     while (c != EOF)
  99.     {
  100.  
  101.         /* This token is a word, if it starts with a letter. */
  102.         if (isalpha(c))
  103.             count++;
  104.  
  105.         /* Skip the rest of this token. */
  106.         while (!isspace(c) && c != EOF)
  107.             c = getc(in);
  108.  
  109.         /* Skip the trailing whitespace. */
  110.         while (isspace(c))
  111.             c = getc(in);
  112.     }
  113.  
  114.     /* Paranoid checking for I/O errors. */
  115.     if (!feof(in) || ferror(in))
  116.     {
  117.         fclose(in);
  118.         fprintf(stderr, "Warning: %s: %s.\n", filename, strerror(EIO));
  119.         errnum = EIO;
  120.     }
  121.     else if (fclose(in))
  122.     {
  123.         fprintf(stderr, "Warning: %s: %s.\n", filename, strerror(EIO));
  124.         errnum = EIO;
  125.     }
  126.     errno = errnum;
  127.  
  128.     return count;
  129. }
  130.  
  131. int fun(const char *filepath){
  132.     int t=0;
  133.     t= fork();
  134.     if(t>0)exit(1);
  135.     else
  136.             printf("%lutot1\n",total_words);
  137.             total_words += count_words_in_file(filepath);
  138.             printf("%lutot2\n",total_words);
  139. }
  140. // Recursively go in folders
  141. int nftw_callback(const char *filepath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
  142. {
  143.  
  144.     // Directory
  145.     if (typeflag == FTW_DP || typeflag == FTW_D)
  146.     {
  147.  
  148.         total_dirs++;
  149.         printf("%*s%s\n", ftwbuf->level * 4, "", filepath);
  150.        
  151.         countInEveryFolder(filepath);
  152.      
  153.  
  154.     }
  155.     // Folder
  156.     else if (typeflag == FTW_F)
  157.     {
  158.         total_files++;
  159.         fun(filepath);
  160.         printf("%*s%s\n", ftwbuf->level * 4, "", filepath);
  161.    }
  162.    else
  163.     printf("unknown%d\n",typeflag);
  164.    return 0;
  165. }
  166.  
  167. /* Error message */
  168. void err_sys(const char *msg)
  169. {
  170.     perror(msg);
  171.     fflush(stdout);
  172.     exit(EXIT_FAILURE);
  173. }
  174.  
  175.  
  176.  
  177. int main(int argc, char *argv[])
  178. {
  179.  
  180.  
  181.     total_files = total_dirs = total_words = 0UL;
  182.     if (nftw(argv[1], nftw_callback, 15, FTW_PHYS) == 0)
  183.     {
  184.         /* Success! */
  185.         printf("%s: %lu files, %lu directories, %lu words total.\n",
  186.                argv[1], total_files, total_dirs, total_words);
  187.  
  188.     }
  189.     else
  190.     {
  191.         /* Failed... */
  192.         err_sys("ntfw");
  193.     }
  194.     putchar('\n');
  195.  
  196.  
  197.  
  198.     //printf( "\nTotal words = %d\n\n", *wordCount);
  199.     //printf( "\nTotal folders = %d\n\n", *folderCount);
  200.     //printf( "\nTotal childs = %d\n\n", *childCount);      //fork()
  201.  
  202.  
  203.     return 0;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement