home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1998 February / PCOnline_02_1998.iso / filesbbs / os2 / pgp263.arj / PGP263I.SRC / PGP263II.ZIP / src / c370.c < prev    next >
C/C++ Source or Header  |  1995-06-19  |  10KB  |  337 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "c370.h"
  5.  
  6. void printhex( void *buf, int len )
  7. {  int i;
  8.    char *s = buf;
  9.    while (len >= 0) {
  10.       for (i=0;i < 16 && len >= 0; s++,i++,len--) {
  11.          int ch = (*s) & 0xff;
  12.          printf( "%3d ", ch );
  13.       }
  14.       printf( "\n" );
  15.    }
  16.    printf( "\n" );
  17. }
  18.  
  19. #undef fseek
  20. int myfseek( FILE *stream, long int offset, int origin )
  21. {  int i;
  22.    i = fseek( stream, offset, origin );
  23.    if (i != 0 && origin == SEEK_CUR) {
  24.       i = fseek( stream, ftell(stream) + offset, SEEK_SET );
  25.       if (i != 0)
  26.          printf("myfseek: ftell=%i, orig=%i, offset=%i, ret=%i\n",
  27.                 ftell(stream), origin, offset, i );
  28.    }
  29.    return i;
  30. }
  31.  
  32. #undef fread
  33. int myfread( void *buffer, size_t size, size_t count, FILE *stream )
  34. {  size_t i;
  35.    i = fread( buffer, size, count, stream );
  36.    if (count == 1 && size == 1 && i == 1) {  /* skip padding zeroes */
  37.       char ch = *(char *)buffer;
  38.       fpos_t pos;
  39.       fgetpos( stream, &pos );
  40.       while ( ch == '\0' && fread(&ch, 1, 1, stream) == 1);
  41.       fsetpos( stream, &pos );
  42.       if (ch == '\0')
  43.          return 0;
  44.    }
  45.    return i;
  46. }
  47.  
  48. int      fileno( FILE *fp )
  49. {
  50.    return (int)fp;
  51. }
  52.  
  53. int      access( const char *filename, int how )
  54. {
  55.    struct stat buf;
  56.    return stat( filename, &buf );       /* dummy 'access' */
  57. }
  58.  
  59. int      close( int fd )
  60. {
  61.    return fclose( (FILE *)fd );
  62. }
  63.  
  64. int      isatty( int fd )
  65. {
  66.    return (fd==((int)stdin) || fd==((int)stdout) || fd==((int)stderr));
  67. }
  68.  
  69. off_t    lseek( int fd, off_t offset, int whence )
  70. {
  71.    return fseek( (FILE *)fd, offset, whence );
  72. }
  73.  
  74. size_t   read( int fd, void *buf, size_t size )
  75. {
  76.    return fread( buf, 1, size, (FILE *)fd );
  77. }
  78.  
  79. int      unlink( char *filename )
  80. {
  81.    return remove( filename );
  82. }
  83.  
  84. int      write( int fd, void *buf, size_t len )
  85. {
  86.    return fwrite( buf, 1, len, (FILE *)fd );
  87. }
  88.  
  89. char dcb_flags[50];
  90.  
  91. static void access2mode( int access, char *mode)
  92. {
  93.    /* O_EXCL not supported */
  94.    switch (access & ~(O_BINARY | O_MEMORY | O_EXCL | O_DCB)) {
  95.       case O_RDONLY                       : strcpy( mode, "r" ); break;
  96.       case O_RDWR                         : strcpy( mode, "r+" ); break;
  97.       case O_WRONLY                       :
  98.       case O_CREAT | O_WRONLY             : strcpy( mode, "w" ); break;
  99.       case O_CREAT | O_RDWR               : strcpy( mode, "w+" ); break;
  100.       case O_APPEND | O_WRONLY            :
  101.       case O_APPEND | O_CREAT | O_WRONLY  : strcpy( mode, "a" ); break;
  102.       case O_APPEND | O_RDWR              :
  103.       case O_APPEND | O_CREAT | O_RDWR    : strcpy( mode, "a+" ); break;
  104.       default                             : strcpy( mode, "" );
  105.    }
  106.    if (access & O_BINARY) strcat( mode, "b" );
  107.    if (access & O_MEMORY) strcat( mode, ",type=memory" );
  108.    if (access & O_RECORD) strcat( mode, ",type=record" );
  109.    if (access & O_DCB) strcat( mode, dcb_flags );
  110. }
  111.  
  112. int  open(const char *path, int access)
  113. {
  114.    char mode[30];
  115.    FILE *f;
  116.    access2mode( access, mode );
  117.    f = fopen( path, mode );
  118.    return (f == NULL ? -1 : fileno(f) );
  119. }
  120.  
  121. int  eof( int fd )
  122. {
  123.     return feof( (FILE *)fd );
  124. }
  125.  
  126. int  tell( int fd )
  127. {
  128.     return ftell( (FILE *)fd );
  129. }
  130.  
  131. int  setmode(int fd, int access)
  132. {
  133.    char mode[30];
  134.    FILE *f;
  135.  
  136.    switch (fd) {
  137.       case 0: access |= O_RDONLY; fd = fileno(stdin);  break;
  138.       case 1: access |= O_WRONLY; fd = fileno(stdout); break;
  139.       case 2: access |= O_WRONLY; fd = fileno(stderr); break;
  140.    }
  141.    access2mode( access, mode );
  142.    f = freopen( "", mode, (FILE *)fd );
  143.    return (f == NULL ? -1 : fileno(f) );
  144. }
  145.  
  146. int    chmod( const char* filename, mode_t mode )
  147. {
  148.    return 0;
  149. }
  150.  
  151. int    stat(const char *filename, struct stat *buf )
  152. {
  153.    if ((buf->fp = fopen(filename, "r")) != NULL) {
  154.       fldata_t fdata;
  155.       if (fldata( buf->fp, buf->fname, &fdata ) == 0) {
  156.          buf->st_dev  = fdata.__device;
  157.          buf->st_mode = *(short *)(&fdata);
  158.       }
  159.       strcpy( buf->fname, filename );
  160.       fseek( buf->fp, 0, SEEK_END );
  161.       buf->st_size = ftell( buf->fp );
  162.       fclose(buf->fp);
  163.    }
  164.    return (buf->fp != NULL ? 0 : -1);
  165. }
  166.  
  167. int    fstat(int fd, struct stat *buf )
  168. {
  169.    fldata_t fdata;
  170.  
  171.    if ((fd != -1) && (fldata( (FILE *)fd, buf->fname, &fdata ) == 0))
  172.    {
  173.       buf->st_dev  = fdata.__device;
  174.       buf->st_mode = *(short *)(&fdata);
  175.       buf->fp      = (FILE *)fd;
  176.       return 0;
  177.    }
  178.    return -1;
  179. }
  180.  
  181.  
  182. #define ALIAS_MASK  (unsigned int) 0x80
  183. #define SKIP_MASK   (unsigned int) 0x1F
  184. #define TTRLEN      3
  185. #define RECLEN      254
  186. typedef _Packed struct {
  187.    unsigned short int count;
  188.    char rest[RECLEN];
  189. } RECORD;
  190. char    *endmark = "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF";
  191.  
  192. static int gen_node( DIR *dirp, RECORD *recptr )
  193. {
  194.    char *ptr, *name, ttr[TTRLEN];
  195.    int skip, count = 2;
  196.    unsigned int info_byte, alias, ttrn;
  197.    struct dirent *new;
  198.  
  199.    ptr = recptr->rest;
  200.    while (count < recptr->count) {
  201.       if (!memcmp( ptr, endmark, NAMELEN ))
  202.          return 1;
  203.       name = ptr;                    /* member name */
  204.       ptr += NAMELEN;
  205.       memcpy( ttr, ptr, TTRLEN );    /* ttr name    */
  206.       ptr += TTRLEN;
  207.       info_byte = (unsigned int) (*ptr);   /* info byte */
  208.       if ( !(info_byte & ALIAS_MASK) ) {   /* no alias  */
  209.          new = malloc( sizeof(struct dirent) );
  210.          if (dirp->D_list == NULL)
  211.             dirp->D_list = dirp->D_curpos = new;
  212.          else
  213.             dirp->D_curpos = (dirp->D_curpos->d_next = new);
  214.          new->d_next = NULL;
  215.          memcpy( new->d_name, name, NAMELEN );
  216.          new->d_name[NAMELEN] = '\0';
  217.          if ((name = strchr( new->d_name, ' ' )) != NULL)
  218.             *name = '\0';      /* skip trailing blanks */
  219.       }
  220.       skip = (info_byte & SKIP_MASK) * 2 + 1;
  221.       ptr += skip;
  222.       count += (TTRLEN + NAMELEN + skip);
  223.    }
  224.    return 0;
  225. }
  226.  
  227. DIR *opendir(const char *dirname)
  228. {
  229.    int bytes, list_end = 0;
  230.    DIR *dirp;
  231.    FILE *fp;
  232.    RECORD rec;
  233.  
  234.    fp = fopen( dirname, "rb" );
  235.    if (fp != NULL) {
  236.       dirp = malloc( sizeof(DIR) );
  237.       if (dirp != NULL) {
  238.          dirp->D_list = dirp->D_curpos = NULL;
  239.          strcpy( dirp->D_path, dirname );
  240.          do {
  241.             bytes = fread( &rec, 1, sizeof(rec), fp );
  242.             if (bytes == sizeof(rec))
  243.                list_end = gen_node( dirp, &rec );
  244.          } while (!feof(fp) && !list_end);
  245.          fclose( fp );
  246.          dirp->D_curpos = dirp->D_list;
  247.          return dirp;
  248.       }
  249.       fclose( fp );
  250.    }
  251.    return NULL;
  252. }
  253.  
  254. struct dirent *readdir(DIR *dirp)
  255. {
  256.    struct dirent *cur;
  257.  
  258.    cur = dirp->D_curpos;
  259.    if (cur != NULL)
  260.       dirp->D_curpos = cur->d_next;
  261.    return cur;
  262. }
  263.  
  264. void rewinddir(DIR *dirp)
  265. {
  266.    dirp->D_curpos = dirp->D_list;
  267. }
  268.  
  269. int closedir(DIR *dirp)
  270. {
  271.    struct dirent *node;
  272.  
  273.    while (dirp->D_list != NULL) {
  274.       node = dirp->D_list;
  275.       dirp->D_list = dirp->D_list->d_next;
  276.       free( node );
  277.    }
  278.    free( dirp );
  279.    return 0;
  280. }
  281.  
  282.  
  283.  
  284.  
  285. /* ebcdic-ascii converting, accustom to your local MVS-settings */
  286.  
  287. unsigned char ebcdic_ascii[] = {
  288.     0x00,0x00,0x00,0x00,0x00,0x09,0x00,0x00, 0x00,0x00,0x00,0x0b,0x0c,0x0d,0x00,0x00,
  289.     0x00,0x00,0x00,0x00,0x00,0x0a,0x08,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  290.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,
  291.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  292.     0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x2e,0x3c,0x28,0x2b,0x7c,
  293.     0x26,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x21,0x24,0x2a,0x29,0x3b,0x5e,
  294.     0x2d,0x2f,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x7c,0x2c,0x25,0x5f,0x3e,0x3f,
  295.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x60,0x3a,0x23,0x40,0x27,0x3d,0x22,
  296.     0x00,0x61,0x62,0x63,0x64,0x65,0x66,0x67, 0x68,0x69,0x00,0x00,0x00,0x00,0x00,0x00,
  297.     0x00,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,0x70, 0x71,0x72,0x00,0x00,0x00,0x00,0x00,0x00,
  298.     0x00,0x7e,0x73,0x74,0x75,0x76,0x77,0x78, 0x79,0x7a,0x00,0x00,0x00,0x5b,0x00,0x00,
  299.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x5d,0x00,0x00,
  300.     0x7b,0x41,0x42,0x43,0x44,0x45,0x46,0x47, 0x48,0x49,0x00,0x00,0x00,0x00,0x00,0x00,
  301.     0x7d,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50, 0x51,0x52,0x00,0x00,0x00,0x00,0x00,0x00,
  302.     0x5c,0x00,0x53,0x54,0x55,0x56,0x57,0x58, 0x59,0x5a,0x00,0x00,0x00,0x00,0x00,0x00,
  303.     0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37, 0x38,0x39,0x00,0x00,0x00,0x00,0x00,0x00
  304. };
  305.  
  306. unsigned char ascii_ebcdic[] = {
  307.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x2f, 0x16,0x05,0x15,0x0b,0x0c,0x0d,0x00,0x00,
  308.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  309.     0x40,0x5a,0x7f,0x7b,0x5b,0x6c,0x50,0x7d, 0x4d,0x5d,0x5c,0x4e,0x6b,0x60,0x4b,0x61,
  310.     0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7, 0xf8,0xf9,0x7a,0x5e,0x4c,0x7e,0x6e,0x6f,
  311.     0x7c,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7, 0xc8,0xc9,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,
  312.     0xd7,0xd8,0xd9,0xe2,0xe3,0xe4,0xe5,0xe6, 0xe7,0xe8,0xe9,0xad,0xe0,0xbd,0x5f,0x6d,
  313.     0x79,0x81,0x82,0x83,0x84,0x85,0x86,0x87, 0x88,0x89,0x91,0x92,0x93,0x94,0x95,0x96,
  314.     0x97,0x98,0x99,0xa2,0xa3,0xa4,0xa5,0xa6, 0xa7,0xa8,0xa9,0xc0,0x6a,0xd0,0xa1,0x00,
  315.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  316.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  317.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  318.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  319.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  320.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  321.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  322.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  323. };
  324.  
  325. void ebcdic2ascii( unsigned char *buf, unsigned size )
  326. {
  327.    while (size--)
  328.      *(buf+size) = ebcdic_ascii[ *(buf+size) ];
  329. }
  330.  
  331. void ascii2ebcdic( unsigned char *buf, unsigned size )
  332. {
  333.    while (size--)
  334.      *(buf+size) = ascii_ebcdic[ *(buf+size) ];
  335. }
  336.  
  337.