1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
snow_tiger [21]
3 years ago
9

Write a C program to implement a command called ​displaycontent ​that takes a (text) file name as argument and display its conte

nts. Report an appropriate message if the file does not exist or can’t be opened (i.e. the file doesn’t have read permission). You are to use ​open()​, ​read()​, ​write() ​and
Computers and Technology
1 answer:
Zanzabum3 years ago
7 0

Answer:

/*

Student: Hugo Meza

Date: February 15, 2019

Description: This program mimic the cat command from Linux. Checks whether the source file

has the right permissions, and then writes in the console its content.

*/

#include <stdio.h>

#include <fcntl.h>

#include <unistd.h>

#include <errno.h>

int hasPermission(char *filepath){

int returnval;

// Check file existence

returnval = access (filepath, F_OK);

if(errno == ENOENT){

printf ("%s does not exist\n", filepath);

return 0;

}

else if (errno == EACCES){

printf ("%s is not accessible\n", filepath);

return 0;

}

// Check read access

returnval = access (filepath, R_OK);

if(errno == ENOENT){

printf ("%s does not have read access\n", filepath);

return 0;

}

else if (errno == EACCES){

printf ("%s is not accessible\n",filepath);

return 0;

}

// Check write access

returnval = access (filepath, W_OK);

if(errno == ENOENT){

printf ("%s does not have read access\n", filepath);

return 0;

}

else if (errno == EACCES){

printf ("%s is not accessible\n",filepath);

return 0;

}

return 1;

}

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

if(!argv[1]){

printf("Error. Specify file to open\n");

return 0;

}

int fd;

char *fp = argv[1], content[fd];

if(hasPermission(fp) == 0)

return 1;

fd = open(fp, O_RDONLY);

int bytes = read(fd,content,sizeof(content)-1);

write(1, content, bytes);

close(fd);

return 0;

}

Explanation:

You might be interested in
What are the principles for computer programming?<br> Please add some examples
vaieri [72.5K]
Computer Programming is the process of writing, testing, troubleshooting, debugging and maintaining of a computer program. Good programming practices mix art, craft and engineering discipline.
3 0
3 years ago
Which of the following practices can help you avoid harassment (or the appearance of
Advocard [28]

Answer:

B. Respect other users' requests not to send any jokes via e-mail or IM.

7 0
3 years ago
Read 2 more answers
What are the hardware and software components of a computer<br>​
elena-s [515]

Answer: See explanation

Explanation:

Computer hardware simply refers to the part of the computer that we can see and also touch. This includes the mouse, keyboard, monitor, central processing unit, monitor etc.

The computer software simply refers to the set of instructions which are used in the operation of the computer. It includes the application programs, operating system etc.

4 0
3 years ago
Before entering a loop to compute a running total, the program should first do this
Slav-nsk [51]
Initialize it's variable:

double total = 0.0
while( whatever )
8 0
3 years ago
What is Telerobotic?
Dennis_Churaev [7]

Answer:

Telepresence refers to a set of technologies which allow a person to feel as if they were present, to give the appearance that they were present, or to have an effect, at a location other than their true location. Teleoperation is the operation of a machine at a distance.

Explanation:

4 0
2 years ago
Read 2 more answers
Other questions:
  • Which type of footwear should be worn while working on a hybrid vehicle?
    8·2 answers
  • Questions 1 )When does a spring tide take place?
    14·2 answers
  • If you are a driver under 21 with a breath or blood alcohol level of ____ or higher, you will be required to attend a substance
    11·2 answers
  • Is it possible to uninstall a program that's on your phone from computer?
    8·1 answer
  • I want to discard my old computer and securely erase the data from my hard drive.
    12·1 answer
  • Testing for information would be most likely to occur in which type of engineering?
    5·2 answers
  • Define a function below called process_grades. The function takes one argument: a list of integers. Each integer in the list cor
    6·1 answer
  • Given the integer variables x, y, and z, write a fragment of code that assigns the smallest of x, y, and z to another integer va
    7·1 answer
  • Advantages and disadvantages of screen reading​
    8·1 answer
  • What kind of advertising is used in Saunders's story when a man and his grandson walk down the street? O A. An individualized au
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!