home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 6 File
/
06-File.zip
/
HPFSLS.ZOO
/
lsdir.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-02-19
|
3KB
|
105 lines
/*
* Modulo di scansione delle directory.
* (C)1990 by redmax@alessia.dei.unipd.it (Massimo A. Santin)
* $Log: lsdir.c $
* Revision 1.2 91/05/11 00:11:47 Unknown
* Aggiunta del messaggio che riferisce che la versione DOS non e`
* ancora stata implementata.
*
* Revision 1.1 91/05/08 20:00:35 Unknown
* Initial revision
*
*/
#include "ls.h"
static char rcsid[] = "$Header: d:/usr/utils/ls/rcs/lsdir.c 1.2 91/05/11 00:11:47 Unknown Exp $";
/* Scandisce il contenuto della directory */
long ls_dir(char* name, unsigned attr, option_type opt, int level)
{
HDIR hdir;
FILEFINDBUF ffbuf;
USHORT csearch = 1;
char *buf, *p, *cwd;
int j;
long file_counter = 0;
option_type opt2;
BYTE mode;
DosGetMachineMode(&mode);
if (mode == MODE_PROTECTED)
hdir = HDIR_CREATE;
else
hdir = HDIR_SYSTEM;
if (DosFindFirst(name, &hdir, attr, &ffbuf, sizeof ffbuf, &csearch, 0L)) {
fprintf(stderr, "LS: error in open %s.\n", name);
exit(1);
}
do {
file_counter += print_name(&ffbuf, opt, level);
if (opt.recursive && ffbuf.attrFile & FILE_DIRECTORY) {
if (STRCMP(ffbuf.achName, ".") != 0 && STRCMP(ffbuf.achName, "..") != 0) {
cwd = (char *) malloc(CCHMAXPATHCOMP);
buf = (char *) malloc(CCHMAXPATHCOMP);
getcwd(cwd, CCHMAXPATHCOMP);
buf = (char*) malloc(CCHMAXPATHCOMP);
strcpy(buf, name);
p = &buf[strlen(name) - 1];
while (*p == '*' || *p == '?') {
*p = '\0';
--p;
}
strcat(buf, ffbuf.achName);
chdir(buf);
file_counter += ls_dir("*", attr, opt, level + 1);
free((void *) buf);
chdir(cwd);
free((void *) cwd);
}
}
}
while (DosFindNext(hdir, &ffbuf, sizeof ffbuf, &csearch) == 0);
DosFindClose(hdir);
return file_counter;
} /* ls_dir */
/* Prepara il nome per la scansione (anche ricorsiva) */
void ls(char *name, unsigned attr, option_type opt)
{
struct stat st;
char buffer[CCHMAXPATHCOMP];
int l;
char *fname;
long file_counter = 0;
strcpy(buffer, name);
if (buffer[l = (strlen(buffer) - 1)] == ':')
buffer[l + 1] = '.';
if ((stat(buffer, &st) != -1) && (st.st_mode & S_IFDIR)) {
ls_chdir(buffer);
if (opt.volume)
ls_volume();
file_counter = ls_dir("*", attr, opt, 0);
}
else {
fname = strrchr(buffer, '\\');
if (fname == NULL)
fname = strchr(buffer, '/');
if (fname == NULL)
fname = buffer;
else {
*fname++ = '\0';
ls_chdir(buffer);
}
if (opt.volume)
ls_volume();
file_counter = ls_dir(fname, attr, opt, 0);
}
printf("%ld files\n", file_counter);
} /* ls */