Question

In: Computer Science

Assignment 1 – Writing a Linux Utility Program Instructions For this programming assignment you are going...

Assignment 1 – Writing a Linux Utility Program

Instructions

For this programming assignment you are going to implement a simple C version of the UNIX cat program called lolcat. The cat program allows you to display the contents of one or more text files. The lolcat program will only display one file. The correct usage of your program should be to execute it on the command line with a single command line argument consisting of the name you want to display. However, your program should also respond well when it is used incorrectly.

Processing Command-Line Arguments

Unless you have done Linux programming before you probably haven’t needed to process command line arguments. A typical C program has a main function that looks like this:

int main()
{
// body of main function
}

This works just fine if your program does not take any command line arguments. If it does, as is the case with lolcat, you will need access to those arguments. You will need to write your main function like this:

int main(int argc, char *argv[])
{
// body of main function
}

The argc parameter is the number of command line arguments provided to the program including the name of the command itself. The lolcat program should have two command line arguments if it is used correctly: the name of the command and the file to display. The argv parameter is an array of C-strings, or null-terminated character arrays. This means that argv[0] is the name of the program, argv[1] is the first command line argument, argv[2] is the second command line argument, and so on.

The lolcat program should display a usage message to standard error and exit the program using exit(1) if argc is anything other than 2. Otherwise it should use the C-string contained in argv[1] as the name of the input file to open. Note that exit function requires the following preprocessor statement:

#include <stdlib.h>

Streams

The standard way to deal with files, the console, and other sources of input and output in C is by usingstreams. For historical reasons, the data type used to deal with a stream, whether or not it uses a file, is FILE *. Working with streams requires the following preprocessor statement:

#include <stdio.h>

To declare a stream variable use the FILE * data type:

FILE *stream;

To open a file use the fopen function:

stream = fopen(filename, opentype);

  • fopen has two parameters: filename is the name of the input file as a C-string, and opentype is a C-string containing information about how the file is to be opened.
  • If the file is to be opened for reading only, the second argument should be "r".
  • fopen returns a value of type FILE * if the file opened successfully, and NULL otherwise.
  • If your program can not open the file, display a message that the file was not found to standard error and exit the program.

To read a single character from a file, use the fgetc function:

character = fgetc(stream);

  • fgetc has one parameter: the stream that was returned by fopen.
  • fgetc returns the character read if it was successful and the special EOF (end of file) character if it was not successful.

To close a file use the fclose function:

fclose(stream);

  • fclose has one parameter: the stream representing the file to be closed.
  • fclose returns 0 if the file closed correctly and EOF if it did not. The latter case is rare, so the return value is typically ignored.

To write a string to standard output use the puts function:

puts(string);

  • puts has one parameter: the C-string to be printed. A newline character is appended to the output.
  • puts returns a non-negative value if successful and EOF if unsuccessful. The return value is typically ignored.
  • You will not need this function for this assignment, but it may be helpful to you for debugging purposes.

To write a string to standard error use the fputs function:

fputs(string, stream);

  • fputs has two parameters: string is the C-string to be printed, and stream is the stream representing the destination of the output.
  • To print to standard error, use stderr as the second argument.
  • fputs returns a non-negative value if successful and EOF if unsuccessful. The return value is typically ignored.

To write a single character to the console use the putchar function:

putchar(character);

  • putchar takes a single argument: the character to be printed.
  • putchar returns the character if it printed successfully and EOF if it does not. The return value is typically ignored.

Linux Development Tools

You should not be using Windows development tools for this class! Instead, you should use the development tools available on Linux.

Writing Your Code

If you only have access to a Linux shell, the simplest text editor available is probably nano, which can be started by typing nano at the command line. You can then write the text file, type control-o to save it, and control-x to exit the program. You can also open an existing file by typing nano <filename> at the command line (with the actual name of the file instead of <filename>.) If you want to use a text editor with more features for writing source code you can try using vim or emacs.

If you are using a Linux distribution with a graphical interface, then you can use a GUI text editor like gedit, kedit, gvim, or the GUI version of emacs, depending on what is available on your system. Ultimately it doesn’t matter what text editor you use, as long as you keep track of where you are saving your files.

Compiling Your Code

Your source file should be called yourlastnameAssign1.c except with your actual last name. To compile this code you should use the gcc compiler on the Linux server. To create an executable file called lolcat use the following command:

gcc -o lolcat yourlastnameAssign1.c

If your program compiled, you should see an entry for lolcat when you execute the ls command.

Running Your Code

Since your home directory on Linux is not in your execution path, you will need to specify the file you are executing directly by putting a ./ before the name of the executable. Here are some examples of what several runs should look like:

$ ./lolcat foo.txt
This is a text file that I created
in a text editor in order to test
out the lolcat program.

$ ./lolcat
usage: lolcat <filename>

$ ./lolcat foo.txt otherFile.txt
usage: lolcat <filename>

$ ./lolcat no_such_file.txt
error: file not found

What to Hand In

Your source file should have comments at the top listing your name, Assignment 1, and a brief explanation of what the program does. Upload the source file to D2L in the dropbox called Assignment 1.

Solutions

Expert Solution

