home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 1997 December
/
VPR9712A.ISO
/
OLS
/
OS2
/
LHA2P205
/
LHA2P205.LZH
/
lha2-2.05pre
/
source.lzh
/
src
/
util.c
< prev
Wrap
C/C++ Source or Header
|
1996-02-25
|
4KB
|
283 lines
/*
* util.c --- utility routines
* Copyright (C) 1988-1992, Haruyasu YOSHIZAKI
* Copyright (C) 1991-1996, Satoshi HIRAMATSU (OS/2 HPFS version)
*
* $Log$
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <io.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h>
#include "port2.h"
#include "typedef.h"
#include "lh.h"
#include "intrface.h"
#include "slidehuf.h"
#include "errmes.h"
#include "disp.h"
uchar pathdelim = '\\';
uchar swchar = '/';
/*
* convert path delimiter
*
* returns *filename
*/
char *
convdelim(char *path, char delim)
{
uchar c;
uchar *p;
int kflg;
kflg = 0;
for(p = path; (c = *p) != 0; p++)
{
if(kflg)
kflg = 0;
else if(iskanji(c))
kflg = 1;
else if(c == '\\' || c == DELIM || c == DELIM2)
{
*p = delim;
path = p + 1;
}
}
return path;
}
char *
getfilename(char *path)
{
char *q;
if((q = strrchr(path, DELIM)) != NULL || (q = strchr(path, ':')) != NULL)
return q + 1;
else
return path;
}
/* get back to parent directory */
char *
backpath(char *p)
{
char *q;
q = getfilename(p);
*q = '\0';
return q;
}
/* ask 'Y' or 'N' */
int
getyn(void)
{
int yn;
do
{
yn = getch();
yn = toupper(yn);
if(yn == '\x03')
error(CTRLBRK, NULL);
}
while(yn != 'Y' && yn != 'N');
eprintf("%c\n", yn);
return yn;
}
void
getswchar(void)
{
swchar = '-';
pathdelim = '/';
}
time_t
dos2unix(struct ftime *ft)
{
struct tm tm;
tm.tm_sec = ft -> ft_tsec * 2;
tm.tm_min = ft -> ft_min;
tm.tm_hour = ft -> ft_hour;
tm.tm_mday = ft -> ft_day;
tm.tm_mon = ft -> ft_month - 1;
tm.tm_year = ft -> ft_year + 80;
#ifdef __API16__
tm.tm_isdst = timezone;
#else
tm.tm_isdst = _timezone;
#endif
return mktime(&tm);
}
struct ftime *
unix2dos(time_t utc)
{
static struct ftime ft;
struct tm *tm;
tm = localtime(&utc);
ft.ft_tsec = tm -> tm_sec / 2;
ft.ft_min = tm -> tm_min ;
ft.ft_hour = tm -> tm_hour;
ft.ft_day = tm -> tm_mday;
ft.ft_month = tm -> tm_mon + 1;
ft.ft_year = tm -> tm_year - 80;
return &ft;
}
/* ratio * 1000 */
int
ratio(ulong a, ulong b, int ord)
{
int i;
for(i = 0; i < ord && a < 0x19999999; i++)
a *= 10; /* while not overflow */
/* upto 10^ord times */
for(; i < ord; i++) /* the case of overflow */
b /= 10;
if(b == 0) /* if diviser == 0 */
return 0;
a += b / 2; /* for round up */
return (a / b); /* return (a * 1000 / b) */
}
hword
copyfile(FILE *f1, FILE *f2, long size, int crc_flg)
{
int xsize;
int dicsiz;
hword crc = 0;
int read;
if(f2)
fflush(f2);
dicsiz = 1 << interface.dicbit;
dispflg = crc_flg;
while(size > 0)
{
xsize = (size > TEXT_SIZE) ? TEXT_SIZE : size;
if((read = fread(text, 1, xsize, f1)) != xsize)
fileerror(RDERR, f1);
fwrite_crc(text, xsize, f2, &crc);
size -= xsize;
if(crc_flg)
{
while(xsize > 0)
{
if(!flg_n)
dispmark('c');
xsize -= dicsiz;
}
}
}
return crc;
}
#ifndef __DEBUG__
void *
e_malloc(size_t size)
{
void *p;
if((p = malloc(size)) == NULL)
error(MEMOVRERR, NULL);
return p;
}
void *
e_realloc(void *buf, size_t size)
{
void *p;
if((p = realloc(buf, size)) == NULL)
error(MEMOVRERR, NULL);
return p;
}
#endif
FILE *
myeopen(const char *path, const char *mode, char *errmes)
{
FILE *f;
int fd;
f = NULL;
if(*mode != 'w')
f = fopen(path, mode);
else
{
fd = open(path, O_CREAT|O_TRUNC|O_BINARY|O_RDWR, S_IREAD|S_IWRITE);
if(fd >= 0)
f = fdopen(fd, mode);
}
if(f == NULL && errmes != NULL)
error(errmes, path);
return f;
}
FILE *
mywopen(const char *path, char *errmes)
{
return myeopen(path, "wb", errmes);
}
FILE *
myropen(const char *path)
{
return myeopen(path, "rb", NOFILEERR);
}
void
eprintf(char *fmt, ...)
{
va_list p;
va_start(p, fmt);
vfprintf(stderr, fmt, p);
va_end(p);
fflush(stderr);
}