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

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