⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ff.c

📁 使用于OS/2下的小工具的C源程序,使用于OS/2下的小工具的C源程序
💻 C
字号:
/*
* FF.C - Find Files using the DosGetFirst, DosGetNext and
*	    DosFindClose-functions.
*
* PROGRAMMER:	    Martti Ylikoski
* CREATED:	    30.11.1990
* VERSION:	    1.0
*
*/
//#define FF
//#define DU
//#define DELQ
//#define SCAN
//#define CAT

//#undef MSDOS
#ifdef MSDOS
#include <dos.h>
#include <time.h>
#include <sys\types.h>
#include <sys\stat.h>
#define TRUE 1
#define FALSE 0
#endif
#ifndef MSDOS
#define INCL_DOS
#ifdef DU
#define INCL_DOSFILEMGR
#endif
#include <os2.h>
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <direct.h>
#include <errno.h>
#include <io.h>
#include <param.h>
#include <paramstd.h>
#include "dirscan.h"

typedef struct {
   char cmdopt[3] ;
   int flag ;
} TABLE_ENTRY ;
#ifdef MSDOS
TABLE_ENTRY findflags[6] = {
    "+H", _A_HIDDEN,
    "+D", _A_SUBDIR,
    "+S", _A_SYSTEM,
    "+N", _A_NORMAL,
    "+R", _A_RDONLY,
    "+A", _A_ARCH
} ;
#endif
#ifndef MSDOS
TABLE_ENTRY findflags[6] = {
    "+H", FILE_HIDDEN,
    "+D", FILE_DIRECTORY,
    "+S", FILE_SYSTEM,
    "+N", FILE_NORMAL,
    "+R", FILE_READONLY,
    "+A", FILE_ARCHIVED
} ;
#endif
extern unsigned long pflags ;

ParamEntry pentry[12] = {
     "NOD", &ParamSetNoDefault, 1,
     "P", &ParamSetPause, 1,
     "F", &ParamSetFold, 0,
     "V", &ParamSetVerbose, 1,
     "R", &ParamSetReport, 0,
#if defined ( FF )  || defined (SCAN) 
     "S", &ParamSetSubDirs, 0,
#endif
#if defined(DU) || defined(DELQ) || defined (CAT)
     "S", &ParamSetSubDirs, 1,
#endif
     "?", &ParamSetHelp, 1,
     "H", &ParamSetHelp, 1,
#if defined (DELQ) || defined (SCAN)
     "TEST", &ParamSetTest, 1,
#else
     "TEST", &ParamSetTest, 0,
#endif
     "Y", &ParamSetYes, 0,
     "N", &ParamSetTest, 0,
     "\0", NULL, 0
} ;

ParamBlock params = {
    "/-",   IGNORECASE | PRIORITY | NOTRIGGERSALLOWED ,
    pentry
} ;

static char *progname ;
static char *dirs[20] ;
static int dcount = 0 ;
static char *command[100] ;
static int ccount = 0 ;
static char *files[100] ;
static int ffound = FALSE ;
static int no_default = FALSE ;

#ifdef MSDOS
unsigned findattr = _A_NORMAL ;
#endif
#ifndef MSDOS
static USHORT findattr = FILE_NORMAL;
#endif
int Pageprintf(int rows, char *fmt, ...) ;
static void show_options( void ) ;
/* local prototypes */
int (*DirInitFn) (char *) ;
int (*DirExitFn)(char *) ;
int (* FileHandlerFn)(char *) ;
int DownCnt ;
#ifdef FF
static void FF_explain( void ) ;
static int FF_work( char *fname ) ;
static int FF_dirinit( char *fname ) ;
static int sc_flags = SC_AFTERSUBDIRS ;
#endif
#ifdef DU
static void DU_explain( void ) ;
static int DU_dirinit( char *buf ) ;
static int DU_direxit( char *buf ) ;
static DU_work( char *fname ) ;
static long consumed_space ;
static long total_consumed_space ;
static long filescnt ;
static long total_files ;
static int sc_flags = SC_BEFORESUBDIRS ;
char *StrInsNumDelim( char *str, int strlength, int numdelim) ;
#endif
#ifdef DELQ
static void DELQ_explain( void ) ;
static int DELQ_work( char *fname ) ;
static int sc_flags = SC_AFTERSUBDIRS ;
#endif
#ifdef SCAN
static void SCAN_explain( void ) ;
static int SCAN_work( char *fname ) ;
static int sc_flags = SC_AFTERSUBDIRS ;
#endif
#ifdef CAT
static void CAT_explain( void ) ;
static int CAT_work( char *fname ) ;
static int sc_flags = SC_AFTERSUBDIRS ;
#endif


int StrHasWildChars( char *str ) ;

