Chinaunix首页 | 论坛 | 博客
  • 博客访问: 495757
  • 博文数量: 102
  • 博客积分: 4001
  • 博客等级: 上校
  • 技术积分: 756
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-21 16:01
文章分类

全部博文(102)

文章存档

2011年(1)

2010年(1)

2009年(56)

2008年(44)

我的朋友

分类: C/C++

2009-02-26 17:20:33

#include
#include
#include
#include "busybox.h"

int main(int argc, char **argv)
{
        int status = EXIT_SUCCESS;  

        if (argc == 1) {         
                print_file(stdin);                   //如果什么参数也没有走print_file(stdin)函数
                return status;
        }

        while (--argc > 0) {                             //有参数时,有几个文件,cat几次
                if(!(strcmp(*++argv, "-"))) {  // 当cat后第一个参数是‘-’则print_file(stdin)
                        print_file(stdin);
                } else if (print_file_by_name(*argv) == FALSE) {  //否则print_file_by_name
                        status = EXIT_FAILURE;
                }
        }
        return status;
}

//下面是print_file.c文件,都是通过busybox.h->libbb.h连接,具体略,下面都类似!


#include
#include
#include
#include "libbb.h"
extern void print_file(FILE *file)
{
        fflush(stdout);                                  //刷新输出缓冲
        copyfd(fileno(file), fileno(stdout));      //调用copyfd函数,从打开的文件流拷贝标准输出;不影响源文件
        fclose(file);                                      //关闭文件流
}

extern int print_file_by_name(char *filename)
{
        struct stat statBuf;
        int status = TRUE;          //默认永远是无连结文件;见下

        if(is_directory(filename, TRUE, &statBuf)==TRUE) {  //判断是否目录
                error_msg("%s: Is directory", filename);
                status = FALSE;
        } else {
                FILE *f = fopen(filename, "r");            //否,打开流文件
                if(f!=NULL)
                        print_file(f);                     //读其中东西到标准输出
                else
                        status = FALSE;
        }

        return status;
}


//下面是copyfd.c文件

#include
#include
#include
#include "libbb.h"


extern int copyfd(int fd1, int fd2)
{
        char buf[8192];
        ssize_t nread, nwrote;

        while (1) {
                nread = safe_read(fd1, buf, sizeof(buf));    //直接调用safe_read读出文件
                if (nread == 0)
                        break;
                if (nread == -1) {
                        perror_msg("read");                
                        return -1;
                }

                nwrote = full_write(fd2, buf, nread);          //调用full_write写到fd2中
                if (nwrote == -1) {
                        perror_msg("write");         
                        return -1;
                }
        }

        return 0;
}



//接下来是is_directory文件

#include
#include
#include
#include "libbb.h"

/*
* Return TRUE if a fileName is a directory.
* Nonexistant files return FALSE.
*/
int is_directory(const char *fileName, const int followLinks, struct stat *statBuf)
{
        int status;
        int didMalloc = 0;

        if (statBuf == NULL) {
            statBuf = (struct stat *)xmalloc(sizeof(struct stat));    //如果不存在状态结构则申请
            ++didMalloc;
        }

        if (followLinks == TRUE)
                status = stat(fileName, statBuf);              //如果无连接则stat得到起状态
        else
                status = lstat(fileName, statBuf);        //如果有连接则lstat得到起状态

        if (status < 0 || !(S_ISDIR(statBuf->st_mode))) { //如果不存在或不是目录,status=f
            status = FALSE;
        }
        else status = TRUE;

        if (didMalloc) {               //如果申请了当然要释放 :)
            free(statBuf);
            statBuf = NULL;
        }
        return status;
}



//后面是safe_read.c文件,和copyfd文件相联系,忘了没有?


#include
#include
#include
#include "libbb.h"


ssize_t safe_read(int fd, void *buf, size_t count)
{
        ssize_t n;

        do {
                n = read(fd, buf, count);                //read直接读文件,返回状态,多了点安全的检查
        } while (n < 0 && errno == EINTR);

        return n;
}

//最后是,full_write.c


int full_write(int fd, const char *buf, int len)
{
        int cc;
        int total;

        total = 0;

        while (len > 0) {
                cc = write(fd, buf, len);      //write写操作

                if (cc < 0)
                        return -1;

                buf += cc;                        //  接着读
                total += cc;                      //  总数返回
                len -= cc;                         //  判断条件
        }

        return total;
}
阅读(1334) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~