Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, could you help me fix this code per the assignment instructions? /* mwc0.c JM */ #include int open(const char *filename, int flags); long read(int

Hello, could you help me fix this code per the assignment instructions?

/* mwc0.c JM */

#include int open(const char *filename, int flags); long read(int fd, void *buffer, unsigned long byte_count); long write(int fd, void *buffer, unsigned long byte_count); void perror (const char *s); int snprintf(char *str, unsigned long size, const char *format, ...); void exit(int status); int close(int fd);

#define BUFFERSIZE 100

int count_bytes(int fd) { int count = 0; char BUFF[1]; while (read(fd, BUFF, 1) > 0) { count++; printf("count_bytes"); } return count; }

int count_lines(int fd) { int count = 1; char BUFF[1]; while (read(fd, BUFF, 1) > 0) { if (BUFF[0] == ' ') count++; printf("count_lines"); } return count; }

int count_words(int fd) { int count = 1; char BUFF[2]; while (read(fd, BUFF, 2) > 0) { if (BUFF[0] == ' ' && BUFF[1] != ' ') count++; printf("count_words"); } }

int digits(int number) { int count = 0;

do { printf("digits"); count++; number = number/10; } while (number > 0);

return count; }

int main(int argc, char *args[]){ int fd; char outbuffer[100]; char *output = outbuffer; char* file_name = args[1];

if (fd = open(args[1], 0)

int lines = count_lines(fd); int words = count_words(fd); int bytes = count_bytes(fd); int maxlength = digits(bytes);

snprintf(output, 100, "%1$*5$d %2$*5$d %3$*5$d %4$s ", lines, words, bytes, file_name, maxlength); return 0; }

image text in transcribed

In this review project we will write a version of the utility wc. This project only uses resources already introduced in the previous projects. (1) Work in the project3 subdirectory of projectsFL local repository. Read the man page for wc, and understand how it works. Understand what the c,1,L, and -w options do as well as any combination of them. (2) Write a basic version (no options) of this particular command, called mwc0.c using no library functions, except for snprintf in order to format the output (along the lines of the error message constructed in the mhead project) and strlen to determine the length of the output line. Use a buffer of size 100 bytes to hold the output line. How to do this is shown below. Access this buffer with a single write system call. Enable your command to get the file to work with from the command line; you do not need to support multiple files on the command line. (3) Your program should compile without warnings. You may include explict headers or the relevant header files in order to remove any warnings rather than explicit system call headers. (4) You will know that your version works correctly when it reproduces the exact behaviour of the real wc without options. (5) The output of wc uses a variable field width. Here is the snprintf call which formats this output so that it reproduces the spacing wc uses in the default case: char outbuffer[100]; char output = outbuffer; snprintffoutput, 100,%1$5$d%2$5$d%3$5$d%4$ , lines,words,bytes,"filename",fieldwidth);//argument1//argument2//argument3//argument4//argument5 The variables to print are named by their argument locations: the 1$,2$, 3$,4$ format codes. The 5$ format code following each location specifies the location of the fieldwidth as the fifth variable. Section IV. Project 3r:wc 132 The wc program uses the field width of the largest number-bytes, which counts the number of bytes in the file. The field width that wc uses equals the number of numerals in this byte count. (6) Here is a function which counts the digits in a number: This function also illustrates how to write a do loop in c. In this review project we will write a version of the utility wc. This project only uses resources already introduced in the previous projects. (1) Work in the project3 subdirectory of projectsFL local repository. Read the man page for wc, and understand how it works. Understand what the c,1,L, and -w options do as well as any combination of them. (2) Write a basic version (no options) of this particular command, called mwc0.c using no library functions, except for snprintf in order to format the output (along the lines of the error message constructed in the mhead project) and strlen to determine the length of the output line. Use a buffer of size 100 bytes to hold the output line. How to do this is shown below. Access this buffer with a single write system call. Enable your command to get the file to work with from the command line; you do not need to support multiple files on the command line. (3) Your program should compile without warnings. You may include explict headers or the relevant header files in order to remove any warnings rather than explicit system call headers. (4) You will know that your version works correctly when it reproduces the exact behaviour of the real wc without options. (5) The output of wc uses a variable field width. Here is the snprintf call which formats this output so that it reproduces the spacing wc uses in the default case: char outbuffer[100]; char output = outbuffer; snprintffoutput, 100,%1$5$d%2$5$d%3$5$d%4$ , lines,words,bytes,"filename",fieldwidth);//argument1//argument2//argument3//argument4//argument5 The variables to print are named by their argument locations: the 1$,2$, 3$,4$ format codes. The 5$ format code following each location specifies the location of the fieldwidth as the fifth variable. Section IV. Project 3r:wc 132 The wc program uses the field width of the largest number-bytes, which counts the number of bytes in the file. The field width that wc uses equals the number of numerals in this byte count. (6) Here is a function which counts the digits in a number: This function also illustrates how to write a do loop in c

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Accounting questions

Question

Assess three steps in the selection process.

Answered: 1 week ago

Question

Identify the steps in job analysis.

Answered: 1 week ago