home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / newc_dev / for16.lha / For.c < prev    next >
C/C++ Source or Header  |  1992-10-07  |  5KB  |  165 lines

  1. /***************************************************************************/
  2. /*                                       */
  3. /*            Itzz                           */
  4. /*                                       */
  5. /***************************************************************************/
  6. /*                                       */
  7. /* This magnificient piece of software is Freeware, so you do not need to  */
  8. /* send any money to me, but I would like to receive some good sourcecodes */
  9. /* especially for OS2.0 [ amiga, of course ;-) ]               */
  10. /*                                       */
  11. /***************************************************************************/
  12. /*                                       */
  13. /* This program makes it possible to use wildcards with all programs, even */
  14. /* if they do not support wildcards by themself.               */
  15. /* Please read the docs for more information and some examples !       */
  16. /*                                       */
  17. /***************************************************************************/
  18. /*                                       */
  19. /* This program was written, conceived and compiled by               */
  20. /*                                       */
  21. /*    Michael Illgner                            */
  22. /*    Theodorstr. 27                             */
  23. /*    W-4790 Paderborn                           */
  24. /*    Germany                                */
  25. /*    Tel.: 05251/26488 or 05251/60-2331                   */
  26. /*                                       */
  27. /*    email: fillg1@uni-paderborn.de                     */
  28. /*                                       */
  29. /***************************************************************************/
  30.  
  31. #include <exec/types.h>
  32. #include <exec/memory.h>
  33. #include <dos/dostags.h>
  34. #include <proto/dos.h>
  35. #include <proto/exec.h>
  36.  
  37. #include <string.h>
  38.  
  39. #define ExecFlag ArgArray[2]
  40. #define AllFlag  ArgArray[3]
  41.  
  42. char  Version[]  = "$VER: For 1.6 "__DATE__" "__TIME__; /* Version string */
  43. char  Template[] = "FILE,CMD,EXEC/S,ALL/S";             /* Template for command line */
  44. char  DefPath[]  = "#?";                            /* default wildcard, use all files */
  45. char  DefArg[]   = "%s";                            /* default command, filename only */
  46.  
  47. /* taglist for Dos.System(), I'm using a UserShell */
  48.  
  49. /* size of various buffers, should be enough for most cases. Be carefull, no checking !*/
  50. #define BUF_SIZE 255
  51.  
  52. /* no startup-code, no link-library */
  53. void __saveds main(void)
  54. {
  55.   struct Library *DOSBase;
  56.   
  57.   /* used for argument parsing, thanks to DOS 2.0 */
  58.   ULONG             ArgArray[4];
  59.   struct RDArgs     *rd;
  60.   struct AnchorPath  *ap;
  61.   BOOL            error;
  62.  
  63.   /* memory for command and pathname */
  64.   char            Buffer[BUF_SIZE+1], Pathname[BUF_SIZE];
  65.   /* flag %f, %b, %p etc */
  66.   BOOL            Special;
  67.   char            *s, *s1, *t;
  68.   
  69.   /* no startup, I've to open DOS myself ! */
  70.   if (DOSBase = OpenLibrary("dos.library", 37))
  71.     {
  72.       if (ap = AllocVec(sizeof(struct AnchorPath), MEMF_CLEAR))
  73.     {
  74.       /* setup the argument parser */
  75.       ArgArray[0] = (ULONG)DefPath;
  76.       ArgArray[1] = (ULONG)DefArg;
  77.       ArgArray[2] = FALSE;
  78.       ArgArray[3] = FALSE;
  79.    
  80.       rd = ReadArgs(Template, ArgArray, NULL);
  81.   
  82.       /* treat all matching files */
  83.       error = MatchFirst((UBYTE *)ArgArray[0], ap);
  84.       while (!(error || (SetSignal(0, 0) & SIGBREAKF_CTRL_C)))
  85.         {
  86.           if (ap->ap_Info.fib_DirEntryType > 0)
  87.         {
  88.           if (!(ap->ap_Flags & APF_DIDDIR) && AllFlag) ap->ap_Flags |= APF_DODIR;
  89.           ap->ap_Flags &= ~APF_DIDDIR;
  90.         }
  91.           else
  92.         {
  93.           /* build the command string, copy and insert */
  94.           s = (char *)ArgArray[1]; t = Buffer; Special = FALSE;
  95.           while (*s)
  96.             {
  97.               if (*s == '%') Special = TRUE;
  98.               else if (Special)
  99.             {
  100.               switch (*s)
  101.                 {
  102.                 case '%' : /* %% is a single % */
  103.                   *t++ = '%';
  104.                   break;
  105.                 case 'b' :
  106.                 case 'B' : /* insert the base filename, up to the first '.' */
  107.                   s1 = ap->ap_Info.fib_FileName;
  108.                   while ((*s1) && (*s1 != '.')) *t++ = *s1++;
  109.                   break;
  110.                 case 'f' :
  111.                 case 'F' : /* insert the whole filename including path */
  112.                   NameFromLock(ap->ap_Current->an_Lock, Pathname, sizeof(Pathname));
  113.                   AddPart(Pathname, ap->ap_Info.fib_FileName, sizeof(Pathname));
  114.                   s1 = Pathname;
  115.                   *t++ = '"';
  116.                   while (*s1) *t++ = *s1++;
  117.                   *t++ = '"';
  118.                   break;
  119.                 case 's' :
  120.                 case 'S' : /* insert the filename */
  121.                   s1 = ap->ap_Info.fib_FileName;
  122.                   while (*s1) *t++ = *s1++;
  123.                   break;
  124.                 case 'p' :
  125.                 case 'P' : /* insert the pathname. caution : no trailing '/' */
  126.                   NameFromLock(ap->ap_Current->an_Lock, Pathname, sizeof(Pathname));
  127.                   s1 = Pathname;
  128.                   while (*s1) *t++ = *s1++;
  129.                   break;
  130.                 }
  131.               Special = FALSE;
  132.             }
  133.               else *t++ = *s; /* simple normal character */
  134.               s++;
  135.             }
  136.           if (ExecFlag)
  137.             {
  138.               /* end string, execute it ! */
  139.               *t = '\0';
  140.               SystemTags(Buffer,
  141.                  SYS_UserShell, TRUE,
  142.                  TAG_END);
  143.             }
  144.           else
  145.             {
  146.               /* append newline, print it ! */
  147.               *t++ = '\n'; *t = '\0';
  148.               Write(Output(), Buffer, strlen(Buffer));
  149.             }
  150.         }
  151.           error = MatchNext(ap);
  152.         }
  153.       MatchEnd(ap);
  154.       
  155.       if (rd) FreeArgs(rd);
  156.       FreeVec(ap);
  157.     }
  158.       CloseLibrary(DOSBase);
  159.       /* I'm off ! */
  160.     }
  161. }
  162.  
  163.  
  164.  
  165.