home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
VSCPPv8.zip
/
VACPP
/
IBMCPP
/
samples
/
COMPILER
/
TOUCH
/
TOUCH.C
< prev
next >
Wrap
Text File
|
1995-05-29
|
20KB
|
322 lines
/*┌──────────────────────────────────────────────────────────────────────────┐*/
/*│ │*/
/*│ PROGRAM NAME: TOUCH │*/
/*│ ------------- │*/
/*│ An OS/2 program that changes the date and time of selected files to the │*/
/*│ date and time at which the command is run. │*/
/*│ │*/
/*│ COPYRIGHT: │*/
/*│ ---------- │*/
/*│ Copyright (C) International Business Machines Corp., 1991,1992,1993. │*/
/*│ │*/
/*│ DISCLAIMER OF WARRANTIES: │*/
/*│ ------------------------- │*/
/*│ The following [enclosed] code is sample code created by IBM Corporation.│*/
/*│ This sample code is not part of any standard IBM product and is provided│*/
/*│ to you solely for the purpose of assisting you in the development of │*/
/*│ your applications. The code is provided "AS IS", without warranty of │*/
/*│ any kind. IBM shall not be liable for any damages arising out of your │*/
/*│ use of the sample code, even if they have been advised of the │*/
/*│ possibility of such damages. │*/
/*│ │*/
/*│ REVISION LEVEL: 1.0 │*/
/*│ --------------- │*/
/*│ │*/
/*│ WHAT THIS PROGRAM DOES: │*/
/*│ ----------------------- │*/
/*│ This program uses the parameters passed to determine which files are │*/
/*│ to have their date and time updated to the current date and time. │*/
/*│ │*/
/*│ WHAT THIS PROGRAM DEMONSTRATES: │*/
/*│ ------------------------------- │*/
/*│ This program demonstrates how to query the date and time, how to find │*/
/*│ files based on a file mask, how to open those files, how to update the │*/
/*│ date and time of those files, and how to close the files when done. │*/
/*│ │*/
/*│ WHAT YOU NEED TO COMPILE THIS PROGRAM: │*/
/*│ -------------------------------------- │*/
/*│ │*/
/*│ REQUIRED FILES: │*/
/*│ --------------- │*/
/*│ │*/
/*│ TOUCH C - touch source file │*/
/*│ TOUCH MAK - touch make file │*/
/*│ │*/
/*│ REQUIRED LIBRARIES: │*/
/*│ ------------------- │*/
/*│ │*/
/*│ OS2386.LIB - OS/2 API library │*/
/*│ │*/
/*│ REQUIRED PROGRAMS: │*/
/*│ ------------------ │*/
/*│ │*/
/*│ IBM C Set++ Compiler (icc.exe) │*/
/*│ IBM Linker (link386.exe) │*/
/*│ │*/
/*│ EXPECTED INPUT: │*/
/*│ --------------- │*/
/*│ │*/
/*│ One or more file masks indicating the files that are to have their │*/
/*│ date and time updated. │*/
/*│ │*/
/*│ EXPECTED OUTPUT: │*/
/*│ ---------------- │*/
/*│ │*/
/*│ A message for each file indicated the successful update of the date │*/
/*│ and time. │*/
/*│ │*/
/*└──────────────────────────────────────────────────────────────────────────┘*/
/*┌──────────────────────────────────────────────────────────────────────────┐*/
/*│ TOUCH.C │*/
/*│ │*/
/*│ Touch main source file │*/
/*└──────────────────────────────────────────────────────────────────────────┘*/
#include <stdio.h>
#include <stdlib.h>
#define INCL_DOSFILEMGR
#define INCL_DOSDATETIME
#define INCL_DOSERRORS
#define INCL_NOCOMMON
#include <os2.h>
main(int argc, char *argv[], char *envp[])
/***************************************************************************/{
/* variables needed for OS/2 DosXxxx() calls */
/***************************************************************************/
HFILE FileHandle; /* for DosOpen/Close, DosQuery/SetFileInfo */
ULONG ActionTaken; /* for DosOpen */
USHORT rc; /* for all DosXxxx calls */
PBYTE FileInfoBuf; /* for DosQuery/SetFileInfo */
USHORT FileInfoBufSize; /* for DosQuery/SetFileInfo */
DATETIME DateTime; /* for DosGetDateTime */
FILESTATUS FileStatus; /* for DosQuery/SetFileInfo */
FILEFINDBUF3 FileFindBuf; /* for DosFindFirst/Next */
HDIR hdir; /* for DosFindFirst/Next */
USHORT attrib; /* for DosFindFirst/Next */
ULONG count; /* for DosFindFirst/Next */
/***************************************************************************/
/* internal variables */
/***************************************************************************/
int i; /* index into argv array for file mask */
BOOL error = FALSE; /* error flag */
int errors = 0; /* error count used as return code */
/***************************************************************************/
/* Check if any parameters were given, if not, display some help infomation*/
/***************************************************************************/
if (argc == 1)
{
fprintf(stderr, "%s: Syntax is '%s arg1 [arg2...argn]'.\n", argv[0], argv[0]);
fprintf(stderr, "%s: where argX are filenames or patterns.\n", argv[0]);
exit(1);
}
/***************************************************************************/
/* Get the current date and time and use it for all files 'touch'ed. */
/***************************************************************************/
if (rc = DosGetDateTime(&DateTime))
{
fprintf(stderr, "%s: DosGetDateTime() failed with OS/2 rc = %d.\n", argv[0], rc);
exit(rc);
}
/***************************************************************************/
/* Initialize variables for DosQueryFileInfo() */
/***************************************************************************/
FileInfoBuf = (char *) &FileStatus;
FileInfoBufSize = sizeof(FileStatus);
/***************************************************************************/
/* initialize variables for DosFindFirst() */
/***************************************************************************/
hdir = HDIR_CREATE;
attrib = FILE_NORMAL | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_ARCHIVED;
count = 1;
/***************************************************************************/
/* for each arguments provided: */
/* - find each file that matches the argument */
/* - update the file's date and time to the time obtained earlier */
/***************************************************************************/
for (i = 1; i < argc; i++)
{
/************************************************************************/
/* find the first file that matches the file pattern (argument) */
/************************************************************************/
rc = DosFindFirst(argv[i],
&hdir,
attrib,
&FileFindBuf,
sizeof(FileFindBuf),
&count,
FIL_STANDARD);
if (rc)
{
/*********************************************************************/
/* Indicate to user that no files matched the pattern supplied */
/*********************************************************************/
fprintf(stderr, "%s: Failed to find any files matching '%s' (OS/2 rc = %d).\n", argv[0], argv[i], rc);
errors += 1;
}
else
{
/*********************************************************************/
/* touch the file, find the next one and repeat */
/*********************************************************************/
while (!rc)
/******************************************************************/{
/* Open the file found so that the current date/time structure */
/* can be used to set the new date/time */
/******************************************************************/
if (rc = DosOpen(FileFindBuf.achName, &FileHandle, &ActionTaken, 0L,
0x0000, 0x0001, 0x2011, 0L))
/***************************************************************/{
/* Indicate to user that the open failed */
/***************************************************************/
fprintf(stderr, "%s: DosOpen() failed on %s with OS/2 rc = %d.\n", argv[0], FileFindBuf.achName, rc);
error = TRUE;
}
else
{
/***************************************************************/
/* Get the information associated with the current file */
/***************************************************************/
if (rc = DosQueryFileInfo(FileHandle, 1, FileInfoBuf, FileInfoBufSize))
/************************************************************/{
/* Indicate to user that the query failed */
/************************************************************/
fprintf(stderr, "%s: DosQueryFileInfo() failed on %s with OS/2 rc = %d.\n", argv[0], FileFindBuf.achName, rc);
error = TRUE;
}
else
{
#if DEBUG
/************************************************************/
/* Print the old date of the file if debugging */
/************************************************************/
printf("DEBUG: Old date is: %d/%02d/%02d\n",
FileStatus.fdateLastWrite.year + 1980,
FileStatus.fdateLastWrite.month,
FileStatus.fdateLastWrite.day);
printf("DEBUG: Old time is: %d:%02d:%02d\n",
FileStatus.ftimeLastWrite.hours,
FileStatus.ftimeLastWrite.minutes,
FileStatus.ftimeLastWrite.twosecs * 2);
#endif
/************************************************************/
/* change the FileStatus structure to contain the date/time */
/* obtained earlier (making adjustments for differences) */
/************************************************************/
FileStatus.fdateLastWrite.day = DateTime.day;
FileStatus.fdateLastWrite.month = DateTime.month;
FileStatus.fdateLastWrite.year = DateTime.year - 1980;
FileStatus.ftimeLastWrite.twosecs = DateTime.seconds / 2;
FileStatus.ftimeLastWrite.minutes = DateTime.minutes;
FileStatus.ftimeLastWrite.hours = DateTime.hours;
#if DEBUG
/************************************************************/
/* Print the new date of the file if debugging */
/************************************************************/
printf("DEBUG: New date is: %d/%02d/%02d\n",
FileStatus.fdateLastWrite.year + 1980,
FileStatus.fdateLastWrite.month,
FileStatus.fdateLastWrite.day);
printf("DEBUG: New time is: %d:%02d:%02d\n",
FileStatus.ftimeLastWrite.hours,
FileStatus.ftimeLastWrite.minutes,
FileStatus.ftimeLastWrite.twosecs * 2);
#endif
/************************************************************/
/* Set the information for the current file with the */
/* structure that was just modified */
/************************************************************/
if (rc = DosSetFileInfo(FileHandle, 1, FileInfoBuf, FileInfoBufSize))
{
/*********************************************************/
/* Indicate to user that the update failed */
/*********************************************************/
fprintf(stderr, "%s: DosSetFileInfo() failed on %s with OS/2 rc = %d.\n", argv[0], FileFindBuf.achName, rc);
error = TRUE;
}
}
/***************************************************************/
/* Close the current file so that the new information is */
/* written */
/***************************************************************/
if (rc = DosClose(FileHandle))
/************************************************************/{
/* Indicate to user that an error occured closing the file */
/************************************************************/
fprintf(stderr, "%s: DosClose() failed on %s with OS/2 rc = %d.\n", argv[0], FileFindBuf.achName, rc);
error = TRUE;
}
}
/******************************************************************/
/* Check to see if an error occurred */
/******************************************************************/
if (error)
{
/***************************************************************/
/* If it has, reset the flag and continue on to the next file */
/* since a message should already have been printed */
/***************************************************************/
error = FALSE;
errors += 1;
}
else
{
/***************************************************************/
/* Else, print a message indicating the successful update of */
/* the date/time on the file */
/***************************************************************/
printf("%s: %s has been successfully 'touch'ed.\n", argv[0], FileFindBuf.achName);
}
/******************************************************************/
/* Find the next file matching the same pattern */
/******************************************************************/
count = 1;
rc = DosFindNext(hdir, &FileFindBuf, sizeof(FileFindBuf), &count);
}
/*********************************************************************/
/* Indicate to user that an error occured finding the next file if */
/* a return code other than expected is returned */
/*********************************************************************/
if (rc != ERROR_NO_MORE_FILES)
{
fprintf(stderr, "%s: DosFindNext() failed on %d with OS/2 rc = %d.\n", argv[0], argv[i], rc);
}
}
count = 1;
}
/***************************************************************************/
/* Clean up the DosFindFirst/Next handle that was allocated by the system */
/* and print a message if there was an error */
/***************************************************************************/
rc = DosFindClose(hdir);
if (rc != 0 && rc != ERROR_INVALID_HANDLE)
{
fprintf(stderr, "%s: DosFindClose() failed with OS/2 rc = %d.\n", argv[0], rc);
}
/***************************************************************************/
/* Indicate to user that everything is done */
/***************************************************************************/
printf("%s: All done!\n", argv[0]);
return errors;
}