统计代码行数的小工具

有时我们需要统计一下代码的行数,作为一个工作量的考评项。所以就写了这段小程序。
/*

 * Name:        zfcntl.c

 * Author:      zhoufanking

 * Date:        6/28/2008

 *

 * Purpose:

 *      Get a file or dir name form command line,count how many lines

 *      in the file or files under the dir.

 *      This is useful when you count the number of lines in your code.

 * Usage:

 *      1.      gcc -o zfcntl zfcntl.c

 *      2.      ./zfcntl  file_name/dir_name

 */



#include <stdio.h>

#include <sys/types.h>

#include <dirent.h>

#include <malloc.h>

#include <assert.h>

#include <sys/stat.h>

#include <stdlib.h>

#include <string.h>



static int totallines = 0;



void  dopath(const char *pathname);

void usage(void)__attribute__((__noreturn__));

int  countf(const char *fname);


int main(int argc, char **argv)

{

    if(argc != 2)

        usage();



    char *fpath = argv[1];

    

    struct stat stinfo;



    if ( lstat(fpath,&stinfo) < 0)

        perror("lstat error!/n");



    if(S_ISDIR(stinfo.st_mode) != 0)

         dopath(fpath);
        
    else

        if(S_ISREG(stinfo.st_mode) != 0)

           totallines =  countf(fpath);
   

    printf("%s contains %d lines./n",fpath,totallines);


    return 0;

}

        
void dopath(const char *pathname)

{

    DIR *dr;

    struct dirent *dp; 

    struct stat *statinfo;
   

    statinfo = (struct stat*) malloc(sizeof(struct stat));

    assert(statinfo);


    char *fullpath;



    fullpath = (char *) malloc( 1024*sizeof(char));

    assert(fullpath);

    memset(fullpath,0,sizeof(fullpath));



    strcat(fullpath,pathname);



    dr = opendir(fullpath);

    

    if(!dr)

    {

        perror("open dir failed!/n");

        free (fullpath);

        free (statinfo);

        fullpath = NULL;

        statinfo = NULL;

        exit (-1);

    }



    while( (dp = readdir(dr) ) != NULL)

    {   

        if(dp->d_name[0] =='.')

            continue;

     

        strcat(fullpath,dp->d_name);

        

        if (lstat(fullpath,statinfo)<0)

            perror("dopath():lstat error/n");



        if( S_ISDIR(statinfo->st_mode)!=0)

        {

            strcat(fullpath,"/");

            dopath(fullpath);

        }

        else    // encounter a regular file, count the lines number

        {

            totallines += countf(fullpath);

        }

        memset(fullpath,0,sizeof(fullpath));

        strcat(fullpath,pathname);

    }

    

    closedir(dr);

    

    free (fullpath);

    free (statinfo);

    fullpath = NULL;

    statinfo = NULL;

        

}



void usage()

{

    printf("Usage:cntl filename/directotyname/n");

    exit(-1);

}



int countf(const char *fname)

{

    FILE *fp;

    int  cur;

    int linescnt = 0;



    if( fp = fopen(fname,"r"),fp==NULL )

    {

        printf("count():cannot open file %s/n",fname);

        return -1;

    }



    fseek(fp,0,SEEK_SET);      

    

    while(cur = fgetc(fp),cur != EOF)

    {

        if(cur == '/n')

            linescnt++;

        else

            continue;

    }



    fclose(fp);

    return linescnt;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值