1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
struct stat statbuff;
int recls(char *);
int lsdetails(struct stat*); //note alternative prototype
int printperms(mode_t mode);
int printowner(int uid);
int main(int argc, char **argv){
recls(argv[1]);
printf("\n");
}
int recls(char *path){
DIR *dir;
//Pointer of type DIR
struct dirent *dirslot;
chdir(path);
dir=opendir(".");
//Open the current directory
while (1) {
dirslot=readdir(dir);
if (dirslot==NULL) break;//note change
stat(dirslot->d_name, &statbuff);
lsdetails(&statbuff);
printf(" \t%s\n ", dirslot->d_name);
//Display the name of file
}
rewinddir(dir);
while (1) {
dirslot=readdir(dir);
//Read the directory
if (dirslot==NULL) return;//note
if (strcmp(dirslot->d_name, ".")==0) continue;
// If pointer d_name current fonction continue
if (strcmp(dirslot->d_name, "..")==0) continue;
// If pointer d_name parent fonction continue
stat(dirslot->d_name, &statbuff);
if (S_ISDIR(statbuff.st_mode)) {
printf("\n %s:\n", dirslot->d_name);
// Display the name of the directory
chdir(dirslot->d_name);
recls(".");
//Call function recls
chdir("..");
//Change directory into the parent
}
}
}
int lsdetails(struct stat *astatbuff) {
printperms(astatbuff->st_mode);
//call function printperms
printf(" %d",astatbuff->st_nlink);
//Hard link
printowneruid(astatbuff->st_uid);
//Call printowneruid
printownergid(astatbuff->st_gid);
//Call function printownergid
printf(" %d\t",astatbuff->st_size);
//Display size
lastmodif();
//Call function lastmodif
return 0;
}
int printowneruid(int uid) {
printf("\t%s\t", getpwuid (uid)->pw_name);
// Display user name ID
}
int printownergid(int gid) {
printf("%s\t", getgrgid (gid)->gr_name);
//Display group name ID
}
int printperms(mode_t mode) {
//Display all permition
char perms[11]; int i;
for (i=0;i<11;i++) perms[i]='-'; //regular file
perms[10]=0;
if (S_ISDIR(mode))perms[0]='d'; //Directory
if (S_ISBLK(mode))perms[0]='b'; //Block device
if (S_ISCHR(mode))perms[0]='c'; //Character device
if (S_ISLNK(mode))perms[0]='l'; //Symbolic link
if (S_ISSOCK(mode))perms[0]='s'; //Socket
if (mode & S_IRUSR) perms[1]='r'; //Permission owner read
if (mode & S_IWUSR) perms[2]='w'; //Permission owner write
if (mode & S_IXUSR) perms[3]='x'; //Permission owner execute
if (mode & S_IRGRP) perms[4]='r'; //Permission group read
if (mode & S_IWGRP) perms[5]='w'; //Permission group write
if (mode & S_IXGRP) perms[6]='x'; //Permission group execute
if (mode & S_IROTH) perms[7]='r'; //Permission others read
if (mode & S_IWOTH) perms[8]='w'; //Permission others write
if (mode & S_IXOTH) perms[9]='x'; //Permission others execute
printf("%s\t",perms);
//Display all perms end after display /t it for tab
}
int lastmodif()
//New prog for date and hours
{
struct tm *date;
date=localtime(&statbuff.st_mtime);
//It's latest date of modification
//Date
printf("%d-",date->tm_year+1900);
//Display year linux begin in 1900 for display good Year add +1900
//display months correctly
if(date->tm_mon<10){printf("0%d-",date->tm_mon+1);}
//Display month and linux display 0 to 11 and for display good month add +1
else printf("%d:",date->tm_mon+1);
if (date->tm_mday<10) {printf("0%d",date->tm_mday);} // Display Day
else printf("%d",date->tm_mday);
// ex: March-08-2008
// The fonction ls-lr display March-08-2008 but for display 08 add 0 if number of month is <10
//Display hour correctly
if (date->tm_hour<10){printf(" 0%d:",date->tm_hour);}
// Display Hour
else printf(" %d:",date->tm_hour);
if (date->tm_min<10){printf("0%d:",date->tm_min);}
// Display Minute
else printf("%d:",date->tm_min);
if (date->tm_sec<10){printf("0%d",date->tm_sec);}
// Display Seconde
else printf("%d",date->tm_sec);
// ex: 10:06:55
// The fonction ls-lr display 10:06:55 but for display 06 add 0 if hours or minute or seconde <10
} |
Partager