-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathdirectls.c
78 lines (55 loc) · 1.42 KB
/
directls.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
//
// directls.c
// Algorithms - Sorting a directory listing
//
// Created by YourtionGuo on 10/05/2017.
// Copyright © 2017 Yourtion. All rights reserved.
//
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "directls.h"
#include "sort.h"
#pragma mark - Private
/**
比较两个目录
@param key1 目录1
@param key2 目录2
@return 大于返回 1,小于返回 -1,等于返回 0
*/
static int compare_dir(const void *key1, const void *key2)
{
int retval = strcmp(((const Directory *)key1)->name, ((const Directory *)key2)->name);
if (retval > 0) return 1;
if (retval < 0) return -1;
return 0;
}
#pragma mark - Public
int directls(const char *path, Directory **dir)
{
DIR *dirptr;
Directory *temp;
struct dirent *curdir;
int count;
/// 打开目录
if ((dirptr = opendir(path)) == NULL) return -1;
/// 获得目录内容
*dir = NULL;
count = 0;
while ((curdir = readdir(dirptr)) != NULL) {
count++;
if ((temp = (Directory *)realloc(*dir, count * sizeof(Directory))) == NULL) {
free(*dir);
return -1;
} else {
*dir = temp;
}
strcpy(((*dir)[count - 1]).name, curdir->d_name);
}
closedir(dirptr);
/// 对目录中条目按照名字进行排序
if (qksort(*dir, count, sizeof(Directory), 0, count - 1, compare_dir) != 0) return -1;
/// 返回目录条目数量
return count;
}