int main(int argc, char *argv[])
{
int i, j, k, fcount ;
char *p ;
int ret ;
#ifdef MSDOS
char curdir[80] ;
unsigned curdrive, numdrives ;
#endif
#ifdef DU
char p1[17], p2[17] ;
#endif

   progname = argv[0] ;

   ParamHandle(&params, &argc, argv) ;

   fcount = 0 ;
   ccount = 0 ;

   if (pflags & PA_HELP)
   {
#ifdef FF
   FF_explain() ;
   show_options() ;
#endif
#ifdef DU
   DU_explain() ;
   show_options() ;
#endif
#ifdef DELQ
   DELQ_explain() ;
   show_options() ;
#endif
#ifdef SCAN
   SCAN_explain() ;
   show_options() ;
#endif
#ifdef CAT
   CAT_explain() ;
   show_options() ;
#endif
      return( 1 ) ;
   }
#ifdef FF
   FF_explain() ;
   DirInitFn =	FF_dirinit ;
   DirExitFn = NULL ;
   FileHandlerFn = FF_work ;
   DownCnt = SC_RECURSIVE ;
#endif
#ifdef DU
   DU_explain() ;
   DirInitFn =	DU_dirinit ;
   DirExitFn = DU_direxit ;
   FileHandlerFn = DU_work ;
   total_consumed_space = 0 ;
   total_files = 0 ;
   if (pflags & PA_SUBDIRS)
      DownCnt = SC_RECURSIVE ;
   else
      DownCnt = SC_NONRECURSIVE ;
#endif
#ifdef DELQ
   DELQ_explain() ;
   if (pflags & PA_TEST)
      printf("TEST option set - no files will be affected \n") ;
   DirInitFn =	NULL ;
   DirExitFn = NULL	;
   FileHandlerFn = DELQ_work ;
   if (pflags & PA_SUBDIRS)
      DownCnt = SC_RECURSIVE ;
   else
      DownCnt = SC_NONRECURSIVE ;
#endif
#ifdef SCAN
   SCAN_explain() ;
   DirInitFn =	NULL ;
   DirExitFn = NULL ;	;
   FileHandlerFn = SCAN_work ;
   DownCnt = SC_RECURSIVE ;
   if (pflags & PA_TEST)
      printf("TEST option set - no files will be affected \n") ;
#endif
#ifdef CAT
   CAT_explain() ;
   DirInitFn =	NULL ;
   DirExitFn = NULL ;	;
   FileHandlerFn = CAT_work ;
   if (pflags & PA_SUBDIRS)
      DownCnt = SC_RECURSIVE ;
   else
      DownCnt = SC_NONRECURSIVE ;
#endif


#ifdef MSDOS
   curdrive = _getdrive() ;
   getcwd(curdir, (int) sizeof(curdir)) ;
#endif
   if (pflags & PA_NODEFAULT)
	 no_default = TRUE ;

   for(i = 1 ; i < argc ; i ++)
   {
      if (argv[i] != NULL && argv[i][0] == '+')
	 for (j = 0 ; j < (int) (sizeof(findflags)/sizeof(TABLE_ENTRY)) ; j++)
	 {
	    if (strcmpi(findflags[j].cmdopt, argv[i]) == 0)
	    {
	       findattr |= findflags[j].flag ;

	       if (strcmpi(argv[i], "+N") == 0)
		  no_default = FALSE ;

	       argv[i] = NULL ;
	       break ; /* out of inner for */
	    }
	 }
       else
       {
	  if (argv[i] != NULL && argv[i][0] == '-')
	  {
	     command[ccount++] = &argv[i][1] ;
	     argv[i] = NULL ;
	  }
       }
   }

   for(i = 1 ; i < argc ; i ++)
   {
      if (argv[i] != NULL)
      {
	  if (( p = strchr(argv[i], (int) '\\')) != NULL ||
	      ( p = strchr(argv[i], (int) ':')) != NULL ||
	      strcmp(argv[i], ".") == 0 || strcmp(argv[i], "..") == 0)
	  {
	     if (StrHasWildChars(argv[i]) == TRUE)
	     {
		printf("%s: directory and filename information must be separated [%s] \n", progname, argv[i]) ;
		printf("E.g. %s C:\\TEST *.txt and NOT %s C:\\TEST\\*.txt\n", progname, progname) ;
		continue ;
	     }
	     dirs[dcount++] = argv[i] ;
	  }
	  else
	  {
	     files[fcount++] = argv[i] ;
	  }
      }
   }

   if (fcount == 0)
   {
#if defined (DELQ)
      puts("No files found.") ;
      return( 0 )  ;
#else
      fcount = 1 ;
      files[0] = "*.*" ;
#endif
   }

   if (dcount != 0)
      for ( k = 0 ; k < dcount ; k++)
      {

	 if (strlen(dirs[k]) >=2 && dirs[k][1] == ':')
	 {
#ifdef MSDOS
	    if (( ret = _dos_setdrive( (unsigned)toupper((int)dirs[k][0]) -'A'+ 1, &numdrives)) != 0 )
#endif
#ifndef MSDOS
	    if (( ret = DosSelectDisk(toupper((int)dirs[k][0]) -'A'+ 1)) != 0 )
#endif
	    {
	       fprintf(stderr,"%s cannot change to disk %c\n", progname, dirs[k][0]) ;
	       fprintf(stderr, "bypassing disk\n") ;
	       continue ;
	    }
	    dirs[k] += 2 ;
	 }

	 if (strlen(dirs[k]) != 0)
	 {
	    if (chdir(dirs[k]) != 0)
	    {
	       if (access(dirs[k], 0) != 0)
	       {
		  fprintf(stderr,"%s: %s does not exist\n", progname, dirs[k]) ;
		  continue ;
	       }
	       FileHandlerFn(dirs[k]) ;
	       continue ;
	    }
	 }
#if defined( FF ) || defined ( SCAN )
 	 else
	    chdir("\\") ;
#endif
	 DirScan( fcount, &files[0], findattr, no_default, DirInitFn, DirExitFn, FileHandlerFn, DownCnt, sc_flags ) ;
      }
   else
   {
#if defined ( FF )  || defined ( SCAN )
      chdir("\\") ;
#endif
      DirScan( fcount, &files[0], findattr, no_default, DirInitFn, DirExitFn, FileHandlerFn, DownCnt, sc_flags ) ;
   }

#if defined ( FF )  || defined (SCAN)
   if (ffound == FALSE)
      puts("No files found.") ;
#endif
#ifdef DU
   sprintf(p1, "%15.15lu", total_consumed_space) ;
   sprintf(p2, "%15.15lu", total_files) ;
   printf("Grand Total %s bytes in %s files.\n", StrInsNumDelim(p1, strlen(p1), ','), StrInsNumDelim(p2, strlen(p2), ',') ) ;

#endif

#ifdef MSDOS
   _dos_setdrive(curdrive, &numdrives) ;
   chdir(curdir) ;
#endif
   return( 0 ) ;
}

