home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / futils / futils~1 / src / misc1s.zoo / misc1 / combine / unix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-06  |  2.6 KB  |  156 lines

  1. #include <sys/types.h>
  2. #include <sys/ioctl.h>
  3. #include <sys/file.h>
  4. #include "util.h"
  5. #include "os_dep.h"
  6. #include <stdio.h>
  7. #include <ctype.h>
  8.  
  9. /*
  10.  * os_dep_file_pos -- Return current position in file
  11.  *
  12.  * This routine returns the current position in the file. The value
  13.  * returned is suitable for passing to os_dep_file_seek and is suitable
  14.  * for no other purpose.
  15.  *
  16.  * Return value:
  17.  *   Current position in the file.
  18.  */
  19. rfa_type os_dep_file_pos (fp)
  20. FILE * fp;
  21. {
  22.  
  23.     return (ftell (fp));
  24. }
  25. /*
  26.  * os_dep_file_read -- Read a record from a file.
  27.  *
  28.  * This routine reads a record from a file.
  29.  *
  30.  * Return value:
  31.  *   SS_NORMAL      Operation performed as requested.
  32.  *   SS_EOT         End of disk area reached
  33.  *   SS_READ_ERR    Error on read operation.
  34.  *   SS_SEEK_ERR    Seek error occured
  35.  *
  36.  */
  37.  
  38. STS_type os_dep_file_read (fp,
  39.     buf_ptr,
  40.     buf_len,
  41.     ret_len_ptr)
  42. FILE * fp;
  43. char   *buf_ptr;
  44. int     buf_len;
  45.  
  46. int    *ret_len_ptr;    /* output */
  47.              /* Returns the number of bytes read. */
  48.              /* A value of -1 indicates that an EOF record was read */
  49.  
  50. {
  51.     char    c;
  52.     char   *char_ptr;
  53.     STS_type status;
  54.     int     i;
  55.  
  56.  
  57.     if (TRUE) {
  58.  
  59.         char_ptr = fgets (buf_ptr, buf_len + 2, fp);
  60.         /* printf( "(%d:%d) %s", buf_len, strlen( buf_ptr), buf_ptr ) ;
  61.         */
  62.  
  63.         if (char_ptr == NULL) {
  64.             return (SS_EOT);
  65.         }
  66.  
  67.         i = strlen (buf_ptr) - 1;
  68.         if (buf_ptr[i] != '\n') {
  69.             i++;
  70.             for (;;) {
  71.                 c = getc (fp);
  72.                 if (feof (fp)) {
  73.                 /* not (c==EOF) because of binary files */
  74.                     break;
  75.                 } else if (c == '\n') {
  76.                     break;
  77.                 }
  78.             }
  79.         }
  80.         *ret_len_ptr = i;
  81.         buf_ptr[i] = '\0';
  82.  
  83.     } else {
  84.  
  85.         status = read (fileno (fp), buf_ptr, buf_len);
  86.         if (status == (-1)) {
  87.             return (SS_READ_ERR);
  88.         }
  89.  
  90.     }
  91.  
  92.     return (SS_NORMAL);
  93.  
  94. }
  95.  
  96. /*
  97.  *
  98.  * os_dep_file_seek -- Seek to record within file
  99.  *
  100.  * This routine seek to a particular record within a file.
  101.  *
  102.  * Return value:
  103.  *   SS_NORMAL      Operation performed as requested.
  104.  *   SS_SEEK_ERR    Error on seek operation.
  105.  *
  106.  */
  107.  
  108. STS_type os_dep_file_seek (fp, rfa)
  109. FILE * fp;
  110. rfa_type rfa;
  111.  
  112. {
  113.  
  114.     STS_type status;
  115.  
  116.  
  117.  
  118.     status = fseek (fp, rfa, 0);
  119.  
  120.     if (status == -1) {
  121.         return (SS_SEEK_ERR);
  122.     } else {
  123.         return (SS_NORMAL);
  124.     }
  125.  
  126. }
  127. /*
  128.  *
  129.  * os_dep_file_write -- Write a record to a file.
  130.  *
  131.  * This routine writes a record to a file.
  132.  *
  133.  * Return value:
  134.  *   SS_NORMAL      Operation performed as requested.
  135.  *   SS_WRITE_ERR   Error on write operation.
  136.  *
  137.  */
  138.  
  139. STS_type os_dep_file_write (fp,
  140.     buf_ptr,
  141.     buf_len)
  142. FILE * fp;
  143. char   *buf_ptr;
  144. int     buf_len;        /* buffer length in bytes */
  145.  
  146. {
  147.  
  148.  
  149.  
  150.     buf_ptr[buf_len] = '\0';
  151.     fputs (buf_ptr, fp);
  152.     fputc ('\n', fp);
  153.     return (SS_NORMAL);
  154.  
  155. }
  156.