| 1 |
/* |
| 2 |
* Copyright 2008-2011 Various Authors |
| 3 |
* Copyright 2004 Timo Hirvonen |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU General Public License as |
| 7 |
* published by the Free Software Foundation; either version 2 of the |
| 8 |
* License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#ifndef _LOAD_DIR_H |
| 20 |
#define _LOAD_DIR_H |
| 21 |
|
| 22 |
#include <sys/stat.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <dirent.h> |
| 25 |
|
| 26 |
struct directory { |
| 27 |
DIR *d; |
| 28 |
int len; |
| 29 |
/* we need stat information for symlink targets */ |
| 30 |
int is_link; |
| 31 |
/* stat() information. ie. for the symlink target */ |
| 32 |
struct stat st; |
| 33 |
char path[1024]; |
| 34 |
}; |
| 35 |
|
| 36 |
int dir_open(struct directory *dir, const char *name); |
| 37 |
void dir_close(struct directory *dir); |
| 38 |
const char *dir_read(struct directory *dir); |
| 39 |
|
| 40 |
|
| 41 |
struct ptr_array { |
| 42 |
/* allocated with malloc(). contains pointers */ |
| 43 |
void *ptrs; |
| 44 |
int alloc; |
| 45 |
int count; |
| 46 |
}; |
| 47 |
|
| 48 |
/* ptr_array.ptrs is either char ** or struct dir_entry ** */ |
| 49 |
struct dir_entry { |
| 50 |
mode_t mode; |
| 51 |
char name[]; |
| 52 |
}; |
| 53 |
|
| 54 |
#define PTR_ARRAY(name) struct ptr_array name = { NULL, 0, 0 } |
| 55 |
|
| 56 |
void ptr_array_add(struct ptr_array *array, void *ptr); |
| 57 |
|
| 58 |
static inline void ptr_array_plug(struct ptr_array *array) |
| 59 |
{ |
| 60 |
ptr_array_add(array, NULL); |
| 61 |
array->count--; |
| 62 |
} |
| 63 |
|
| 64 |
static inline void ptr_array_sort(struct ptr_array *array, |
| 65 |
int (*cmp)(const void *a, const void *b)) |
| 66 |
{ |
| 67 |
int count = array->count; |
| 68 |
if (count) |
| 69 |
qsort(array->ptrs, count, sizeof(void *), cmp); |
| 70 |
} |
| 71 |
|
| 72 |
static inline void ptr_array_unique(struct ptr_array *array, |
| 73 |
int (*cmp)(const void *a, const void *b)) |
| 74 |
{ |
| 75 |
void **ptrs = array->ptrs; |
| 76 |
int i, j = 0; |
| 77 |
|
| 78 |
for (i = 1; i < array->count; i++) { |
| 79 |
if (cmp(&ptrs[i-1], &ptrs[i]) != 0) |
| 80 |
ptrs[j++] = ptrs[i]; |
| 81 |
} |
| 82 |
array->count = j; |
| 83 |
} |
| 84 |
|
| 85 |
#endif |