-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathls.cpp
68 lines (60 loc) · 1.83 KB
/
ls.cpp
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
//2018201005 Vatsal Soni
#include "config.h"
int fileInfo(char* path,char* name,int chk) // It prints all file info like permission/name etc..
{
struct stat statObj;
char* fname;
register uid_t uid;
register gid_t gid;
register struct group *g;
register struct passwd *pw;
double size;
if(chk==0)
{
string s=string(path)+"/"+string(name);
fname=new char[s.length()+1];
strcpy(fname, s.c_str());
}
else
{
string s=string(name);
fname=new char[s.length()+1];
strcpy(fname, s.c_str());
}
if(stat(fname,&statObj) < 0)
{
cout<<"Error";
//return 1;
}
// permission of a file (r-w-x of user,group and other)
printf( (S_ISDIR(statObj.st_mode)) ? "d" : "-");
printf( (statObj.st_mode & S_IRUSR) ? "r" : "-");
printf( (statObj.st_mode & S_IWUSR) ? "w" : "-");
printf( (statObj.st_mode & S_IXUSR) ? "x" : "-");
printf( (statObj.st_mode & S_IRGRP) ? "r" : "-");
printf( (statObj.st_mode & S_IWGRP) ? "w" : "-");
printf( (statObj.st_mode & S_IXGRP) ? "x" : "-");
printf( (statObj.st_mode & S_IROTH) ? "r" : "-");
printf( (statObj.st_mode & S_IWOTH) ? "w" : "-");
printf( (statObj.st_mode & S_IXOTH) ? "x" : "-");
size=statObj.st_size/1024.0;
printf("\t%10.4f KB",size); // size of a file
uid = geteuid ();
pw = getpwuid (uid);
if (pw)
{
printf("\t%s",pw->pw_name);
}
gid=statObj.st_gid;
g=getgrgid(gid);
printf("\t%s",g->gr_name);
string dt=ctime(&statObj.st_mtime);
//printf(" %s", ctime(&statObj.st_mtime)); // last modified date of a file
printf("\t");
for(unsigned int i=0;i<dt.length()-1;i++)
printf("%c",dt[i]);
//strcpy(date,dt.c_str());
printf(" %s",name); // name of a file
printf("\n");
return 0;
}