Created
          March 25, 2012 18:10 
        
      - 
      
- 
        Save ryangraham/2198748 to your computer and use it in GitHub Desktop. 
    Messing around this morning...
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #include <stdio.h> | |
| #include <sys/stat.h> | |
| #include <sys/types.h> | |
| #include <unistd.h> | |
| #include <stdlib.h> | |
| typedef struct { | |
| char *name; | |
| FILE *file; | |
| long inode; | |
| int failure_count; | |
| } log; | |
| int open_log(log *this) | |
| { | |
| struct stat stats; | |
| this->file = fopen(this->name,"r"); | |
| if(this->file != NULL) { | |
| /*grab inode number*/ | |
| fstat(fileno(this->file),&stats); | |
| this->inode = (long)stats.st_ino; | |
| return 0; | |
| } else { | |
| this->failure_count = this->failure_count + 1; | |
| return 1; | |
| } | |
| } | |
| void* init_log(char *path) | |
| { | |
| log *this = NULL; | |
| int err = 0; | |
| if(path == NULL) { | |
| return NULL; | |
| } | |
| this = (log *)malloc(sizeof(log)); | |
| if(this == NULL) { | |
| return (void *)this; | |
| } | |
| this->name = path; | |
| this->failure_count = 0; | |
| err = open_log(this); | |
| if(err != 0) { | |
| return NULL; | |
| } | |
| fseek(this->file,0,SEEK_END); | |
| return (void *)this; | |
| } | |
| int read_line(log *this) | |
| { | |
| char line[1024]; | |
| struct stat stats; | |
| int err = 0; | |
| stat(this->name,&stats); | |
| if(this->inode != (long)stats.st_ino) { | |
| /* if inode no longer matches. re-open. */ | |
| fclose(this->file); | |
| this->file = NULL; | |
| err = open_log(this); | |
| if(err != 0) { | |
| return err; | |
| } | |
| } | |
| if(fgets(line,1023,this->file) != NULL) { | |
| printf("Newest line: %s",line); | |
| } | |
| return 0; | |
| } | |
| int main() | |
| { | |
| char path[] = "test1"; | |
| log *this = NULL; | |
| this = (log *)init_log(path); | |
| printf("Inode: %ld\n",this->inode); | |
| while(1) | |
| { | |
| read_line(this); | |
| } | |
| return 0; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment