2 * Copyright (c) 2001 Damien Miller. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 RCSID("$OpenBSD: sftp-glob.c,v 1.8 2001/07/14 15:10:17 stevesk Exp $");
34 #include "sftp-common.h"
35 #include "sftp-client.h"
36 #include "sftp-glob.h"
49 fudge_opendir(const char *path)
51 struct SFTP_OPENDIR *r;
53 r = xmalloc(sizeof(*r));
55 if (do_readdir(cur.fd_in, cur.fd_out, (char*)path, &r->dir))
63 static struct dirent *
64 fudge_readdir(struct SFTP_OPENDIR *od)
66 /* Solaris needs sizeof(dirent) + path length (see below) */
67 static char buf[sizeof(struct dirent) + MAXPATHLEN];
68 struct dirent *ret = (struct dirent *)buf;
69 #ifdef __GNU_LIBRARY__
71 #endif /* __GNU_LIBRARY__ */
73 if (od->dir[od->offset] == NULL)
76 memset(buf, 0, sizeof(buf));
79 * Solaris defines dirent->d_name as a one byte array and expects
80 * you to hack around it.
82 #ifdef BROKEN_ONE_BYTE_DIRENT_D_NAME
83 strlcpy(ret->d_name, od->dir[od->offset++]->filename, MAXPATHLEN);
85 strlcpy(ret->d_name, od->dir[od->offset++]->filename,
88 #ifdef __GNU_LIBRARY__
90 * Idiot glibc uses extensions to struct dirent for readdir with
91 * ALTDIRFUNCs. Not that this is documented anywhere but the
92 * source... Fake an inode number to appease it.
97 #endif /* __GNU_LIBRARY__ */
103 fudge_closedir(struct SFTP_OPENDIR *od)
105 free_sftp_dirents(od->dir);
110 attrib_to_stat(Attrib *a, struct stat *st)
112 memset(st, 0, sizeof(*st));
114 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
115 st->st_size = a->size;
116 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
120 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
121 st->st_mode = a->perm;
122 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
123 st->st_atime = a->atime;
124 st->st_mtime = a->mtime;
129 fudge_lstat(const char *path, struct stat *st)
133 if (!(a = do_lstat(cur.fd_in, cur.fd_out, (char*)path, 0)))
136 attrib_to_stat(a, st);
142 fudge_stat(const char *path, struct stat *st)
146 if (!(a = do_stat(cur.fd_in, cur.fd_out, (char*)path, 0)))
149 attrib_to_stat(a, st);
155 remote_glob(int fd_in, int fd_out, const char *pattern, int flags,
156 int (*errfunc)(const char *, int), glob_t *pglob)
158 pglob->gl_opendir = fudge_opendir;
159 pglob->gl_readdir = (struct dirent *(*)(void *))fudge_readdir;
160 pglob->gl_closedir = (void (*)(void *))fudge_closedir;
161 pglob->gl_lstat = fudge_lstat;
162 pglob->gl_stat = fudge_stat;
164 memset(&cur, 0, sizeof(cur));
168 return(glob(pattern, flags | GLOB_ALTDIRFUNC, errfunc,