home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 4 / CDPD_IV.bin / fish / 911-930 / ff919a / touch / touch.c < prev    next >
C/C++ Source or Header  |  1994-05-04  |  5KB  |  248 lines

  1. /*
  2. Auto:        sc <file> LINK NODEBUG NOSTRCONS OPT SINT NOSTACKCHECK DATA=NEAR STARTUP=cres PARM=REGISTER UTILLIB NOSTKCHK
  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.0
  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.0
  21. ** --------------
  22. ** created on Monday, 26.07.93 22:15:08  by  Kai Iske.   LogMessage :
  23. **     --- Initial release ---
  24. **
  25. *********************************************************************************/
  26. #define REVISION "1.0"
  27. #define REVDATE  "26.07.93"
  28. #define REVTIME  "22:15:08"
  29. #define AUTHOR   "Kai Iske"
  30. #define VERNUM   1
  31. #define REVNUM   0
  32.  
  33.  
  34. #include    <string.h>
  35. #include    <stdlib.h>
  36. #include    <exec/types.h>
  37. #include    <exec/memory.h>
  38. #include    <dos/dos.h>
  39. #include    <dos/exall.h>
  40. #include    <proto/exec.h>
  41. #include    <proto/dos.h>
  42.  
  43.  
  44. extern struct WBStartUp *_WBenchMsg;
  45.  
  46.  
  47. /**********************************************************************/
  48. /*                           Version String                           */
  49. /**********************************************************************/
  50. static char *MyVer    = "$VER: Touch "REVISION" ("REVDATE")\0";
  51.  
  52.  
  53. /**********************************************************************/
  54. /*                  Template for Commandline Parsing                  */
  55. /**********************************************************************/
  56. static char *Template    = "FILES/M/A";
  57. enum    {FILE_ARG, LAST_ARG};
  58.  
  59.  
  60. /**********************************************************************/
  61. /*                       The small main program                       */
  62. /**********************************************************************/
  63. void main(void)
  64. {
  65.     struct    RDArgs        *RDArgs    = NULL;
  66.     struct    ExAllControl    *EAC    = NULL;
  67.     struct    ExAllData    *EAD;
  68.     struct    DateStamp    DS;
  69.     APTR    *EAB            = NULL,
  70.         *Args;
  71.     BPTR    OutHandle,
  72.         TouchFile;
  73.     char    **FileNameList        = NULL,
  74.         FileName[512],
  75.         Pattern[512],
  76.         TouchName[512];
  77.     WORD    FileNameType;
  78.     BOOL    GoOn;
  79.  
  80.         // Not available from Workbench
  81.  
  82.     if(_WBenchMsg)
  83.         exit(0);
  84.  
  85.         // Get handle to Output
  86.  
  87.     OutHandle = Output();
  88.  
  89.         // Get current DateStamp
  90.  
  91.     DateStamp(&DS);
  92.  
  93.         // Get Buffer for arguments
  94.  
  95.     if((Args = AllocVec((LAST_ARG * sizeof(ULONG)), MEMF_CLEAR)))
  96.     {
  97.             // Parse Commandline
  98.  
  99.         if((RDArgs = ReadArgs(Template, (ULONG *)Args, NULL)))
  100.         {
  101.                 // Get buffer for ExAll
  102.  
  103.             if((EAB = AllocVec((sizeof(struct ExAllData) * 20), MEMF_CLEAR)))
  104.             {
  105.                     // Get ExAllControl Structure
  106.  
  107.                 if((EAC = AllocDosObject(DOS_EXALLCONTROL, NULL)))
  108.                 {
  109.                         // Get pointers to FileNames passed
  110.  
  111.                     FileNameList = Args[FILE_ARG];
  112.  
  113.                         // Loop for all filenames
  114.  
  115.                     while(FileNameList && *FileNameList)
  116.                     {
  117.                             // Copy current filename
  118.  
  119.                         strcpy(FileName, *FileNameList);
  120.  
  121.                             // Create pattern
  122.  
  123.                         if((FileNameType = ParsePatternNoCase(FilePart(FileName), Pattern, 512)) != -1)
  124.                         {
  125.                                 // Check pattern type
  126.  
  127.                             if(FileNameType)
  128.                             {
  129.                                     // Real pattern. Remove trailing name
  130.  
  131.                                 *(PathPart(FileName)) = '\0';
  132.  
  133.                                     // Try to get lock to directory
  134.  
  135.                                 if((TouchFile = Lock(FileName, ACCESS_READ)))
  136.                                 {
  137.                                         // Set up ExAllControl
  138.  
  139.                                     EAC->eac_LastKey    = 0;
  140.                                     EAC->eac_MatchString    = Pattern;
  141.                                     EAC->eac_MatchFunc    = NULL;
  142.  
  143.                                     do
  144.                                     {
  145.                                             // Loop directory
  146.  
  147.                                         GoOn = ExAll(TouchFile, (struct ExAllData *)EAB, (108*20), ED_NAME, EAC);
  148.  
  149.                                             // Error occured ???
  150.  
  151.                                         if((!GoOn) && (IoErr() != ERROR_NO_MORE_ENTRIES))
  152.                                             PrintFault(IoErr(), "Man ");
  153.  
  154.                                             // End of dir reached ;
  155.  
  156.                                         if(EAC->eac_Entries == 0)
  157.                                             GoOn = FALSE;
  158.                                         else
  159.                                         {
  160.                                                 // Get buffer to ExAll Buffer
  161.  
  162.                                             EAD = (struct ExAllData *)EAB;
  163.  
  164.                                             do
  165.                                             {
  166.                                                     // Clear Touchname and create new one
  167.  
  168.                                                 TouchName[0]    = '\0';
  169.                                                 AddPart(TouchName, FileName, 512);
  170.                                                 AddPart(TouchName, EAD->ed_Name, 512);
  171.  
  172.                                                     // Set new date
  173.  
  174.                                                 SetFileDate(TouchName, &DS);
  175.  
  176.                                                     // Loop for entries
  177.  
  178.                                                 EAD = EAD->ed_Next;
  179.                                             } while(EAD);
  180.                                         }
  181.  
  182.                                     } while(GoOn);
  183.  
  184.                                         // UnLock directory
  185.  
  186.                                     UnLock(TouchFile);
  187.                                 }
  188.                             }
  189.                             else
  190.                             {
  191.                                     // File to be touched there ???
  192.  
  193.                                 if((TouchFile = Lock(FileName, ACCESS_READ)))
  194.                                 {
  195.                                         // Unlock it
  196.  
  197.                                     UnLock(TouchFile);
  198.  
  199.                                         // ... and set filedate
  200.  
  201.                                     SetFileDate(FileName, &DS);
  202.                                 }
  203.                                     // Otherwise create new file
  204.  
  205.                                 else if((TouchFile = Open(FileName, MODE_NEWFILE)))
  206.                                     Close(TouchFile);
  207.                             }
  208.                         }
  209.                         else
  210.                         {
  211.                             PrintFault(IoErr(), "Touch ");
  212.                             FileNameList = NULL;
  213.                         }
  214.                             // Loop for files
  215.  
  216.                         FileNameList++;
  217.                     }
  218.                 }
  219.                 else
  220.                     PrintFault(IoErr(), "Touch ");
  221.             }
  222.             else
  223.                 FPuts(OutHandle, "Touch : Buffer for Directory Scan could not be allocated\n");
  224.         }
  225.         else
  226.             PrintFault(IoErr(), "Touch ");
  227.     }
  228.     else
  229.         FPuts(OutHandle, "Touch : Buffer for Commandline could not be allocated\n");
  230.  
  231.  
  232.     if(EAC)
  233.         FreeDosObject(DOS_EXALLCONTROL, EAC);
  234.  
  235.     if(EAB)
  236.         FreeVec(EAB);
  237.  
  238.     if(RDArgs)
  239.         FreeArgs(RDArgs);
  240.  
  241.     if(Args)
  242.         FreeVec(Args);
  243.  
  244.     if(FileNameList)
  245.         exit(0);
  246.     else
  247.         exit(10);
  248. }