No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / external / bsd / openssh / dist / sftp-glob.c
blobf2b1ef0fce31600939ccc3ed4e7fbaad982eac07
1 /* $NetBSD$ */
2 /* $OpenBSD: sftp-glob.c,v 1.22 2006/08/03 03:34:42 deraadt Exp $ */
3 /*
4 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include "includes.h"
20 __RCSID("$NetBSD: sftp-glob.c,v 1.13 2006/09/28 21:22:15 christos Exp $");
21 #include <sys/types.h>
22 #include <sys/stat.h>
24 #include <dirent.h>
25 #include <glob.h>
26 #include <string.h>
28 #include "xmalloc.h"
29 #include "sftp.h"
30 #include "buffer.h"
31 #include "sftp-common.h"
32 #include "sftp-client.h"
34 int remote_glob(struct sftp_conn *, const char *, int,
35 int (*)(const char *, int), glob_t *);
37 struct SFTP_OPENDIR {
38 SFTP_DIRENT **dir;
39 int offset;
42 static struct {
43 struct sftp_conn *conn;
44 } cur;
46 static void *
47 fudge_opendir(const char *path)
49 struct SFTP_OPENDIR *r;
51 r = xmalloc(sizeof(*r));
53 if (do_readdir(cur.conn, (char *)path, &r->dir)) {
54 xfree(r);
55 return(NULL);
58 r->offset = 0;
60 return((void *)r);
63 static struct dirent *
64 fudge_readdir(struct SFTP_OPENDIR *od)
66 static struct dirent ret;
68 if (od->dir[od->offset] == NULL)
69 return(NULL);
71 memset(&ret, 0, sizeof(ret));
72 strlcpy(ret.d_name, od->dir[od->offset++]->filename,
73 sizeof(ret.d_name));
75 return(&ret);
78 static void
79 fudge_closedir(struct SFTP_OPENDIR *od)
81 free_sftp_dirents(od->dir);
82 xfree(od);
85 static int
86 fudge_lstat(const char *path, struct stat *st)
88 Attrib *a;
90 if (!(a = do_lstat(cur.conn, (char *)path, 0)))
91 return(-1);
93 attrib_to_stat(a, st);
95 return(0);
98 static int
99 fudge_stat(const char *path, struct stat *st)
101 Attrib *a;
103 if (!(a = do_stat(cur.conn, (char *)path, 0)))
104 return(-1);
106 attrib_to_stat(a, st);
108 return(0);
112 remote_glob(struct sftp_conn *conn, const char *pattern, int flags,
113 int (*errfunc)(const char *, int), glob_t *pglob)
115 pglob->gl_opendir = fudge_opendir;
116 pglob->gl_readdir = (struct dirent *(*)(void *))fudge_readdir;
117 pglob->gl_closedir = (void (*)(void *))fudge_closedir;
118 pglob->gl_lstat = fudge_lstat;
119 pglob->gl_stat = fudge_stat;
121 memset(&cur, 0, sizeof(cur));
122 cur.conn = conn;
124 return(glob(pattern, flags | GLOB_ALTDIRFUNC, errfunc, pglob));