q3.c - #include#include#include#include#include#include#include#include#include#include#include <stdio.h> <stdlib.h> <unistd.h> <sys\/wait.h>

q3.c -...

This preview shows page 1 - 2 out of 3 pages.

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/wait.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/sysmacros.h> #include <time.h> #include <pwd.h> #include <grp.h> #include <dirent.h> int main(int argc, char *argv[]) { int i; struct stat buf; char *p; char *file; if(argc==1){ //when no command line argument is provided DIR *directory; struct dirent *dir; directory = opendir("."); printf("Files in the current directory:\n"); if(directory){ while((dir = readdir(directory))!=NULL){ printf("%s\n", dir->d_name); } closedir(directory); } } else{ for(int i=1;i<argc;i++){ //for loop to get all the command line arguments printf("%s: \n", argv[i]); if(lstat(argv[i], &buf)<0){ perror("lstat error:"); continue; } //User name struct passwd *name; int userId = buf.st_uid; name = getpwuid(userId); printf("User name: %s\n", name->pw_name); //Group name struct group *grName; int groupId = buf.st_gid; grName = getgrgid(groupId); printf("Group name: %s\n", grName->gr_name); //Type of file// if(S_ISREG(buf.st_mode)) //regular p = "-", file = "regular"; else if (S_ISDIR(buf.st_mode)) //directory p = "d", file = "Directory"; else if (S_ISCHR(buf.st_mode)) //chracter special
Image of page 1
Image of page 2

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture