summaryrefslogtreecommitdiffstats
path: root/test/stdlib/qsort.c
blob: 9e706c78d7436ef2b02b5436ca8ce603bf086f26 (plain)
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
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>

int select_files(const struct dirent *dirbuf)
{
    if (dirbuf->d_name[0] == '.')
	return 0;
    else         
	return 1;
}


int main(void)
{
    struct dirent **array;
    struct dirent *dirbuf;

    int i, numdir;        

    chdir("/");
    numdir = scandir(".", &array, select_files, NULL);
    printf("\nGot %d entries from scandir().\n", numdir);              
    for (i = 0; i < numdir; ++i) {      
	dirbuf = array[i];
	printf("[%d] %s\n", i, dirbuf->d_name);
	free(array[i]);
    }                  
    free(array);
    numdir = scandir(".", &array, select_files, alphasort);
    printf("\nGot %d entries from scandir() using alphasort().\n", numdir);                   
    for (i = 0; i < numdir; ++i) {      
	dirbuf = array[i];
	printf("[%d] %s\n", i, dirbuf->d_name);
    }                                          
    printf("\nCalling qsort()\n");                   
    qsort(array, numdir, sizeof(struct dirent *), alphasort);
    for (i = 0; i < numdir; ++i) {                           
	dirbuf = array[i];
	printf("[%d] %s\n", i, dirbuf->d_name);
	free(array[i]);
    }
    free(array);
    return(0);
}