home *** CD-ROM | disk | FTP | other *** search
/ hobbes.nmsu.edu / 2008-06-02_hobbes.nmsu.edu.zip / dos / sfrename.zip / SFRENAME.C < prev    next >
C/C++ Source or Header  |  1993-10-03  |  2KB  |  84 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <dos.h>
  4. #include <io.h>
  5. void do_rename(const char *oldname, const char *newname, const int attrib);
  6. #define NORMAL    0
  7. #define RHS        (FA_RDONLY | FA_HIDDEN | FA_SYSTEM)
  8. #define HS        (FA_HIDDEN | FA_SYSTEM)
  9.  
  10. /* sfrename - toggle os/2 file names (on the current drive) between
  11.     "ea data. sf" and "wp root. sf" and "ea_data._sf" and "wp_root._sf"
  12.     so that DOS backup and disk defragmenting routines will handle them.
  13.  */
  14.  
  15. int main(void)
  16.     {
  17.     int ea_space, ea_nospace, wp_space, wp_nospace;
  18.     char *state[2] = {"doesn't exist", "exists"};
  19.  
  20.     printf("Sfrename version 1.0, Copyright Oct. 2, 1993 by\n"
  21.             "Robert W. Babcock and WSS Division of DDC\n\n");
  22.  
  23. /* Check for existence of files (access returns 0 if file exists)
  24.  */
  25.     ea_space = !access("\\ea data. sf", 0);
  26.     wp_space = !access("\\wp root. sf", 0);
  27.     ea_nospace = !access("\\ea_data._sf", 0);
  28.     wp_nospace = !access("\\wp_root._sf", 0);
  29.  
  30.     if(ea_space && wp_space && !ea_nospace && !wp_nospace)
  31.         {
  32. /* OS/2 names exist, insert underscores to make valid DOS names
  33.  */
  34.         do_rename("\\ea data. sf", "\\ea_data._sf", NORMAL);
  35.         do_rename("\\wp root. sf", "\\wp_root._sf", NORMAL);
  36.         }
  37.     else if(!ea_space && !wp_space && ea_nospace && wp_nospace)
  38.         {
  39. /* DOS names exist, change to OS/2 form
  40.  */
  41.         do_rename("\\ea_data._sf", "\\ea data. sf", RHS);
  42.         do_rename("\\wp_root._sf", "\\wp root. sf", HS);
  43.         }
  44.     else
  45.         {
  46. /* Unexpected combination of names found, print error message and exit
  47.     without doing anything
  48.  */
  49.         printf("Expected file names not found:\n  EA DATA. SF %s\n"
  50.                 "  WP ROOT. SF %s\n  EA_DATA._SF %s\n  WP_ROOT._SF %s\n"
  51.                 "No file names changed\n", state[ea_space], state[wp_space],
  52.                 state[ea_nospace], state[wp_nospace]);
  53.         return(1);
  54.         }
  55.     return(0);
  56.     }
  57.  
  58. /* Rename files, change attributes, check for errors, print messages
  59.  */
  60. void do_rename(const char *oldname, const char *newname, const int attrib)
  61.     {
  62.     int old_archive;
  63.  
  64.     if(0 == rename(oldname, newname))
  65.         printf("  %s renamed to %s\n", oldname, newname);
  66.     else
  67.         {
  68.         printf("  Error, unable to rename %s to %s\n", oldname, newname);
  69.         exit(1);
  70.         }
  71.     if(-1 == (old_archive = _chmod(newname, 0)))
  72.         {
  73.         printf("Unable to read attributes of %s\n", newname);
  74.         exit(1);
  75.         }
  76.     old_archive &= FA_ARCH;
  77.     if(-1 == _chmod(newname, 1, attrib | old_archive))
  78.         {
  79.         printf("Unable to change attributes of %s\n", newname);
  80.         exit(1);
  81.         }
  82.     return;
  83.     }
  84.