home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d09xx / d0962.lha / Touch / Touch.c < prev    next >
C/C++ Source or Header  |  1994-01-31  |  8KB  |  361 lines

  1. /*
  2. Auto:        sc <file>
  3. */
  4.  
  5. /* $Revision Header built automatically *************** (do not edit) ************
  6. **
  7. ** ⌐ Copyright by GuntherSoft
  8. **
  9. ** File             : SnakeSYS:CPrgs/Utils/Touch.c
  10. ** Created on       : Monday, 26.07.93 22:15:08
  11. ** Created by       : Kai Iske
  12. ** Current revision : V1.2
  13. **
  14. **
  15. ** Purpose
  16. ** -------
  17. **   - Small touch command which will create a file, if it doesn`s
  18. **     exists yet.
  19. **
  20. ** Revision V1.2
  21. ** --------------
  22. ** created on Friday, 31.12.93 12:56:39  by  Kai Iske.   LogMessage :
  23. **   - Touch will issue a warning if no matching files could be
  24. **     found for a pattern
  25. **     (Suggested by : Dan Barret)
  26. **
  27. ** Revision V1.1
  28. ** --------------
  29. ** created on Wednesday, 08.12.93 22:04:39  by  Kai Iske.   LogMessage :
  30. **   - Reduced stack usage
  31. **   - Reduced executable size
  32. **   - Recompiled using SAS 6.50
  33. **   - Added CTRL-C checking
  34. **
  35. ** Revision V1.0
  36. ** --------------
  37. ** created on Monday, 26.07.93 22:15:08  by  Kai Iske.   LogMessage :
  38. **     --- Initial release ---
  39. **
  40. *********************************************************************************/
  41. #define REVISION "1.2"
  42. #define REVDATE  "31.12.93"
  43. #define REVTIME  "12:56:39"
  44. #define AUTHOR   "Kai Iske"
  45. #define VERNUM   1
  46. #define REVNUM   2
  47.  
  48.  
  49. #include    <string.h>
  50. #include    <stdlib.h>
  51. #include    <exec/types.h>
  52. #include    <exec/memory.h>
  53. #include    <dos/dos.h>
  54. #include    <dos/exall.h>
  55. #include    <proto/exec.h>
  56. #include    <proto/dos.h>
  57.  
  58.  
  59.  
  60.  
  61. /**********************************************************************/
  62. /*                           Version String                           */
  63. /**********************************************************************/
  64. static const char *MyVer    = "$VER: Touch "REVISION" ("REVDATE")\0";
  65.  
  66.  
  67.  
  68.  
  69. /**********************************************************************/
  70. /*                  Template for Commandline Parsing                  */
  71. /**********************************************************************/
  72. static const char *Template    = "FILES/M/A";
  73. enum    {FILE_ARG, LAST_ARG};
  74.  
  75.  
  76.  
  77.  
  78.  
  79. /**********************************************************************/
  80. /*                       The small main program                       */
  81. /**********************************************************************/
  82. ULONG __saveds main(void)
  83. {
  84.     struct    ExecBase    *SysBase;
  85.     struct    DosLibrary    *DOSBase;
  86.     struct    Process        *MyProc;
  87.     struct    RDArgs        *RDArgs;
  88.     struct    ExAllControl    *EAC;
  89.     struct    ExAllData    *EAD;
  90.     struct    DateStamp    DS;
  91.     APTR    *EAB,
  92.         *Args;
  93.     BPTR    OutHandle,
  94.         TouchFile;
  95.     char    **FileNameList    = NULL,
  96.         *MainBuffer,
  97.         *FileName,
  98.         *Pattern,
  99.         *TouchName;
  100.     WORD    FileNameType;
  101.     ULONG    MySig;
  102.     BOOL    GoOn,
  103.         Breaked        = FALSE;
  104.  
  105.         // Get Base of Exec
  106.  
  107.     SysBase    = *((struct ExecBase **)0x4L);
  108.  
  109.         // Don`t start from WB
  110.  
  111.     MyProc    = (struct Process *)FindTask(NULL);
  112.  
  113.     if(!MyProc->pr_CLI)
  114.     {
  115.         struct    WBStartup    *Msg;
  116.  
  117.         WaitPort(&MyProc->pr_MsgPort);
  118.         Msg    = (struct WBStartup *)GetMsg(&MyProc->pr_MsgPort);
  119.         Disable();
  120.         ReplyMsg((struct Message *)Msg);
  121.         return(0);
  122.     }
  123.  
  124.         // Get DOSBase
  125.  
  126.     if(!(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 0)))
  127.         return(20);
  128.  
  129.         // Get handle to Output
  130.  
  131.     OutHandle = Output();
  132.  
  133.         // Check for System we`re running on
  134.  
  135.     if(((struct Library *)SysBase)->lib_Version < 37)
  136.     {
  137.         Write(OutHandle, "You must use KickStart 2.04 (37.175) or higher for Touch\n", 55);
  138.         CloseLibrary((struct Library *)DOSBase);
  139.         return(20);
  140.     }
  141.  
  142.         // Get current DateStamp
  143.  
  144.     DateStamp(&DS);
  145.  
  146.  
  147.         // Get buffer for filename etc.
  148.  
  149.     if((MainBuffer = AllocVec((1024 * 3), MEMF_CLEAR)))
  150.     {
  151.             // Set up buffers
  152.  
  153.         FileName    = MainBuffer;
  154.         Pattern        = (FileName + 1024);
  155.         TouchName    = (Pattern + 1024);
  156.  
  157.             // Get Buffer for arguments
  158.  
  159.         if((Args = AllocVec((LAST_ARG * sizeof(ULONG)), MEMF_CLEAR)))
  160.         {
  161.                 // Parse Commandline
  162.  
  163.             if((RDArgs = ReadArgs((char *)Template, (LONG *)Args, NULL)))
  164.             {
  165.                     // Get buffer for ExAll
  166.  
  167.                 if((EAB = AllocVec((sizeof(struct ExAllData) * 20), MEMF_CLEAR)))
  168.                 {
  169.                         // Get ExAllControl Structure
  170.  
  171.                     if((EAC = AllocDosObject(DOS_EXALLCONTROL, NULL)))
  172.                     {
  173.                             // Get pointers to FileNames passed
  174.  
  175.                         FileNameList = Args[FILE_ARG];
  176.  
  177.                             // Loop for all filenames
  178.  
  179.                         while(FileNameList && *FileNameList && !Breaked)
  180.                         {
  181.                                 // Check for CTRL-C
  182.  
  183.                             MySig    = CheckSignal(SIGBREAKF_CTRL_C);
  184.                             if(!(MySig & SIGBREAKF_CTRL_C))
  185.                             {
  186.                                     // Copy current filename
  187.  
  188.                                 strcpy(FileName, *FileNameList);
  189.  
  190.                                     // Create pattern
  191.  
  192.                                 if((FileNameType = ParsePatternNoCase(FilePart(FileName), Pattern, 512)) != -1)
  193.                                 {
  194.                                         // Check pattern type
  195.  
  196.                                     if(FileNameType)
  197.                                     {
  198.                                             // Real pattern. Remove trailing name
  199.  
  200.                                         *(PathPart(FileName)) = '\0';
  201.  
  202.                                             // Try to get lock to directory
  203.  
  204.                                         if((TouchFile = Lock(FileName, ACCESS_READ)))
  205.                                         {
  206.                                             BOOL    DoneOnce;
  207.  
  208.                                                 // Set up ExAllControl
  209.  
  210.                                             EAC->eac_LastKey    = 0;
  211.                                             EAC->eac_MatchString    = Pattern;
  212.                                             EAC->eac_MatchFunc    = NULL;
  213.  
  214.                                             DoneOnce    = FALSE;
  215.  
  216.                                             do
  217.                                             {
  218.                                                     // Check for CTRL-C
  219.  
  220.                                                 MySig    = CheckSignal(SIGBREAKF_CTRL_C);
  221.                                                 if((MySig & SIGBREAKF_CTRL_C))
  222.                                                     Breaked = TRUE;
  223.  
  224.                                                     // Loop directory
  225.  
  226.                                                 GoOn = ExAll(TouchFile, (struct ExAllData *)EAB, (108*20), ED_NAME, EAC);
  227.  
  228.                                                     // Error occured ???
  229.  
  230.                                                 if((!GoOn) && (IoErr() != ERROR_NO_MORE_ENTRIES))
  231.                                                     PrintFault(IoErr(), "Touch ");
  232.  
  233.                                                     // End of dir reached ;
  234.  
  235.                                                 if(EAC->eac_Entries == 0)
  236.                                                 {
  237.                                                     if(!DoneOnce)
  238.                                                     {
  239.                                                         strcpy(TouchName, "Touch : No pattern match for ");
  240.                                                         strcat(TouchName, *FileNameList);
  241.                                                         strcat(TouchName, "\n");
  242.                                                         FPuts(OutHandle, TouchName);
  243.                                                     }
  244.                                                     GoOn = FALSE;
  245.                                                 }
  246.                                                 else if(!Breaked)
  247.                                                 {
  248.                                                     DoneOnce    = TRUE;
  249.  
  250.                                                         // Get buffer to ExAll Buffer
  251.  
  252.                                                     EAD = (struct ExAllData *)EAB;
  253.  
  254.                                                     do
  255.                                                     {
  256.                                                             // Check for CTRL-C
  257.  
  258.                                                         MySig    = CheckSignal(SIGBREAKF_CTRL_C);
  259.                                                         if((MySig & SIGBREAKF_CTRL_C))
  260.                                                             Breaked = TRUE;
  261.                                                         else
  262.                                                         {
  263.  
  264.                                                                 // Clear Touchname and create new one
  265.  
  266.                                                             TouchName[0]    = '\0';
  267.                                                             AddPart(TouchName, FileName, 512);
  268.                                                             AddPart(TouchName, EAD->ed_Name, 512);
  269.  
  270.                                                                 // Set new date
  271.  
  272.                                                             SetFileDate(TouchName, &DS);
  273.  
  274.                                                                 // Loop for entries
  275.  
  276.                                                             EAD = EAD->ed_Next;
  277.                                                         }
  278.                                                     } while(EAD && !Breaked);
  279.                                                 }
  280.                                             } while(GoOn);
  281.  
  282.                                                 // UnLock directory
  283.  
  284.                                             UnLock(TouchFile);
  285.                                         }
  286.                                     }
  287.                                     else
  288.                                     {
  289.                                             // File to be touched there ???
  290.  
  291.                                         if((TouchFile = Lock(FileName, ACCESS_READ)))
  292.                                         {
  293.                                                 // Unlock it
  294.  
  295.                                             UnLock(TouchFile);
  296.  
  297.                                                 // ... and set filedate
  298.  
  299.                                             SetFileDate(FileName, &DS);
  300.                                         }
  301.                                             // Otherwise create new file
  302.  
  303.                                         else if((TouchFile = Open(FileName, MODE_NEWFILE)))
  304.                                             Close(TouchFile);
  305.                                     }
  306.                                 }
  307.                                 else
  308.                                 {
  309.                                     PrintFault(IoErr(), "Touch ");
  310.                                     FileNameList = NULL;
  311.                                 }
  312.                                     // Loop for files
  313.  
  314.                                 FileNameList++;
  315.                             }
  316.                             else
  317.                                 Breaked = TRUE;
  318.                         }
  319.  
  320.                         FreeDosObject(DOS_EXALLCONTROL, EAC);
  321.                     }
  322.                     else
  323.                         PrintFault(IoErr(), "Touch ");
  324.  
  325.                     FreeVec(EAB);
  326.                 }
  327.                 else
  328.                     FPuts(OutHandle, "Touch : Buffer for Directory Scan could not be allocated\n");
  329.  
  330.                 FreeArgs(RDArgs);
  331.             }
  332.             else
  333.                 PrintFault(IoErr(), "Touch ");
  334.  
  335.             FreeVec(Args);
  336.         }
  337.         else
  338.             FPuts(OutHandle, "Touch : Buffer for Commandline could not be allocated\n");
  339.  
  340.         FreeVec(MainBuffer);
  341.     }
  342.     else
  343.         PrintFault(ERROR_NO_FREE_STORE, "Touch ");
  344.  
  345.         // Display break status if needed
  346.  
  347.     if(Breaked)
  348.         FPuts(OutHandle, "Touch : ^C...\n");
  349.  
  350.         // Close DOS
  351.  
  352.     CloseLibrary((struct Library *)DOSBase);
  353.  
  354.         // Return appropriate code
  355.  
  356.     if(FileNameList)
  357.         return(0);
  358.     else
  359.         return(20);
  360. }
  361.