-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls6.c
201 lines (178 loc) · 3.5 KB
/
ls6.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
#define SCREEN_WIDTH 80 /* for now */
#define CHUNKLEN 100
/*
*引入快递排序
*
*/
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<dirent.h>
#include<string.h>
int count=0;
void *emalloc(unsigned), *erealloc(void*, unsigned);
char **namelist = NULL;
int listlen = 0;
int taken = 0;
void do_ls(char []);
int compare(char **p1,char **p2);
int compare(char **p1,char **p2)
{
//逆序输出
return strcmp((char*)*p2,(char *)*p1);
//升序排序
//return strcmp((char*)*p1,(char *)*p2);
}
//读取参数
main(int ac, char *av[])
{
if ( ac == 1 )
do_ls( "." );
else
while ( --ac ){
printf("%s:\n", *++av );
do_ls( *av );
}
}
void do_ls( char dirname[] )
/*
* * list files in directory called dirname
* */
{
DIR *dir_ptr; /* the directory */
struct dirent *direntp; /* each entry */
list_init();
if ( ( dir_ptr = opendir( dirname ) ) == NULL )
fatal("cannot open ", dirname);
else
{
while ( ( direntp = readdir( dir_ptr ) ) != NULL ){
list_append( direntp->d_name );
count++;
}
closedir(dir_ptr);
}
qsort((void *)namelist,(size_t)taken,sizeof(char *),compare);
list_show();
list_finish();
}
/*
* * list functions here
* * a list of names is an array that grows as needed
* */
list_init()
{
namelist = (char **) emalloc(CHUNKLEN * sizeof(char *));
listlen = CHUNKLEN;
taken = 0;
}
/*
* * add an item to the list, extending it if needed
* */
list_append(char *str)
{
//函数指针
char *newstring(char *);
//
char *name = newstring(str);
if ( taken == listlen ){
listlen += CHUNKLEN;
namelist = (char **) erealloc(namelist,listlen);
}
namelist[taken++] = name ;
}
/*
* * figure out max length, then figure out number of cols we
* * can do and then list them
* */
list_show()
{
int i;
int maxlen = 0;
int pagewidth;
int cols, col_len, col_wid;
int row, col;
char *name;
/* figure out lengths and widths of columns */
//确定最大字符串的长度
for(i=0; i<taken; i++)
if ( strlen(namelist[i]) > maxlen )
maxlen = strlen(namelist[i]);
//得到每一列的长度
col_wid = maxlen+1;
//得到页面的宽度
pagewidth = get_screen_width();
//得到列的数目
cols = pagewidth / col_wid ;
//达到列的宽度
col_len = taken / cols ;
//如果有多余则列的宽度加1
if ( taken % cols ) /* for uneven cols, put the */
col_len++; /* extra on the left */
/* print the list using these numbers */
// 打印
for( row = 0 ; row < col_len ; row++ )
for( col = 0 ; col < cols ; col++ ){
i = row + col*col_len ;
if ( i < taken ){
if ( i + col_len < taken )
printf("%-*s",col_wid,namelist[i]);
else
printf("%s\n",namelist[i]);
}
}
}
/*
* * delete the words and the list
* */
list_finish()
{
int i;
//释放指针
for(i = 0 ; i<taken ; i++ )
free(namelist[i]);
//释放namelist自身
free(namelist);
namelist = NULL;
//重置taken listlen
taken = listlen = 0 ;
}
/*
* * in the future, we can ask the system for the width of
* * the screen
* */
int get_screen_width()
{
return SCREEN_WIDTH;
}
/*
* * construct a string
* */
char *newstring(char *s)
{
char *rv = emalloc( 1 + strlen(s) );
strcpy(rv, s);
return rv;
}
void *emalloc(unsigned n)
{
void *rv = malloc(n);
if ( rv )
return rv;
fatal("out of memory","");
}
void *erealloc(void *p, unsigned n)
{
void *rv = realloc(p,n);
if ( rv )
return rv;
fatal("out of memory","");
}
/*
* * report and die
* */
fatal(char *s1, char *s2)
{
fprintf(stderr,"ls1: %s%s\n", s1, s2);
exit(1);
}