Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MahendraDani/cat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cat

Implementation of cat(1) CLI utility from scratch in C following the Build Your Own Cat Tool coding challenge.

It took me ~2hrs to write the program, and over a day to write test :/

Setting up locally

To setup and run this project locally, please follow the below steps:

Prerequisites

  1. C/C++ Compiler: Please make sure that you have a C/C++ compiler installed on your system, preferably gcc or clang.

  2. Install make:

If you are using Mac, and have Homebrew installed, use :

brew install make

Setup

  1. Clone the project on your machine.
https://github.com/MahendraDani/cat.git
  1. Build from source

First change directory, cd cat, then

make

This command will build the project from source, and output the executable bin/cat.

To verify clean installation, try running:

bin/cat data/test.txt

You should be able to see the data/test.txt file printed on the console.

Usage

For usage, please read the man page of cat(1). Note that, only -n and -b are supported in the current version.

  1. Read input from a file
bin/cat file.txt
  1. Read from multiple files and concatenate
bin/cat file1.txt file2.txt file3.txt
  1. Read from stdin
bin/cat -
  1. Show line numbers
bin/cat -n file.txt
  1. Show line numbers (excluding blank lines)
bin/cat -b file.txt

Testing

Although writing tests was not included in the challenge, any program to be called "production-ready" should and must be appropriately tested. So I wrote a few tests in shell scripts to test my code.

Much of the inspiration of writing tests is taken from the git project (as you can see from the names of test files :P) which has a lot of tests written in shell scripts.

This also forced me to learn about shell scripting, unit testing and much more, which is also essential in writing good software.

To run the test suite, simply run :

make test

If you want to specially run a particular test file:

tests/t0001.sh

Future Work

The project itself was meant to complete the coding challenge, which it has full-filled. Though, if found necessary, I might refactor it in the future.

Things that can be improved:

  • namings : struct config doesn't not explain what configurations it holds. I mean using name config for storing the flags specified by the user is really not a config, is it?

  • parsing arguments and flags : Supporting two flags with if-else is ok, but not scalable. I would probably use a library to parse flags, but I really want to write my own, as I see myself doing the same thing again and again for all the challenges I will attempt in the future that require building a CLI utility.

References

  1. The Shell Scripting Tutorial
  2. Learn Makefiles
  3. Beej's Guide to C Programming

About

Implementation of cat(1) CLI utility from scratch in C

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors