home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 2001 January
/
VPR0101A.BIN
/
OLS
/
TAR32053
/
tar32053.exe
/
SRC
/
MISC.C
< prev
next >
Wrap
C/C++ Source or Header
|
1999-05-23
|
10KB
|
556 lines
#ifndef __DEFCONF_H
#include "defconf.h"
#endif
/*
This file was hacked for kmtar for WIN32
at 1996-05-06.
by tantan SGL00213@niftyserve.or.jp
*/
/*
* misc - miscellaneous functions
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <errno.h>
#ifdef MSC
#include <io.h>
#endif
#include "defs.h"
#include "main.h"
#include "misc.h"
#include <sys/stat.h>
#include "archio.h"
#include <setjmp.h>
extern jmp_buf jmp_env;
extern char g_command;
extern bool wild_flag;
#if __DJGPP__==2
char **__crt0_glob_function(char *_argument)
{
return 0;
}
#endif
/*
* Report error, s: object msg: reason
*/
global void error(char *s, char *msg)
{
fprintf(stderr, "%s: ", Progname);
if (msg == NULL) {
if (errno == 0)
fprintf(stderr, "%s: Device not ready\n", s);
else
perror(s);
} else {
if (s)
fprintf(stderr, "%s: ", s);
fprintf(stderr, "%s\n", msg);
}
/* Exitcode = 1;*/ /* comment out by tsuneo*/
}
/*
* Report error and terminate program
*/
global void fatal(char *s, char *msg)
{
if (s != NULL || msg != NULL)
error(s, msg);
if(Exitcode==0)Exitcode = 1;/* ...tsuneo*/
if (wild_flag && g_command == 't'){
// close_arch('t');
longjmp(jmp_env,1);
}
exit(Exitcode);
}
char *mbschr(unsigned char *str,int c)
{
while(*str){
if(*str == c){
return str;
}
if(iskanji(c)){str++;}
str++;
}
return NULL;
}
/*
* Return true if string t's head is same as string p
*/
global int strmatch(char *t, char *p)
{
return (strncmp(t, p, strlen(p)) == 0);
}
int strcmpl(char *s, char *t)
{
for (; *s; s++, t++) {
if (iskanji(*s) && iskanji2(s[1])
&& iskanji(*t) && iskanji2(t[1])) {
if (*s != *t) {
break;
}
s++;
t++;
if (*s != *t) {
break;
}
} else if (iskanji(*s) && iskanji2(s[1])
|| iskanji(*t) && iskanji2(t[1])) {
break;
} else if (isascii(*s) && isascii(*t)) {
if (tolower(*s) != tolower(*t)) {
break;
}
} else if (*s != *t) {
break;
}
}
return (*s - *t);
}
FILE *fopenpath(char *file)
{
FILE *fp;
char *p, *d;
char buf[FNAME_BUF];
if ((p = getenv("TAR")) != NULL) {
strcat(addsl(strcpy(buf, p)), file);
if ((fp = fopen(buf, "r")) != NULL)
return (fp);
}
if ((fp = fopen(file, "r")) != NULL)
return (fp);
if ((p = getenv("PATH")) != NULL) {
while (*p) {
d = buf;
while (*p && *p != ';')
*d++ = *p++;
*d = '\0';
strcat(addsl(buf), file);
if ((fp = fopen(buf, "r")) != NULL)
return (fp);
if (*p == ';')
p++;
}
}
return (NULL);
}
/*
* Return device-description string
*/
global char *get_dev_alias(char *device)
{
FILE *fp;
char *p, *s;
char line[201];
if ((fp = fopenpath("_tar")) == NULL)
return (NULL);
while (fgets(line, 200, fp)) {
for (p = line; isspace(*p); p++)
;
if (*p == '#' || *p == '\0')
continue;
for (s = p; *s && !isspace(*s); s++)
;
if (*s == '\0')
continue;
*s++ = '\0';
if (device == NULL || strcmpl(p, device) == 0) {
while (isspace(*s))
s++;
for (p = s; *p && !isspace(*p); p++)
;
*p = '\0';
if ((p = malloc(strlen(s) + 1)) == NULL)
fatal("misc", "Out of memory");
strcpy(p, s);
fclose(fp);
return (p);
}
}
fclose(fp);
return (NULL);
}
/*
* Append '/' to string buf
*/
global char *addsl(char *buf)
{
char *p;
int c;
if (buf[0]) {
c = '\0';
p = buf;
while (*p != '\0') {
c = *p;
if (iskanji(*p) && iskanji2(p[1])) {
p++;
}
p++;
}
if (c != '/' && c != ':') {
strcat(buf, "/");
}
}
return (buf);
}
/*
* Read with ASCII conversion
*/
global int read_a(int fd, char *buff, int size)
{
int n;
char *p, *q, *t;
do {
n = read(fd, buff, size);
if (n == 0 || !Aflag)
break;
t = buff + n;
for (q = p = buff; p < t; ) {
char *s;
if (*p == '\r')
p++;
s = memchr(p, '\r', t - p);
if (s == NULL)
s = t;
if (p != q)
memcpy(q, p, s - p);
q += s - p;
p = s;
}
n = q - buff;
} while (n == 0);
return (n);
}
/*
* EUC filter from " Network Kanji Filter. (PDS Version)"
** Copyright (C) Fujitsu LTD. (Itaru ICHIKAWA), 1987
** (E-Mail Address: ichikawa@flab.fujitsu.co.jp)
*
*/
int s_oconv (int fd,int c2,int c1)
{
unsigned char buf[5];
if (c2 == 0){
buf[0] = (unsigned char)(c1 & 0x7f);
return write (fd,buf,1);
}else {
c2 &= 0x07f;
c1 &= 0x07f;
buf[0] = (unsigned char)((c2 - 1) >> 1) + ((c2 <= 0x5f) ? 0x71 : 0xb1);
buf[1] = (unsigned char)(c1 + ((c2 & 1) ? ((c1 < 0x60) ? 0x1f : 0x20) : 0x7e));
return write(fd,buf,2);
}
}
/*
* Write with EUC filter
*/
global int write_f(int fd, unsigned char *buff, int size)
{
static int c1,c2=0;
int i;
if (!EucFlag)
return write(fd,buff,size);
for(i=0;i<size;i++,buff++){
if ((c1 = *buff) == EOF)
return -1;
if (!c2 && c1 >0x7f){
c2 = c1;
continue;
}
if (s_oconv(fd,c2,c1) == -1)
return -1;
c2 = 0;
}
return i;
}
/*
* Write with ASCII conversion
*/
global int write_a(int fd, char *buff, int size)
{
char *q, *p, *s, *t;
int n, m;
char buff2[512];
if (!Aflag)
return (write_f(fd, (unsigned char *)buff, size));
m = 0;
t = buff + size;
q = buff2;
for (s = p = buff; p < t; ) {
s = memchr(s, '\n', t - p);
if (s == NULL)
s = t;
do {
n = s - p;
if (q + n > buff2 + sizeof buff2)
n = buff2 + sizeof buff2 - q;
memcpy(q, p, n);
q += n;
p += n;
if (q >= buff2 + sizeof buff2) {
m += write_f(fd, (unsigned char *)buff2, sizeof buff2);
q = buff2;
}
} while (p < s);
if (s < t)
*q++ = '\r';
s++;
}
if (q > buff2)
m += write_f(fd, (unsigned char *)buff2, q - buff2);
return (m);
}
/*
* Return 1 if contents of file fd and *buff are different
*/
global int compare_a(int fd, char *buff, int size)
{
char buff2[512];
while (size > 0) {
int n = sizeof buff2;
if (n > size)
n = size;
n = read_a(fd, buff2, n);
if (n == 0 || memcmp(buff, buff2, n) != 0)
return 1;
size -= n;
buff += n;
}
return 0;
}
/*
* Return real size of file fd
*/
global long realsize(int fd)
{
char buff[512];
int n;
long size;
char *p, *t;
size = 0;
while ((n = read(fd, buff, sizeof buff)) > 0) {
t = buff + n;
for (p = buff;(long)(p = memchr(p, '\r', t - p)); p++)
n--;
size += n;
}
lseek(fd, 0L, 0);
return (size);
}
/*
* Convert date-string s (mm/dd[/yyyy] [hh:mm[:ss]]) to time_t type
* djgpp の関数名と衝突するので名前を変更。 1994-08-03 tantan
* getname -> __getname
*/
global time_t __getdate(char *s)
{
time_t now;
struct tm t, *nowp;
memset(&t, 0, sizeof t);
now = time(NULL);
nowp = localtime(&now);
t.tm_mon = strtol(s, &s, 10) - 1;
if (*s++ != '/')
return (time_t)(-1L);
t.tm_mday = strtol(s, &s, 10);
if (*s == '/') {
s++;
t.tm_year = strtol(s, &s, 10);
if (t.tm_year >= 1900)
t.tm_year -= 1900;
} else {
t.tm_year = nowp->tm_year;
if (t.tm_mon > nowp->tm_mon
|| t.tm_mon == nowp->tm_mon && t.tm_mday > nowp->tm_mday)
t.tm_year--;
}
while (isspace(*s))
s++;
if (*s) {
t.tm_hour = strtol(s, &s, 10);
if (*s != ':')
return (-1L);
s++;
t.tm_min = strtol(s, &s, 10);
if (*s == ':') {
s++;
t.tm_sec = strtol(s, &s, 10);
}
}
while (isspace(*s))
s++;
if (*s)
return (-1L);
return (mktime(&t));
}
#ifndef WIN32
/*
* Check machine PC9801 or PC/AT or FMR
*/
global int chk_machine(void)
{
unsigned short *id=(unsigned short *)0xffff0003;
}
#endif
/* strncpyの改造版。countを超えないようにコピーし、必ず最後に'\0'がつくようにした。*/
/* by tsuneo */
global char *strncpy2( char *string1, const char *string2, size_t count )
{
if (strlen(string2)<count){
strcpy(string1,string2);
}else{ /* strlen(string2)>=count */
strncpy(string1,string2,count-1);
string1[count-1]='\0';
}
return string1;
}
/* ファイルが存在するか? ...tsuneo */
global int existfile(char *fname)
{
FILE *fp;
if((fp=fopen(fname,"r"))==NULL){
return 0;
}else{
fclose(fp);
return 1;
}
}
/* ファイルのdirnameとbasenameから、ディレクトリ付きのファイル名を得る */
/* (例) c:\hoo(dirname) bar(basename) -> c:\hoo\bar(filename) */
char *make_filename(char *fname,int fname_limit,const char *dirname,const char *basename)
{
char delimiter[2]="";
/* basenameが一文字以上あると仮定すれば長さのチェックは不要 */
if(dirname==NULL || *dirname=='\0' || *basename=='\\' || (*basename != '\0' && (strncmp(basename+1,":\\",2)==0 || strncmp(basename+1,":/",2)==0))){
strcpy(fname,basename);
return fname;
}
if(dirname[strlen(dirname)-1] != '\\'){
strcpy(delimiter,"\\");
}
if(strlen(dirname)+strlen(delimiter)+strlen(basename)+1 >= (size_t)fname_limit){
return NULL;
}
sprintf(fname,"%s%s%s",dirname,delimiter,basename);
return fname;
}
#ifdef DLL
/* split CommandLine String to Array */
/* 文字列cmdLineをmainに渡すコマンド引数に分割する。*/
char * splitCmdLine(int *argcPtr,char **argv,const char *cmdLine)
{
unsigned char *tmpCmdLine;
unsigned char *p;
int argc=0;
tmpCmdLine=strdup(cmdLine);
p=tmpCmdLine;
/* argv[argc++]="tar"; */
if(*p!='\0' && (!isspace(*p))){
argv[argc++]=p;
}
while(*p!='\0'){
if(isspace(*p)){
*p='\0';
p++;
while(isspace(*p)){
p++;
}
if(*p!='\0'){
argv[argc++]=p;
}
}else if(*p=='"'){
p++;
while((*p!='\0') && (*p!='"')){
if(iskanji(*p) && iskanji2(*(p+1))){
p++;p++;
}else{
p++;
}
}
if(*p=='\0'){break;}
p++;
}else{
p++;
}
}
argv[argc]=NULL;
*argcPtr=argc;
{
/* Cut * from argv(s) */
int i=0;
while(argv[i]!=NULL){
char *oldp=argv[i]; /* pointer that include [*] */
char *newp=oldp; /* pointer that not include [*] */
while(*oldp!='\0'){
if(*oldp!='"'){
*newp=*oldp;
newp++;
}
oldp++;
}
*newp=*oldp;
i++;
}
}
return tmpCmdLine;
}
#endif /* DLL */