home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2001 January / VPR0101A.BIN / OLS / TAR32053 / tar32053.exe / SRC / TARDIR.C < prev    next >
C/C++ Source or Header  |  1999-05-23  |  5KB  |  181 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.  *    tardir.c - handle tar directory
  11.  */
  12.  
  13. #ifdef RCSID
  14. static char rcsid[] = "$Header: RCS/tardir.c 2.6 91/02/19 14:27:58 kmori Exp $";
  15. #endif
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21. #include <time.h> /* for debug */
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24.  
  25. #include "defs.h"
  26.  
  27. #include "tar.h"
  28. #include "tardir.h"
  29. #include "main.h"
  30. #include "misc.h"
  31. #include "nkf.h"
  32. #include "tar32.h"
  33.  
  34. static int signed_char_type=0;  /* In LSI-C  unsigned char is default */
  35.  
  36. unsigned compsum(HEADER *block)
  37. {
  38.     int    i;
  39.     unsigned sum;
  40.  
  41.     sum = 0;
  42.     for (i = 0; i < NAMSIZ + 8 + 8 + 8 + 12 + 12; i++)
  43.         sum += signed_char_type ? block->dummy[i]: (unsigned char) block->dummy[i];
  44.     sum += ' ' * 8;
  45.     for (i += 8; i < TBLOCK; i++)
  46.         sum += signed_char_type ? block->dummy[i]: (unsigned char) block->dummy[i];
  47.     return (sum);
  48. }
  49.  
  50. /* 書庫からファイル名を得るときにファイル名をsrcからdestに変換する。*/
  51. global void get_filename_conversion(char *dest,char *src)
  52. {
  53. #ifdef USE_NKF_DLL    /* ...tsuneo */
  54.     SetNkfOption(OPTION_nkf_get_filename_conversion);
  55.     NkfConvert(dest,src);
  56. #else
  57.     strcpy(dest,src);
  58. #endif
  59. }
  60. global char decode_dir_e(char *name, struct stat *p, HEADER *block)
  61. {
  62.     unsigned sum;
  63.     char nametmp[FNAME_BUF];
  64.  
  65. #ifndef    MSC
  66. #define _off_t long
  67. #define time_t long
  68. #endif
  69.  
  70.     p->st_mode = (unsigned short)strtol(block->dbuf.mode, NULL, 8);
  71.     p->st_size = (_off_t)strtol(block->dbuf.size, NULL, 8);
  72.     p->st_mtime = (time_t)strtol(block->dbuf.mtime, NULL, 8);
  73. #if    (MSC_VER==7)
  74.     p->st_mtime += 0x83ABD000;
  75. #endif    
  76. #ifdef    WIN32
  77.     p->st_uid = (short)strtol(block->dbuf.uid, NULL, 8);
  78.     p->st_gid = (short)strtol(block->dbuf.gid, NULL, 8);
  79. #else
  80.     p->st_uid = strtol(block->dbuf.uid, NULL, 8);
  81.     p->st_gid = strtol(block->dbuf.gid, NULL, 8);
  82. #endif
  83.     sum = strtol(block->dbuf.chksum, NULL, 8);
  84.     strncpy(nametmp, block->dbuf.name,NAMSIZ /* 100*/);/*... tsuneo*/
  85.     nametmp[FNAME_BUF - 1] = '\0';
  86.  
  87.     get_filename_conversion(name,nametmp);
  88.     
  89.     if (sum != compsum(block))
  90.         return ('e');
  91.     return (block->dbuf.typeflag);
  92. }
  93.  
  94.  
  95.  
  96. global char decode_dir(char *name, struct stat *p, HEADER *block)
  97. {
  98.     char type;
  99.  
  100.     if ((type = decode_dir_e(name, p, block)) == 'e'){
  101.         char temp;
  102.         signed_char_type = 1;
  103.         temp = decode_dir_e(name, p, block);
  104.         signed_char_type = 0;
  105.         if (temp != 'e'){
  106.             if (Vflag)
  107.                 printf("Warrning: header check sum. this file seems to ceated by old MS-C version.\n");
  108.             type = temp;
  109.         }
  110.     }
  111.     if (type == 'e'){
  112.         Exitcode=ERROR_FILE_CRC;
  113.         puts("dir: Check sum error");
  114.         //fatal("dir", "Check sum error");
  115.     }
  116.     return (type);
  117. }
  118.  
  119.  
  120. /* 書庫に格納する前にファイル名をsrcからdestに変換する */
  121. /* encode_dir及びaddfile_lname(main.c)で使われている*/
  122. global void set_filename_conversion(char *dest,char *src)
  123. {
  124. #ifdef USE_NKF_DLL    /* ...tsuneo */
  125.     SetNkfOption(OPTION_nkf_set_filename_conversion);
  126.     NkfConvert(dest,src);
  127. #else
  128.     strcpy(dest,src);
  129. #endif
  130. }
  131. global void encode_dir(HEADER *block, char *name, struct stat *p, char flag)
  132. {
  133.     char *user;
  134.     char nametmp[FNAME_BUF];
  135.  
  136.     set_filename_conversion(nametmp,name);
  137.  
  138.     strncpy(block->dbuf.name, nametmp,NAMSIZ-1); /* tarnt 0.96->0.97 */
  139.     if ((p->st_mode & S_IFMT) == S_IFDIR)
  140.         addsl(block->dbuf.name);
  141.     sprintf(block->dbuf.mode, "%6o ", (unsigned int)p->st_mode);
  142.     strcpy(block->dbuf.uid, "000000 ");
  143.     strcpy(block->dbuf.gid, "000000 ");
  144.     sprintf(block->dbuf.size, "%11lo ", p->st_size);
  145. /*#ifdef MSC*/
  146. #if (MSC_VER==7)
  147.     p->st_mtime -= 0x83ABD000;
  148. #endif
  149.     sprintf(block->dbuf.mtime, "%11lo ", p->st_mtime);
  150.     block->dbuf.typeflag = flag;
  151.     memset(block->dbuf.linkname, 0, NAMSIZ);
  152.     if (Gflag) {
  153.         memcpy(block->dbuf.magic, TOMAGIC, TOMAGLEN);
  154.     } else {
  155.         memcpy(block->dbuf.magic, TMAGIC, TMAGLEN);
  156.         memcpy(block->dbuf.version, TVERSION, TVERSLEN);
  157.     }
  158.     if ((user = getenv("USER")) != NULL)
  159.         strncpy(block->dbuf.uname, user, sizeof block->dbuf.uname);
  160. #ifdef    WIN32
  161.     else if ((user = getenv("USERNAME")) != NULL)
  162.         strncpy(block->dbuf.uname, user, sizeof block->dbuf.uname);
  163. #endif
  164.     else
  165.         strcpy(block->dbuf.uname, "root");
  166.     sprintf(block->dbuf.chksum, "%6o ", compsum(block));
  167. }
  168.  
  169.  
  170.  
  171. global int eofblock(char *block)
  172. {
  173.     int n;
  174.  
  175.     for (n = TBLOCK; n; n--) {
  176.         if (*block++)
  177.             return (0);
  178.     }
  179.     return (1);
  180. }
  181.