home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / cv112.zip / convert.c next >
Text File  |  1997-08-29  |  7KB  |  170 lines

  1. /*********************************************************************/
  2. /*                                                                   */
  3. /* Convert Version 1.12                                              */
  4. /*                                                                   */
  5. /* Converts uppercase file names to lower case and visa versa        */
  6. /* Written by :  John Ballment                                       */
  7. /* Written on :  06 June, 1997                                       */
  8. /* Last updated : 28 August, 1997                                    */
  9. /*                                                                   */
  10. /* I wrote this small application to convert files from uppercase    */
  11. /* to lowercase after doing a project with a couple of friends who   */
  12. /* where using Windows 95.                                           */
  13. /*                                                                   */
  14. /* Everytime they saved files onto thier disks, the filenames were   */
  15. /* in uppercase, which made it hard to port onto my OS/2 system and  */
  16. /* the UNIX system which the project was being designed for.         */
  17. /*                                                                   */
  18. /* The program command-line is as follows:                           */
  19. /*                                                                   */
  20. /*      convert -u/l <filename>                                      */
  21. /*                                                                   */
  22. /* -u = convert to uppercase (I wrote it to convert both ways)       */
  23. /* -l = convert to lowercase                                         */
  24. /* <filename> = a filename including wildcards like * and ?          */
  25. /*                                                                   */
  26. /* If you find the program useful please send a post card to:        */
  27. /*                                                                   */
  28. /*     John Ballment                                                 */
  29. /*     98 Kilsay Cres                                                */
  30. /*     Meadowbrook  Q  4131                                          */
  31. /*     Australia, plant Earth, the Milky-Way solar system, etc       */
  32. /*                                                                   */
  33. /* Please send any bug reports to:                                   */
  34. /*                                                                   */
  35. /*     n1237462@droid.fit.qut.edu.au                                 */
  36. /*                                                                   */
  37. /*********************************************************************/
  38.  
  39. /*********************************************************************/
  40. /*  History -                                                        */
  41. /*                                                                   */
  42. /*  1.12 Add counter to show how many files were converted and how   */
  43. /*       many failed.                                                */
  44. /*                                                                   */
  45. /*  1.11 Made to be UNIX compatible.                                 */
  46. /*                                                                   */
  47. /*  1.10 Fix temperary environment bug.  Also simplified the code.   */
  48. /*       Convert does not work on FAT drives only on HPFS.           */
  49. /*                                                                   */
  50. /*  1.00 I thought it was such a simple program, there should not    */
  51. /*       be any bugs.                                                */
  52. /*********************************************************************/
  53.  
  54. #include <stdio.h>
  55. #include <string.h>
  56. #include <ctype.h>
  57. #include <io.h>
  58.  
  59. #define CONVERT_VERSION 1.12
  60. #define OK 0
  61. #define FAILED 1
  62. #define FilenameLength 256
  63. #define FILENAME 2
  64. #define CONVERT 1
  65. #define PROGRAM 0
  66. #define TEMPNAME "convert.tmp"
  67.  
  68.  
  69. int Rename(const char *oldname, const char *newname);
  70. char *strupr(char *string);
  71. char *strlwr(char *string);
  72.  
  73. int main (int argc, char *argv[]) {
  74.  
  75.    char name[FilenameLength], *newname;
  76.    int index;
  77.    int converted = 0;
  78.    int failed = 0;
  79.    newname = name;
  80.  
  81.    printf("\n%s V%3.2f\n", argv[PROGRAM], CONVERT_VERSION);
  82.    if (argc < 3) {
  83.       printf("\nexample usage is: %s <convert> <filename>\n", argv[PROGRAM]);
  84.       printf("<convert> must be either -l<ower> or -u<pper>\n", argv[PROGRAM]);
  85.       return FAILED;
  86.       }
  87.  
  88.    if (_access(TEMPNAME, 00) != -1) {
  89.       printf("Convert uses a temperary file called convert.tmp in the current directory.\n");
  90.       printf("A file by this name already exists in this directory,\n");
  91.       printf("please correct this problem\n");
  92.       return FAILED;
  93.       }
  94.  
  95.    for (index = FILENAME; index <= argc - 1; index++) {
  96.  
  97.       if ((strcmp(strlwr(argv[CONVERT]), "-u")) == OK)  {
  98.          strcpy(newname, argv[index]);
  99.          strupr(newname);
  100.          if ((Rename(argv[index], newname)) != OK) {
  101.             failed++;
  102.          } else {
  103.             converted++;
  104.             }
  105.          }
  106.       else if (strcmp(strlwr(argv[CONVERT]), "-l") == OK) {
  107.          strcpy(newname, argv[index]);
  108.          strlwr(newname);
  109.          if ((Rename(argv[index], newname)) != OK) {
  110.             failed++;
  111.          } else {
  112.             converted++;
  113.             }
  114.          }
  115.       else {
  116.          printf("\nexample usage is: %s <convert> <filename>\n", argv[PROGRAM]);
  117.          printf("<convert> must be either -l<ower> or -u<pper>\n", argv[PROGRAM]);
  118.          return FAILED;
  119.          }
  120.       }
  121.    printf("\nconverted %d files successfully and %d files failed.\n", converted, failed);
  122.    return OK;
  123.    }
  124.  
  125. int Rename(const char *oldname, const char *newname) {
  126.    char *tempname;
  127.    tempname = TEMPNAME;
  128.  
  129.    /* tempname = tmpnam(NULL);    Removed as it did not allow
  130.                                   the program to work across
  131.                                   multipule drives.
  132.                                   The fix uses a standard temp
  133.                                   name for all conversions,
  134.                                   this may be a problem if
  135.                                   multi-tasking.  */
  136.  
  137.    if ((rename(oldname, tempname)) != OK) {
  138.       printf("\nCould not rename file %s to %s\n", oldname, newname);
  139.       printf("Please check that you have permission to modify %s.\n", oldname);
  140.       return FAILED;
  141.       }
  142.  
  143.    if ((rename(tempname, newname)) != OK) {
  144.       printf("\nInternal error:  Could not rename file %s to %s\n", tempname, newname);
  145.       printf("Please manually rename this file, i.e. ren %s %s\n", tempname, newname);
  146.       return FAILED;
  147.       }
  148.    return OK;
  149.    }
  150.  
  151. char *strlwr(char *string) {
  152.    char *s;
  153.    s = string;
  154.    while (*s != '\0') {
  155.       *s = tolower(*s);
  156.       s++;
  157.       }
  158.    return string;
  159.    }
  160.  
  161. char *strupr(char *string) {
  162.    char *s;
  163.    s = string;
  164.    while (*s != '\0') {
  165.       *s = toupper(*s);
  166.       s++;
  167.       }
  168.    return string;
  169.    }
  170.