Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a C program lslong that display the details of files for the directory. lslong should work just like a linux command, ls -al (ls

Write a C program lslong that display the details of files for the directory. lslong should work just like a linux command, ls -al (ls with -al option). It must use the two C files below.

/**===================ls1.c================**/ #include #include #include /** ** ls version 1.0 ** purpose list contents of directory or directories ** action if no args, use . else list files in args **/ main(int ac, char *av[]) { if ( ac == 1 ) do_ls( "." ); else while ( --ac ){ printf("%s: ", *++av ); do_ls( *av ); } } /** list files in directory called dirname*/ 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); } }

//===============================

/**===================stat1.c================**/

#include #include #include /* * stat1.c a user interface to the stat system call * for each arg, it lists all interesting * file info. Has a lot of numbers, though.. */ main(int ac, char *av[]){ while (--ac) dostat(*++av); } dostat(char *filename){ struct stat info; printf("%s: ", filename); /* print name */ if ( stat(filename, &info) == -1) /* cannot stat */ perror(filename); /* say why */ else /* else show info */ { printf("\t mode: %o ", (int) info.st_mode); /* mode */ printf("\t links: %d ", (int) info.st_nlink); /* links */ printf("\t owner: %d ", (int) info.st_uid); /* owner */ printf("\t group: %d ", (int) info.st_gid); /* group */ printf("\t size: %ld ", (long)info.st_size); /* size */ printf("\t mod: %ld ", (long)info.st_mtime); /* mod */ printf("\taccess: %ld ", (long)info.st_atime); /* access*/ } }

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

More Books

Students also viewed these Databases questions