2012-04-14 87 views
1

我有/ proc文件。我不明白一些閱讀功能的說法。 閱讀功能看起來像:如何讀到/ proc文件的末尾

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

我不知道如何使用offset參數。

一些實例的使用offset參數那樣:

if (offset > 0) 
return 0; 

和他們解釋說:這是重要的,因爲從庫標準讀取功能將繼續issuethe讀系統調用直到內核答覆說,它沒有更多信息,或直到其緩衝區被填滿。

我有一個比緩衝區大的文件。我怎麼讀到文件的末尾?

回答

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 [email protected]) 
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       */