/*****************************************************/
/************* Name: ***********/
/************* Assignment 1 ************************/
/*****************************************************/

//header files declaration
#include <stdio.h>
#include <stdlib.h>

//main function
int main(int argc, char *argv[])
{
   //declare file stream
   FILE *f;
   //declare character variable
   char c;
   //check the number of arguments
   if(argc!=2)
   {
       printf("usage: lolcat <filename>\n");
       exit(1);
   }
   //open a file in read-only mode
   f = fopen(argv[1], "r");
   //check error on opening the file
   if(f==NULL)
   {
       fputs("error: file not found\n", stderr);
   exit(1);
   }
   //read a character and check EOF
   while((c=fgetc(f))!=EOF)
   {  
       //print the character to the console
       putchar(c);
   }
      
   //close a file
   fclose(f);
}

Output:


Related Solutions

Code in C Instructions For this programming assignment you are going to implement a simulation of...
Code in C Instructions For this programming assignment you are going to implement a simulation of Dijkstra’s solution to the Dining Philosophers problem using threads, locks, and condition variables. Dijkstra’s Solution Edsgar Dijkstra’s original solution to the Dining Philosophers problem used semaphores, but it can be adapted to use similar mechanisms: • Each philosopher is in one of three states: THINKING, HUNGRY, or EATING. • Every philosopher starts out in the THINKING state. • When a philosopher is ready to...
Writing Assignment #1 Set of Instructions for a Website Summary of the Assignment: • Task: In...
Writing Assignment #1 Set of Instructions for a Website Summary of the Assignment: • Task: In this assignment, you will write a set of instructions that explain how to accomplish a task on a website. • Length: There is no minimum or maximum word count. However, your instructions must have 6 or more steps. More information on the number of steps is provided below. • Graphics: You must include at least one graphic for each step. o at least 6...
Writing Assignment #1 Instructions The following assignment should be typed and printed or handwritten and turned...
Writing Assignment #1 Instructions The following assignment should be typed and printed or handwritten and turned in to the CA office in room​ 201 TMCB.​ If there is no one in the CA Office, you can slip your assignment through the slot in the door. You must follow the instructions below or you will not receive credit.​ You can turn in the assignment up until 5:00 PM on the due date. Important Notices: If you do not staple multiple pages,...
CS 400 Assignment 2: applying ArrayList In this assignment, you are going to build a program...
CS 400 Assignment 2: applying ArrayList In this assignment, you are going to build a program that can help rental car companies to manage their rental fleet. Requirement: - build ArrayList class as container. - build Car class/structure to represent car objects. - An ArrayList object is to hold Car objects. Car class should have following fields: - id (int) - make (string) - model (string) - color (string) Instructions: - make up 15 cars and save them into a...
Java Programming In this assignment we are going to create our own programming language, and process...
Java Programming In this assignment we are going to create our own programming language, and process it Java. programming language has 6 commands enter add subtract multiply divide return enter, add, subtract, multiply, divide all take 1 parameter (a double value). return takes no parameters.
In this assignment you are going to use the menu you created in Assignment 1 to...
In this assignment you are going to use the menu you created in Assignment 1 to test both your Double and Integer classes. You should add functionality to the menu to allow you test the add, sub, mul, div functions for instances of both classes You are going to have make a modification to your menu class. Currently it uses an array to hold a fixed amount of menu items. While this may be OK for most applications we want...
Assignment Instructions Module 4 Writing Assignment (M1-4) You will soon find yourself fulfilling roles in organizations...
Assignment Instructions Module 4 Writing Assignment (M1-4) You will soon find yourself fulfilling roles in organizations where the ability to think ethically about issues will make you much more valuable to employers. In this activity you will refer to the materials in your text and lectures to respond to the questions. Part A The questions in Part A refer to the material discussed in Module 1 of this course. Respond to the following. Discuss reasons why ethics are an important...
1. INTRODUCTION The goal of this programming assignment is for students to write a Python program...
1. INTRODUCTION The goal of this programming assignment is for students to write a Python program that uses repetition (i.e. “loops”) and decision structures to solve a problem. 2. PROBLEM DEFINITION  Write a Python program that performs simple math operations. It will present the user with a menu and prompt the user for an option to be selected, such as: (1) addition (2) subtraction (3) multiplication (4) division (5) quit Please select an option (1 – 5) from the...
Program Assignment 1 C++ please Instructions This assignment will require the use of three arrays, which...
Program Assignment 1 C++ please Instructions This assignment will require the use of three arrays, which will be used in parallel. Create a program that keeps track of the sales of BBQ sauces for a company. The company makes several different types of sauces, Original, Sticky Sweet, Spicy, Sweet Heat, Hickory Bourbon and Smokey Mesquite. One array will contain the names of the different BBQ sauces. This array will be initialized from a text file with the 6 different names....
This programming assignment is intended to demonstrate your knowledge of the following:  Writing a while...
This programming assignment is intended to demonstrate your knowledge of the following:  Writing a while loop  Write methods and calling methods Text Processing There are times when a program has to search for and replace certain characters in a string with other characters. This program will look for an individual character, called the key character, inside a target string. It will then perform various functions such as replacing the key character with a dollar-sign ($) wherever it occurs...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT