如何读到/ proc文件的末尾

时间:2012-04-14 16:36:40

标签: c linux-kernel proc

我有/ proc文件。我不明白读函数的一些论点。 读函数看起来像这样:

int procfile_read(char *buffer,  char **buffer_location,  off_t offset, int buffer_length, int *eof, void *data)

我不知道如何使用偏移量参数。

一些示例使用偏移参数,如:

if (offset > 0)
return 0;

他们解释说:这很重要,因为来自库的标准读取函数将继续发出读取系统调用,直到内核回复它没有更多信息,或者直到其缓冲区被填满。

我的文件大于缓冲区。我怎么能读到文件的末尾???

1 个答案:

答案 0 :(得分:2)

请参阅proc_file_read函数实现,尤其是注释:

  74                        /*
  75                         * How to be a proc read function
  76                         * ------------------------------
  77                         * Prototype:
  78                         *    int f(char *buffer, char **start, off_t offset,
  79                         *          int count, int *peof, void *dat)
  80                         *
  81                         * Assume that the buffer is "count" bytes in size.
  82                         *
  83                         * If you know you have supplied all the data you
  84                         * have, set *peof.
  85                         *
  86                         * You have three ways to return data:
  87                         * 0) Leave *start = NULL.  (This is the default.)
  88                         *    Put the data of the requested offset at that
  89                         *    offset within the buffer.  Return the number (n)
  90                         *    of bytes there are from the beginning of the
  91                         *    buffer up to the last byte of data.  If the
  92                         *    number of supplied bytes (= n - offset) is 
  93                         *    greater than zero and you didn't signal eof
  94                         *    and the reader is prepared to take more data
  95                         *    you will be called again with the requested
  96                         *    offset advanced by the number of bytes 
  97                         *    absorbed.  This interface is useful for files
  98                         *    no larger than the buffer.
  99                         * 1) Set *start = an unsigned long value less than
 100                         *    the buffer address but greater than zero.
 101                         *    Put the data of the requested offset at the
 102                         *    beginning of the buffer.  Return the number of
 103                         *    bytes of data placed there.  If this number is
 104                         *    greater than zero and you didn't signal eof
 105                         *    and the reader is prepared to take more data
 106                         *    you will be called again with the requested
 107                         *    offset advanced by *start.  This interface is
 108                         *    useful when you have a large file consisting
 109                         *    of a series of blocks which you want to count
 110                         *    and return as wholes.
 111                         *    (Hack by Paul.Russell@rustcorp.com.au)
 112                         * 2) Set *start = an address within the buffer.
 113                         *    Put the data of the requested offset at *start.
 114                         *    Return the number of bytes of data placed there.
 115                         *    If this number is greater than zero and you
 116                         *    didn't signal eof and the reader is prepared to
 117                         *    take more data you will be called again with the
 118                         *    requested offset advanced by the number of bytes
 119                         *    absorbed.
 120                         */