home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
kill2.zip
/
kill2src.zip
/
xtime.c
< prev
Wrap
C/C++ Source or Header
|
1992-12-21
|
4KB
|
165 lines
#define INCL_DOS
#include <os2.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <io.h>
#include <fcntl.h>
#include <share.h>
#define SECS_PER_DAY 86400L
struct _ftime { /* Structs & unions for file date/time */
unsigned int sec: 5;
unsigned int min: 6;
unsigned int hour:5;
};
struct _fdate {
unsigned int day: 5;
unsigned int mon: 4;
unsigned int year:7;
};
union _fd {
struct _fdate fd;
unsigned int x;
};
union _ft {
struct _ftime ft;
unsigned int x;
};
struct _file_buffer { /* For DosFindFirst/DosFindNext */
union _fd cd;
union _ft ct;
union _fd ld;
union _ft lt;
union _fd wd;
union _ft wt;
long size;
long falloc;
unsigned int attr;
unsigned char namelen;
char filename[257];
};
time_t _fastcall dosfile_2_unix (struct _ftime *ft,struct _fdate *fd) {
struct tm tm;
memset(&tm,0,sizeof(tm));
tm.tm_sec = ft->sec;
tm.tm_min = ft->min;
tm.tm_hour = ft->hour;
tm.tm_mday = fd->day;
tm.tm_mon = fd->mon - 1;
tm.tm_year = fd->year + 80;
return mktime(&tm);
}
/* note 1980 base */
time_t _fastcall unixgetftime (int handle) {
struct tm tm;
struct {
struct _fdate cfd;
struct _ftime cft;
struct _fdate afd;
struct _ftime aft;
struct _fdate wfd;
struct _ftime wft;
long size;
long alc;
int attrib;
} fdata;
if(DosQFileInfo(handle,1,&fdata,sizeof(fdata))) return -1L;
memset(&tm,0,sizeof(tm));
tm.tm_sec = fdata.wft.sec;
tm.tm_min = fdata.wft.min;
tm.tm_hour = fdata.wft.hour;
tm.tm_mday = fdata.wfd.day;
tm.tm_mon = fdata.wfd.mon - 1;
tm.tm_year = fdata.wfd.year + 80;
return mktime(&tm);
}
int _fastcall unixsetftime (int handle,time_t t) {
struct tm *tm;
struct {
struct _fdate cfd;
struct _ftime cft;
struct _fdate afd;
struct _ftime aft;
struct _fdate wfd;
struct _ftime wft;
} fdata;
char macmode;
tm = localtime(&t);
if(tm) {
fdata.wft.sec = tm->tm_sec;
fdata.wft.min = tm->tm_min;
fdata.wft.hour = tm->tm_hour;
fdata.wfd.day = tm->tm_mday;
fdata.wfd.mon = tm->tm_mon + 1;
fdata.wfd.year = tm->tm_year - 80;
fdata.cfd = fdata.afd = fdata.wfd;
fdata.cft = fdata.aft = fdata.wft;
DosGetMachineMode(&macmode);
if(!macmode) { /* Messydos alert -- gaaah! run away! */
memset(&fdata.cfd,0,sizeof(fdata.cfd));
memset(&fdata.cft,0,sizeof(fdata.cft));
fdata.afd = fdata.cfd;
fdata.aft = fdata.cft;
}
return
DosSetFileInfo(handle,1,&fdata,sizeof(fdata));
}
return -1;
}
int _fastcall oldenough (char *filename,ULONG days) {
int handle,ret = 0;
time_t filetime,curtime;
ULONG daysdiff;
if(!days) return 1; /* always match if 0 */
handle = sopen(filename,O_RDONLY | O_BINARY,SH_DENYNO);
if(handle == -1)
return 0; /* can't open */
filetime = unixgetftime(handle);
if(filetime != -1L) {
curtime = time(NULL);
if(curtime != -1L) {
if(filetime < curtime) {
daysdiff = (curtime - filetime) / SECS_PER_DAY;
if(daysdiff >= days)
ret = 1;
}
}
}
close(handle);
return ret;
}