home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2001 January / VPR0101A.BIN / OLS / TAR32053 / tar32053.exe / SRC / MISC.C < prev    next >
C/C++ Source or Header  |  1999-05-23  |  10KB  |  556 lines

  1. #ifndef __DEFCONF_H
  2. #include "defconf.h"
  3. #endif
  4. /*
  5.    This file was hacked for kmtar for WIN32
  6.                                     at 1996-05-06.
  7.                                     by tantan SGL00213@niftyserve.or.jp 
  8. */
  9. /*
  10.  * misc - miscellaneous functions
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <time.h>
  17. #include <ctype.h>
  18. #include <errno.h>
  19.  
  20. #ifdef MSC
  21.     #include <io.h>
  22. #endif
  23.  
  24. #include "defs.h"
  25.  
  26. #include "main.h"
  27. #include "misc.h"
  28.  
  29.     #include <sys/stat.h>
  30.     #include "archio.h"
  31.     #include <setjmp.h>
  32.     extern jmp_buf    jmp_env;
  33.     extern char    g_command;
  34.     extern bool    wild_flag;
  35.  
  36. #if    __DJGPP__==2
  37. char **__crt0_glob_function(char *_argument)
  38. {
  39.    return 0;
  40. }
  41. #endif
  42.  
  43. /*
  44.  * Report error, s: object msg: reason
  45.  */
  46. global void error(char *s, char *msg)
  47. {
  48.     fprintf(stderr, "%s: ", Progname);
  49.     if (msg == NULL) {
  50.         if (errno == 0)
  51.             fprintf(stderr, "%s: Device not ready\n", s);
  52.         else
  53.             perror(s);
  54.     } else {
  55.         if (s)
  56.             fprintf(stderr, "%s: ", s);
  57.         fprintf(stderr, "%s\n", msg);
  58.     }
  59.     /* Exitcode = 1;*/ /* comment out by tsuneo*/
  60. }
  61.  
  62. /*
  63.  * Report error and terminate program
  64.  */
  65. global void fatal(char *s, char *msg)
  66. {
  67.     if (s != NULL || msg != NULL) 
  68.     error(s, msg);
  69.     if(Exitcode==0)Exitcode = 1;/* ...tsuneo*/
  70.     if (wild_flag && g_command == 't'){
  71.         // close_arch('t');
  72.         longjmp(jmp_env,1);
  73.     }
  74.     exit(Exitcode);
  75. }
  76.  
  77.  
  78. char *mbschr(unsigned char *str,int c)
  79. {
  80.     while(*str){
  81.         if(*str == c){
  82.             return str;
  83.         }
  84.         if(iskanji(c)){str++;}
  85.         str++;
  86.     }
  87.     return NULL;
  88. }
  89. /*
  90.  * Return true if string t's head is same as string p
  91.  */
  92. global int strmatch(char *t, char *p)
  93. {
  94.     return (strncmp(t, p, strlen(p)) == 0);
  95. }
  96.  
  97.  
  98.  
  99. int    strcmpl(char *s, char *t)
  100. {
  101.     for (; *s; s++, t++) {
  102.         if (iskanji(*s) && iskanji2(s[1])
  103.             && iskanji(*t) && iskanji2(t[1])) {
  104.             if (*s != *t) {
  105.                 break;
  106.             }
  107.             s++;
  108.             t++;
  109.             if (*s != *t) {
  110.                 break;
  111.             }
  112.         } else if (iskanji(*s) && iskanji2(s[1])
  113.                  || iskanji(*t) && iskanji2(t[1])) {
  114.             break;
  115.         } else if (isascii(*s) && isascii(*t)) {
  116.             if (tolower(*s) != tolower(*t)) {
  117.                 break;
  118.             }
  119.         } else if (*s != *t) {
  120.             break;
  121.         }
  122.     }
  123.     return (*s - *t);    
  124. }
  125.  
  126.  
  127.  
  128. FILE *fopenpath(char *file)
  129. {
  130.     FILE *fp;
  131.     char *p, *d;
  132.     char buf[FNAME_BUF];
  133.  
  134.     if ((p = getenv("TAR")) != NULL) {
  135.         strcat(addsl(strcpy(buf, p)), file);
  136.         if ((fp = fopen(buf, "r")) != NULL)
  137.             return (fp);
  138.     }
  139.     if ((fp = fopen(file, "r")) != NULL)
  140.         return (fp);
  141.     if ((p = getenv("PATH")) != NULL) {
  142.         while (*p) {
  143.             d = buf;
  144.             while (*p && *p != ';')
  145.                 *d++ = *p++;
  146.             *d = '\0';
  147.             strcat(addsl(buf), file);
  148.             if ((fp = fopen(buf, "r")) != NULL)
  149.                 return (fp);
  150.             if (*p == ';')
  151.                 p++;
  152.         }
  153.     }
  154.     return (NULL);
  155. }
  156.  
  157.  
  158.  
  159. /*
  160.  * Return device-description string
  161.  */
  162. global char *get_dev_alias(char *device)
  163. {
  164.     FILE *fp;
  165.     char *p, *s;
  166.     char line[201];
  167.  
  168.     if ((fp = fopenpath("_tar")) == NULL)
  169.         return (NULL);
  170.     while (fgets(line, 200, fp)) {
  171.         for (p = line; isspace(*p); p++)
  172.             ;
  173.         if (*p == '#' || *p == '\0')
  174.             continue;
  175.         for (s = p; *s && !isspace(*s); s++)
  176.             ;
  177.         if (*s == '\0')
  178.             continue;
  179.         *s++ = '\0';
  180.         if (device == NULL || strcmpl(p, device) == 0) {
  181.             while (isspace(*s))
  182.                 s++;
  183.             for (p = s; *p && !isspace(*p); p++)
  184.                 ;
  185.             *p = '\0';
  186.             if ((p = malloc(strlen(s) + 1)) == NULL)
  187.                 fatal("misc", "Out of memory");
  188.             strcpy(p, s);
  189.             fclose(fp);
  190.             return (p);
  191.         }
  192.     }
  193.     fclose(fp);
  194.     return (NULL);
  195. }
  196.  
  197.  
  198.  
  199. /*
  200.  * Append '/' to string buf
  201.  */
  202. global char *addsl(char *buf)
  203. {
  204.     char *p;
  205.     int c;
  206.  
  207.     if (buf[0]) {
  208.         c = '\0';
  209.         p = buf;
  210.         while (*p != '\0') {
  211.             c = *p;
  212.             if (iskanji(*p) && iskanji2(p[1])) {
  213.                 p++;
  214.             }
  215.             p++;
  216.         }
  217.         if (c != '/' && c != ':') {
  218.             strcat(buf, "/");
  219.         }
  220.     }
  221.     return (buf);
  222. }
  223.  
  224.  
  225.  
  226. /*
  227.  * Read with ASCII conversion
  228.  */
  229. global int read_a(int fd, char *buff, int size)
  230. {
  231.     int n;
  232.     char *p, *q, *t;
  233.  
  234.     do {
  235.         n = read(fd, buff, size);
  236.         if (n == 0 || !Aflag)
  237.             break;
  238.         t = buff + n;
  239.         for (q = p = buff; p < t; ) {
  240.             char *s;
  241.  
  242.             if (*p == '\r')
  243.                 p++;
  244.             s = memchr(p, '\r', t - p);
  245.             if (s == NULL)
  246.                 s = t;
  247.             if (p != q)
  248.                 memcpy(q, p, s - p);
  249.             q += s - p;
  250.             p = s;
  251.         }
  252.         n = q - buff;
  253.     } while (n == 0);
  254.     return (n);
  255. }
  256.  
  257. /* 
  258.  * EUC filter from " Network Kanji Filter. (PDS Version)"
  259.  ** Copyright (C) Fujitsu LTD. (Itaru ICHIKAWA), 1987
  260.  ** (E-Mail Address: ichikawa@flab.fujitsu.co.jp)
  261.  *
  262.  */
  263. int s_oconv (int fd,int c2,int c1)
  264. {
  265.     unsigned char buf[5];
  266.  
  267.     if (c2 == 0){
  268.         buf[0] = (unsigned char)(c1 & 0x7f);
  269.         return write (fd,buf,1);
  270.     }else {
  271.         c2 &= 0x07f;
  272.         c1 &= 0x07f;
  273.         buf[0] = (unsigned char)((c2 - 1) >> 1) + ((c2 <= 0x5f) ? 0x71 : 0xb1);
  274.         buf[1] = (unsigned char)(c1 + ((c2 & 1) ? ((c1 < 0x60) ? 0x1f : 0x20) : 0x7e));
  275.         return write(fd,buf,2);
  276.     }
  277. }
  278.  
  279. /*
  280.  * Write with EUC filter
  281.  */
  282. global int write_f(int fd, unsigned char *buff, int size)
  283. {
  284.     static int c1,c2=0;
  285.     int i;
  286.  
  287.     if (!EucFlag)
  288.         return write(fd,buff,size);
  289.     for(i=0;i<size;i++,buff++){
  290.         if ((c1 = *buff) == EOF)
  291.             return -1;
  292.         if (!c2 && c1 >0x7f){
  293.             c2 = c1;
  294.             continue;
  295.         }
  296.         if (s_oconv(fd,c2,c1) == -1)
  297.             return -1;
  298.         c2 = 0;
  299.     }
  300.     return i;
  301. }    
  302. /*
  303.  * Write with ASCII conversion
  304.  */
  305. global int write_a(int fd, char *buff, int size)
  306. {
  307.     char *q, *p, *s, *t;
  308.     int n, m;
  309.     char buff2[512];
  310.  
  311.     if (!Aflag)
  312.         return (write_f(fd, (unsigned char *)buff, size));
  313.     m = 0;
  314.     t = buff + size;
  315.     q = buff2;
  316.     for (s = p = buff; p < t; ) {
  317.         s = memchr(s, '\n', t - p);
  318.         if (s == NULL)
  319.             s = t;
  320.         do {
  321.             n = s - p;
  322.             if (q + n > buff2 + sizeof buff2)
  323.                 n = buff2 + sizeof buff2 - q;
  324.             memcpy(q, p, n);
  325.             q += n;
  326.             p += n;
  327.             if (q >= buff2 + sizeof buff2) {
  328.                 m += write_f(fd, (unsigned char *)buff2, sizeof buff2);
  329.                 q = buff2;
  330.             }
  331.         } while (p < s);
  332.         if (s < t)
  333.             *q++ = '\r';
  334.         s++;
  335.     }
  336.     if (q > buff2)
  337.         m += write_f(fd, (unsigned char *)buff2, q - buff2);
  338.     return (m);
  339. }
  340.  
  341.  
  342.  
  343. /*
  344.  * Return 1 if contents of file fd and *buff are different
  345.  */
  346. global int compare_a(int fd, char *buff, int size)
  347. {
  348.     char buff2[512];
  349.  
  350.     while (size > 0) {
  351.         int n = sizeof buff2;
  352.         if (n > size)
  353.             n = size;
  354.         n = read_a(fd, buff2, n);
  355.         if (n == 0 || memcmp(buff, buff2, n) != 0)
  356.             return 1;
  357.         size -= n;
  358.         buff += n;
  359.     }
  360.     return 0;
  361. }
  362.  
  363.  
  364.  
  365. /*
  366.  * Return real size of file fd
  367.  */
  368. global long realsize(int fd)
  369. {
  370.     char buff[512];
  371.     int n;
  372.     long size;
  373.     char *p, *t;
  374.  
  375.     size = 0;
  376.     while ((n = read(fd, buff, sizeof buff)) > 0) {
  377.         t = buff + n;
  378.         for (p = buff;(long)(p = memchr(p, '\r', t - p)); p++)
  379.             n--;
  380.         size += n;
  381.     }
  382.     lseek(fd, 0L, 0);
  383.     return (size);
  384. }
  385.  
  386.  
  387.  
  388. /*
  389.  * Convert date-string s (mm/dd[/yyyy] [hh:mm[:ss]]) to time_t type
  390.  * djgpp の関数名と衝突するので名前を変更。 1994-08-03 tantan
  391.  * getname  -> __getname
  392.  */
  393. global time_t __getdate(char *s)
  394. {
  395.     time_t now;
  396.     struct tm t, *nowp;
  397.  
  398.     memset(&t, 0, sizeof t);
  399.     now = time(NULL);
  400.     nowp = localtime(&now);
  401.     t.tm_mon = strtol(s, &s, 10) - 1;
  402.     if (*s++ != '/')
  403.         return (time_t)(-1L);
  404.     t.tm_mday = strtol(s, &s, 10);
  405.     if (*s == '/') {
  406.         s++;
  407.         t.tm_year = strtol(s, &s, 10);
  408.         if (t.tm_year >= 1900)
  409.             t.tm_year -= 1900;
  410.     } else {
  411.         t.tm_year = nowp->tm_year;
  412.         if (t.tm_mon > nowp->tm_mon
  413.            || t.tm_mon == nowp->tm_mon && t.tm_mday > nowp->tm_mday)
  414.             t.tm_year--;
  415.     }
  416.     while (isspace(*s))
  417.         s++;
  418.     if (*s) {
  419.         t.tm_hour = strtol(s, &s, 10);
  420.         if (*s != ':')
  421.             return (-1L);
  422.         s++;
  423.         t.tm_min = strtol(s, &s, 10);
  424.         if (*s == ':') {
  425.             s++;
  426.             t.tm_sec = strtol(s, &s, 10);
  427.         }
  428.     }
  429.     while (isspace(*s))
  430.         s++;
  431.     if (*s)
  432.         return (-1L);
  433.     return (mktime(&t));
  434. }
  435.  
  436. #ifndef    WIN32
  437. /*
  438.  * Check machine PC9801 or PC/AT or FMR
  439.  */
  440. global int chk_machine(void)
  441. {
  442.     unsigned short *id=(unsigned short *)0xffff0003;
  443.  
  444. }
  445. #endif
  446.  
  447. /* strncpyの改造版。countを超えないようにコピーし、必ず最後に'\0'がつくようにした。*/
  448. /*            by tsuneo */
  449. global char *strncpy2( char *string1, const char *string2, size_t count )
  450. {
  451.     if (strlen(string2)<count){
  452.         strcpy(string1,string2);
  453.     }else{    /* strlen(string2)>=count */
  454.         strncpy(string1,string2,count-1);
  455.         string1[count-1]='\0';
  456.     }
  457.     return string1;
  458. }
  459.  
  460. /* ファイルが存在するか? ...tsuneo */
  461. global int existfile(char *fname)
  462. {
  463.     FILE *fp;
  464.     if((fp=fopen(fname,"r"))==NULL){
  465.         return 0;
  466.     }else{
  467.         fclose(fp);
  468.         return 1;
  469.     }
  470. }
  471.  
  472. /*  ファイルのdirnameとbasenameから、ディレクトリ付きのファイル名を得る */
  473. /*  (例)  c:\hoo(dirname)  bar(basename)  -> c:\hoo\bar(filename)  */
  474. char *make_filename(char *fname,int fname_limit,const char *dirname,const char *basename)
  475. {
  476.     char delimiter[2]="";
  477.     
  478.     /* basenameが一文字以上あると仮定すれば長さのチェックは不要 */
  479.     if(dirname==NULL || *dirname=='\0' || *basename=='\\' || (*basename != '\0' && (strncmp(basename+1,":\\",2)==0  || strncmp(basename+1,":/",2)==0))){
  480.         strcpy(fname,basename);
  481.         return fname;
  482.     }
  483.     if(dirname[strlen(dirname)-1] != '\\'){
  484.         strcpy(delimiter,"\\");
  485.     }
  486.     if(strlen(dirname)+strlen(delimiter)+strlen(basename)+1 >= (size_t)fname_limit){
  487.         return NULL;
  488.     }
  489.     sprintf(fname,"%s%s%s",dirname,delimiter,basename);
  490.     return fname;
  491. }
  492.  
  493. #ifdef DLL
  494. /* split CommandLine String to Array */
  495. /* 文字列cmdLineをmainに渡すコマンド引数に分割する。*/
  496. char * splitCmdLine(int *argcPtr,char **argv,const char *cmdLine)
  497. {
  498.     unsigned char *tmpCmdLine;
  499.     unsigned char *p;
  500.     int argc=0;
  501.     
  502.     tmpCmdLine=strdup(cmdLine);
  503.     p=tmpCmdLine;
  504.  
  505.     /* argv[argc++]="tar"; */
  506.     if(*p!='\0' && (!isspace(*p))){
  507.         argv[argc++]=p;
  508.     }
  509.     while(*p!='\0'){
  510.         if(isspace(*p)){
  511.             *p='\0';
  512.             p++;
  513.             while(isspace(*p)){
  514.                 p++;
  515.             }
  516.             if(*p!='\0'){
  517.                 argv[argc++]=p;
  518.             }
  519.         }else if(*p=='"'){
  520.             p++;
  521.             while((*p!='\0') && (*p!='"')){
  522.                 if(iskanji(*p) && iskanji2(*(p+1))){
  523.                     p++;p++;
  524.                 }else{
  525.                     p++;
  526.                 }
  527.             }
  528.             if(*p=='\0'){break;}
  529.             p++;
  530.         }else{
  531.             p++;
  532.         }
  533.     }
  534.     argv[argc]=NULL;
  535.     *argcPtr=argc;
  536.     {
  537.     /* Cut * from argv(s) */
  538.         int i=0;
  539.         while(argv[i]!=NULL){
  540.             char *oldp=argv[i];    /* pointer that include [*] */
  541.             char *newp=oldp;    /* pointer that not include [*] */
  542.             while(*oldp!='\0'){
  543.                 if(*oldp!='"'){
  544.                     *newp=*oldp;
  545.                     newp++;
  546.                 }
  547.                 oldp++;
  548.             }
  549.             *newp=*oldp;
  550.             i++;
  551.         }
  552.     }
  553.             
  554.     return tmpCmdLine;
  555. }
  556. #endif /* DLL */