-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
sys_findfile.c
162 lines (130 loc) · 3.75 KB
/
sys_findfile.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
/* DE1: $Id$
* Copyright (C) 2003, 2004 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not: http://www.opensource.org/
*/
/*
* sys_findfile.c: Win32-Style File Finding (findfirst/findnext)
*/
// HEADER FILES ------------------------------------------------------------
#include <stdlib.h>
#include <string.h>
#include <glob.h>
#include <sys/stat.h>
#include "../include/sys_findfile.h"
#include "../include/sys_path.h"
// MACROS ------------------------------------------------------------------
#define FIND_ERROR -1
// TYPES -------------------------------------------------------------------
typedef struct fdata_s {
char *pattern;
glob_t buf;
int pos;
} fdata_t;
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
// PRIVATE DATA DEFINITIONS ------------------------------------------------
// CODE --------------------------------------------------------------------
/*
* Get the info for the next file.
*/
static int nextfinddata(finddata_t * fd)
{
fdata_t *data = fd->finddata;
char *fn, *last;
char ext[256];
struct stat st;
if(data->buf.gl_pathc <= data->pos)
{
// Nothing was found.
return FIND_ERROR;
}
// Nobody needs these...
fd->date = 0;
fd->time = 0;
// Get the size of the file.
fn = data->buf.gl_pathv[data->pos];
if(!stat(fn, &st))
fd->size = st.st_size;
else
fd->size = 0;
if(fd->name)
free(fd->name);
fd->name = malloc(strlen(fn) + 1);
// Is it a directory?
last = fn + strlen(fn) - 1;
if(*last == '/')
{
// Return the name of the last directory in the path.
char *slash = last - 1;
int len;
while(*slash != '/' && slash > fn) --slash;
len = last - slash - 1;
strncpy(fd->name, slash + 1, len);
fd->name[len] = 0;
fd->attrib = A_SUBDIR;
}
else
{
_splitpath(fn, NULL, NULL, fd->name, ext);
strcat(fd->name, ext);
fd->attrib = 0;
}
// Advance the position.
data->pos++;
return 0;
}
/*
* Returns zero if successful.
*/
int myfindfirst(const char *filename, finddata_t * fd)
{
fdata_t *data;
// Allocate a new glob struct.
fd->finddata = data = calloc(1, sizeof(fdata_t));
fd->name = NULL;
// Make a copy of the pattern.
data->pattern = malloc(strlen(filename) + 1);
strcpy(data->pattern, filename);
// Do the glob.
glob(filename, GLOB_MARK, NULL, &data->buf);
return nextfinddata(fd);
/* dta->hFile = _findfirst(filename, &dta->data);
dta->date = dta->data.time_write;
dta->time = dta->data.time_write;
dta->size = dta->data.size;
dta->name = dta->data.name;
dta->attrib = dta->data.attrib;
return dta->hFile<0; */
}
/*
* Returns zero if successful.
*/
int myfindnext(finddata_t * fd)
{
if(!fd->finddata)
return FIND_ERROR;
return nextfinddata(fd);
}
void myfindend(finddata_t * fd)
{
globfree(&((fdata_t *) fd->finddata)->buf);
free(((fdata_t *) fd->finddata)->pattern);
free(fd->name);
free(fd->finddata);
fd->finddata = NULL;
}