home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / TOUCH.CMM < prev    next >
Text File  |  1996-01-02  |  6KB  |  213 lines

  1. /*
  2.  * Touch.cmm
  3.  *
  4.  * Update a file's date and time to the current time. Also, set
  5.  * the archive bit.
  6.  */
  7.  
  8. #include "Netware.lib"
  9.  
  10. usage()
  11. {
  12.   printf("Use the touch command to update one or more file(s) to have\n");
  13.   printf("their timestamp set to the current time and their archive bit\n");
  14.   printf("set.\n");
  15.   printf("Syntax:\n");
  16.   printf("  TOUCH [drive][path][filename]\n");
  17.   printf("where:\n");
  18.   printf("  drive/path/filename   Specifies the file to be touched.\n");
  19.   exit(EXIT_FAILURE);
  20. }
  21.  
  22. /* ---------------------------------------------------------------------- */
  23.  
  24. /*
  25.  * Some DOS functions
  26.  */
  27. SetFileAttributes(pFileName,pAttributes)
  28. {
  29.    lReg.ah = 0x43;
  30.    lReg.al = 1;
  31.    lReg.cx = pAttributes;
  32.    if !defined(_DOS32_)
  33.       lReg.ds = segment(pFileName), lReg.dx = offset(pFileName);
  34.    else
  35.       lReg.dx = pointer(pFileName);
  36.    return interrupt(0x21,lReg);
  37. }
  38.  
  39. SetFileDateAndTime(pFileName,pTime)
  40. {
  41.    lSuccess = False;
  42.    // Open file to get a handle
  43.    lReg.ah = 0x3D;
  44.    lReg.al = 0x42;
  45.    if !defined(_DOS32_)
  46.       lReg.ds = segment(pFileName), lReg.dx = offset(pFileName);
  47.    else
  48.       lReg.dx = pointer(pFileName);
  49.    if ( interrupt(0x21,lReg,lRegout) ) {
  50.       lHandle = lRegout.ax;
  51.       // write date to file
  52.       undefine(lReg);
  53.       lReg.ah = 0x57;
  54.       lReg.al = 1;
  55.       lReg.bx = lHandle;
  56.       lTm = localtime(pTime);
  57.       lReg.cx = (lTm.tm_sec / 2)
  58.               | (lTm.tm_min << 5)
  59.               | (lTm.tm_hour << 11);
  60.       lReg.dx = lTm.tm_mday
  61.               | ((lTm.tm_mon+1) << 5)
  62.               | ((lTm.tm_year-80) << 9);
  63.       lSuccess = interrupt(0x21,lReg);
  64.       // close file
  65.       undefine(lReg);
  66.       lReg.ah = 0x3E;
  67.       lReg.bx = lHandle;
  68.       interrupt(0x21,lReg);
  69.    }
  70.    return lSuccess;
  71. }
  72.  
  73. DOSTimeFromCalendar(time)
  74. {
  75.   dos = DOSTimeStructFromCalendar(time);
  76.   return dos.time<<16 | dos.date;
  77. }
  78.  
  79. DOSTimeStructFromCalendar(time)
  80. {
  81.   tm = localtime(time);
  82.   dos.time = (tm.tm_sec/2) | (tm.tm_min<<5) | (tm.tm_hour<<11);
  83.   dos.date = tm.tm_mday | ((tm.tm_mon+1)<<5) | ((tm.tm_year-80)<<9);
  84.   return dos;
  85. }
  86.  
  87. GMDOSTimeStructFromCalendar(time)
  88. {
  89.   tm = gmtime(time);
  90.   dos.time = (tm.tm_sec/2) | (tm.tm_min<<5) | (tm.tm_hour<<11);
  91.   dos.date = tm.tm_mday | ((tm.tm_mon+1)<<5) | ((tm.tm_year-80)<<9);
  92.   return dos;
  93. }
  94.  
  95. /* ---------------------------------------------------------------------- */
  96.  
  97. dir[0] = "";
  98. it = 1;
  99.  
  100. parse_command_line(argc,argv)
  101. {
  102.   for( i=1;i<argc;i++ )
  103.     {
  104.       if( argv[i][0]=='-' || argv[i][0]=='/' )
  105.     {
  106.           usage();
  107.     } else {
  108.           if( it )
  109.             {
  110.               dir[0] = argv[i]; it = 0;
  111.             } else {
  112.               dir[GetArraySpan(dir)+1] = argv[i];
  113.             }
  114.     }
  115.     }
  116. }
  117.  
  118. /* ---------------------------------------------------------------------- */
  119.  
  120. touch_file(dir)
  121. {
  122.   curtime = time();
  123.   if( defined(_NWNLM_) )
  124.     {
  125.       tempcreate[0] = DOSTimeFromCalendar(curtime);
  126.       tempaccess[0] = DOSTimeFromCalendar(curtime);
  127.       tempdate[0] =   DOSTimeFromCalendar(curtime);
  128.       tempbackup[0] = DOSTimeFromCalendar(curtime);
  129.       
  130.       code = NLMLink("SetFileInfo", dir.name, 0x06, dir.attrib | _A_ARCH,
  131.                      tempcreate,
  132.                      tempaccess,
  133.                      tempdate,
  134.                      tempbackup,
  135.                      dir.uid);
  136.       if( code )
  137.         printf("\nError setting attributes for file %s\n",dir.name);
  138.       return;
  139.     }
  140.   if( defined(_DOS_) || defined(_DOS32_) || defined(_WINDOWS_) )
  141.     {
  142.       SetFileDateAndTime(dir.name,curtime);
  143.       SetFileAttributes(dir.name,dir.attrib | _A_ARCH);
  144.       return;
  145.     }
  146.   if( defined(_NTCON_) || defined(_NTWIN_) || defined(_95CON_) ||
  147.       defined(_95WIN_) )
  148.     {
  149. #define GENERIC_WRITE 0x40000000
  150. #define FILE_SHARE_READ 0x01
  151. #define FILE_SHARE_WRITE 0x02
  152. #define OPEN_EXISTING 3
  153. #define INVALID_HANDLE_VALUE -1
  154.  
  155.       newbuf = ""; SetArraySpan(newbuf,4);
  156.  
  157.  
  158.       DynamicLink("KERNEL32","SetFileAttributesA",STDCALL,dir.name,
  159.                   dir.attrib | _A_ARCH);
  160.  
  161.       if( (handle = DynamicLink("KERNEL32","CreateFileA",STDCALL,dir.name,
  162.                                 GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,
  163.                                 NULL,OPEN_EXISTING,0,NULL))
  164.            !=INVALID_HANDLE_VALUE)
  165.         {
  166.           dos = GMDOSTimeStructFromCalendar(time());
  167.           DynamicLink("KERNEL32","DosDateTimeToFileTime",STDCALL,
  168.                       dos.date,dos.time,newbuf);
  169.  
  170.           if( !DynamicLink("KERNEL32","SetFileTime",STDCALL,handle,newbuf,
  171.                            newbuf,newbuf) )
  172.             {
  173.               err = DynamicLink("KERNEL32","GetLastError",STDCALL);
  174.               printf("Unable to set new file time.\n",err);
  175.             }
  176.  
  177.           if( !DynamicLink("KERNEL32","CloseHandle",STDCALL,handle) )
  178.              printf("Unable to close the handle.\n");
  179.         } else {
  180.           printf("Unable to open the file to touch it.\n");
  181.         }
  182.       return;
  183.     }
  184.  
  185.   if( defined(_OS2_) )
  186.     {
  187.       printf("OS/2 not yet supported.\n");
  188.       return;
  189.     }
  190. }
  191.  
  192.  
  193. main(argc,argv)
  194. {
  195.   if( defined(_OS2_) )
  196.     {
  197.       printf("This script does not yet support OS/2.\n");
  198.       exit(EXIT_FAILURE);
  199.     }
  200.  
  201.   parse_command_line(argc,argv);
  202.   if( it ) Usage();
  203.  
  204.   for( i=0;it==0 && i<=GetArraySpan(dir);i++ )
  205.     {
  206.       files = Directory(dir[i]);
  207.       for( j=0;files && j<=GetArraySpan(files);j++ )
  208.         {
  209.           touch_file(files[j]);
  210.         }
  211.     }
  212. }
  213.