ffsseeeekk() -- STDIO Function (libc)

Seek on file stream
#iinncclluuddee <ssttddiioo.hh>
iinntt ffsseeeekk(_f_p, _w_h_e_r_e, _h_o_w)
FFIILLEE *_f_p; lloonngg _w_h_e_r_e; iinntt _h_o_w;

ffsseeeekk() changes  where the next  read or write operation  will occur within
the file stream _f_p. It handles  any effects the seek routine might have had
on the  internal buffering strategies  of the system.   The arguments _w_h_e_r_e
and _h_o_w  specify the desired  seek position.  _w_h_e_r_e indicates  the new seek
position in  the file.  It  is measured from  the start of the  file if _h_o_w
equals  SSEEEEKK_SSEETT  (zero), from  the  current seek  position  if _h_o_w  equals
SSEEEEKK_CCUURR (one),  and from the  end of the  file if _h_o_w  equals two SSEEEEKK_EENNDD
(two).

ffsseeeekk()  differs from  its cousin  llsseeeekk()  in that  llsseeeekk() is  a COHERENT
system call  and takes a file  number, whereas ffsseeeekk() is  a STDIO function
and takes a FFIILLEE pointer.

_E_x_a_m_p_l_e
This  example opens  file aarrggvv[11]  and prints  its last  aarrggvv[22] characters
(default,  100).   It  demonstrates  the  functions ffsseeeekk(),  fftteellll(),  and
ffcclloossee().

#include <stdio.h>
extern long atol();

void fatal(message)
char *message;
{
    fprintf(stderr, "tail: %s\n", message);
    exit(1);
}

main(argc, argv)
int argc; char *argv[];
{
    register FILE *ifp;
    register int c;
    long nchars, size;

    if (argc < 2 || argc > 3)
        fatal("Usage: tail file [ nchars ]");
    nchars = (argc == 3) ? atol(argv[2]) : 100L;

    if ((ifp = fopen(argv[1], "r")) == NULL)
        fatal("cannot open input file");
    /* Seek to end */
    if (fseek(ifp, 0L, 2) == -1)
        fatal("seek error");

    /* Find current size */
    size = ftell(ifp);
    size = (size < nchars) ? 0L : size - nchars;

    /* Seek to point */
    if (fseek(ifp, size, 0) == -1)
        fatal("seek error");
    while ((c = getc(ifp)) != EOF)
        /* Copy rest to stdout */
        putchar(c);
    if (fclose(ifp) == EOF)
        fatal("cannot close");
    exit(0);
}

_S_e_e _A_l_s_o
ffsseettppooss(), fftteellll(), lliibbcc, llsseeeekk()
ANSI Standard, section 7.9.9.2
POSIX Standard, section 8.1

_D_i_a_g_n_o_s_t_i_c_s
For any  diagnostic error, ffsseeeekk() returns -1;  otherwise, it returns zero.
If ffsseeeekk()  goes beyond the  end of the  file, it will not  return an error
message until the corresponding read or write is performed.
