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
When you ____ an exception, you send a message that an error has occurred to another part of the program.
jekas [21]
The correct answer is most definitely B
8 0
2 years ago
Read 2 more answers
Complete the sentence to identify disadvantages of top-down programming design. Choose all that apply. Top-down programming desi
pav-90 [236]

so is it all about codes and codes and codes and codes and codes lol

4 0
3 years ago
Can i have help for a ggogle class room
andrew11 [14]

Answer:

or tell ur teacher to add u in ur class

Explanation:

3 0
2 years ago
Read 2 more answers
What size hard disc is recommended for powerpoint writting
mixas84 [53]

Answer:

Depends on how many PowerPoint files you want to store and how long they are. I'd recommend at least a 256GB hard drive to store your OS and software and then leave space for PowerPoints.

5 0
3 years ago
An exclusion contract A. gives a firm the right to be the exclusive provider of a good in a particular market. B. is a form of e
irina1246 [14]

Answer:

D. All of the above

7 0
3 years ago
Other questions:
  • Which of the following locations is the option to show hidden files or folders location
    7·2 answers
  • How do you change between worksheets inside an excel workbook?
    13·2 answers
  • Computers store temporary Internet files in the Recycle Bin. These files take up space and slow down a computer. Which tool can
    10·1 answer
  • Which of the following are some of the ways that the media influences consumers behavior
    9·1 answer
  • Pls Help need it before 1pm PLS.
    15·2 answers
  • Technological advances have made cyberbullying
    5·1 answer
  • The sun can be an excellent source of natural light.<br> True.<br> False.
    8·2 answers
  • How many times is the body of the loop executed?
    8·1 answer
  • What are the minimum number of bits an address bus must have in a byte addressable memory system containing only 4 KiB of memory
    8·1 answer
  • Write the technical terms for the following statements: The repeated working capacity of computer.
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!