Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Q1: Hi, having some trouble with porgramming not the best at it, i'll rate and can somebody show the work by using Putty or Terminal

Q1: Hi, having some trouble with porgramming not the best at it, i'll rate and can somebody show the work by using Putty or Terminal please and thanks

Compiling, linking, and executing program cc, vi or vim or notepad, cat, running program

A C program is given at the end of this question. It is the program in your textbook in the Systems Programming chapter. I have made few minor changes and added more documentation. This program reads the content of a directory given as an argument when running the program.

Create a program file with extension .c (for example, hw3q1.c) with the given program as the content. To create the file, you can use one of the two methods described below: NOTE: Do not issue script command while creating this source file.

Remember to exit from script before using the vi command.

1) You can type the given program in the vi editor.

Alternately, you can copy and paste the program from this Word homework file into Notepad or Wordpad on your PC. Then, delete and retype each and every double quotes () one by one. You must do this because the double quote in Word is different from double quote in Notepad or Wordpad. Your program will not work without this change. After changing ALL the double quotes, save the file. Finally, transfer the file from your PC to the UNIX server using ftp command.

After creating the program source file on the UNIX server, issue script command and do the following.

2) Compile and create an executable file for this program using the cc command.

The following command will produce a.out as the executable file assuming hw3q1.c is the program file name:

cc hw3q1.c

The following command will produce hw3q1 as the executable file assuming hw3q1.c is the program file name. The file name after the o option is the executable filename.

cc hw3q1.c o hw3q1

Use the second method as it is the preferred choice by professionals and it is easy to identify the executable file and the corresponding .c program file.

3) Display the content of the program file, which is also called the source code file.

4) Run the program by typing the executable file name followed by a directory name as argument. The argument could be current working directory or any directory.

5) Look at the output of the program, then using echo command answer what are the two information stored in a directory file.

6) Using echo command, answer what are . and .. displayed in the output

C program to read the content of a given a directory:

Note: This C program is in your textbook in the Systems Programming chapter. I made couple of minor changes: replaced arg_check by an if statement. I have added documentation to the program to show you how to document a function and a program. You must create the program file as given including documentation. You should practice to properly document your program and the functions in it. Refer to the book for details about this program. This program should compile without any error, if you type it correctly.

If the cc command produces syntax errors, it is because of your typing mistakes. With each syntax error, the line number where the error occurred will be displayed. To fix the problem, carefully compare the line in your file with the line in the program below. Fix any differences and recompile. Keep doing it until all the errors are fixed.

/*

* Program filename: *** type the actual filename you chose here like in the textbook. ***

*

* Program Description:

* This program reads the content of a given directory and displays it.

* Uses the system call function opendir( ) to open the directory file.

* Uses the system call function readdir( ) to read the directory content one file entry at time

* in a the while loop until there is no more entry (end of file).

* Finally, after reading all the entries, the directory file is closed using the

* system call function closedir( ).

*

* Function: main ( )

* Input parameter:

* Directory name

* Output parameter:

* None

*

* Return value:

* 0, if successful

* -1, if a directory name is not given as argument

* -2, if opening the directory file (opendir) fails

*

* Program Defect:

* This program does not explicitly check for readdir system call failure.

* But returns successful status.

*/

#include /* For DIR and struct dirent */

#include /* For standard input and output */

int main (

int argc, /* Number of arguments supplied when running the program */

char **argv /* Values of arguments */

)

{

DIR *dir; /* Directory file pointer returned by opendir */

struct dirent *direntry; /* Information for one file in the directory */

/* returned by readir */

/* Check number of arguments supplied */

if (argc != 2)

{

printf(Run the program specifying one directory name as argument. );

exit (-1); /* return error status */

}

/* open the directory file */

if ((dir = opendir(argv[1])) == NULL)

{

printf(%s directory file open failed. , argv[1]);

exit (-2); /* return error status */

}

/* Read directory entries one file entry at a time until end of file or error */

while ((direntry = readdir(dir)) != NULL) /* until entries are exhausted */

{

printf (%10d %s , direntry->d_ino, direntry->d_name);

}

/* Close directory file */

closedir (dir);

exit (0); /* return successful status */

} /* end main ( ) */

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

SQL Database Programming

Authors: Chris Fehily

1st Edition

1937842312, 978-1937842314

More Books

Students also viewed these Databases questions

Question

Discuss all branches of science

Answered: 1 week ago