home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / V2 / DJLSR200.ZIP / src / libc / ansi / stdio / ftell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-04  |  1.1 KB  |  54 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <libc/stubs.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <libc/file.h>
  6. #include <fcntl.h>
  7. #include <libc/dosio.h>
  8.  
  9. long
  10. ftell(FILE *f)
  11. {
  12.   long tres;
  13.   int adjust=0;
  14.   int idx;
  15.  
  16.   if (f->_cnt < 0)
  17.     f->_cnt = 0;
  18.   if (f->_flag&_IOREAD)
  19.   {
  20.     if (__file_handle_modes[f->_file] & O_TEXT) /* if a text file */
  21.     {
  22.       if (f->_cnt && f->_ptr != f->_base)
  23.       {
  24.     char *cp;
  25.     adjust = - f->_bufsiz + (f->_ptr-f->_base);
  26.     for (cp=f->_base; cp < f->_ptr; cp++) /* for every char in buf */
  27.       if (*cp == '\n')    /* if it's LF */
  28.         adjust++;        /* there was a CR also */
  29.       }
  30.     }
  31.     else
  32.       adjust = - f->_cnt;
  33.   }
  34.   else if (f->_flag&(_IOWRT|_IORW))
  35.   {
  36.     adjust = 0;
  37.     if (f->_flag&_IOWRT && f->_base && (f->_flag&_IONBF)==0)
  38.     {
  39.       adjust = f->_ptr - f->_base;
  40.       if (__file_handle_modes[f->_file] & O_TEXT)
  41.     for (idx=0; idx<adjust; idx++)
  42.       if (f->_base[idx] == '\n')
  43.         adjust++;
  44.     }
  45.   }
  46.   else
  47.     return -1;
  48.   tres = lseek(fileno(f), 0L, 1);
  49.   if (tres<0)
  50.     return tres;
  51.   tres += adjust;
  52.   return tres;
  53. }
  54.