1 answer

Using system calls you have learned so far, build your own command called fpart to display...

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

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 man's 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 man's 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

Answers

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.

.

Similar Solved Questions

1 answer
Need some help. The financial statements for Castile Products, Inc., are given below: Castile Products, Inc....
Need some help. The financial statements for Castile Products, Inc., are given below: Castile Products, Inc. Balance Sheet December 31 Assets Current assets: Cash Accounts receivable, net Merchandise inventory Prepaid expenses Total current assets Property and equipment, net Total assets Liabilitie...
1 answer
What substance is produced by the ceruminous glands?
What substance is produced by the ceruminous glands?...
1 answer
A small portion of the human transport protein amino acid sequence is shown below. The upper...
A small portion of the human transport protein amino acid sequence is shown below. The upper sequence is associated with darker skin, and the lower sequence is associated with lighter skin. What DNA base-pair change created the light-skin form of the human protein from the gene that coded for the da...
1 answer
9:09 115GE 1 HWweek1: Attempt 1 Exercise Content Question 1 2 Points Identify the following accounts...
9:09 115GE 1 HWweek1: Attempt 1 Exercise Content Question 1 2 Points Identify the following accounts of Advanced Services Co. as asset, liability, owner's equity, revenue, or expense, and state in each case whether the normal balance is a debit or a credit Accounts Receivable A Asset B Liability...
1 answer
S&S Air’s Mortgage ark Sexton and Todd Story, the owners of S&S Air, Inc., greatest Interest...
S&S Air’s Mortgage ark Sexton and Todd Story, the owners of S&S Air, Inc., greatest Interest savings. At Todd's prompting, she goes IV Iwere impressed by the work Chris had done on finan on to explain a bullet loan. The monthly payments of a clal planning. Using Chris's analysi...
1 answer
Please show how to do in excel file if possible Please complete the following problems: 1....
please show how to do in excel file if possible Please complete the following problems: 1. The Sea Wharf Restaurant would like to determine the best way to allocate a monthly advertising budget of $1000 between newspaper advertising and radio advertising Management decided that at least 25 percen...
1 answer
Top managers of Best Video are alarmed by their operating losses. They are considering dropping the...
Top managers of Best Video are alarmed by their operating losses. They are considering dropping the DVD product line. Company accountants have prepared the following analysis to help make this decision: E (Click the icon to view the analysis.) Assume that Best Video can avoid $45,000 of fixed costs ...
1 answer
Integrative-Leverage and risk Firm has sales of 104.000 units at $2.05 per unit, variable operating costs...
Integrative-Leverage and risk Firm has sales of 104.000 units at $2.05 per unit, variable operating costs of $1.71 per unit, and fixed operating costs of $6,000 Interest is $10.080 per year Firm Whas sales of 104 000 units at $2.56 per unit, variable operating costs of $0.98 per unit, and fixed oper...
1 answer
How do you find the domain of #f(x) = sqrt((3 - x) / (x + 2))#?
How do you find the domain of #f(x) = sqrt((3 - x) / (x + 2))#?...
1 answer
Any help is much appreciated :) Let p be a prime, and n a positive integer....
Any help is much appreciated :) Let p be a prime, and n a positive integer. Prove that NoTE: This appears to be an infinite sum. Eventulo in fact after a point all of the terms are 0...
1 answer
Item 22 A compound is isolated from the rind of lemons that is found to be...
Item 22 A compound is isolated from the rind of lemons that is found to be 88.14% carbon and 11.86% hydrogen by mass Part B How many moles of C and H? Express your answers using three significant figures separated by a comma. 0 AP O 2 ? ne, n = mol Submit Request Answer...
1 answer
Please solve for yellow highlighted ? and show work Problem V: The following are condensed financial...
Please solve for yellow highlighted ? and show work Problem V: The following are condensed financial statements for Talley Inc. for the month of August. Solve for all spots marked with a "?". You may either write or type your answers over the question marks or enter them in the chart on the...
1 answer
A bullet of mass m is fired horizontally into a block of wood of mass M...
A bullet of mass m is fired horizontally into a block of wood of mass M lying on a table. The bullet remains in the block and they slide a distance d after the collision. The coefficient of kinetic friction between the wooden block and the table is u. What was the speed of the bullet before the coll...
1 answer
Dicated initial condition. In #4-5, find the particular solution for each differential equation given the indicated...
dicated initial condition. In #4-5, find the particular solution for each differential equation given the indicated initial 4 de -cosx=0, 600)=1. s. 1-2x = 0, y(0)= In 2...
1 answer
Problem I (20 marks) The beam in the following figure is constructed from the two channels...
Problem I (20 marks) The beam in the following figure is constructed from the two channels at the bottom and the cover plate on the top. If each channel has a cross-sectional area of Ac 12 cmi and a moment of inertia about a horizontal axis passing through its own centroid, Co of 350 cm, then determ...
1 answer
6. Wakanda and Valhalla are two countries. Each have 3000 active workers who work 10 hours...
6. Wakanda and Valhalla are two countries. Each have 3000 active workers who work 10 hours every day, meaning they have 30000 labor/hours in a day. The rest are pro gamers who don’t work and play video games and drink beers only. For Wakanda: Producing 1 gaming laptop requires 2 hours. Produci...