Question

Using system calls you have learned so far, build your own command called fpart to display (or echo) a portion of any text file. Please follow the following requirements 1. Your command must be typed in these styles a. Şfpart filename.txt start byte end _byte b. Şfpart filename.txt start_byte 2. If the input file (filename.txt) is a non-text file, then a message must be displayed like this: Input file is not a text file 3. Here is an example run. Assume your input text file is like this: (file name story.txt) One night, three thieves stole a lot of money from a rich mans house. They put the money in a bag and went to the forest. They felt very hungry. So, one of them went to a nearby village to buy food. The other two remained in the forest to take care of the bag of money. The thief that went for food had an evil idea. He ate his food at a hotel. Then he bought food for his two mates in the forest. He mixed a strong poison with the food. He thought, Those two will eat this poisoned food and die. Then I will get all the money for myself. Meanwhile, the two wicked men in the forest decided to kill their mate on return. They thought that they would divide the money between the two of them. All the three wicked men carried out their cruel plans. 7he thief who wanted all the money for himself came to the forest with the poisoned food. The two men in the forest hit him and killed him. Then they ate the poisoned food and died. Thus, these evil people met with n evil end You run the command like this: Example 1 $fpart story.txt 10 100 , three thieves stole a lot of money from a rich mans house. They put the money in a bag a The output about printed out the byte 10 to 100 of the text file. Example 2 $fpart story.txt 500 Then I will get all the money for myself. Meanwhile, the two wicked men in the forest decided to kill their mate on return. They thought that they would divide the money between the two of them. All the three wicked men carried out their cruel plans. The thief who wanted all the money for himself came to the forest with the poisoned food. The two men in the forest hit him and killed him. Then they ate the poisoned food and died. Thus, these evil people met with an evil end. In the above example, all text starting from the 500th byte is printed on the screen See next page for some more example scenarios

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The soluion for the given question is given below with screenshot of output.

-------------------------------------------------------------------------------------------------------------------------------------

I have kept the logic simple and added comments for understandiing i have used system call in the program

If there is anything else let me know in comments

Input File use : textfile.txt

The Lion and the Mouse Once when a lion, the king of the jungle, was asleep,
a little mouse began running up and down on him. This soon awakened the lion,
who placed his huge paw on the mouse, and opened his big jaws to swallow him.
"Pardon, O King!" cried the little mouse. "Forgive me this time. I shall never
repeat it and I shall never forget your kindness. And who knows, I may be able
to do you a good turn one of these days!The lion was so tickled by the idea
of the mouse being able to help him that he lifted his paw and let him go.
Sometime later, a few hunters captured the lion, and tied him to a tree.
After that they went in search of a wagon, to take him to the zoo.Short
StoriesJust then the little mouse happened to pass by. On seeing the
lion’s plight, he ran up to him and gnawed away the ropes that bound him,
the king of the jungle."Was I not right?" said the little mouse, very
happy to help the lion.
MORAL: Small acts of kindness will be rewarded greatly.

-------------------------------------------------------------------------------------------------------------------------------------

-------- CODE TO COPY ----------------------------------------------------------------------------------------------------

/*

* fpart is a command to show the part of a test file.

* USAGE : ./fpart text-file-name start-byte <end-byte>

*/

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <fcntl.h>

#include <unistd.h>

int main(int argc, char *argv[])

{

if ( argc < 3 )

{

printf("Too less parameters\n");

printf("usage fpart filename start [end]\n");

return 1;

}

  

// file name is stored in filename

// display file start at start byte

// display file end at end byte

char filename[32] = "";

int start = 0;

int end = 0;

char buf;

  

strcpy( filename, argv[1] );

  

if ( strstr(filename, ".txt") == NULL )

{

printf("Input file not a text file\n");

return 1;

}

  

start = atoi( argv[2] );

  

if ( argc == 4 )

end = atoi( argv[3] );

  

int fp = open(filename, O_RDONLY);

if ( fp == -1 )

{

printf("Error opening file for reading [%s]\n",filename);

return 1;

}

  

// Reading the file byte by byte

int bytes_read = 1;

  

while ( read( fp, &buf, 1) != 0 ){

  

// if start byte is found start printing

if ( bytes_read >= start )

printf("%c",buf);

  

// if end byte is found stop printing

if ( end != 0 && bytes_read > end )

break;

  

bytes_read++;

}

  

printf("\n");

return 0;

}

-------------------------------------------------------------------------------------------------------------------------------------

Output :

sh-4.4$ sh-4.4$ sh-4.4 ./fpart Too less parameters usage fpart filename start [end] sh-4.4$ sh-4.4$ sh-4.4$ ./fpart textfile.

Add a comment
Know the answer?
Add Answer to:
Using system calls you have learned so far, build your own command called fpart to display...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Evaluate the arical writ the response in which you state your agreement or disagreement with writer...

    Evaluate the arical writ the response in which you state your agreement or disagreement with writer up un these questions guidelines 1) can empathy lead us astrary? how 2) our heart will always go out to the baby in the well, its a measure of our humanity. but empathy will have to yield to reason if humanity is to have a future can empathy yield to reason? how? thank you The Baby in the Well: The Case against Empathy* -Paul...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT