Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6769357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:11:03+00:00 2026-05-26T15:11:03+00:00

I am implementing my own version of the (cat) command in Unix for practice.

  • 0

I am implementing my own version of the (“cat”) command in Unix for practice. After i did that i became interested in implementing some flags like (-n) and (-b).

My Question: I am looking for a way to locate the blank and new lines while reading from my file. I can’t remember what library or function i should use.

Here is the source code I am working on:

#include <fcntl.h>
#include <unistd.h>

static int cat_fd(int fd) 
{
   char buf[4096];
   ssize_t nread;

   while ((nread = read(fd, buf, sizeof buf)) > 0) 
   {
      ssize_t ntotalwritten = 0;
      while (ntotalwritten < nread) 
      {
         ssize_t nwritten = write(STDOUT_FILENO, buf + ntotalwritten, nread - ntotalwritten);

         if (nwritten < 1)
         {
            return -1;
         }

         ntotalwritten += nwritten;
      }
   }

   return (nread == 0) ? 0 : -1;
}

static int cat(const char *fname) 
{
   int fd, success;

   if ((fd = open(fname, O_RDONLY)) == -1)
   {
      return -1;
   }

   success = cat_fd(fd);

   if (close(fd) != 0)
   {
      return -1;
   }

   return success;
}


int main(int argc, char **argv) 
{
    int i;

    if (argc == 1) 
    {
       if (cat_fd(STDIN_FILENO) != 0)
          goto error;
    } 

    else 
    {
      for (i = 1; i < argc; i++)
      {
         if (cat(argv[i]) != 0)
         {
            goto error;
         }
      }
    }

    return 0;

    error:
      write(STDOUT_FILENO, "error\n", 6);
      return 1;
}

Any ideas or suggestions concerning my question are greatly appreciated.
I would be even more grateful if you can type for me the complete function prototype that i shall be using as i am not an experienced programmer.

Thanks in advance for your help.

P.S: I am implementing the (-n) and (-b) flags. Thus, i am looking forward to write the line number at the beginning of each line in the file that i am reading.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:11:04+00:00Added an answer on May 26, 2026 at 3:11 pm

    While there is a function that does line-based file input in C (it’s called fgets), you can’t really use it for cat, because:

    • There’s no way to know the maximum length of the line beforehand;
    • You’ll lose portions of the input if it contains null bytes.

    You’ll have to look for newline symbols in your buffer after you read it, and once you find any, print the prefix of the buffer, followed by newline, line number, and the rest of the buffer (with additional processing of remaining newlines, of course).

    An easier solution would be to switch to processing input one byte at a time; you can use FILE* and fgetc to use CRT-provided buffering so that you don’t actually do a syscall for each read/write, or read file in blocks as you do now, and do byte processing inside the loop. Then it’s a matter of writing a state machine – if a previous read character was a newline, then output a line number, unless this character is a newline and -b option is used, etc.

    This still results in a less efficient solution, so you may want to treat cat without arguments specially – i.e. switch to byte-per-byte processing only if you need it. In fact, this is exactly what at least one of actual cat implementations does.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been implementing my own version of a red-black tree, mostly basing my algorithms
Microsoft like implementing their own versions of popular open-source frameworks and assemblies, for example:
Where can I get info about implementing my own methods that have the ellipsis
I'm implementing my own version of sscanf() in a different language (not C or
Let's say you're implementing your own version of stackoverflow (once again, yes) You have
I am implementing my own shell. But to supprt for command history, I need
Using the below code I am implementing my own Navigation Bar. For some reason
Let's say I'm implementing my own version of Scrabble. I currently have a Board
I'm implementing my own NSBitmapImageRep (to draw PBM image files). To draw them, I'm
As a learning excercise, I've just had an attempt at implementing my own 'merge

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.