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 / mdir.c < prev    next >
C/C++ Source or Header  |  1990-05-10  |  5KB  |  209 lines

  1. /*
  2.  * Display a MSDOS directory
  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 i, entry, files, blocks, fargn, wide, faked;
  30.     long size;
  31.     char name[9], ext[4], *date, *time, *convdate(), *convtime();
  32.     char *strncpy(), newpath[MAX_PATH], *getname(), *getpath(), *pathname;
  33.     char *newfile, *filename, *unixname(), volume[12], *sep;
  34.     char *strcpy(), *strcat(), newname[MAX_PATH], *strncat();
  35.     void exit(), reset_dir(), free();
  36.     struct directory *dir, *search();
  37.  
  38.     if (init(0)) {
  39.         fprintf(stderr, "mdir: Cannot initialize diskette\n");
  40.         exit(1);
  41.     }
  42.                     /* find the volume label */
  43.     reset_dir();
  44.     volume[0] = '\0';
  45.     for (entry=0; entry<dir_entries; entry++) {
  46.         dir = search(entry);
  47.                     /* if empty */
  48.         if (dir->name[0] == 0x0)
  49.             break;
  50.                     /* if not volume label */
  51.         if (!(dir->attr & 0x08))
  52.             continue;
  53.  
  54.         strncpy(volume, (char *) dir->name, 8);
  55.         volume[8] = '\0';
  56.         strncat(volume, (char *) dir->ext, 3);
  57.         volume[11] = '\0';
  58.         break;
  59.     }
  60.     if (volume[0] == '\0')
  61.         printf(" Volume in drive has no label\n");
  62.     else
  63.         printf(" Volume in drive is %s\n", volume);
  64.     fargn = 1;
  65.     wide = 0;
  66.                     /* first argument number */
  67.     if (argc > 1) {
  68.         if (!strcmp(argv[1], "-w")) {
  69.             wide = 1;
  70.             fargn = 2;
  71.         }
  72.     }
  73.                     /* fake an argument */
  74.     faked = 0;
  75.     if (argc == fargn) {
  76.         faked++;
  77.         argc++;
  78.     }
  79.     files = 0;
  80.     for (i=fargn; i<argc; i++) {
  81.         if (faked) {
  82.             filename = getname(".");
  83.             pathname = getpath(".");
  84.         }
  85.         else {
  86.             filename = getname(argv[i]);
  87.             pathname = getpath(argv[i]);
  88.         }
  89.                     /* move to first guess subdirectory */
  90.                     /* required by isdir() */
  91.         if (subdir(pathname)) {
  92.             free(filename);
  93.             free(pathname);
  94.             continue;
  95.         }
  96.                     /* is filename really a subdirectory? */
  97.         if (isdir(filename)) {
  98.             strcpy(newpath, pathname);
  99.             if (strcmp(pathname,"/") && strcmp(pathname, "\\")) {
  100.                 if (*pathname != '\0')
  101.                     strcat(newpath, "/");
  102.             }
  103.             strcat(newpath, filename);
  104.                     /* move to real subdirectory */
  105.             if (subdir(newpath)) {
  106.                 free(filename);
  107.                 free(pathname);
  108.                 continue;
  109.             }
  110.             strcpy(newname, "*");
  111.         }
  112.         else {
  113.             strcpy(newpath, pathname);
  114.             strcpy(newname, filename);
  115.         }
  116.  
  117.         if (*filename == '\0')
  118.             strcpy(newname, "*");
  119.  
  120.         if (*newpath == '/' || *newpath == '\\')
  121.             printf(" Directory for %s\n\n", newpath);
  122.         else if (!strcmp(newpath, "."))
  123.             printf(" Directory for %s\n\n", mcwd);
  124.         else {
  125.             if (strlen(mcwd) == 1 || !strlen(newpath))
  126.                 sep = "";
  127.             else
  128.                 sep = "/";
  129.             printf(" Directory for %s%s%s\n\n", mcwd, sep, newpath);
  130.         }
  131.         for (entry=0; entry<dir_entries; entry++) {
  132.             dir = search(entry);
  133.                     /* if empty */
  134.             if (dir->name[0] == 0x0)
  135.                 break;
  136.                     /* if erased */
  137.             if (dir->name[0] == 0xe5)
  138.                 continue;
  139.                     /* if a volume label */
  140.             if (dir->attr & 0x08)
  141.                 continue;
  142.  
  143.             strncpy(name, (char *) dir->name, 8);
  144.             strncpy(ext, (char *) dir->ext, 3);
  145.             name[8] = '\0';
  146.             ext[3] = '\0';
  147.  
  148.             newfile = unixname(name, ext);
  149.             if (!match(newfile, newname)) {
  150.                 free(newfile);
  151.                 continue;
  152.             }
  153.             free(newfile);
  154.  
  155.             files++;
  156.             if (wide && files != 1) {
  157.                 if (!((files-1) % 5))
  158.                     putchar('\n');
  159.             }
  160.             date = convdate(dir->date[1], dir->date[0]);
  161.             time = convtime(dir->time[1], dir->time[0]);
  162.             size = dir->size[2]*0x10000L + dir->size[1]*0x100 + dir->size[0];
  163.                     /* is a subdirectory */
  164.             if (dir->attr & 0x10) {
  165.                 if (wide)
  166.                     printf("%-9.9s%-6.6s", name, ext);
  167.                 else
  168.                     printf("%8s %3s  <DIR>     %s  %s\n", name, ext, date, time);
  169.                 continue;
  170.             }
  171.             if (wide)
  172.                 printf("%-9.9s%-6.6s", name, ext);
  173.             else
  174.                 printf("%8s %3s %8d   %s  %s\n", name, ext, size, date, time);
  175.         }
  176.         if (argc > 2)
  177.             putchar('\n');
  178.  
  179.         free(filename);
  180.         free(pathname);
  181.     }
  182.  
  183.     blocks = getfree() * MSECSIZ;
  184.     if (!files)
  185.         printf("File \"%s\" not found\n", newname);
  186.     else
  187.         printf("     %3d File(s)     %6ld bytes free\n", files, blocks);
  188.     close(fd);
  189.     exit(0);
  190. }
  191.  
  192. /*
  193.  * Get the amount of free space on the diskette
  194.  */
  195.  
  196. int
  197. getfree()
  198. {
  199.     register int i, total;
  200.  
  201.     total = 0;
  202.     for (i=2; i<num_clus+2; i++) {
  203.                     /* if getfat returns zero */
  204.         if (!getfat(i))
  205.             total += clus_size;
  206.     }
  207.     return(total);
  208. }
  209.