Annotation of src/usr.bin/ssh/sftp-glob.c, Revision 1.13.6.1
1.1 djm 1: /*
1.13.6.1! brad 2: * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
1.1 djm 3: *
1.13.6.1! brad 4: * Permission to use, copy, modify, and distribute this software for any
! 5: * purpose with or without fee is hereby granted, provided that the above
! 6: * copyright notice and this permission notice appear in all copies.
1.1 djm 7: *
1.13.6.1! brad 8: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
! 9: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
! 10: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
! 11: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
! 12: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
! 13: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
! 14: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 djm 15: */
16:
17: #include "includes.h"
1.13.6.1! brad 18: RCSID("$OpenBSD: sftp-glob.c,v 1.15 2004/02/17 07:17:29 djm Exp $");
1.1 djm 19:
20: #include <glob.h>
21:
22: #include "buffer.h"
23: #include "bufaux.h"
24: #include "xmalloc.h"
25: #include "log.h"
26:
27: #include "sftp.h"
28: #include "sftp-common.h"
29: #include "sftp-client.h"
1.13.6.1! brad 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.11 deraadt 87: if (!(a = do_lstat(cur.conn, (char *)path, 0)))
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.11 deraadt 100: if (!(a = do_stat(cur.conn, (char *)path, 0)))
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: }