Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implementing ls command In this assignment, you will write a program in C that can print the names of files in the current directory. What

Implementing ls command

In this assignment, you will write a program in C that can print the names of files in the current directory.

What to do?

Write a C program myls1 whose behavior resembles that of the system command ls.

myls1 must accept the following parameters:

~$ myls1 [-ars] [directory]

If myls1 is provided with no parameters, it lists the names of all files in the current directory, one file name per line. directory specifies the name of a directory, in which case myls1 lists the names of files within that directory.

-a switch forces myls1 to display the names of hidden files (whose names start with a "."). In the absence of this switch, hidden files are not listed.

-s switch sorts the filenames in the lexicographical order.

-r switch sorts the filenames in the reverse lexicographical order.

Test myls1 using the files archived a ZIP file (this zip file can be found on Blackboard with the assignment), which contains several nested directories (named dirA, dirB, and dirA1, each with a number of short text files (named file*.txt). Use the following test cases when running myls1 from the directory at the same level with dirA and dirB:

~$ myls1

~$ myls1 -a

~$ myls1 -s dirA

~$ myls1 -a dirB

~$ myls1 -r dirA/dirA1

~$ myls1 dirB/dirB1

Use the following code as a guide:

/** ls1.c ** This program list contents of directory or directories ** If no args is provided, use . else list files in args ** usage: ./ls1 ** usage: ./ls1 demodir/a demodir **/ #include  #include  #include  void do_ls(char []); int main(int argc, char *argv[]) { // If no args, call do_ls function using ".", which is the current directory if ( argc == 1 ){ do_ls( "." ); } else{ for (int i = 1; i < argc; i++) { printf("%s: ", argv[i]); do_ls(argv[i]); } } return 0; } /* * list files in directory called dirname */ void do_ls( char dirname[] ) { DIR *dir_ptr; /* the directory */ struct dirent *direntp; /* each entry */ if ( ( dir_ptr = opendir( dirname ) ) == NULL ) fprintf(stderr,"ls1: cannot open %s ", dirname); else { while ( ( direntp = readdir( dir_ptr ) ) != NULL ) printf("%s ", direntp->d_name ); closedir(dir_ptr); } }

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_2

Step: 3

blur-text-image_3

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

Relational Database And Transact SQL

Authors: Lucy Scott

1st Edition

1974679985, 978-1974679980

More Books

Students also viewed these Databases questions

Question

Evaluate the importance of diversity in the workforce.

Answered: 1 week ago

Question

Identify the legal standards of the recruitment process.

Answered: 1 week ago