DeanReid

tolower.c

Mar 23rd, 2015
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.06 KB | None | 0 0
  1. /**
  2.     @file   tolower.c
  3.     @brief  Recursively converts all file-names to lower-case.
  4.  
  5.     Copyright © 2002 by Josef Pelikan, MFF UK Prague
  6.     http://cgg.ms.mff.cuni.cz/~pepca/
  7.     @since  21.8.2002
  8.     @date   19.10.2002
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <dirent.h>
  16. #include <unistd.h>
  17. #include <string.h>
  18. #include <ctype.h>
  19.  
  20. int verbose = 0;
  21.  
  22. int convertDirectory ( const char *name )
  23.     /* returns number of converted file-names, -1 if failed */
  24. {
  25.     DIR *dir;
  26.     char fn[2048], oldFn[2048], *ptr;
  27.     int len, rw, counter, needs, subCounter;
  28.     struct dirent *entry;
  29.     struct stat st;
  30.  
  31.     if ( !name || !name[0] ) return -1;
  32.     dir = opendir(name);
  33.     if ( !dir ) return -1;
  34.     strcpy(fn,name);
  35.     len = strlen(fn);
  36.     fn[len++] = '/';
  37.     rw = 0;                                 /* the directory is writtable? */
  38.     counter = 0;                            /* number of changed file-names */
  39.     while ( (entry = readdir(dir)) ) {      /* process one directory entry */
  40.         if ( entry->d_name[0] == '.' &&
  41.              (!entry->d_name[1] ||
  42.               entry->d_name[1] == '.' &&
  43.               !entry->d_name[2]) )
  44.             continue;                       /* special directory */
  45.             /* assemble the item name: */
  46.         strcpy(fn+len,entry->d_name);
  47.             /* lower-case conversion: */
  48.         needs = 0;
  49.         for ( ptr = fn + len; *ptr; ptr++ )
  50.             if ( isupper(*ptr) ) {
  51.                 needs = 1;
  52.                 break;
  53.                 }
  54.             /* the directory-entry needs to be converted: */
  55.         if ( needs ) {
  56.             if ( !rw ) {                    /* make the mother directory writable */
  57.                 fn[len-1] = (char)0;
  58.                 if ( !stat(fn,&st) && !(st.st_mode & S_IWUSR) )
  59.                     chmod(fn,st.st_mode|S_IWUSR);
  60.                 fn[len-1] = '/';
  61.                 rw = 1;
  62.                 }
  63.             strcpy(oldFn,fn);
  64.             for ( ptr = fn + len; *ptr; ptr++ )
  65.                 if ( isupper(*ptr) )
  66.                     *ptr = tolower(*ptr);
  67.             rename(oldFn,fn);
  68.             if ( verbose ) printf("%s -> %s\n",oldFn,fn);
  69.             counter++;
  70.             }
  71.             /* stat the entry .. subdirectories must be traversed */
  72.         if ( !stat(fn,&st) &&
  73.              S_ISDIR(st.st_mode) ) {        /* directory */
  74.             subCounter = convertDirectory(fn);
  75.             if ( subCounter >= 0 )
  76.                 counter += subCounter;
  77.             else
  78.                 if ( verbose )
  79.                     printf("Error converting '%s' directory\n",fn);
  80.             }
  81.         }
  82.     closedir(dir);
  83.    
  84.     return counter;
  85. }
  86.  
  87. int main ( int argc, char *argv[] )
  88. {
  89.     /* TODO: command-line argument: directory to convert */
  90.     /* TODO: -v (verbose) */
  91.     int converted = convertDirectory(".");
  92.     if ( converted <= 0 )
  93.         printf("No file-names were converted\n");
  94.     else
  95.         printf("%d file-names were converted\n",converted);
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment