⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cat.c

📁 linux下实现cat操作,主要使显示文件的内容
💻 C
字号:
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

#define  BUFFSIZE 4096

int main(int argc,char *argv[])
{
    char         path[255];
    int          size;
    int          fd;
    int          n;
    int          i;
    struct stat  statbuf;
    void         *buf;
    /*if(argc == 1)
    {
        while(1)
        {
           if((n = read(STDIN_FILENO,buf,BUFFSIZE)) < 0)
           {
              perror("read error!\n");
              exit(0);
           }
           if((n = write(STDOUT_FILENO,buf,n)) != n)
           {
              perror("write error!\n");
              exit(0);
           }
        }
    }*/
    if(argc == 2&&(strcmp(argv[1],"--help") == 0))
    {
         printf("Usage:cat [FILE]\n");
         printf("concatenate FILE(s) or standard input to standard output\n");
    }
    else
    {  
       for(i = 1;i < argc;i++)
       {
          if(lstat(argv[i],&statbuf) < 0)
          {
               perror("lstat error!");
               exit(0);
          }
          if(S_ISDIR(statbuf.st_mode))
          {
               printf("cat: %s: Is a directory!\n",argv[1]);
               exit(0);
          }
          else
               size = statbuf.st_size; 
          if((fd = open(argv[i],O_RDONLY)) == -1)
          {
               perror("open error!\n");
               exit(0);
          }
          if(read(fd,buf,size) < 0)
          {
               perror("read error!\n");
               exit(0);
          }
          if(write(STDOUT_FILENO,buf,size) != size)
          {
               perror("write error!\n");
               exit(0);
          }
       }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -