我不想按照男子的页数使用q,但不管我所试图的是什么,我还是想看到一个ault子。
兹在法典中规定事项
int compare_dirent(const void *a, const void *b)
{
const struct dirent *first = (const struct dirent *) a;
const struct dirent *second = (const struct dirent *) b;
return first->d_ino - second->d_ino;
}
int process(FILE* output,const char *dirname, int flags)
{
struct dirent *entries = NULL;
struct dirent *table[256];
int entry_num = 0;
DIR *directory = NULL;
char cwd[1024];
getcwd(cwd,1024);
bzero(table,256);
directory = opendir(dirname);
while((entries = readdir(directory))!=NULL)
{
if(entries->d_type == DT_REG)
{
fprintf(output,"%s
",entries->d_name);
table[entry_num] = entries;
entry_num++;
}
}
fprintf(stderr,"last entry: %s
", table[entry_num-1]->d_name);
/* RIGHT HERE */
qsort(table, entry_num, sizeof(struct dirent), &compare_dirent);
return entry_num;
}
在编造假时,请见每版<代码>fprintf的目录中的档案清单,同时请见最后条目。 我把一个断点放在比较中,那个时候是N是档案数量,然后我立即从_qsort那里获得一个SEGFAULT。
关于Nth电话,对坠毁的q子进行对比。
Here is the gdb output
Starting program: /Users/luke/Documents/Dev/code/cs647/prog2/bin/prog2 ./
Reading symbols for shared libraries +........................ done
.main.c.swp
get_pdf.sh
main.c
main.o
Makefile
program2.pdf
test.txt
last entry: test.txt
Breakpoint 1, compare_dirent (a=0x7fff5fbff018, b=0x7fff5fbfec00) at main.c:88
88 const struct dirent *first = (const struct dirent *) a;
(gdb) n
89 const struct dirent *second = (const struct dirent *) b;
(gdb) n
91 return first->d_ino - second->d_ino;
(gdb) c
Continuing.
Breakpoint 1, compare_dirent (a=0x7fff5fbff430, b=0x7fff5fbfec00) at main.c:88
88 const struct dirent *first = (const struct dirent *) a;
(gdb) c
Continuing.
Breakpoint 1, compare_dirent (a=0x7fff5fbff848, b=0x7fff5fbfec00) at main.c:88
88 const struct dirent *first = (const struct dirent *) a;
(gdb) c
Continuing.
Breakpoint 1, compare_dirent (a=0x7fff5fbffc60, b=0x7fff5fbfec00) at main.c:88
88 const struct dirent *first = (const struct dirent *) a;
(gdb) c
Continuing.
Breakpoint 1, compare_dirent (a=0x7fff5fc00078, b=0x7fff5fbfec00) at main.c:88
88 const struct dirent *first = (const struct dirent *) a;
(gdb) c
Continuing.
Breakpoint 1, compare_dirent (a=0x7fff5fc00490, b=0x7fff5fbfec00) at main.c:88
88 const struct dirent *first = (const struct dirent *) a;
(gdb) c
Continuing.
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00007fff5fc00490
0x00007fff8e238540 in _qsort ()
解决办法(完整)是两个答案中的一部分。
int compare_dirent(const void *a, const void *b)
{
const struct dirent *first = (const struct dirent *) a;
const struct dirent *second = (const struct dirent *) b;
return first->d_ino - second->d_ino;
}
int process(FILE* output,const char *dirname, int flags)
{
struct dirent *entries = NULL;
struct dirent table[256];
int entry_num = 0;
DIR *directory = NULL;
char cwd[1024];
getcwd(cwd,1024);
bzero(table,256);
directory = opendir(dirname);
while((entries = readdir(directory))!=NULL)
{
if(entries->d_type == DT_REG)
{
fprintf(output,"%s
",entries->d_name);
memcpy(table+entry_num, entries, sizeof(struct dirent));
entry_num++;
}
}
fprintf(stderr,"size: %lu
", sizeof(struct dirent));
qsort(table, entry_num, sizeof(struct dirent) , compare_dirent);
fprintf(output,"
");
for(int i=0;i<entry_num;i++)
{
fprintf(output,"%s
", table[i].d_name);
}
return entry_num;
}