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