home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / dm14.lzh / dirread.c next >
Text File  |  1995-04-03  |  6KB  |  164 lines

  1. /* dirread.c function for DISKMASTER.C        */
  2. /* copyright (c) 1995 by Bob Devries          */
  3. /* email: bdevries@gil.ipswichcity.qld.gov.au */
  4.  
  5. /* dirread.c takes a pathname pointer, opens it as a directory  */
  6. /* in read mode, reads its size, mallocs enough memory to store */
  7. /* the filenames (32 per entry), the attribute (and a TAG flag) */
  8. /* and file descriptors (128 bytes per entry).                  */
  9. /* It treats a malloc error as fatal, and exits. If it cannot   */
  10. /* read a directory entry (file permission?) it ignores it.     */
  11. /* It will return the number of files (including directories it */
  12. /* found, and return 0 if it could not open the directory, or   */
  13. /* if it could not open the parent directory, neither of which  */
  14. /* should ever happen.                                          */
  15.  
  16. #include <stdio.h>
  17. #include <direct.h>
  18. #include <modes.h>
  19. #include <errno.h>
  20. #include <strings.h>
  21. #include "diskmaster.h"
  22.  
  23. char *buffer;          /* filename buffer                        */
  24. char **nameptr;        /* array of pointers to names in buffer   */
  25. short *attrptr;        /* array of (short) for attr and tag flag */
  26. char *fildesbuf;       /* FD buffer                              */
  27. char **fildesptr;      /* array of pointers to FD's in fildesbuf */
  28.  
  29. int
  30. dirread(path)
  31. char *path;
  32. {
  33.        char *tmpptr;
  34.        char *tmpfdptr;
  35.        char tempname[32];
  36.        struct fildes fdes, *desptr;
  37.        struct dirent dir, *dirptr;
  38.        int dcnt, fsize, newpath;
  39.        FILE *dirp, *fopen();
  40.        char *malloc();
  41.        dirptr = &dir;
  42.        desptr = &fdes;
  43.        dcnt = 0;
  44.        
  45.        if((dirp = fopen(path,"d")) == NULL) {
  46.               open_err(path,errno);    /* should never happen */
  47.               return(FAIL);       /* pretend there's no files */
  48.        }
  49.        
  50.        gotoxy(1,24);
  51.        cleol();
  52.        printf("     Reading directory entries ...");
  53.        fsize = _gs_size(fileno(dirp));
  54.        if(fsize == FAIL) {
  55.                  if(errno == 216) {
  56.                         fsize = 128 * 32;
  57.                  } else {
  58.                           return(FAIL);
  59.                  }
  60.        }
  61.        buffer = malloc(fsize);
  62.        if(buffer == NULL) {
  63.               gotoxy(1,24);
  64.               cleanup();
  65.               exit(_errmsg(errno,"Can't allocate enough memory.\n"));
  66.        }
  67.        tmpptr = buffer;
  68.        attrptr = (short *) calloc((unsigned)fsize/32-1, sizeof(short));
  69.        if(attrptr == (short *) 0) {
  70.               gotoxy(1,24);
  71.               cleanup();
  72.               exit(_errmsg(errno,"Can't allocate enough memory\n"));
  73.        }
  74.        nameptr = (char **) calloc((unsigned)fsize/32-1, sizeof(char *));
  75.        if(nameptr == (char **) 0) {
  76.               gotoxy(1,24);
  77.               cleanup();
  78.               exit(_errmsg(errno,"Can't allocate enough memory\n"));
  79.        }
  80.        fildesbuf = malloc((fsize/32-1)*(sizeof(fdes)));
  81.        if(fildesbuf == NULL) {
  82.               gotoxy(1,24);
  83.               cleanup();
  84.               exit(_errmsg(errno,"Can't allocate enough memory\n"));
  85.        }
  86.        fildesptr = (char **) calloc((unsigned)fsize/32-1, sizeof(char *));
  87.        if(fildesptr == (char **) 0) {
  88.               gotoxy(1,24);
  89.               cleanup();
  90.               exit(_errmsg(errno,"Can't allocate enough memory\n"));
  91.        }
  92.        tmpfdptr = fildesbuf;
  93.        fread(dirptr,sizeof(dir),1,dirp);       /* read past "." entry */
  94.        fread(dirptr,sizeof(dir),1,dirp);
  95.        strcpy(buffer,"==PARENT==");
  96.        if((newpath = open("..",S_IFDIR+S_IREAD)) == -1) {
  97.               open_err("PARENT DIRECTORY",errno);  /* should never happen */
  98.               commands();
  99.               return(0);       /* pretend there's no files */
  100.        }
  101.        _gs_gfd(newpath,desptr,sizeof(fdes));
  102.        _strass(fildesbuf,desptr,sizeof(fdes));
  103.        fildesptr[dcnt] = fildesbuf;
  104.        tmpfdptr += sizeof(fdes);
  105.        close(newpath);
  106.        attrptr[dcnt] = (((short)fdes.fd_att) & 0xFF) + UNTAG;
  107.        nameptr[dcnt] = buffer;
  108.        dcnt += 1;
  109.        tmpptr += 11;
  110.        while(fread(dirptr,sizeof(dir),1,dirp) != 0) {
  111.               if(dir.dir_name[0] == '\0') {
  112.                      continue;
  113.                }
  114.                strHcpy(tempname,dir.dir_name);
  115.                if((newpath = open(tempname,S_IREAD)) == -1) {
  116.                       if((newpath = open(tempname,S_IFDIR+S_IREAD))==-1) {
  117.                              open_err(tempname,errno);
  118.                              continue; /* skip it if we can't open it */
  119.                        }
  120.                }
  121.                strcpy(tmpptr,tempname);
  122.                nameptr[dcnt] = tmpptr;
  123.                tmpptr += 29;    /* to allow for rename to insert new name */
  124.                _gs_gfd(newpath,desptr,sizeof(fdes));
  125.                _strass(tmpfdptr,desptr,sizeof(fdes));
  126.                fildesptr[dcnt] = tmpfdptr;
  127.                tmpfdptr += sizeof(fdes);
  128.                close(newpath);
  129.                attrptr[dcnt] = (((short)fdes.fd_att) & 0xFF) + UNTAG;
  130.                dcnt++;
  131.        }
  132.        fclose(dirp);
  133.        gotoxy(1,24);
  134.        commands();
  135.        shellsort(dcnt);
  136.        return(dcnt - 1);
  137. }
  138.  
  139. /*    Replacement for strhcpy() since strhcpy() does not handle 0-terminated
  140.     strings.... BVDP, 95/3/18
  141. */
  142.  
  143. #asm
  144.  
  145. strHcpy
  146.  move.l a0,-(sp)    save register
  147.  move.l d0,a0        argument 1 (destination) to reg
  148.  exg.l d1,a1        argument 2 (source) to reg
  149.  
  150. loop
  151.  move.b (a1)+,(a0)+    move 1 byte
  152.  beq.b done            if 0-terminator, exit
  153.  bpl.b loop            if 1..127 loop
  154.  clr.b (a0)            add new terminator
  155.  andi.b #$7f,-(a0)    fix high-bit byte
  156. done
  157.  move.l (sp)+,a0    restore regs
  158.  exg d1,a1
  159.  rts                go home
  160.  
  161. #endasm
  162.  
  163. /* EOF dirread.c */
  164.