home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
TOUCH12.ZIP
/
TOUCH.C
< prev
next >
Wrap
Text File
|
1990-01-03
|
7KB
|
177 lines
/*
TOUCH.C Force time and date stamps on file(s), for OS/2 1.2.
This program does not require Microsoft or IBM Programmer's Toolkit
header files, but must be linked using LINK.EXE and DOSCALLS.LIB
from retail OS/2 version 1.2.
Compile: cl /Zi -c touch.c
link touch,touch,,doscalls,touch.def;
Usage: touch pathname [ pathname... ]
A pathname may include a drive and a relative or
complete path. A pathname must include a filename,
which may include wildcard characters.
Copyright (C) 1989 Ray Duncan
*/
#include <stdio.h>
#include <string.h>
#define API unsigned extern far pascal // OS/2 API function prototypes
API DosFindClose(unsigned);
API DosFindFirst2(char far *, unsigned far *, unsigned, void far *,
unsigned, int far *, int, unsigned long);
API DosFindNext(unsigned, void far *, unsigned, unsigned far *);
API DosGetDateTime(void far *);
API DosQPathInfo(char far *, unsigned, void far *, unsigned, unsigned long);
API DosSetPathInfo(char far *, unsigned, void far *, unsigned,
unsigned, unsigned long);
#define MAXPATHNAME 260 // max length of pathname
#define MAXFILENAME 255 // max length of filename
#define NORMAL 0x00 // file attribute bits
#define READ_ONLY 0x01
#define HIDDEN 0x02
#define SYSTEM 0x04
#define DIRECTORY 0x10
#define ARCHIVE 0x20
struct _finfo { // used by DosSetFileInfo
unsigned cdate;
unsigned ctime;
unsigned adate;
unsigned atime;
unsigned wdate;
unsigned wtime;
long fsize;
long falloc;
unsigned fattr;
} ;
struct _dinfo { // used by DosGetDateTime
char hour;
char min;
char sec;
char csec;
char day;
char mon;
int year;
int zone;
char dow;
} dinfo;
struct _sbuf { // result buffer format
unsigned cdate; // for DosFindFirst2
unsigned ctime; // info level 1
unsigned adate;
unsigned atime;
unsigned wdate;
unsigned wtime;
long fsize;
long falloc;
unsigned fattr;
char fcount;
char fname[MAXFILENAME];
} ;
main(int argc, char *argv[])
{
int i; // scratch variable
if(argc < 2) // check command line
{
printf("\nUsage: touch pathname [pathname... ]\n");
printf("\n A pathname may include a drive and a partial");
printf("\n or complete path. A pathname must include a");
printf("\n filename, which may contain wildcards.\n");
printf("\nExamples: touch foo.dat");
printf("\n touch myfile* *.c\n");
exit(1);
}
DosGetDateTime(&dinfo); // get current date&time
for(i = 1; i < argc; i++) // process all pathnames
findfiles(argv[i]); // in the command line
puts(""); // final blank line
}
/*
Search for all files matching a command line argument
*/
findfiles(char *cname)
{
unsigned status; // receives function status
unsigned handle = -1; // directory search handle
int matches = 1; // no. of matches requested/found
struct _sbuf sbuf; // receives search results
// is there any match?
if(!DosFindFirst2(cname, // pathname from command line
&handle, // receives search handle
NORMAL, // attribute of files to match
&sbuf, // receives search results
sizeof(sbuf), // length of result buffer
&matches, // receives match count
1, // retrieve info level 1
0L)) // reserved
{
do
{
stampfile(cname, sbuf.fname); // match found, set time/date
} while(!DosFindNext(handle, &sbuf, sizeof(sbuf), &matches));
}
else printf("\nno matches: %s", strlwr(cname));
DosFindClose(handle); // release search handle
}
/*
Set the time and date stamp on a file
*/
stampfile(char *cname, char *sname)
{
char *p; // scratch pointer
char pname[MAXPATHNAME]; // target pathname
char qname[MAXPATHNAME]; // qualified pathname
struct _finfo finfo; // file information structure
memset(pname, 0, MAXPATHNAME); // initialize buffer
p = strrchr(cname, '\\'); // look for backslash
if(p != NULL) // any path present?
memcpy(pname,cname,p-cname+1); // yes, copy it
else // no, is drive present?
if((strlen(cname) >= 2) && (cname[1] == ':'))
{
pname[0] = cname[0]; // yes, copy drive
pname[1] = cname[1];
}
strcat(pname,sname); // form target pathname
// get fully qualified filename
DosQPathInfo(pname, 5, qname, sizeof(qname), 0L);
// read time and date stamps
if(DosQPathInfo(pname, 1, &finfo, sizeof(finfo), 0L))
printf("\ncan't touch: %s", strlwr(qname));
else
{ // force date/time of last write
finfo.wdate = ((dinfo.year-1980)<<9) + (dinfo.mon<<5) + dinfo.day;
finfo.wtime = (dinfo.hour<<11) + (dinfo.min<<5);
// write new date & time stamps
if(!DosSetPathInfo(pname, 1, &finfo, sizeof(finfo), 0x10, 0L))
printf("\ntouched: %s", strlwr(qname));
else printf("\ncan't touch: %s", strlwr(qname));
}
}