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