home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cenvi23.zip
/
DIR.CMM
< prev
next >
Wrap
Text File
|
1996-02-12
|
12KB
|
422 lines
/*
* dir.cmm
*
* This .CMM file implements a DOS-like DIR command.
*/
#include "netware.lib"
#include "filename.lib"
usage()
{
printf("Use the DIR command to list the files and subdirectories.\n");
printf("Syntax:\n");
printf(" DIR [drive:][path][filename][/W or /F][/P][/A][/B][/O][/R][/S][/L]\n");
printf("where:\n");
printf(" drive:/path/filename Specifies directories and files to list.\n");
printf(" /W Displays directory listing horizontally.\n");
printf(" /F Displays fully-qualified file names.\n");
printf(" /P Pauses after each screen of information.\n");
printf(" /A Displays specified attributes.\n");
printf(" /B Displays file name and extension.\n");
printf(" /O Orders the display by specified fields.\n");
printf(" /S Displays all subdirectories.\n");
printf(" /L Displays directory info in lower-case.\n");
exit(EXIT_FAILURE);
}
/* ---------------------------------------------------------------------- */
height = ScreenSize().row;
width = ScreenSize().col;
attributes = 0x3f & ~( _A_HIDDEN | _A_SYSTEM );
must = 0;
ltr = "RHSEDA";
wide = FALSE;
pause = FALSE;
fully = FALSE;
bare = FALSE;
lower = FALSE;
recursive = FALSE;
dir[0] = "";
startedDir = FALSE;
dirfirst = 0; // 1 = true, -1 = last
sortflag = ' ';
sortminus = 0;
/* ---------------------------------------------------------------------- */
//
// We set the attributes we are looking for based on the given string
// which lists attribute bits.
//
set_attributes(a)
{
// The user will specify the attributes.
for( i=0;i<strlen(a);i++ )
{
if( (j = strchr(ltr,toupper(a[i])))==NULL )
{
printf("Illegal attribute %c\n",a[i]);
exit(EXIT_FAILURE);
}
attributes |= (1<<(j-ltr));
must |= (1<<(j-ltr));
}
}
//
// Examine and save the sort field.
//
set_sort(aa)
{
dirfirst = 0;
sortflag = 'N';
sortminus = 1;
a = strupr(aa);
while( a[0] )
{
minus = 0;
if( a[0]=='-' )
{
minus = 1;
a++;
}
if( a[0]=='G' )
{
dirfirst = minus?-1:1;
} else {
if( strchr("NEDS",a[0])==NULL )
{
printf("Sort by [N]ame, [E]xtension, [D]ate, or [S]ize only.\n");
exit(EXIT_FAILURE);
}
sortminus = minus?-1:1;
sortflag = a[0];
}
a++;
}
}
/* ---------------------------------------------------------------------- */
parse_command_line(argc,argv)
{
for( i=1;i<argc;i++ )
{
if( argv[i][0]=='-' || argv[i][0]=='/' )
{
switch( toupper(argv[i][1]) )
{
default: usage();
case 'W':
if( fully )
{
fully = FALSE;
printf("WARNING: /f param previously encountered overriden.\n");
}
wide = TRUE;
break;
case 'F':
if( wide )
{
wide = FALSE;
printf("WARNING: /w param previously encountered overriden.\n");
}
fully = TRUE;
break;
case 'P': pause = TRUE; break;
case 'B': bare = TRUE; break;
case 'A': set_attributes(argv[i]+2); break;
case 'O': set_sort(argv[i]+2); break;
case 'L': lower = TRUE; break;
case 'S': recursive = TRUE; break;
}
} else {
if( !startedDir )
{
dir[0] = argv[i]; startedDir = TRUE;
} else {
dir[GetArraySpan(dir)+1] = argv[i];
}
}
}
if( bare && wide ) bare = FALSE;
}
/* ---------------------------------------------------------------------- */
compare_directory(elem1,elem2)
{
if( dirfirst )
{
if( dirfirst && (elem1.attrib & _A_SUBDIR)!=(elem2.attrib & _A_SUBDIR) )
{
return dirfirst * ((elem1.attrib&_A_SUBDIR)?-1:1);
}
}
if( sortflag==' ' ) return 0;
switch( sortflag )
{
default:
printf("Internal error - illegal sorting character.\n"); exit(EXIT_FAILURE);
case 'N': return sortminus * stricmp(elem1.name,elem2.name);
case 'E': return sortminus *
stricmp(SplitFileName(elem1.name).ext,SplitFileName(elem2.name).ext);
case 'D':
ret = 0;
if( elem1.Write>elem2.Write ) ret = -1;
if( elem1.Write<elem2.Write ) ret = 1;
return sortminus * ret;
case 'S':
ret = 0;
if( elem1.size>elem2.size ) ret = -1;
if( elem1.size<elem2.size ) ret = 1;
return sortminus * ret;
}
}
sort_directory(dirarray)
{
if( dirarray && (dirfirst || sortflag!=' ') )
{
qsort(dirarray,"compare_directory");
}
}
/* ---------------------------------------------------------------------- */
//
// Stringize the number, inserting appropriate commas.
//
comma(value)
{
tmp = ""; SetArraySpan(tmp,32);
sprintf( tmp, "%d", value );
j = strlen( tmp );
len = i = j + j / 3;
ret[ i ] = '\0';
for( ; j >= 0; i--, j-- )
{
if( (len-i) % 4 == 0 && i > 0 && i != len )
ret[ i-- ] = ',';
ret[ i ] = tmp[ j ];
}
return( ret+i+1 );
}
get_directory(whatdir,files,bytes)
{
files = 0; bytes = 0;
lines = 0; column =0;
if( defined(_UNIX_) )
dirarray = Directory(whatdir,FALSE);
else
dirarray = Directory(whatdir,FALSE,attributes,must);
if( dirarray!=NULL )
{
sort_directory(dirarray);
if( !fully && !bare )
{
printf("\nDirectory of %s\n\n",whatdir); lines += 3;
}
// get the size of the directory
dirSize = GetArraySpan(dirarray);
for( i=0; i <= dirSize ;i++ )
{
if( !(dirarray[i].attrib&_A_SUBDIR) )
{
bytes += dirarray[i].size;
files ++;
}
filestruct = SplitFileName(dirarray[i].name);
sprintf(filename,"%s%s",filestruct.name,filestruct.ext);
if( lower )
{
strlwr(filename);
strlwr(dirarray[i].name);
}
// Bare takes precedence over fully
if( bare )
{
printf("%s\n",filename);
}
else if( fully )
{
printf("%s\n",dirarray[i].name);
}
else if( wide )
{
if( (column + 16) > width )
{
if(column!=width)
putchar('\n');
lines++;
column = 0;
}
if( dirarray[i].attrib&_A_SUBDIR )
{
sprintf(filename,"[%s]",filename);
}
// print the filename and account for it's width
printf("%-16s",filename);
column+=16;
}
else
{
printf("%-20s ",filename);
sprintf(datetime,"%s",ctime(dirarray[i].Write));
// strip the annoying newline.
datetime[strlen(datetime)-1] = '\0';
// Set the display Attributes "RHSEDA";
fileAttrib = dirarray[i].attrib;
attribs[0] = (fileAttrib & 0x01)? 'R' : ' ';
attribs[1] = (fileAttrib & 0x02)? 'H' : ' ';
attribs[2] = (fileAttrib & 0x04)? 'S' : ' ';
attribs[3] = (fileAttrib & 0x08)? 'E' : ' ';
attribs[4] = (fileAttrib & 0x10)? 'D' : ' ';
attribs[5] = (fileAttrib & 0x20)? 'A' : ' ';
if( defined(_NWNLM_) )
{
owner = ""; SetArraySpan(owner,256);
undefine(junk);
if( NLMLink("GetBinderyObjectName", dirarray[i].uid, owner, junk ) )
strcpy( owner, "FALSE owner.");
if( dirarray[i].attrib & _A_SUBDIR )
{
printf(" <DIR> [%s ] %s %s\n",attribs,datetime,owner);
} else {
printf("%7d [%s%c] %s %s\n",dirarray[i].size,
attribs,(dirarray[i].attrib & _A_TRANS)?'T':'_',
datetime,owner);
}
}
else
{
if( dirarray[i].attrib & _A_SUBDIR ) {
printf(" <DIR> [%s] %s\n",attribs,datetime);
} else {
printf("%7d [%s] %s\n",dirarray[i].size,attribs,datetime);
}
}
}
if(!wide) lines++;
if( pause && lines>=height-1 )
{
if( defined(_NWNLM_) )
{
if( NLMLink("PressEscapeToQuit") ) break;
} else {
printf("Press escape to quit or any other key to continue...");
fflush(stdout);
if( getch()=='\x1b' ) break;
printf("\r \r");
fflush(stdout);
}
lines = 0;
}
}
if( wide ) putchar('\n');
if( !bare && !fully && files )
{
printf("\n %s byte%s in %d file%s\n\n",
comma(bytes),(bytes==1)?"":"s",files,(files==1)?"":"s");
}
}
if( recursive )
{
namestruct = SplitFileName(whatdir);
if( defined(_UNIX_) )
{
sprintf(newdir,"%s*",namestruct.dir);
dirarray = Directory(newdir,FALSE);
} else {
sprintf(newdir,"%s*.*",namestruct.dir);
dirarray = Directory(newdir,FALSE,0x3f);
}
for( i=0;dirarray!=NULL && i<=GetArraySpan(dirarray);i++ )
{
if( dirarray[i].attrib&_A_SUBDIR )
{
sprintf(newdir,"%s%c%s%s",
newdir = dirarray[i].name,
strchr(namestruct.dir,'/')?'/':'\\',
namestruct.name,namestruct.ext);
get_directory(newdir,files2,bytes2);
bytes += bytes2;
files += files2;
}
}
}
}
main(argc,argv)
{
// First thing to do is parse the command line.
parse_command_line(argc,argv);
numfiles = 0; bytes = 0;
for( i=0;i<=GetArraySpan(dir);i++ )
{
if( dir[i][0]!='\0' )
{
files = Directory(dir[i]);
if( !strcmp(dir[i],".") || !stricmp(dir[i],"..") ||
(files && GetarraySpan(files)==0 &&
files[0].attrib & _A_SUBDIR && strpbrk(dir[i],"*?")==NULL)
)
strcat(dir[i],filename_separator);
}
s = strlen(dir[i]);
if( s && dir[i][s-1]==':' )
{
strcat(dir[i],filename_separator); s++;
}
if( s && (dir[i][s-1]==filename_separator[0]) ) strcat(dir[i],"*.*");
if( s==0 ) dir[i] = "*.*";
strcpy(full_path,dir[i]);
files1 = 0; bytes1 = 0;
get_directory(full_path,files1,bytes1);
numfiles += files1; bytes += bytes1;
}
if( (GetArraySpan(dir)>0 || recursive) && !bare && !fully && files > 0 )
{
printf("\nTotal files listed:\n %s byte%s in %d file%s\n\n",
comma(bytes),(bytes==1)?"":"s",numfiles,(numfiles==1)?"":"s");
}
}