系统编程之模拟tail命令

如果是做作业的孩子找到了这里,希望不要直接copy,供参考,其实我做的也不一定好嘻嘻。

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<fcntl.h>
#define BUFFERSIZE 512
char *file;
int getRows(){
    int chars,i,rows=0,fd;
    char buf[BUFFERSIZE];
    fd=open(file,O_RDONLY);
    if(fd==-1)
        return -1;
    while((chars = read(fd, buf, BUFFERSIZE)) > 0){
        i=0;
        while(i<chars){
            if(buf[i]=='
')
                rows++;
            i++;
        }
    }
    close(fd);
    return rows;
}
int toInt(char *str){    
    int res=0;
    int now=1;
    int len=strlen(str);
    int i=len-1;

    while(i>=0){
        res=res+(int)(str[i]-'0')*now;
        now*=10;
        i--;
        if(i==0 && str[i]=='-'){
            return res;
        }
    }
    return res;
}

int isValid(char *str){
    int len = strlen(str);
    int i=0;    
    if(len>=2 && str[0]=='-') i=1; 
    for(;i<len;i++){
        if(str[i]<='0' || str[i] >= '9')
            return -1;
    }
    return 1;
}

void readFile(int isOk,int allRows,int num){
    int chars;
    char buf[BUFFERSIZE];
    int i,cur=0;
    int fd=open(file,O_RDONLY);
        if(isOk==1){
               while((chars = read(fd, buf, BUFFERSIZE))>0){
                    i=0;
                        while(i<chars){
                            if(cur>=(allRows - num)){
                                printf("%c",buf[i]);
                            }    
                            if(buf[i]=='
'){
                                cur++;
                            }    
                            i++;
                        }
                }
             close(fd);
        }
}
int main(int argc,char **argv){
    int ch,fd,cur;
    int num=10,isOk=1;
    int allRows;

    long time1,time2;
    struct stat buf;
    file = (char*)malloc(100*(sizeof(char)));
    
    if(argc==1){
        strcpy(file,"a.txt");
    } else if(argc==2 && strcmp(argv[1],"-n")==0){
        printf("Please input like -n [number] [filename]
");
        return;
    } else if(argc==2 && strcmp(argv[1],"-n")!=0){
        strcpy(file,argv[1]);
    }else if(argc==4){
        strcpy(file,argv[3]); 
    } else {
        printf("Please input like [filename] / -n [number] [filename]
");
        return;
    }
    
    allRows = getRows();
    
    if(allRows == -1){
        printf("File not exists
");
        return;
    }
    if(allRows < 10){
        num = allRows;
    }

    while((ch = getopt(argc, argv,"n:f::"))!=EOF){
        switch(ch){
            case 'n':
                if(isValid(optarg)==-1){
                    printf("The option 'n' is not valid.
");
                    isOk = 0;
                    break;
                }        
                num=toInt(optarg);
                if(num > allRows || num==0){
                    printf("The option 'n' is over the max rows
");
                    isOk = 0;
                    break;
                }
                break;
            case '?':isOk=0;break;
            default:break;
        }
    }
    readFile(isOk,allRows,num);
}
原文地址:https://www.cnblogs.com/rimochiko/p/8168626.html