⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 touch.c

📁 使用于OS/2下的小工具的C源程序,使用于OS/2下的小工具的C源程序
💻 C
字号:
/*
* TOUCH.C - Change the last modification date and time of a file.
*
* PROGRAMMER:	    Martti Ylikoski
* CREATED:	    8.12.1990
*/
static char* VERSION="Version 1.1. Copyright (c) Martti Ylikoski. 1990, 1991." ;
/*
*/

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <param.h>
#include <paramstd.h>
#define INCL_DOS
#include <os2.h>

static char *progname, *fname	;

extern unsigned long pflags ;

ParamEntry pentry[12] = {
     "P", &ParamSetPause, 0,
     "F", &ParamSetFold, 0,
     "V", &ParamSetVerbose, 0,
     "R", &ParamSetReport, 0,
     "S", &ParamSetSubDirs, 0,
     "?", &ParamSetHelp, 1,
     "H", &ParamSetHelp, 1,
     "NOD", &ParamSetNoDefault, 0,
     "TEST", &ParamSetTest, 0,
     "Y", &ParamSetYes, 0,
     "N", &ParamSetTest, 0,
     "\0", NULL, 0
} ;

ParamBlock params = {
    "/-",   IGNORECASE | NOPRIORITY | NOTRIGGERSALLOWED ,
    pentry
} ;

/* local prototypes */
static int touch(char *fname) ;

int main(int argc, char *argv[])
{
int i ;

   progname = argv[0] ;

   ParamHandle(&params, &argc, argv) ;

   if (pflags & PA_HELP || argc == 1)
   {
      printf("%s - change the last modification time and date of a file.\n", progname) ;
      puts(VERSION) ;
      puts("Usage: touch file(s) [/H | /?]") ;
      puts("Where,") ;
      puts("     /H,/? = Help") ;
      return( 0 ) ;
   }

   fname = argv[1] ;

   for (i = 1; i <argc; i++)
      touch(argv[i]) ;
   return( 0 ) ;
}

static int touch(char *fname)
{
HFILE hf ;
int c ;
USHORT action, ret ;
FILESTATUS fstat ;
DATETIME dtim ;
      
   /* open file */

   if ((ret = DosOpen(fname, &hf, &action, 0L, FILE_NORMAL,
       FILE_OPEN|FILE_CREATE,OPEN_ACCESS_READWRITE|OPEN_SHARE_DENYREADWRITE, 0L)) != 0)
   {
      printf("%s error %d opening file %s\n", progname, ret, fname) ;
      DosClose(hf) ;
      return( (int) ret ) ;
    }

   if (( ret = DosQFileInfo(hf, 1, (PVOID) &fstat, sizeof(fstat)))	!= 0)
   {
      printf("%s error in DosSetFileInfo\n", progname) ;
      DosClose(hf) ;
      return( (int) ret ) ;
    }

   if (( ret = DosGetDateTime(&dtim)) != 0)
   {
      printf("%s error %d in DosGetDateTime\n", progname, ret) ;
      DosClose(hf) ;
      return( (int) ret ) ;
    }

   fstat.fdateLastAccess.day  = dtim.day  ;
   fstat.fdateLastAccess.month = dtim.month ;
   fstat.fdateLastAccess.year =  (unsigned) dtim.year - 1980 ; /* years represented relative to 1980 */
   fstat.ftimeLastAccess.twosecs = dtim.seconds/2 ;
   fstat.ftimeLastAccess.minutes = dtim.minutes ;
   fstat.ftimeLastAccess.hours = dtim.hours ;
   fstat.fdateLastWrite = fstat.fdateLastAccess ;
   fstat.ftimeLastWrite = fstat.ftimeLastAccess ;
   if ((ret = DosSetFileInfo(hf, 1, (PBYTE) &fstat, (USHORT) sizeof(fstat))) != 0)
   {
      if (( ret = DosNewSize(hf, fstat.cbFile)) != 0) /* clever no-op = change files size
							 to what it already was. This is needed because in DOS-
							compatility mode DosSetFileInfo does not work in 1.21 */
      {

	 printf("%s unable to set time\n", progname) ;
	 DosClose(hf) ;
	 return( (int) ret ) ;
       }
    }

   /* close file */
   DosClose(hf) ;
   return( 0 ) ;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -