home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / SRC / msdos_diskaccess.lzh / MS_DISK_ACCESS / mren.c < prev    next >
C/C++ Source or Header  |  1990-05-10  |  3KB  |  126 lines

  1. /*
  2.  * Rename an existing MSDOS file
  3.  *
  4.  * Emmet P. Gray            US Army, HQ III Corps & Fort Hood
  5.  * ...!uunet!uiucuxc!fthood!egray    Attn: AFZF-DE-ENV
  6.  *                     Directorate of Engineering & Housing
  7.  *                     Environmental Management Office
  8.  *                     Fort Hood, TX 76544-5057
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include "msdos.h"
  13.  
  14. int fd;                /* the file descriptor for the floppy */
  15. int dir_start;            /* starting sector for directory */
  16. int dir_len;            /* length of directory (in sectors) */
  17. int dir_entries;        /* number of directory entries */
  18. int dir_chain[25];        /* chain of sectors in directory */
  19. int clus_size;            /* cluster size (in sectors) */
  20. int fat_len;            /* length of FAT table (in sectors) */
  21. int num_clus;            /* number of available clusters */
  22. unsigned char *fatbuf;        /* the File Allocation Table */
  23. char *mcwd;            /* the Current Working Directory */
  24.  
  25. main(argc, argv)
  26. int argc;
  27. char *argv[];
  28. {
  29.     int entry, ismatch, nogo, fargn, verbose, got_it;
  30.     char *filename, *newfile, *fixname(), *strncpy(), *unixname();
  31.     char *getpath(), *pathname, tname[9], text[4], *getname(), *target;
  32.     char *new, ans[10], *temp, *strcpy();
  33.     void exit(), writedir(), free();
  34.     struct directory *dir, *search();
  35.  
  36.     if (init(2)) {
  37.         fprintf(stderr, "mren: Cannot initialize diskette\n");
  38.         exit(1);
  39.     }
  40.     fargn = 1;
  41.     verbose = 0;
  42.     if (argc > 1) {
  43.         if (!strcmp(argv[1], "-v")) {
  44.             fargn = 2;
  45.             verbose = 1;
  46.         }
  47.     }
  48.     if (argc != fargn+2) {
  49.         fprintf(stderr, "Usage: mren [-v] sourcefile targetfile\n");
  50.         exit(1);
  51.     }
  52.     filename = getname(argv[fargn]);
  53.     pathname = getpath(argv[fargn]);
  54.     if (subdir(pathname))
  55.         exit(1);
  56.  
  57.     temp = getname(argv[fargn+1]);
  58.     target = fixname(argv[fargn+1], verbose);
  59.  
  60.     strncpy(tname, target, 8);
  61.     strncpy(text, target+8, 3);
  62.     tname[8] = '\0';
  63.     text[3] = '\0';
  64.  
  65.     new = unixname(tname, text);
  66.     nogo = 0;
  67.                     /* the name supplied may be altered */
  68.     if (strcmp(temp, new) && verbose) {
  69.         while (!nogo) {
  70.             printf("Do you accept \"%s\" as the new file name (y/n) ? ", new);
  71.             gets(ans);
  72.             if (ans[0] == 'y' || ans[0] == 'Y')
  73.                 break;
  74.             if (ans[0] == 'n' || ans[0] == 'N')
  75.                 nogo = 1;
  76.         }
  77.     }
  78.     if (nogo)
  79.         exit(0);
  80.                     /* see if exists and do it */
  81.     ismatch = 0;
  82.     for (entry=0; entry<dir_entries; entry++) {
  83.         dir = search(entry);
  84.                     /* if empty */
  85.         if (dir->name[0] == 0x0)
  86.             break;
  87.                     /* if erased */
  88.         if (dir->name[0] == 0xe5)
  89.             continue;
  90.                     /* if volume label */
  91.         if (dir->attr == 0x08)
  92.             continue;
  93.                     /* you may rename a directory */
  94.         strncpy(tname, (char *) dir->name, 8);
  95.         strncpy(text, (char *) dir->ext, 3);
  96.         tname[8] = '\0';
  97.         text[3] = '\0';
  98.  
  99.         newfile = unixname(tname, text);
  100.  
  101.                     /* if the new name already exists */
  102.         if (!strcmp(new, newfile)) {
  103.             fprintf(stderr, "mren: File \"%s\" already exists\n", new);
  104.             exit(1);
  105.         }
  106.                     /* if the old name exists */
  107.         if (!strcmp(filename, newfile)) {
  108.             ismatch = 1;
  109.             got_it = entry;
  110.         }
  111.         free(newfile);
  112.     }
  113.     if (!ismatch) {
  114.         fprintf(stderr, "mren: File \"%s\" not found\n", filename);
  115.         exit(1);
  116.     }
  117.                     /* so go ahead and do it */
  118.     dir = search(got_it);
  119.     strncpy((char *) dir->name, target, 8);
  120.     strncpy((char *) dir->ext, target+8, 3);
  121.     writedir(got_it, dir);
  122.  
  123.     close(fd);
  124.     exit(0);
  125. }
  126.