home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 6 File
/
06-File.zip
/
FBE11.ZIP
/
FE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-09-06
|
12KB
|
446 lines
//---------------------------------------------------------------------------
//
// Copyright (C) 1990, Dave Peckham
//
//
// Name: FE.EXE
//
// Purpose: Show files in current directory by file extension
//
// Parameters: -h show help screen
//
// Description: FE lists files in the current directory by their file
// extension. Extensions and files within each extension
// are sorted alphabetically.
//
// Please refer to FE.DOC or FE.WP for more information.
//
// Filename: FE.C - Microsoft C 5.1, Small Model
//
//----------------------------- Current Revision ----------------------------
//
// Revision: 1.1
//
// Date: 27 Aug 1990
//
// Author: Dave Peckham
//
//----------------------------- Revision History ----------------------------
//
// Rev 1.0 27 Aug 1990 dhp
// Initial revision. Please distribute freely.
//
//---------------------------------------------------------------------------
#define INCL_DOSPROCESS
#define INCL_DOSSEMAPHORES
#define INCL_DOSQUEUES
#define INCL_DOSFILEMGR
#define INCL_DOSMEMMGR
#define INCL_DOSMISC
#define INCL_KBD
#define INCL_VIO
#include <stdio.h>
#include <stdlib.h>
#include <os2.h>
#include <fcntl.h>
#include <ctype.h>
#include <string.h>
#include "getopt.h"
/* For now, limits are constants */
#define MAXPATHLEN 256 /* max length of path names */
#define MAXNAMELEN 14 /* max length of file name */
#define MAXEXTLEN 3 /* max length of file extension */
#define MAXEXTS 100 /* max file extensions in a directory */
#define MAXFILES 500 /* max files per extension */
/* Macro and string buffer for VioWrtTTY call */
#define Write(s) VioWrtTTY(s, strlen (s), 0)
char write_buf [80] ;
/* Globals */
static char *aExt [MAXEXTS] ; /* file extensions */
static USHORT usExt = 0 ;
static char *aFile [MAXFILES] ; /* files */
static USHORT usFile = 0 ;
static USHORT usFileTotal = 0 ;
static USHORT usSubDirs = 0 ;
static USHORT usColumns = 6 ;
static USHORT usTotals = 0 ;
/* Function prototypes */
int compare (char **arg1, char **arg2) ;
USHORT GetExtensions (void) ;
USHORT GetFiles (char *szExt) ;
void ShowHelp (void) ;
void CheckForKey (void) ;
//---------------------------------------------------------------------------
//
// Function: ShowHelp ()
//
// Syntax: void ShowHelp (void)
//
// Parameters: None
//
// Returns: Nothing
//
// Description: Displays information about this program
//
//---------------------------------------------------------------------------
void ShowHelp (void)
{
Write (" FE [-H] [-T] [-C#]\r\n\n") ;
Write ("Switches\r\n") ;
Write (" -H Show this help information\r\n") ;
Write (" -T Show file totals\r\n") ;
Write (" -C# Display files in # columns (1..255)\r\n\n") ;
Write ("Author\r\n") ;
Write (" Dave Peckham\r\n") ;
Write (" 6809 Prince Georges Avenue\r\n") ;
Write (" Takoma Park, MD 20912-4862\r\n") ;
}
//---------------------------------------------------------------------------
//
// Function: compare ()
//
// Syntax: int compare (char **arg1, char **arg2)
//
// Parameters: char **arg1 first string
// char **arg2 second string
//
// Returns: Zero if identical
// Negative if first string is less than second
// Positive if first string is greater than second
//
// Description: Comparison function used by qsort(). Compares
// two strings.
//
//---------------------------------------------------------------------------
int compare (char **arg1, char **arg2)
{
return (strncmp (*arg1, *arg2, max (strlen (*arg1), strlen (*arg2)))) ;
}
//---------------------------------------------------------------------------
//
// Function: CheckForKey ()
//
// Syntax: void CheckForKey (void)
//
// Parameters: None
//
// Returns: None
//
// Description: Checks for user input. If user pressed <Esc>, return
// exit immediately. Any other key pauses display.
//
//---------------------------------------------------------------------------
void CheckForKey (void)
{
KBDKEYINFO kbci ;
/* Was a key pressed? */
KbdCharIn (&kbci,
IO_NOWAIT,
0);
if (kbci.fbStatus == FINAL_CHAR_IN) {
if (kbci.chChar != 27) {
Write ("Paused: Press any key to continue...") ;
KbdFlushBuffer (0) ;
KbdCharIn (&kbci,
IO_WAIT,
0);
Write ("\r \r") ;
}
else
DosExit (EXIT_PROCESS, 1) ;
}
}
//---------------------------------------------------------------------------
//
// Function: GetExtensions ()
//
// Syntax: USHORT GetExtensions (void)
//
// Parameters: None
//
// Returns: 1 if successful, 0 otherwise
//
// Description: Finds all unique file extensions in the current
// directory.
//
//---------------------------------------------------------------------------
USHORT GetExtensions (void)
{
HDIR hdir = HDIR_CREATE ;
USHORT usSearchCount = 1 ;
FILEFINDBUF findbuf ;
USHORT found ;
char *buffer ;
USHORT result, i = 0 ;
if (DosFindFirst ("*.*", /* filename to search for */
&hdir, /* address of directory handle */
FILE_NORMAL, /* type of files to search for */
&findbuf, /* address of buffer */
sizeof (findbuf), /* size of buffer */
&usSearchCount, /* number of matching entries */
0L)) /* reserved */
Write ("No files in this directory\r\n") ;
else {
/* Create the unsorted list */
usExt = 0 ;
do
{
buffer = strchr (findbuf.achName, '.') ;
buffer++ ;
/* Do we have this extension already? */
found = 0 ;
for (i = 0 ; i < usExt ; i++) {
if (strncmp (buffer, aExt [i],
max (strlen (buffer), strlen (aExt [i]))) == 0)
found = 1 ;
}
if (!found) {
aExt [usExt] = (char *) calloc (1, MAXEXTLEN + 1) ;
if (aExt [usExt] == NULL) {
Write ("Not enough memory for file extensions\r\n") ;
DosExit (EXIT_PROCESS, 1) ;
}
else
strcpy (aExt [usExt++], buffer) ;
}
}
while (DosFindNext (hdir, /* handle of directory */
&findbuf, /* address of buffer */
sizeof (findbuf), /* length of buffer */
&usSearchCount) /* number of files to find */
== 0) ; /* while no error */
/* Sort the list of file extensions */
qsort ((void *) aExt, (size_t) usExt, (size_t) sizeof (char *), compare) ;
result = 1 ;
}
return (result) ;
}
//---------------------------------------------------------------------------
//
// Function: GetFiles ()
//
// Parameters: char *szExt File extension to be processed
// USHORT usColumns Number of file columns in display
// USHORT usTotals If 1, show count of files
//
// Returns: 1 if successful, 0 otherwise
//
// Description: Finds all files in the current directory with
// extension szExt.
//
//---------------------------------------------------------------------------
USHORT GetFiles (char *szExt)
{
HDIR hdir = HDIR_CREATE ;
USHORT usSearchCount = 1 ;
FILEFINDBUF findbuf ;
char buffer [MAXEXTLEN + 3] ;
USHORT result, i = 0 ;
/* fe.c globals */
extern USHORT usColumns ;
extern USHORT usTotals ;
extern USHORT usSubDirs ;
extern USHORT usFile ;
extern USHORT usFileTotal ;
usFile = 0 ;
strcpy (buffer, "*.") ;
strcat (buffer, szExt) ;
if (DosFindFirst (buffer, /* filename to search for */
&hdir, /* address of directory handle */
FILE_NORMAL, /* type of files to search for */
&findbuf, /* address of buffer */
sizeof (findbuf), /* size of buffer */
&usSearchCount, /* number of matching entries */
0L)) { /* reserved */
sprintf (write_buf, "No .%-3s files found\n", szExt) ;
Write (write_buf) ;
}
else {
/* Create the unsorted list */
usFile = 0 ;
do
{
aFile [usFile] = (char *) calloc (1, MAXNAMELEN + 1) ;
if (aFile [usFile] == NULL) {
Write ("Not enough memory for file names\r\n") ;
DosExit (EXIT_PROCESS, 1) ;
}
else
strncpy (aFile [usFile++], findbuf.achName, strcspn (findbuf.achName, ".")) ;
}
while (DosFindNext (hdir, /* handle of directory */
&findbuf, /* address of buffer */
sizeof (findbuf), /* length of buffer */
&usSearchCount) /* number of files to find */
== 0) ; /* while no error */
/* Sort the list of files */
qsort ((void *) aFile, (size_t) usFile, (size_t) sizeof (char *), compare) ;
for (i = 0; i < usFile; i++) {
if ((i > 0) && !(i % usColumns)) {
/* indent first column of file names all but first row */
Write ("\n\r") ;
CheckForKey () ;
sprintf (write_buf, " %-8s ", strlwr (aFile [i])) ;
Write (write_buf) ;
}
else {
/* any other column on this line */
sprintf (write_buf, "%-8s ", strlwr (aFile [i])) ;
Write (write_buf) ;
}
free (aFile [i]) ;
}
if (usTotals) {
sprintf (write_buf, "\r\n %d ", usFile) ;
Write (write_buf) ;
if (usFile == 1)
Write ("file\r\n") ;
else
Write ("files\r\n") ;
}
else
Write ("\r\n") ;
usFileTotal += usFile ;
result = 1 ;
}
return (result) ;
}
void main (int argc, char **argv)
{
/* Machine mode */
// BYTE bMode;
/* Drive and path */
char pszPath [MAXPATHLEN] ;
USHORT cbPath = MAXPATHLEN ;
USHORT usDisk ;
ULONG ulDrives ;
USHORT i ;
char c ;
/* fe.c globals */
extern USHORT usColumns ;
extern USHORT usTotals ;
extern USHORT usSubDirs ;
extern USHORT usFileTotal ;
/* defined in getopt.c */
extern char *optarg ;
extern int opterr ;
opterr = 0 ;
Write ("FE-Files by Extension 1.1, Copyright (C) 1990, Dave Peckham\r\n\n") ;
/* parse command line options */
while ((c = (char) getopt (argc, argv, "c:hst")) != EOF) {
switch (c) {
case 'c':
usColumns = min (max (atoi (optarg), 1), 255) ;
break ;
case 's':
usSubDirs = 1 ;
break ;
case 't':
usTotals = 1 ;
break ;
case 'h':
default :
ShowHelp () ;
DosExit (EXIT_PROCESS, 1) ;
break ;
}
}
/* Find out if we're in protected or real mode */
// DosGetMachineMode (&bMode);
/* Get the current drive and path */
DosQCurDisk (&usDisk, &ulDrives); /* get current drive */
DosQCurDir (usDisk, /* drive number */
pszPath, /* buffer for directory path */
&cbPath) ; /* length of directory buffer */
sprintf (write_buf, " %c:\\%s\r\n\n", usDisk+'A'-1, pszPath) ;
Write (write_buf) ;
if (GetExtensions ()) {
/* Display the sorted list */
for (i = 0 ; i < usExt ; i++) {
sprintf (write_buf, " .%-3s files: ", aExt [i]) ;
Write (write_buf) ;
GetFiles (aExt [i]) ;
free (aExt [i]) ;
CheckForKey () ;
}
if (usTotals) {
sprintf (write_buf, "\r\n %3d file extension", usExt) ;
Write (write_buf) ;
if (usExt == 1)
Write (" found") ;
else
Write ("s found") ;
sprintf (write_buf, "\r\n %3d file", usFileTotal) ;
Write (write_buf) ;
if (usFileTotal == 1)
Write (" found") ;
else
Write ("s found") ;
Write ("\r\n") ;
}
}
DosExit (EXIT_PROCESS, 0) ;
}