Annotation of src/usr.bin/ssh/sftp-glob.c, Revision 1.29
1.29 ! deraadt 1: /* $OpenBSD: sftp-glob.c,v 1.28 2019/10/02 00:42:30 djm 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.26 dtucker 23: #include <stdlib.h>
1.21 stevesk 24: #include <string.h>
1.29 ! deraadt 25: #include <stdarg.h>
1.1 djm 26:
27: #include "xmalloc.h"
28: #include "sftp.h"
29: #include "sftp-common.h"
30: #include "sftp-client.h"
1.15 djm 31:
32: int remote_glob(struct sftp_conn *, const char *, int,
33: int (*)(const char *, int), glob_t *);
1.1 djm 34:
35: struct SFTP_OPENDIR {
36: SFTP_DIRENT **dir;
37: int offset;
38: };
39:
40: static struct {
1.10 djm 41: struct sftp_conn *conn;
1.1 djm 42: } cur;
43:
1.6 itojun 44: static void *
45: fudge_opendir(const char *path)
1.1 djm 46: {
47: struct SFTP_OPENDIR *r;
1.9 deraadt 48:
1.25 djm 49: r = xcalloc(1, sizeof(*r));
1.9 deraadt 50:
1.12 deraadt 51: if (do_readdir(cur.conn, (char *)path, &r->dir)) {
1.24 djm 52: free(r);
1.1 djm 53: return(NULL);
1.12 deraadt 54: }
1.1 djm 55:
56: r->offset = 0;
57:
1.11 deraadt 58: return((void *)r);
1.1 djm 59: }
60:
1.6 itojun 61: static struct dirent *
62: fudge_readdir(struct SFTP_OPENDIR *od)
1.1 djm 63: {
64: static struct dirent ret;
1.9 deraadt 65:
1.1 djm 66: if (od->dir[od->offset] == NULL)
67: return(NULL);
68:
69: memset(&ret, 0, sizeof(ret));
1.4 markus 70: strlcpy(ret.d_name, od->dir[od->offset++]->filename,
1.1 djm 71: sizeof(ret.d_name));
72:
73: return(&ret);
74: }
75:
1.6 itojun 76: static void
77: fudge_closedir(struct SFTP_OPENDIR *od)
1.1 djm 78: {
79: free_sftp_dirents(od->dir);
1.24 djm 80: free(od);
1.1 djm 81: }
82:
1.6 itojun 83: static int
84: fudge_lstat(const char *path, struct stat *st)
1.1 djm 85: {
86: Attrib *a;
1.9 deraadt 87:
1.23 djm 88: if (!(a = do_lstat(cur.conn, (char *)path, 1)))
1.1 djm 89: return(-1);
1.9 deraadt 90:
1.1 djm 91: attrib_to_stat(a, st);
1.9 deraadt 92:
1.1 djm 93: return(0);
94: }
95:
1.6 itojun 96: static int
97: fudge_stat(const char *path, struct stat *st)
1.1 djm 98: {
99: Attrib *a;
1.9 deraadt 100:
1.23 djm 101: if (!(a = do_stat(cur.conn, (char *)path, 1)))
1.1 djm 102: return(-1);
1.9 deraadt 103:
1.1 djm 104: attrib_to_stat(a, st);
1.9 deraadt 105:
1.1 djm 106: return(0);
107: }
108:
109: int
1.10 djm 110: remote_glob(struct sftp_conn *conn, const char *pattern, int flags,
1.5 markus 111: int (*errfunc)(const char *, int), glob_t *pglob)
1.1 djm 112: {
1.7 espie 113: pglob->gl_opendir = fudge_opendir;
114: pglob->gl_readdir = (struct dirent *(*)(void *))fudge_readdir;
115: pglob->gl_closedir = (void (*)(void *))fudge_closedir;
1.1 djm 116: pglob->gl_lstat = fudge_lstat;
117: pglob->gl_stat = fudge_stat;
1.9 deraadt 118:
1.1 djm 119: memset(&cur, 0, sizeof(cur));
1.10 djm 120: cur.conn = conn;
1.1 djm 121:
1.10 djm 122: return(glob(pattern, flags | GLOB_ALTDIRFUNC, errfunc, pglob));
1.1 djm 123: }