-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyls.c
236 lines (205 loc) · 5.17 KB
/
myls.c
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pwd.h> // getpwuid()
#include <grp.h> // getgrgid()
#include <sys/ioctl.h>
#include <err.h>
#include <fcntl.h>
#define PERM_LENGTH 11
#define PATH_LENGTH 100
//#define DEBUG
//=========================================
// dirent 구조체
//=========================================
/*
struct dirent
{
long d_ino; //inode 번호
off_t d_off; // offset
unsigned short d_reclen; //d_name 길이
char d_name[NAME_MAX+1]; //파일 이름
};
*/
//=========================================
// stat 구조체
//=========================================
/*
struct stat {
dev_t st_dev; // ID of device containing file
ino_t st_ino; // inode number
mode_t st_mode; // 파일의 종류 및 접근권한
nlink_t st_nlink; // hardlink 된 횟수
uid_t st_uid; // 파일의 owner
gid_t st_gid; // group ID of owner
dev_t st_rdev; // device ID (if special file)
off_t st_size; // 파일의 크기(bytes)
blksize_t st_blksize; // blocksize for file system I/O
blkcnt_t st_blocks; // number of 512B blocks allocated
time_t st_atime; // time of last access
time_t st_mtime; // time of last modification
time_t st_ctime; // time of last status change
};
*/
//=========================================
// group 구조체
//=========================================
/*
struct group {
char *gr_name; // group name
char *gr_passwd; // group password
gid_t gr_gid; // group ID
char **gr_mem; // group members
};
*/
//=========================================
// group 구조체
//=========================================
/*
struct passwd {
char *pw_name;
char *pw_passwd;
uid_t pw_uid;
gid_t pw_gid;
time_t pw_change;
char *pw_class;
char *pw_gecos;
char *pw_dir;
char *pw_shell;
time_t pw_expire;
};
*/
//=========================================
// Function Name : access Name
// arg1 : permission value
// arg2 : mode
// return : void
// Description :
//=========================================
void access_perm(char *perm, mode_t mode)
{
int i;
char permchar[] = "rwx";
memset(perm, '-', 10);
perm[10] = '\0';
if (S_ISDIR(mode)) perm[0] = 'd';
else if (S_ISCHR(mode)) perm[0] = 'c';
else if (S_ISBLK(mode)) perm[0] = 'b';
else if (S_ISFIFO(mode)) perm[0] = 'p';
else if (S_ISLNK(mode)) perm[0] = 'l';
for (i = 0; i < 9; i++)
{
if ((mode << i) & 0x100)
perm[i+1] = permchar[i%3];
}
if (mode & S_ISUID) perm[3] = 's';
if (mode & S_ISGID) perm[6] = 's';
if (mode & S_ISVTX) perm[9] = 't';
}
struct winsize get_winsize(){
struct winsize ws;
int fd = open("/dev/tty", O_RDWR);
if(fd == -1) err(1, "/dev/tty");
if(ioctl(fd, TIOCGWINSZ, &ws) < 0) err(1, "/dev/tty");
close(fd);
return ws;
}
int main(int argc, char *argv[])
{
DIR *dp; // DIR pointer
struct stat statbuf; // inode info
struct dirent *dent;
struct group *group_entry;
struct passwd *user_pw;
char perm[PERM_LENGTH];
char pathname[PATH_LENGTH];
char dirname[PATH_LENGTH];
int flag;
char temp[20];
struct winsize ws = get_winsize(); // window size(ws_col,ws_row)
int win_index =0; // 현재까지 출력된 window 위치
//time
struct tm *tm;
if (argc==1)
flag = 0;
strcpy(dirname,".");
if(argc>1)
{
// "-al" 문자열을 비교
sprintf(temp,"-al");
if(!strcmp(temp,argv[1]))
{
flag = 1;
strcpy(dirname,".");
}
else // 인자가 폴더명일 때
{
flag = 2;
strcpy(dirname, argv[1]);
}
}
if(argc>2) // -l 같은 인자 처리
{
flag = 3;
strcpy(dirname, argv[2]);
}
#ifdef DEBUG
printf("dir=%s\n",dirname);
printf("flag=%d\n",flag);
#endif
// 디렉토리의 inode정보 읽기
stat(dirname, &statbuf);
// 디렉토리가 아닌경우 에러 메시지 출력 후 종료
if (!S_ISDIR(statbuf.st_mode))
{
fprintf(stderr, "%s is not directory\n",dirname);
exit(1);
}
// 디렉토리가 정상적으로 열리지 않은 경우
if ((dp = opendir(dirname)) == NULL)
{
perror("Error:");
exit(1);
}
#ifdef DEBUG
// 정상적으로 열리면 디렉토리명을 출력
printf("Lists of Directory(%s):\n", dirname);
#endif
// 디렉토리의 내용을 읽어온다.
// 더이상 읽을 디렉토리 내용이 없을 때 까지
while((dent = readdir(dp)) != NULL)
{
if(flag==1 | flag==3)
{
sprintf(pathname, "%s/%s", dirname, dent->d_name);
lstat(pathname, &statbuf);
access_perm(perm, statbuf.st_mode);
user_pw=getpwuid(statbuf.st_uid);
group_entry=getgrgid(statbuf.st_gid);
tm = localtime(&statbuf.st_mtime);
strftime(temp, sizeof(temp), "%m월 %e %H:%M", tm);
printf("%s %3ld %6s %6s %8ld %s %s\n", \
perm, statbuf.st_nlink, user_pw->pw_name, \
group_entry->gr_name, statbuf.st_size, temp, dent->d_name);
}
else
{ win_index += strlen(dent->d_name);
if(win_index < ws.ws_col){
printf("%s ",dent->d_name);
win_index += 2;
}else{
win_index = strlen(dent->d_name)+2;
printf("\n");
printf("%s ",dent->d_name);
}
}
}
printf("\n");
closedir(dp);
return 0;
}