include define SZ_DIRSTRUCT 4 define DIR_NEXT Memi[$1] define DIR_NFILE Memi[$1+1] define DIR_OFFPTR Memi[$1+2] define DIR_BUFPTR Memi[$1+3] define DIR_OFFSET Memi[DIR_OFFPTR($1)+($2)-1] define DIR_FNAME Memc[DIR_BUFPTR($1)+DIR_OFFSET($1,$2)-1] define DEF_MAXFILE 64 define DEF_MAXBUFFER 4096 .help dirlist .nf___________________________________________________________________________ DIRLIST -- Retrieve all non-hidden files in a directory in sorted order B.Simon 01-May-1989 Original The three procedures in this file allow a program to retrieve all the non-hidden files in a directory in sorted order. The first procedure, opndirlist(), creates a data structure containing all the file names sorted in ascending order. The second procedure, getdirlist(), retrieves the file names one at a time. The last procedure, clsdirlist(), releases the data structure which holds the file names. .endhelp_____________________________________________________________________ # OPNDIRLIST -- Open a sorted list of file names in a directory pointer procedure opndirlist (dir) char dir[ARB] # i: Directory name #-- int fd, maxfile, maxbuffer, nextoff, nextbuf, nc pointer d int diropen(), getline() begin # Return a null pointer if directory could not be opened iferr { fd = diropen (dir, SKIP_HIDDEN_FILES) } then { return (NULL) } # Allocate and initialize directory list structure maxfile = DEF_MAXFILE maxbuffer = DEF_MAXBUFFER call malloc (d, SZ_DIRSTRUCT, TY_INT) call malloc (DIR_OFFPTR(d), maxfile, TY_INT) call malloc (DIR_BUFPTR(d), maxbuffer, TY_CHAR) DIR_NEXT(d) = 1 DIR_NFILE(d) = 0 # Read file names from directory nextoff = 1 nextbuf = DIR_BUFPTR(d) repeat { nc = getline (fd, Memc[nextbuf]) if (nc > 1) { DIR_NFILE(d) = DIR_NFILE(d) + 1 DIR_OFFSET(d, DIR_NFILE(d)) = nextoff nextoff = nextoff + nc nextbuf = nextbuf + nc Memc[nextbuf-1] = EOS # Allocate more memory if out of room if (DIR_NFILE(d) == maxfile) { maxfile = 2 * maxfile call realloc (DIR_OFFPTR(d), maxfile, TY_INT) } if ((maxbuffer - nextoff) < SZ_FNAME) { maxbuffer = 2 * maxbuffer call realloc (DIR_BUFPTR(d), maxbuffer, TY_CHAR) nextbuf = DIR_BUFPTR(d) + nextoff - 1 } } } until (nc == EOF) # Close directory and sort file names call close (fd) call strsrt (DIR_OFFSET(d,1), DIR_FNAME(d,1), DIR_NFILE(d)) return (d) end # GETDIRLIST -- Get next file name from directory list int procedure getdirlist (d, fname, maxch) pointer d # i: Pointer to directory list structure char fname[ARB] # o: File name int maxch # i: Declared length of file name #-- int nc int gstrcpy() begin if (d == NULL) { return (EOF) } else if (DIR_NEXT(d) > DIR_NFILE(d)) { return (EOF) } else { nc = gstrcpy (DIR_FNAME(d,DIR_NEXT(d)), fname, maxch) DIR_NEXT(d) = DIR_NEXT(d) + 1 } return (nc) end # CLSDIRLIST -- Close the directory list structure procedure clsdirlist (d) pointer d # io: Pointer to directory list structure #-- begin if (d == NULL) return if (DIR_OFFPTR(d) != NULL) { call mfree (DIR_OFFPTR(d), TY_INT) DIR_OFFPTR(d) = NULL } if (DIR_BUFPTR(d) != NULL) { call mfree (DIR_BUFPTR(d), TY_CHAR) DIR_BUFPTR(d) = NULL } call mfree (d, TY_INT) d = NULL end