Initial revision
[ssr] / StraySrc / Utilities / c / gf
1 /*
2 * gf.c
3 *
4 * Read directories in a buffered way
5 *
6 * © 1998 Straylight/Edgeware
7 */
8
9 /*----- Licensing note ----------------------------------------------------*
10 *
11 * This file is part of Straylight's core utilities (coreutils).
12 *
13 * Coreutils is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
16 * any later version.
17 *
18 * Coreutils is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with coreutils. If not, write to the Free Software Foundation,
25 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "swis.h"
35 #include "swiv.h"
36
37 #include "gf.h"
38
39 /*----- Main code ---------------------------------------------------------*/
40
41 /* --- @gf_init@ --- *
42 *
43 * Arguments: @gf_ctx *g@ = pointer to a context buffer for me
44 * @const char *pat@ = pointer to a (RISC OS) pattern string
45 * @cost char *dir@ = pointer to name of parent directory
46 *
47 * Returns: ---
48 *
49 * Use: Initialise a wildcard match context ready for scanning.
50 */
51
52 void gf_init(gf_ctx *g, const char *pat, const char *dir)
53 {
54 g->ctx = 0;
55 g->pat = pat;
56 g->dir = dir;
57 g->nleft = 0;
58 }
59
60 /* --- @gf_next@ --- *
61 *
62 * Arguments: @gf_ctx *g@ = pointer to my context
63 *
64 * Returns: Pointer to a filename, or zero for end
65 *
66 * Use: Returns the next matching file from the block.
67 */
68
69 char *gf_next(gf_ctx *g)
70 {
71 /* --- See if the buffer is empty --- */
72
73 while (!g->nleft) {
74
75 /* --- Check for end of the list --- */
76
77 if (g->ctx == -1)
78 return (0);
79
80 /* --- Fetch some more items from the list --- */
81
82 g->p = g->buf;
83 if (_swix(OS_GBPB, _inr(0, 6) | _out(3) | _out(4),
84 9, g->dir, g->buf, 1024, g->ctx, sizeof(g->buf), g->pat,
85 &g->nleft, &g->ctx))
86 return (0);
87 }
88
89 /* --- Dole out the next item from the list --- */
90
91 {
92 char *p = g->p;
93
94 while (*g->p)
95 g->p++;
96 g->p++;
97 g->nleft--;
98 return (p);
99 }
100 }
101
102 /*----- That's all, folks -------------------------------------------------*/