home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / VPR_DATA / DOGA / SOURCES / TCH2AVI.LZH / DIR.C < prev    next >
C/C++ Source or Header  |  1996-05-15  |  5KB  |  270 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. #define    MAXARGS        200
  6. #define    MAXARGBUF    4000
  7.  
  8. /*
  9.     proto -s dir.c > temp
  10. */
  11. static    int        _extendargs( int, char*[], int, char*[] );
  12. static    int        extendwild(char *, int, char*[]);
  13. static    char    *dfind( char*, int );
  14. static    char    *dnext( void );
  15.  
  16. static    char    argbuf[MAXARGBUF] ;
  17. static    char    *args[MAXARGS] ;
  18. static    char    *curargbuf ;
  19. static    int        nargs ;
  20.  
  21. int        extendargs( argcp, argvp, extn, ext )
  22. int        *argcp ;
  23. char    **argvp[] ;
  24. int        extn ;
  25. char    *ext[] ;
  26. {
  27. #if !defined(DJ) && !defined(UNIX)
  28.     int        stat ;
  29.     curargbuf = argbuf ;
  30.     nargs = 1 ;
  31.     args[0] = **argvp ;
  32.     stat = _extendargs( *argcp, *argvp, extn, ext );
  33.     *argcp = nargs ;
  34.     *argvp = args ;
  35.     return( stat );
  36. #else
  37.     int i = 1, nargc = 1, j;
  38.     int        stat ;
  39.     char *p;
  40.     curargbuf = argbuf ;
  41.     args[0] = **argvp ;
  42.     nargs = 1;
  43.     stat = _extendargs( *argcp, *argvp, extn, ext );
  44.     for (i = 1; i < nargs; ++i) {
  45.         p = strrchr( args[i], '.' );
  46.         if ( args[i][0] == '-' || args[i][0] == '/'
  47.          || p == NULL ) {
  48.             args[nargc++] = args[i];
  49.         } else {
  50.             for( j = 0 ; j < extn ; ++j ) {
  51.                 if ( strcmpi( p, ext[j] ) == 0 )
  52.                     break ;
  53.             }
  54.             if ( j < extn )
  55.             {
  56.                 args[nargc++] = args[i];
  57.             }
  58.         }
  59.     }
  60. #if 0
  61.     for (i = 0; i < nargc; ++i) {
  62.         printf("(%d)=%s\n", i, args[i]);
  63.     }
  64. #endif
  65.     *argcp = nargc ;
  66.     *argvp = args ;
  67.     return stat;
  68. #endif
  69. }
  70.  
  71. static    int        _extendargs( argc, argv, extn, ext )
  72. int        argc ;
  73. char    *argv[] ;
  74. int        extn ;
  75. char    *ext[] ;
  76. {
  77.     int        i;
  78.     char    *cargv;
  79.  
  80.     for( i = 1 ; i < argc ; ++i )
  81.     {
  82.         cargv = argv[i] ;
  83.         if ( nargs >= MAXARGS )
  84.             return( -1 );
  85.  
  86.         if ( cargv[0] == '@' || cargv[0] == '#')
  87.         {
  88.             FILE *fp;
  89.             char str[256], *sp, *ep;
  90.             if ((fp = fopen(cargv+1, "r")) != NULL) {
  91.                 while (fgets(str,255,fp) != NULL) {
  92.                     sp = str;
  93.                     while (*sp) {
  94.                         while (*sp && isspace(*sp)){
  95.                             sp++;
  96.                         }
  97.                         if (*sp == 0) {
  98.                             break;
  99.                         }
  100.                         for (ep = sp; *ep && !isspace(*ep); ++ep)
  101.                             ;
  102.                         if (*ep == 0) {
  103.                             ep[1] = 0;
  104.                         }
  105.                         *ep = 0;
  106.                         if ( strchr( sp, '*' ) != NULL || strchr( sp, '?' ) != NULL ) {
  107.                             extendwild(sp, extn, ext);
  108.                         } else {
  109.                             strcpy(curargbuf, sp);
  110.                             args[nargs++] = curargbuf ;
  111.                             curargbuf += strlen(sp)+1;
  112.                         }
  113.                         sp = ep+1;
  114.                     }
  115.                 }
  116.             }
  117.         }
  118.         else if ( cargv[0] == '-' || cargv[0] == '/' )
  119.         {
  120.             args[nargs++] = cargv ;
  121.         }
  122.         else
  123.         {
  124.             if ( strchr( cargv, '*' ) != NULL || strchr( cargv, '?' ) != NULL ) {
  125.                 extendwild(cargv, extn, ext);
  126.             } else {
  127.                 args[nargs++] = cargv ;
  128.             }
  129.         }
  130.     }
  131.     return( 0 );
  132. }
  133.  
  134. static int extendwild(cargv, extn, ext)
  135. char *cargv;
  136. int extn;
  137. char *ext[];
  138. {
  139.     int     j, len ;
  140.     char    *file, *p ;
  141.  
  142.     file = dfind( cargv, 0x20 );
  143.     p = strrchr( cargv, '\\' );
  144.     if ( p == NULL )
  145.         p = strchr( cargv, ':' );
  146.     if ( p != NULL )
  147.         *(p+1) = '\0' ;
  148.     else
  149.         cargv = "" ;
  150.  
  151.     while( file != NULL )
  152.     {
  153.         if ( nargs >= MAXARGS )
  154.             return( -1 );
  155.         p = strrchr( file, '.' );
  156.         if ( p != NULL )
  157.         {
  158.             for( j = 0 ; j < extn ; ++j )
  159.             {
  160.                 if ( strcmpi( p, ext[j] ) == 0 )
  161.                     break ;
  162.             }
  163.             if ( j < extn )
  164.             {
  165.                 len = strlen( cargv ) + strlen( file ) + 1 ;
  166.                 if ( curargbuf + len >= argbuf + MAXARGBUF )
  167.                     return( -1 );
  168.                 strcpy( curargbuf, cargv );
  169.                 strcat( curargbuf, file );
  170.                 args[nargs++] = curargbuf ;
  171.                 curargbuf += len ;
  172.             }
  173.         }
  174.         file = dnext();
  175.     }
  176.     return 0;
  177. }
  178.  
  179. /*
  180.  *        directry search
  181.  */
  182.  
  183. #ifdef    X68000
  184.     #include <doslib.h>
  185.     static    struct    FILBUF        buf ;
  186. #endif
  187. #ifdef    MSC
  188.     #include <dos.h>
  189.     static    union    REGS    in, out ;
  190.     static    struct    SREGS    seg ;
  191.     static    char    *dta ;
  192. #endif
  193. #if defined(DJ) || defined(__BORLANDC__)
  194.     #include <dir.h>
  195.     static struct ffblk buf;
  196. #endif
  197.  
  198. static    char    *dfind( name, attr )
  199. char    *name ;
  200. int        attr ;
  201. {
  202.  
  203. #ifdef    X68000
  204.  
  205.     if ( FILES( &buf, name, attr ) != 0 )
  206.         return( NULL );
  207.     return( buf.name );
  208.  
  209. #endif /* XC */
  210.  
  211. #ifdef    MSC
  212.  
  213.     segread( &seg );
  214.  
  215.     /*    DTA アドレスの読み出し  */
  216.     in.h.ah = 0x2F ;
  217.     intdosx( &in, &out, &seg );
  218.     FP_SEG( dta ) = seg.es ;
  219.     FP_OFF( dta ) = out.x.bx ;
  220.  
  221.     /*  ディレクトリサーチ  */
  222.     in.h.ah = 0x4E ;
  223.     in.x.dx = FP_OFF( name );
  224.     in.x.cx = attr ;
  225.     seg.ds = FP_SEG( name );
  226.     intdosx( &in, &out, &seg );
  227.  
  228.     if( out.x.cflag )
  229.         return( NULL );
  230.     return( dta+0x1E );
  231.  
  232. #endif /* MSC */
  233.  
  234. #if defined(DJ) || defined(__BORLANDC__)
  235.     if ( findfirst( name, &buf, attr ) != 0 )
  236.         return( NULL );
  237.     return( buf.ff_name );
  238. #endif
  239. }
  240.  
  241. static    char    *dnext()
  242. {
  243. #ifdef    X68000
  244.  
  245.     if ( NFILES( &buf ) != 0 )
  246.         return( NULL );
  247.     return( buf.name );
  248.  
  249. #endif /* XC */
  250.  
  251. #ifdef    MSC
  252.  
  253.     in.h.ah = 0x4F ;
  254.     intdos( &in, &out );
  255.  
  256.     if( out.x.cflag )
  257.         return( NULL );
  258.     return( dta+0x1E );
  259.  
  260. #endif /* MSC */
  261.  
  262. #if defined(DJ) || defined(__BORLANDC__)
  263.     if (findnext(&buf) != 0)
  264.         return NULL;
  265.     return buf.ff_name;
  266. #endif
  267.  
  268. }
  269.  
  270.