#ifdef FF
static int FF_dirinit( char *fname )
{
   if (pflags & PA_VERBOSE)
      Pageprintf(23,"Changing to dir: %s\n", fname) ;

   return( 0 ) ;
}

static int FF_work( char *fname )
{
int k ;
char sysbuf[256], *p1, *p2 ;
   ffound = TRUE ;
   Pageprintf(23, "%s\n", fname) ;

   return( 0 ) ;
}



static void FF_explain( void )
{
   fputs("FF - File Finder Utility. Copyright (C) Martti Ylikoski, 1990, 1991. Version 1.0", stderr) ;
   fputs("USAGE: FF [/NOD|+H|+D|+S|+N|+R|+A] [/H | /?] [files(s)] \n", stderr) ;
}
#endif

#ifdef DU
static void DU_explain( void )
{
   fputs("DU - Disk usage. Copyright (C) Martti Ylikoski, 1991. Version 1.1", stderr) ;
   fputs("USAGE: DU [dirs | file(s)] [/NOD|/TEST|+H|+D|+S|+N|+R|+A]\n", stderr) ;
}

static int DU_dirinit( char *buf )
{
   printf("Statistics for %s\n\n", buf) ;
   consumed_space = 0L ;
   filescnt = 0L ;   
   return( 0 ) ;
}

static int DU_direxit( char *buf )
{
int	       status, i, j, k ;
char	       p1[12], p2[12] ;

   sprintf(p1, "%10.10lu", consumed_space) ;
   sprintf(p2, "%10.10lu", filescnt) ;   
   total_consumed_space += consumed_space ;
   total_files += filescnt ;

   
    printf("Total %s bytes in %s files.\n\n", StrInsNumDelim(p1, strlen(p1), ','),StrInsNumDelim(p2, strlen(p2), ',')  ) ;
   return( 0 ) ;
}
#ifdef MSDOS
static DU_work( char *fname )
{
struct stat filestat ;
struct tm *times ;
char *p, p1[12], p2[12] ;
int i, j ;
unsigned attr ;

   filescnt ++ ;
   if (stat(fname, &filestat) != 0)
   {
      fprintf(stderr, "%s cannot access...\n", fname) ;
      return( 1 ) ;
   }

   if( ( p = strrchr(fname, (int) '\\' )) != NULL)
   {
      p++ ;
      printf("%15.15s", p) ;
   }
   else
      printf("%15.15s ", fname) ;

   times = gmtime (&filestat.st_atime) ;
   printf("  %2.2d.%2.2d.%4.4d", times->tm_mday, times->tm_mon, 1900 + times->tm_year) ;
   printf("  %2.2d:%2.2d  ", times->tm_hour, times->tm_min) ;
   consumed_space += filestat.st_size ;
   sprintf(p1, "%10.10lu", filestat.st_size) ;


   /* delete leading zeroes */

   printf("%10.10s  ", StrInsNumDelim(p1, strlen(p1), ',')) ;
   _dos_getfileattr(fname, &attr ) ;

   if (attr & _A_SUBDIR)
      fputs("D", stdout) ;
   else
      fputs("

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -