home *** CD-ROM | disk | FTP | other *** search
/ AMIGA PD 1 / AMIGA-PD-1.iso / Programme_zum_Heft / Utilities / Workshops / XFH / xScan / xScan.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-06  |  4.6 KB  |  223 lines

  1.  
  2. #include <exec/alerts.h>
  3. #include <exec/memory.h>
  4. #include <exec/execbase.h>
  5.  
  6. #include <dos/dosextens.h>
  7. #include <dos/dosasl.h>
  8. #include <dos/rdargs.h>
  9.  
  10. #include <libraries/xpk.h>
  11.  
  12. #pragma tagcall XpkBase XpkExamineTags 24 9802
  13.  
  14. #define _USEOLDEXEC_ 1
  15.  
  16. #include <proto/dos.h>
  17. #include <proto/exec.h>
  18.  
  19. #include <string.h>
  20.  
  21. #define XFH_ID "XFH A"
  22.  
  23. void PrintF(char *,...);
  24. void __regargs xScan(struct DosLibrary *,struct Library *,
  25.                      struct FileInfoBlock *,LONG);
  26.  
  27. struct xScanArgs
  28.  {
  29.   char **Files;
  30.   LONG Remove,All;
  31.  };
  32.  
  33. char *VersionString = "$VER: xScan 1.2 "__AMIGADATE__;
  34. char *Template = "FILE/M/A,REMOVE/S,ALL/S";
  35.  
  36. LONG __saveds main(void)
  37.  
  38. {
  39.  struct DosLibrary *DOSBase;
  40.  struct Library *XpkBase;
  41.  struct Process *MyProc;
  42.  struct RDArgs *RDArgs;
  43.  char ProgName[32];
  44.  LONG Result;
  45.  struct xScanArgs Args;
  46.  char **Files;
  47.  struct AnchorPath *AnchorPath;
  48.  
  49.  if ((MyProc=(struct Process *)FindTask(NULL))->pr_CLI==NULL)
  50.   {
  51.    (void)WaitPort(&MyProc->pr_MsgPort);
  52.    Forbid();
  53.    ReplyMsg (GetMsg(&MyProc->pr_MsgPort));
  54.  
  55.    return 0L;
  56.   }
  57.  
  58.  if ((DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",33L))==NULL)
  59.   {
  60.    Alert (AT_Recovery|AG_OpenLib|AO_DOSLib);
  61.  
  62.    return 20L;
  63.   }
  64.  if (DOSBase->dl_lib.lib_Version<37L)
  65.   {
  66.    (void)Write(Output(),"This program requires OS 2.04 or newer.\n",40L);
  67.  
  68.    CloseLibrary (&DOSBase->dl_lib);
  69.    return 20L;
  70.   }
  71.  
  72.  if (!GetProgramName(ProgName,32L)) (void)strcpy(ProgName,"xScan");
  73.  Result=10L;
  74.  
  75.  if ((XpkBase=OpenLibrary(XPKNAME,2L))==NULL)
  76.   {
  77.    PrintF ("%s: %s V2 or newer required !\n",ProgName,XPKNAME);
  78.  
  79.    goto Close1;
  80.   }
  81.  
  82.  Args.Remove=Args.All=FALSE;
  83.  if ((RDArgs=ReadArgs(Template,(LONG *)&Args,NULL))==NULL)
  84.   {
  85.    PrintFault (IoErr(),ProgName);
  86.  
  87.    goto Close2;
  88.   }
  89.  
  90.  if ((AnchorPath=(struct AnchorPath *)AllocVec(sizeof(struct AnchorPath),
  91.                                                MEMF_PUBLIC|MEMF_CLEAR))==NULL)
  92.   {
  93.    PrintFault (ERROR_NO_FREE_STORE,ProgName);
  94.  
  95.    goto Close3;
  96.   }
  97.  AnchorPath->ap_BreakBits=SIGBREAKF_CTRL_C;
  98.  AnchorPath->ap_Strlen=0;
  99.  
  100.  Files=Args.Files;
  101.  while (*Files)
  102.   {
  103.    LONG RetVal;
  104.  
  105.    for (RetVal=MatchFirst(*Files,AnchorPath); RetVal==0L; RetVal=MatchNext(AnchorPath))
  106.     {
  107.      if (AnchorPath->ap_Info.fib_DirEntryType<0L)
  108.       {
  109.        BPTR DirLock;
  110.  
  111.        DirLock=CurrentDir(AnchorPath->ap_Current->an_Lock);
  112.        xScan (DOSBase,XpkBase,&AnchorPath->ap_Info,Args.Remove);
  113.        (void)CurrentDir(DirLock);
  114.       }
  115.      else
  116.       if (AnchorPath->ap_Info.fib_DirEntryType!=ST_SOFTLINK)
  117.        {
  118.         if (((AnchorPath->ap_Flags&APF_DIDDIR)==0L)&&Args.All)
  119.          AnchorPath->ap_Flags|=APF_DODIR;
  120.         AnchorPath->ap_Flags&=~APF_DIDDIR;
  121.        }
  122.     }
  123.    MatchEnd (AnchorPath);
  124.  
  125.    if (RetVal!=ERROR_NO_MORE_ENTRIES)
  126.     {
  127.      PrintF ("%s: %s - ",ProgName,*Files);
  128.      PrintFault (RetVal,NULL);
  129.      
  130.      goto Close4;
  131.     }
  132.    else Files++;
  133.   }
  134.  Result=0L;
  135.  
  136. Close4: /* I hate "goto"s, but it's the only way to keep it short. */
  137.  FreeVec ((APTR)AnchorPath);
  138. Close3:
  139.  FreeArgs (RDArgs);
  140. Close2:
  141.  CloseLibrary (XpkBase);
  142. Close1:
  143.  CloseLibrary (&DOSBase->dl_lib);
  144.  return Result;
  145. }
  146.  
  147. void PrintF(char *FormatString,...)
  148.  
  149. {
  150.  struct DosLibrary *DOSBase;
  151.  
  152.  if (DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",37L))
  153.   {
  154.    (void)VPrintf(FormatString,(LONG *)&FormatString+1L);
  155.    (void)Flush(Output());
  156.  
  157.    CloseLibrary (&DOSBase->dl_lib);
  158.   }
  159. }
  160.  
  161. char __regargs *ULTA(char *Ptr,ULONG LW)
  162.  
  163. {
  164.  UWORD Index;
  165.  
  166.  *Ptr++=' ';
  167.  for (Index=0; Index<8; Index++, LW>>=4) Ptr[7-Index]='A'+(char)(LW&15);
  168.  return &Ptr[8];
  169. }
  170.  
  171. void __regargs xScan(struct DosLibrary *DOSBase,struct Library *XpkBase,
  172.                      struct FileInfoBlock *FIB,LONG Remove)
  173.  
  174. {
  175.  struct XpkFib XpkFib;
  176.  char ErrorBuffer[XPKERRMSGSIZE];
  177.  LONG Error;
  178.  
  179.  if (FIB->fib_Comment[0])
  180.   {
  181.    if (strncmp(FIB->fib_Comment,XFH_ID,strlen(XFH_ID))) return;
  182.   }
  183.  else
  184.   if (Remove) return;
  185.  
  186.  if (XpkExamineTags(&XpkFib,
  187.                     XPK_GetError,ErrorBuffer,
  188.                     XPK_InName,FIB->fib_FileName,TAG_DONE))
  189.   {
  190.    PrintF ("%s\n",ErrorBuffer);
  191.    return;
  192.   }
  193.  
  194.  if (Remove)
  195.   {
  196.    if (!SetComment(FIB->fib_FileName,""))
  197.     {
  198.      Error=IoErr();
  199.      PrintF ("Can't erase comment for %s: ");
  200.      PrintFault (Error,NULL);
  201.      return;
  202.     }
  203.   }
  204.  else
  205.   {
  206.    char Comment[80],*Ptr;
  207.  
  208.    Ptr=strcpy(Comment,XFH_ID)+strlen(XFH_ID);
  209.    Ptr=ULTA(Ptr,FIB->fib_Date.ds_Days);
  210.    Ptr=ULTA(Ptr,FIB->fib_Date.ds_Minute);
  211.    Ptr=ULTA(Ptr,FIB->fib_Date.ds_Tick);
  212.    Ptr=ULTA(Ptr,FIB->fib_Size);
  213.    *ULTA(Ptr,(XpkFib.Type==XPKTYPE_PACKED)?XpkFib.ULen:FIB->fib_Size)='\0';
  214.    if (!SetComment(FIB->fib_FileName,Comment))
  215.     {
  216.      Error=IoErr();
  217.      PrintF ("Can't set comment for %s: ");
  218.      PrintFault (Error,NULL);
  219.      return;
  220.     }
  221.   }
  222. }
  223.