Introduction

This article describes the origins of Unix cat command and covers one of the ways how it could be implemented in C programming language.

Cat Command Origins

The cat command originated in 1970, and is one of the standard commands from Unix operating system.
It can be used for:

  • concatenating files
  • printing files to the standard output

The command's name is believed to have derived from the word (con)catenate, as one of its purposes is to concatenate files. [1],[2]

How to use it

There are a number of applications how the cat command can be used when entered into the Terminal command-line interface (CLI)[2].
# 1. cat command and a single file name: this will display the contents of a single file:

            
              cat [file_name]
            
          

# 2. cat command and multiple file names: this will concatenate multiple files and then display contents of these:

            
              cat [file_names]
            
          

# 3. cat command and options and file

            
              cat [options] [file_names]
            
          

It can also be used in combination with other commands, for example grep.
The below command would identify a word ‘coding’ in file_1.txt file:

            
              cat file_1.txt | grep coding
            
          

How to implement it in C

An implementation of the cat program in C programming language could consist of the following steps:

  • create a file descriptor which will point to the open file
  • create a buffer to read the contents of the file into
  • open the file
  • read the file
  • close the file, in order to free the file pointer for another file

Below is an example of a cat program:

            
              #include <stdio.h>
              #include <fcntl.h>  // for open()
              #include <unistd.h> // for read() and for close()


              int main(int ac, char **av)
              {
                  if (ac < 2)
                  {
                      printf("Usage: Invalid number of arguments.\n");
                      return 1;
                  }
                  
                  int file_descriptor;

                  char buffer[1024];
                  ssize_t bytes_read;

                  for (int i = 1; i < ac; i++)
                  {
                      file_descriptor = open(av[i], O_RDONLY);

                      if (file_descriptor == -1)
                      {
                          printf("Error: Could not open file: %s\n", av[i]);
                          return 1;
                      }

                      while((bytes_read = read(file_descriptor, buffer, sizeof(buffer)))>0)
                      {
                          write(1, buffer, bytes_read);
                      }

                      if (bytes_read == -1)
                      {
                          printf("Error reading file.\n");
                          close(file_descriptor);
                          return -1;
                      }

                      close(file_descriptor);

                  }
                  
                  return 0;
              }
              
          

References:

[1] Kernighan B.W., Ritchie D.M (1988, Second Edition). The C Programming Language. 7.5 File Access, 160-162.

[2] “cat (Unix)”. Wikipedia, Wikimedia Foundation, 15 February 2023, en.wikipedia.org/wiki/Cat_(Unix)


Made at Qwasar SV -- Software Engineering School Qwasar SV -- Software Engineering School's Logo