home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / conv111.zip / convert.c next >
C/C++ Source or Header  |  1997-07-22  |  6KB  |  148 lines

  1. /*********************************************************************/
  2. /*                                                                   */
  3. /* Convert Version 1.11                                              */
  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 : 21 July, 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            */
  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.11 Made to be UNIX compatible.                                */
  43. /*                                                                   */
  44. /*  1.10 Fix temperary environment bug.  Also simplified the code.   */
  45. /*       Convert does not work on FAT drives only on HPFS.           */
  46. /*                                                                   */
  47. /*  1.00 I thought it was such a simple program, there should not    */
  48. /*       be any bugs.                                                */
  49. /*********************************************************************/
  50.  
  51. #include <stdio.h>
  52. #include <string.h>
  53. #include <ctype.h>
  54.  
  55. #define OK 0
  56. #define FAILED 1
  57. #define FilenameLength 256
  58. #define FILENAME 2
  59. #define CONVERT 1
  60. #define PROGRAM 0
  61. #define TEMPNAME "convert.tmp"
  62.  
  63.  
  64. int Rename(const char *oldname, const char *newname);
  65. char *strupr(char *string);
  66. char *strlwr(char *string);
  67.  
  68. int main (int argc, char *argv[]) {
  69.  
  70.    char name[FilenameLength], *newname;
  71.    int count;
  72.  
  73.    newname = name;
  74.  
  75.    if (argc < 3) {
  76.       printf("\nConvert 1.11\n");
  77.       printf("\nexample usage is: %s <convert> <filename>\n", argv[PROGRAM]);
  78.       printf("Convert must be either -l<ower> or -u<pper>\n");
  79.       return FAILED;
  80.       }
  81.    for (count = FILENAME; count <= argc - 1; count++) {
  82.  
  83.       if ((strcmp(strlwr(argv[CONVERT]), "-u")) == OK)  {
  84.          strcpy(newname, argv[count]);
  85.          strupr(newname);
  86.          if ((Rename(argv[count], newname)) != OK) {
  87.             return FAILED;
  88.             }
  89.          }
  90.       else if (strcmp(strlwr(argv[CONVERT]), "-l") == OK) {
  91.          strcpy(newname, argv[count]);
  92.          strlwr(newname);
  93.          if ((Rename(argv[count], newname)) != OK) {
  94.             return FAILED;
  95.             }
  96.          }
  97.       else {
  98.          printf("\nConvert must be either -l<ower> or -u<pper>\n");
  99.          return FAILED;
  100.          }
  101.       }
  102.    return OK;
  103.    }
  104.  
  105. int Rename(const char *oldname, const char *newname) {
  106.    char *tempname;
  107.    tempname = TEMPNAME;
  108.  
  109.    /* tempname = tmpnam(NULL);    Removed as it did not allow
  110.                                   the program to work across
  111.                                   multipule drives.
  112.                                   The fix uses a standard temp
  113.                                   name for all conversions,
  114.                                   this may be a problem if
  115.                                   multi-tasking.  */
  116.    if ((rename(oldname, tempname)) != OK) {
  117.       printf("\nCould not rename file %s to %s\n", oldname, newname);
  118.       printf("Convert uses a temperary file called convert.tmp in the current directory.\n");
  119.       printf("Please ensure that a file by this name does not already exist.\n");
  120.       return FAILED;
  121.       }
  122.    if ((rename(tempname, newname)) != OK) {
  123.       printf("\nCould not rename file %s to %s\n", oldname, newname);
  124.       return FAILED;
  125.       }
  126.    return OK;
  127.    }
  128.  
  129. char *strlwr(char *string) {
  130.    char *s;
  131.    s = string;
  132.    while (*s != '\0') {
  133.       *s = tolower(*s);
  134.       s++;
  135.       }
  136.    return string;
  137.    }
  138.  
  139. char *strupr(char *string) {
  140.    char *s;
  141.    s = string;
  142.    while (*s != '\0') {
  143.       *s = toupper(*s);
  144.       s++;
  145.       }
  146.    return string;
  147.    }
  148.