home *** CD-ROM | disk | FTP | other *** search
- /*
- * dir.cmm
- *
- * This .CMM file implements a DOS-like DIR command.
- */
-
- #include "Netware.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] = "";
- it = 1;
-
- 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( it )
- {
- dir[0] = argv[i]; it = 0;
- } 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;
-
- dirarray = Directory(whatdir,FALSE,attributes,must);
- sort_directory(dirarray);
-
- if( dirarray!=NULL && !fully && !bare )
- {
- printf("\nDirectory of %s\n\n",whatdir); lines += 3;
- }
-
- for( i=0;dirarray!=NULL && i<=GetArraySpan(dirarray);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);
- if( lower ) strlwr(dirarray[i].name);
-
- // Bare takes precedence over fully
- if( bare )
- {
- printf("%s\n",filename);
- goto scroll;
- }
-
- if( fully )
- {
- printf("%s\n",dirarray[i].name);
- goto scroll;
- }
-
- if( wide )
- {
- column+=16;
- if( column>width )
- {
- // if the column exactly matches, we are already on the next line
- if( column-16>width ) printf("\n");
- lines++; column = 16;
- }
-
- if( dirarray[i].attrib&_A_SUBDIR )
- {
- sprintf(filename,"[%s%s]",filestruct.name,filestruct.ext);
- if( lower ) strlwr(filename);
- }
- printf("%-16s",filename);
- goto maybe_pause;
- }
- printf("%-20s ",filename);
-
- ltr = "RHSEDA";
-
- sprintf(datetime,"%s",ctime(dirarray[i].Write));
- // strip the annoying newline.
- datetime[strlen(datetime)-1] = '\0';
-
- for( j=0;j<6;j++ )
- attribs[j] = (dirarray[i].attrib & (1<<j))? ltr[j] : ' ';
-
- 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 )
- {
- if( defined(_NWNLM_) )
- {
- printf(" <DIR> [%s ] %s %s",attribs,datetime,owner);
- } else {
- printf(" <DIR> [%s] %s",attribs,datetime);
- }
- } else {
- if( defined(_NWNLM_) )
- {
- printf("%7d [%s%c] %s %s",dirarray[i].size,
- attribs,(dirarray[i].attrib & _A_TRANS)?'T':'_',
- datetime,owner);
- } else {
- printf("%7d [%s] %s",dirarray[i].size,attribs,datetime);
- }
- }
- printf("\n");
-
- scroll:
- ++lines;
- maybe_pause:
- 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 ) printf("\n");
-
- if( !bare && !fully && files )
- {
- printf("\n %s byte%s in %d file%s\n",
- comma(bytes),(bytes==1)?"":"s",files,(files==1)?"":"s");
- }
- if( recursive )
- {
- namestruct = SplitFileName(whatdir);
- 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);
-
- files = 0; bytes = 0;
-
- for( i=0;i<=GetArraySpan(dir);i++ )
- {
- files = Directory(dir[i]);
- if( files && GetarraySpan(files)==0 && (files[0].attrib & _A_SUBDIR) )
- strcat(dir[i],defined(_NWNLM_)?"/":"\\");
-
- s = strlen(dir[i]);
- if( s && dir[i][s-1]==':' )
- {
- strcat(dir[i],defined(_NWNLM_)?"/":"\\"); s++;
- }
- if( s && (dir[i][s-1]=='/' || dir[i][s-1]=='\\') ) strcat(dir[i],"*.*");
- if( s==0 ) dir[i] = "*.*";
-
- strcpy(full_path,dir[i]);
-
- files1 = 0; bytes1 = 0;
- get_directory(full_path,files1,bytes1);
- files += 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",
- comma(bytes),(bytes==1)?"":"s",files,(files==1)?"":"s");
- }
-