Home

Traffic Server Software Developers Kit

INKfopen

Reads a line from a file to a buffer.

Prototype

INKFile INKfopen (const char *filename, const char *mode)

Arguments

filename is the name of the file to open.

mode specifies whether to open the file for reading or writing. If mode is:

  • r, then the file is opened for reading.
  • w, then the file is opened for writing.
  • a, then the file is opened for appending.

Currently r , w, and a are the only two valid modes for opening a file.

Description

Opens a file for reading/writing and returns a descriptor for accessing the file. Descriptors of type INKFile can be greater than 256. INKfopen can open a file for reading or for writing, but not both (this is a limitation of the current implementation).

Example

The following example is taken from the append-transform plugin, which appends text to the end of HTTP response bodies. This subroutine loads the text to be added from a file.

static int
load (const char *filename)
{
  INKFile fp;
  INKIOBufferBlock blk;
  INKIOBufferData data;
  char *p;
  int avail;
  int err;

  fp = INKfopen (filename, "r");
  if (!fp) {
    return 0;
  }

  append_buffer = INKIOBufferCreate ();
  append_buffer_reader = INKIOBufferReaderAlloc (append_buffer); 

  for (;;) {
    blk = INKIOBufferStart (append_buffer);
    p = INKIOBufferBlockWriteStart (blk, &avail); 

    err = INKfread (fp, p, avail);
    if (err > 0) {
      INKIOBufferProduce (append_buffer, err); 
    } else {
      break;
    }
  }

  append_buffer_length = INKIOBufferReaderAvail (append_buffer_reader); 

  INKfclose (fp);
  return 1;
}