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 >
Wrap
Text File
|
1995-04-03
|
6KB
|
164 lines
/* dirread.c function for DISKMASTER.C */
/* copyright (c) 1995 by Bob Devries */
/* email: bdevries@gil.ipswichcity.qld.gov.au */
/* dirread.c takes a pathname pointer, opens it as a directory */
/* in read mode, reads its size, mallocs enough memory to store */
/* the filenames (32 per entry), the attribute (and a TAG flag) */
/* and file descriptors (128 bytes per entry). */
/* It treats a malloc error as fatal, and exits. If it cannot */
/* read a directory entry (file permission?) it ignores it. */
/* It will return the number of files (including directories it */
/* found, and return 0 if it could not open the directory, or */
/* if it could not open the parent directory, neither of which */
/* should ever happen. */
#include <stdio.h>
#include <direct.h>
#include <modes.h>
#include <errno.h>
#include <strings.h>
#include "diskmaster.h"
char *buffer; /* filename buffer */
char **nameptr; /* array of pointers to names in buffer */
short *attrptr; /* array of (short) for attr and tag flag */
char *fildesbuf; /* FD buffer */
char **fildesptr; /* array of pointers to FD's in fildesbuf */
int
dirread(path)
char *path;
{
char *tmpptr;
char *tmpfdptr;
char tempname[32];
struct fildes fdes, *desptr;
struct dirent dir, *dirptr;
int dcnt, fsize, newpath;
FILE *dirp, *fopen();
char *malloc();
dirptr = &dir;
desptr = &fdes;
dcnt = 0;
if((dirp = fopen(path,"d")) == NULL) {
open_err(path,errno); /* should never happen */
return(FAIL); /* pretend there's no files */
}
gotoxy(1,24);
cleol();
printf(" Reading directory entries ...");
fsize = _gs_size(fileno(dirp));
if(fsize == FAIL) {
if(errno == 216) {
fsize = 128 * 32;
} else {
return(FAIL);
}
}
buffer = malloc(fsize);
if(buffer == NULL) {
gotoxy(1,24);
cleanup();
exit(_errmsg(errno,"Can't allocate enough memory.\n"));
}
tmpptr = buffer;
attrptr = (short *) calloc((unsigned)fsize/32-1, sizeof(short));
if(attrptr == (short *) 0) {
gotoxy(1,24);
cleanup();
exit(_errmsg(errno,"Can't allocate enough memory\n"));
}
nameptr = (char **) calloc((unsigned)fsize/32-1, sizeof(char *));
if(nameptr == (char **) 0) {
gotoxy(1,24);
cleanup();
exit(_errmsg(errno,"Can't allocate enough memory\n"));
}
fildesbuf = malloc((fsize/32-1)*(sizeof(fdes)));
if(fildesbuf == NULL) {
gotoxy(1,24);
cleanup();
exit(_errmsg(errno,"Can't allocate enough memory\n"));
}
fildesptr = (char **) calloc((unsigned)fsize/32-1, sizeof(char *));
if(fildesptr == (char **) 0) {
gotoxy(1,24);
cleanup();
exit(_errmsg(errno,"Can't allocate enough memory\n"));
}
tmpfdptr = fildesbuf;
fread(dirptr,sizeof(dir),1,dirp); /* read past "." entry */
fread(dirptr,sizeof(dir),1,dirp);
strcpy(buffer,"==PARENT==");
if((newpath = open("..",S_IFDIR+S_IREAD)) == -1) {
open_err("PARENT DIRECTORY",errno); /* should never happen */
commands();
return(0); /* pretend there's no files */
}
_gs_gfd(newpath,desptr,sizeof(fdes));
_strass(fildesbuf,desptr,sizeof(fdes));
fildesptr[dcnt] = fildesbuf;
tmpfdptr += sizeof(fdes);
close(newpath);
attrptr[dcnt] = (((short)fdes.fd_att) & 0xFF) + UNTAG;
nameptr[dcnt] = buffer;
dcnt += 1;
tmpptr += 11;
while(fread(dirptr,sizeof(dir),1,dirp) != 0) {
if(dir.dir_name[0] == '\0') {
continue;
}
strHcpy(tempname,dir.dir_name);
if((newpath = open(tempname,S_IREAD)) == -1) {
if((newpath = open(tempname,S_IFDIR+S_IREAD))==-1) {
open_err(tempname,errno);
continue; /* skip it if we can't open it */
}
}
strcpy(tmpptr,tempname);
nameptr[dcnt] = tmpptr;
tmpptr += 29; /* to allow for rename to insert new name */
_gs_gfd(newpath,desptr,sizeof(fdes));
_strass(tmpfdptr,desptr,sizeof(fdes));
fildesptr[dcnt] = tmpfdptr;
tmpfdptr += sizeof(fdes);
close(newpath);
attrptr[dcnt] = (((short)fdes.fd_att) & 0xFF) + UNTAG;
dcnt++;
}
fclose(dirp);
gotoxy(1,24);
commands();
shellsort(dcnt);
return(dcnt - 1);
}
/* Replacement for strhcpy() since strhcpy() does not handle 0-terminated
strings.... BVDP, 95/3/18
*/
#asm
strHcpy
move.l a0,-(sp) save register
move.l d0,a0 argument 1 (destination) to reg
exg.l d1,a1 argument 2 (source) to reg
loop
move.b (a1)+,(a0)+ move 1 byte
beq.b done if 0-terminator, exit
bpl.b loop if 1..127 loop
clr.b (a0) add new terminator
andi.b #$7f,-(a0) fix high-bit byte
done
move.l (sp)+,a0 restore regs
exg d1,a1
rts go home
#endasm
/* EOF dirread.c */