Annotation of src/usr.bin/ssh/sftp-glob.c, Revision 1.23
1.23 ! djm 1: /* $OpenBSD: sftp-glob.c,v 1.22 2006/08/03 03:34:42 deraadt Exp $ */
1.1 djm 2: /*
1.14 djm 3: * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
1.1 djm 4: *
1.14 djm 5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 djm 8: *
1.14 djm 9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 djm 16: */
17:
1.17 stevesk 18: #include <sys/types.h>
19: #include <sys/stat.h>
1.1 djm 20:
1.16 stevesk 21: #include <dirent.h>
1.1 djm 22: #include <glob.h>
1.21 stevesk 23: #include <string.h>
1.1 djm 24:
25: #include "xmalloc.h"
26: #include "sftp.h"
1.22 deraadt 27: #include "buffer.h"
1.1 djm 28: #include "sftp-common.h"
29: #include "sftp-client.h"
1.15 djm 30:
31: int remote_glob(struct sftp_conn *, const char *, int,
32: int (*)(const char *, int), glob_t *);
1.1 djm 33:
34: struct SFTP_OPENDIR {
35: SFTP_DIRENT **dir;
36: int offset;
37: };
38:
39: static struct {
1.10 djm 40: struct sftp_conn *conn;
1.1 djm 41: } cur;
42:
1.6 itojun 43: static void *
44: fudge_opendir(const char *path)
1.1 djm 45: {
46: struct SFTP_OPENDIR *r;
1.9 deraadt 47:
1.1 djm 48: r = xmalloc(sizeof(*r));
1.9 deraadt 49:
1.12 deraadt 50: if (do_readdir(cur.conn, (char *)path, &r->dir)) {
51: xfree(r);
1.1 djm 52: return(NULL);
1.12 deraadt 53: }
1.1 djm 54:
55: r->offset = 0;
56:
1.11 deraadt 57: return((void *)r);
1.1 djm 58: }
59:
1.6 itojun 60: static struct dirent *
61: fudge_readdir(struct SFTP_OPENDIR *od)
1.1 djm 62: {
63: static struct dirent ret;
1.9 deraadt 64:
1.1 djm 65: if (od->dir[od->offset] == NULL)
66: return(NULL);
67:
68: memset(&ret, 0, sizeof(ret));
1.4 markus 69: strlcpy(ret.d_name, od->dir[od->offset++]->filename,
1.1 djm 70: sizeof(ret.d_name));
71:
72: return(&ret);
73: }
74:
1.6 itojun 75: static void
76: fudge_closedir(struct SFTP_OPENDIR *od)
1.1 djm 77: {
78: free_sftp_dirents(od->dir);
1.3 stevesk 79: xfree(od);
1.1 djm 80: }
81:
1.6 itojun 82: static int
83: fudge_lstat(const char *path, struct stat *st)
1.1 djm 84: {
85: Attrib *a;
1.9 deraadt 86:
1.23 ! djm 87: if (!(a = do_lstat(cur.conn, (char *)path, 1)))
1.1 djm 88: return(-1);
1.9 deraadt 89:
1.1 djm 90: attrib_to_stat(a, st);
1.9 deraadt 91:
1.1 djm 92: return(0);
93: }
94:
1.6 itojun 95: static int
96: fudge_stat(const char *path, struct stat *st)
1.1 djm 97: {
98: Attrib *a;
1.9 deraadt 99:
1.23 ! djm 100: if (!(a = do_stat(cur.conn, (char *)path, 1)))
1.1 djm 101: return(-1);
1.9 deraadt 102:
1.1 djm 103: attrib_to_stat(a, st);
1.9 deraadt 104:
1.1 djm 105: return(0);
106: }
107:
108: int
1.10 djm 109: remote_glob(struct sftp_conn *conn, const char *pattern, int flags,
1.5 markus 110: int (*errfunc)(const char *, int), glob_t *pglob)
1.1 djm 111: {
1.7 espie 112: pglob->gl_opendir = fudge_opendir;
113: pglob->gl_readdir = (struct dirent *(*)(void *))fudge_readdir;
114: pglob->gl_closedir = (void (*)(void *))fudge_closedir;
1.1 djm 115: pglob->gl_lstat = fudge_lstat;
116: pglob->gl_stat = fudge_stat;
1.9 deraadt 117:
1.1 djm 118: memset(&cur, 0, sizeof(cur));
1.10 djm 119: cur.conn = conn;
1.1 djm 120:
1.10 djm 121: return(glob(pattern, flags | GLOB_ALTDIRFUNC, errfunc, pglob));
1.1 djm 122: }