1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
/*
** LSARY - A simple directory lister using a filename array
** A public domain C demo program by Bob Stout
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <dos.h>
/* For portability, make everything look like MSC 6 */
#if defined(__TURBOC__)
#include <dir.h>
#define _dos_findfirst(f,a,b) findfirst(f,b,a)
#define _dos_findnext(b) findnext(b)
#define find_t ffblk
#ifndef _A_SUBDIR
#define _A_SUBDIR FA_DIREC
#endif
#define attrib ff_attrib
#define name ff_name
#define size ff_fsize
#define wr_time ff_ftime
#define wr_date ff_fdate
#define _dos_getdiskfree getdfree
#define diskfree_t dfree
#define avail_clusters df_avail
#define sectors_per_cluster df_sclus
#define bytes_per_sector df_bsec
#else /* assume MSC/QC */
#include <errno.h>
#endif
#ifdef TRUE
#undef TRUE
#endif
#ifdef FALSE
#undef FALSE
#endif
#ifdef ERROR
#undef ERROR
#endif
enum LOGICAL {ERROR = -1, SUCCESS, FALSE = 0, TRUE};
#ifndef CAST
#define CAST(new_type,old_object) (*((new_type *)&(old_object)))
#endif
#define LAST_CHAR(s) (((char *)s)[strlen(s) - 1])
struct DirEntry {
char fname[FILENAME_MAX];
struct DirEntry *next;
} DirRoot = {"", NULL};
/*
** Read a directory into an array
*/
int ReaDirArray(char *path)
{
struct find_t ff;
char pattern[67];
struct DirEntry *base = &DirRoot;
strcpy(pattern, path);
if ('/' != LAST_CHAR(pattern) && '\\' != LAST_CHAR(pattern))
strcat(pattern, "\\");
strcat(pattern, "*.*");
if (SUCCESS == _dos_findfirst(pattern, 0xff, &ff)) do
{
struct DirEntry *node;
if (NULL == (node = malloc(sizeof(struct DirEntry))))
return ERROR;
base->next = node;
strcpy(base->fname, ff.name);
node->next = NULL;
*node->fname = '\0';
base = node;
} while (SUCCESS == _dos_findnext(&ff));
return SUCCESS;
}
/*
** Simple directory lister
*/
void main(int argc, char *argv[])
{
char *path;
if (2 > argc)
path = ".";
else path = argv[1];
if (ERROR == ReaDirArray(path))
printf("*** Could not read %s\n", path);
else
{
struct DirEntry *node = &DirRoot;
printf("Directory of %s\n\n", path);
while (node)
{
puts(node->fname);
node = node->next;
}
}
}
|