- size_t grepline(char **lineptr, size_t *n, FILE * stream)
- {
- size_t len = 0;
- size_t offset = ftell(stream);
- char trash = 0;
- char * buf = NULL;
- char sizeofnewline = 0;
-
- if(lineptr != NULL){
- free(*lineptr);
- }
-
- do{
- trash = fgetc(stream);
- ++len;
- }while(!feof(stream) && trash != '\n' && trash != '\r');
-
- if(trash == '\r'){
- trash = fgetc(stream);
- if(trash == '\n'){
- ++sizeofnewline;
- }
- }
-
- fseek(stream,offset,SEEK_SET);
- buf = (char*)malloc(len+1);
- fread(buf,len,1,stream);
- if(sizeofnewline){
- trash = fgetc(stream);
- }
-
- buf[len-1] = '\n';
- buf[len] = '\0';
-
- *n = len;
- *lineptr = buf;
- return len;
- }
You must be logged in to paste new items to the PasteBin