home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / NEWLL.ZIP / NEWLL.C < prev    next >
Text File  |  1990-05-18  |  5KB  |  244 lines

  1.  
  2. #define INCL_DOS
  3.  
  4. #include <os2.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8.  
  9.  
  10.  
  11.  
  12. #define BUFLEN sizeof(buff)
  13. #define PATH_LEN 80
  14. #define ATTR 0x0037
  15. #define DIRECTORY 0x0010
  16. #define NO_MORE_FILES 18
  17.  
  18. #define LINES_PER_PAGE 25
  19.  
  20.  
  21.  
  22. USHORT cdecl main ( INT argc, CHAR *argv[] );
  23. UCHAR *lowercase ( UCHAR *str );
  24. VOID pinfo ( USHORT rc );
  25. VOID GetCommandLineOptions ( INT argc, CHAR *argv[] );
  26.  
  27.  
  28.  
  29. struct _FILEFINDBUF buff;
  30.  
  31. USHORT handle=0xFFFF;
  32. CHAR *filenames[2000];
  33. CHAR msg[80];
  34. CHAR CommandLineOptions[80];
  35. INT num_of_files = 0;
  36. BOOL done = FALSE;
  37. INT i,j;
  38. INT width = 5;
  39. INT option = 0;
  40. ULONG total_bytes = 0;
  41.  
  42. UCHAR *lowercase(UCHAR *str)
  43. {
  44.   while (*str) 
  45.   {
  46.     (*str) = tolower( (*str) );
  47.     str++;
  48.   }
  49. }
  50.  
  51.  
  52. VOID get_commandline_options(INT start, INT argc, CHAR *argv[])
  53. {
  54.   INT i,j,k;
  55.  
  56.   strcpy(CommandLineOptions," ");
  57.   k = 1;
  58.   if (argc < start) return;
  59.   
  60.   for (i=start; i < argc; i++)
  61.     for (j=0; j <= strlen( (CHAR *) argv[i][0] ); j++)
  62.       if ((argv[i][j] == '-') && (j+1 <= strlen((CHAR *) argv[i][0])))
  63.       {
  64.         CommandLineOptions[k++] = argv[i][j+1];
  65.         CommandLineOptions[k] = '\0';
  66.       }
  67. }
  68.  
  69. INT FileCompare ( CHAR ** filename1, CHAR ** filename2 )
  70. {
  71.   return ( strncmp ( *filename1, *filename2, 13 ) );
  72. }
  73.  
  74. USHORT cdecl main ( INT argc, CHAR *argv[] )
  75.  
  76. {
  77.   CHAR path[PATH_LEN];
  78.   USHORT SearchCount = 1;
  79.   INT rc;
  80.  
  81.  
  82.   if (argv[1][0] == '-')
  83.     get_commandline_options(1, argc, argv );
  84.   else
  85.     get_commandline_options(2, argc, argv );
  86.  
  87.  
  88.   printf("Dual Mode Directory Lister (c) 1990 By Mark \n");
  89.  
  90.   lowercase(CommandLineOptions);
  91.   if (strstr(CommandLineOptions,"h") != 0 )
  92.   {
  93.     printf("\n CommandLine Options \n");
  94.     printf(" -s Display File Sizes \n");
  95.     printf(" -h Show Commandline Switches \n");
  96.     return ( FALSE );
  97.   }
  98.   
  99.   if (strstr(CommandLineOptions,"s") != 0 )
  100.   {
  101.     option = 1; width = 3;
  102.   }
  103.   if (strstr(CommandLineOptions,"t") != 0)
  104.   {
  105.     option = 2; width = 2;
  106.   }
  107.  
  108.  
  109.   if ( argc < 2 )
  110.     strcpy ( path , "*.*" );
  111.   else if (argv[1][0] == '-')
  112.     strcpy ( path, "*.*" );
  113.   else if ( strcmp (argv[1], "\\") == 0)
  114.   {
  115.     strcpy ( path, argv[1] );
  116.     strcat ( path, "*.*");
  117.   }
  118.   else if ( strcspn (argv[1],"*?.") < strlen(argv[1] ))
  119.     strcpy ( path, argv[1] );
  120.   else 
  121.   {
  122.     strcpy (path, argv[1]);
  123. /*    strcat(path,"\\*.*"); */
  124.     if (argv[1][0] == ':' && (argv[1][1] == ' ' || argv[1][1] == '\0'))
  125.       strcat(path,"\\*.*");
  126.     else
  127.       strcat(path,"*.*");
  128.   }
  129.   if (_osmode)
  130.     printf("OS/2 Protected Mode ");
  131.   else
  132.     printf("DOS Real Mode ");
  133.   
  134.   printf("[%s] \n",path );
  135.  
  136.   rc = DosFindFirst ( path, &handle, ATTR, &buff,
  137.                       BUFLEN, &SearchCount, 0L );
  138.  
  139.   pinfo ( rc );
  140.  
  141.   while ( !done ) 
  142.   {
  143.     rc = DosFindNext ( handle, &buff, BUFLEN, &SearchCount );
  144.     pinfo ( rc );
  145.   }
  146.  
  147.   qsort ( filenames, num_of_files, sizeof ( char *), FileCompare );
  148.  
  149.   for (i= 0; i < num_of_files; i += width)
  150.   {
  151.     strcpy(msg,filenames[i]);
  152.      j = i+1;
  153.     while (j < num_of_files && j < i+width)
  154.     {
  155.       strcat(msg," "); 
  156.       strcat(msg,filenames[j++]);
  157.     }
  158.     printf("%s\n",msg);
  159.   }
  160.  
  161.   
  162.   if (num_of_files > 0)
  163.     printf("\n %i File(s) , %lu Bytes \n",num_of_files,total_bytes);
  164.   else
  165.     printf("\n No Files were Found! \n");
  166.  
  167.   for (i=0; i < num_of_files; i++)
  168.     free ( filenames[i]);
  169.  
  170.   return ( FALSE );
  171.  
  172. }
  173.  
  174. VOID pinfo ( USHORT rc )
  175. {
  176.   if ( rc )
  177.   {
  178.     if ( rc == NO_MORE_FILES )
  179.     {
  180.       DosFindClose ( handle ) ; 
  181.       done = TRUE;
  182.       return;
  183.     }
  184.     else
  185.     {
  186.       printf(" \n Invalid Specification !\n");
  187.       printf(" Task Aborted Error Code = %i\n",rc);
  188.       num_of_files = -1;
  189.       done = TRUE;
  190.       return;
  191.     }
  192.  
  193.   }
  194.   else
  195.   {
  196.     if ( buff.attrFile & DIRECTORY )
  197.     { 
  198.       switch ( option )
  199.       {
  200.         case 0: sprintf(msg,"/%-14s",buff.achName); break;
  201.         case 1: sprintf(msg,"%-14s%-6s  ",buff.achName," <dir>" );break;
  202.         case 2: sprintf(msg,"%-14s%-6s %2i/%02i/%04i %02i:%02i",
  203.                         buff.achName," <dir>",
  204.                         buff.fdateLastWrite.month,
  205.                         buff.fdateLastWrite.day,
  206.                         buff.fdateLastWrite.year+1980,
  207.                         buff.ftimeLastWrite.hours,
  208.                         buff.ftimeLastWrite.minutes); break;
  209.       }
  210.     }
  211.     else
  212.     {
  213.       switch ( option )
  214.       { 
  215.         case 0: sprintf(msg,"%-14s ",buff.achName); break;
  216.         case 1: sprintf(msg,"%-14s%6lu  ",buff.achName,buff.cbFile); break;
  217.         case 2: sprintf(msg,"%-14s%6lu %2i/%02i/%04i %02i:%02i",
  218.                 buff.achName,buff.cbFile,
  219.                 buff.fdateLastWrite.month,
  220.                 buff.fdateLastWrite.day,
  221.                 buff.fdateLastWrite.year+1980,
  222.                 buff.ftimeLastWrite.hours,
  223.                 buff.ftimeLastWrite.minutes); break;
  224.   
  225.       }
  226.     }
  227.  
  228.     filenames[num_of_files] = (char *) malloc(sizeof(msg));
  229.     if (filenames[num_of_files] == NULL)
  230.     {
  231.       printf("*** Memory Error ***\n"); exit(1);
  232.     }
  233.     lowercase(msg);
  234.     strcpy(filenames[num_of_files],msg);
  235.     num_of_files++;
  236.     total_bytes += buff.cbFile;
  237.   }
  238. }
  239.  
  240.  
  241.     
  242.  
  243. 
  244.