[solved]-Need Help Following Task Rate Thanks Advance Homework Task 3 Task Write Simple Version Ls Q39042105
I need help with the following task – will rate! Thanks inadvance!
Homework- Task 3
Your task is to write a simple version of the ls(1) command thatprints a list of objects in a specified directory and gives someinformation about each object:
-
a single character giving the object type
- ‘d’ if the object is a directory
- ‘-‘ if the object is a regular file
- ‘l’ if the object is a symbolic link
- ‘?’ for all other object types
-
a 9-character string giving the permissions for owner, group andothers in the same -rwxr-x–x-style format as ls(1) gives: youshould complete the rwxmode() function to produce this string; youmay not use strmode(3).
-
the name of the object’s owner; use the username() function toproduce this
-
the name of the object’s group; use the groupname() function toproduce this
-
the object’s size in bytes
-
the object’s name
The program should ignore any objects whose name starts with thecharacter ‘.’ (dot). The program is not required to print theobjects in any particular order (i.e., it doesn’t need to followls’s alphabetical order).
There are some string buffers defined at the top of main() thatyou can use to hold the various string values. You mustuse lstat(2) to collect the object’s information, otherwise youwon’t be able to detect symbolic links. Print the output using thefollowing printf(3) format string:
printf ( “%s %-8.8s %-8.8s %8lld %sn”, rwxmode (ModeInfo, mode), username (OwnerUID, uname), groupname(GroupGID, gname), (long long) Size, ObjectName);
where obviously you need to replace things like Size by valuesfrom the struct stat for the current object.
The main part of your program should be structured somethinglike this:
open the directorywhile there are more entries { ignore the object if its name starts with ‘.’ get the struct stat info for the object (using its path) print the details using the object name and struct stat info}close the directory
Once your program is working correctly, it should produce outputlike:
./myls .lrwxr-xr-x jas cs1521 17 Makefilelrwxr-xr-x jas cs1521 15 stat.c-rw-r—– jas cs1521 2934 myls.c-rwxr-xr-x jas cs1521 28440 myls-rwxr-xr-x jas cs1521 26616 stat-rw-r–r– jas cs1521 10040 stat.o-rw-r–r– jas cs1521 12504 myls.o./myls ~cs1521drwx—— cs1521 cs1521 4096 18s1lrwxrwxrwx cs1521 cs1521 9 workdrwxr-x— cs1521 cs1521 4096 17s2.workdrwxr-xr-x cs1521 cs1521 4096 sharedrwxr-x— cs1521 cs1521 4096 sbindrwxr-x— cs1521 cs1521 4096 19T2.workdrwxr-x— cs1521 cs1521 4096 18s2.workdrwx—— cs1521 cs1521 4096 18s2lrwxrwxrwx cs1521 cs1521 11 web-rw-r—– cs1521 cs1521 60 give.specdrwxr-x— cs1521 cs1521 4096 19T2drwxr-xr-x cs1521 cs1521 4096 bindrwxr-xr-x cs1521 cs1521 4096 libdrwx—— cs1521 cs1521 4096 17s2drwxr-xr-x cs1521 cs1521 4096 spimdrwxr-x— cs1521 cs1521 4096 18s1.worklrwxrwxrwx cs1521 cs1521 4 tmpdrwxr-xr-x cs1521 cs1521 4096 public_html
Although, of course, in the first case, it will be yourusername, rather than jas, and the file ordering, sizes, and typesmay be different.
Given Code:
#include <sys/types.h>#include <sys/stat.h>#include <dirent.h>#include <err.h>#include <errno.h>#include <fcntl.h>#include <grp.h>#include <pwd.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#ifdef __linux__# include <bsd/string.h>#endif#include <sysexits.h>#include <unistd.h>#define MAXDIRNAME 256#define MAXFNAME 256#define MAXNAME 24char *rwxmode (mode_t, char *);char *username (uid_t, char *);char *groupname (gid_t, char *);int main (int argc, char *argv[]){ // string buffers for various names // char uname[MAXNAME+1]; // UNCOMMENT this line // char gname[MAXNAME+1]; // UNCOMMENT this line // char mode[MAXNAME+1]; // UNCOMMENT this line // collect the directory name, with “.” as default char dirname[MAXDIRNAME] = “.”; if (argc >= 2) strlcpy (dirname, argv[1], MAXDIRNAME); // check that the name really is a directory struct stat info; if (stat (dirname, &info) < 0) err (EX_OSERR, “%s”, dirname); if (! S_ISDIR (info.st_mode)) { errno = ENOTDIR; err (EX_DATAERR, “%s”, dirname); } // open the directory to start reading // DIR *df; // UNCOMMENT this line // … TODO … // read directory entries // struct dirent *entry; // UNCOMMENT this line // … TODO … // finish up // closedir(df); // UNCOMMENT this line return EXIT_SUCCESS;}// convert octal mode to -rwxrwxrwx stringchar *rwxmode (mode_t mode, char *str){ // … TODO … return NULL;}// convert user id to user namechar *username (uid_t uid, char *name){ struct passwd *uinfo = getpwuid (uid); if (uinfo != NULL) snprintf (name, MAXNAME, “%s”, uinfo->pw_name); else snprintf (name, MAXNAME, “%d?”, (int) uid); return name;}// convert group id to group namechar *groupname (gid_t gid, char *name){ struct group *ginfo = getgrgid (gid); if (ginfo != NULL) snprintf (name, MAXNAME, “%s”, ginfo->gr_name); else snprintf (name, MAXNAME, “%d?”, (int) gid); return name;}
Expert Answer
Answer to I need help with the following task – will rate! Thanks in advance! Homework- Task 3 Your task is to write a simple vers… . . .
OR

