-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathls.c
185 lines (170 loc) · 4.22 KB
/
ls.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
/******************************************************************
* Filename: ls
* Date: 2012-04-09
* Description: 自己实现ls命令
* Last Modified: 2012-04-09
*****************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
void do_ls(char []);
void dostat(char *fname);
void show_file_info(char *fname, struct stat *info);
void mode_to_letters(int mode, char str[]);
char *uid_to_name( uid_t );
char *gid_to_name( gid_t );
int lsa = 0, lsl = 0;
int main(int argc, char *argv[])
{
const char* optstring = "al";
char *pwd;
int opt = 0;
int ind = 0;
while ((opt = getopt(argc, argv, optstring)) != -1){
switch (opt) {
case 'a':
lsa = 1;
printf("lsa = true\n");
break;
case 'l':
lsl = 1;
printf("lsl = true\n");
break;
case '?':
default:
printf("???\n");
break;
}
}
ind = optind;
if (ind < argc) {
while (ind < argc) {
printf("ind == %d\n", ind);
do_ls(*(argv+ind));
ind++;
if (ind != argc) {
printf("\n");
}
}
}
else {
do_ls(".");
}
/*if (argc == 1) {
do_ls(".");
}
else {
while (--argc) {
printf("%s:\n", * ++argv);
do_ls(*argv);
}
}*/
return 0;
}
void do_ls(char dirname[])
{
DIR *dir_ptr;
struct dirent *direntp;
char pathtofile[300] = "";
char tmp[350];
if ((dir_ptr = opendir(dirname)) == NULL) {
fprintf(stderr, "ls: cannot open %s\n", dirname);
}
else {
printf("%s\n", dirname);
strcat(pathtofile, dirname);
strcat(pathtofile, "/");
printf("path == %s\n", pathtofile);
if (lsl == 1) {
while ((direntp = readdir(dir_ptr)) != NULL) {
tmp[0] = '\0';
strcat(tmp, pathtofile);
strcat(tmp, direntp->d_name);
dostat(tmp);
}
}
else {
while ((direntp = readdir(dir_ptr)) != NULL) {
printf("%s\n", direntp->d_name);
}
}
closedir(dir_ptr);
}
}
void dostat(char * fname)
{
struct stat info;
if (stat(fname, &info) == -1) {
perror(fname);
}
else {
show_file_info(fname, &info);
}
}
void show_file_info(char *fname, struct stat *info)
{
char modestr[11];
mode_to_letters(info->st_mode, modestr);
printf("%s", modestr);
printf(" %4d", (int)info->st_nlink);
printf(" %-6s" , uid_to_name(info->st_uid) );
printf(" %-6s" , gid_to_name(info->st_gid) );
printf(" %8ld" , (long)info->st_size);
printf(" %.12s", 4+ctime(&info->st_mtime));
printf(" %s\n", fname);
}
void mode_to_letters(int mode, char str[])
{
strcpy(str, "----------");
if (S_ISDIR(mode)) {
str[0] = 'd';
}
if (S_ISCHR(mode)) {
str[0] = 'c';
}
if (S_ISBLK(mode)) {
str[0] = 'b';
}
if ( mode & S_IRUSR ) str[1] = 'r'; /* 3 bits for user */
if ( mode & S_IWUSR ) str[2] = 'w';
if ( mode & S_IXUSR ) str[3] = 'x';
if ( mode & S_IRGRP ) str[4] = 'r'; /* 3 bits for group */
if ( mode & S_IWGRP ) str[5] = 'w';
if ( mode & S_IXGRP ) str[6] = 'x';
if ( mode & S_IROTH ) str[7] = 'r'; /* 3 bits for other */
if ( mode & S_IWOTH ) str[8] = 'w';
if ( mode & S_IXOTH ) str[9] = 'x';
}
#include <pwd.h>
char *uid_to_name( uid_t uid )
/*
* returns pointer to username associated with uid, uses getpw()
*/
{
struct passwd *getpwuid(), *pw_ptr;
static char numstr[10];
if ( ( pw_ptr = getpwuid( uid ) ) == NULL ){
sprintf(numstr,"%d", uid);
return numstr;
}
else
return pw_ptr->pw_name ;
}
#include <grp.h>
char *gid_to_name( gid_t gid )
/*
* returns pointer to group number gid. used getgrgid(3)
*/
{
struct group *getgrgid(), *grp_ptr;
static char numstr[10];
if ( ( grp_ptr = getgrgid(gid) ) == NULL ){
sprintf(numstr,"%d", gid);
return numstr;
}
else
return grp_ptr->gr_name;
}