[BACK]Return to sftp-glob.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/sftp-glob.c, Revision 1.17

1.1       djm         1: /*
1.14      djm         2:  * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
1.1       djm         3:  *
1.14      djm         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.14      djm         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.17    ! stevesk    18: RCSID("$OpenBSD: sftp-glob.c,v 1.16 2006/02/08 23:51:24 stevesk Exp $");
        !            19:
        !            20: #include <sys/types.h>
        !            21: #include <sys/stat.h>
1.1       djm        22:
1.16      stevesk    23: #include <dirent.h>
1.1       djm        24: #include <glob.h>
                     25:
                     26: #include "buffer.h"
                     27: #include "bufaux.h"
                     28: #include "xmalloc.h"
                     29: #include "log.h"
                     30:
                     31: #include "sftp.h"
                     32: #include "sftp-common.h"
                     33: #include "sftp-client.h"
1.15      djm        34:
                     35: int remote_glob(struct sftp_conn *, const char *, int,
                     36:     int (*)(const char *, int), glob_t *);
1.1       djm        37:
                     38: struct SFTP_OPENDIR {
                     39:        SFTP_DIRENT **dir;
                     40:        int offset;
                     41: };
                     42:
                     43: static struct {
1.10      djm        44:        struct sftp_conn *conn;
1.1       djm        45: } cur;
                     46:
1.6       itojun     47: static void *
                     48: fudge_opendir(const char *path)
1.1       djm        49: {
                     50:        struct SFTP_OPENDIR *r;
1.9       deraadt    51:
1.1       djm        52:        r = xmalloc(sizeof(*r));
1.9       deraadt    53:
1.12      deraadt    54:        if (do_readdir(cur.conn, (char *)path, &r->dir)) {
                     55:                xfree(r);
1.1       djm        56:                return(NULL);
1.12      deraadt    57:        }
1.1       djm        58:
                     59:        r->offset = 0;
                     60:
1.11      deraadt    61:        return((void *)r);
1.1       djm        62: }
                     63:
1.6       itojun     64: static struct dirent *
                     65: fudge_readdir(struct SFTP_OPENDIR *od)
1.1       djm        66: {
                     67:        static struct dirent ret;
1.9       deraadt    68:
1.1       djm        69:        if (od->dir[od->offset] == NULL)
                     70:                return(NULL);
                     71:
                     72:        memset(&ret, 0, sizeof(ret));
1.4       markus     73:        strlcpy(ret.d_name, od->dir[od->offset++]->filename,
1.1       djm        74:            sizeof(ret.d_name));
                     75:
                     76:        return(&ret);
                     77: }
                     78:
1.6       itojun     79: static void
                     80: fudge_closedir(struct SFTP_OPENDIR *od)
1.1       djm        81: {
                     82:        free_sftp_dirents(od->dir);
1.3       stevesk    83:        xfree(od);
1.1       djm        84: }
                     85:
1.6       itojun     86: static int
                     87: fudge_lstat(const char *path, struct stat *st)
1.1       djm        88: {
                     89:        Attrib *a;
1.9       deraadt    90:
1.11      deraadt    91:        if (!(a = do_lstat(cur.conn, (char *)path, 0)))
1.1       djm        92:                return(-1);
1.9       deraadt    93:
1.1       djm        94:        attrib_to_stat(a, st);
1.9       deraadt    95:
1.1       djm        96:        return(0);
                     97: }
                     98:
1.6       itojun     99: static int
                    100: fudge_stat(const char *path, struct stat *st)
1.1       djm       101: {
                    102:        Attrib *a;
1.9       deraadt   103:
1.11      deraadt   104:        if (!(a = do_stat(cur.conn, (char *)path, 0)))
1.1       djm       105:                return(-1);
1.9       deraadt   106:
1.1       djm       107:        attrib_to_stat(a, st);
1.9       deraadt   108:
1.1       djm       109:        return(0);
                    110: }
                    111:
                    112: int
1.10      djm       113: remote_glob(struct sftp_conn *conn, const char *pattern, int flags,
1.5       markus    114:     int (*errfunc)(const char *, int), glob_t *pglob)
1.1       djm       115: {
1.7       espie     116:        pglob->gl_opendir = fudge_opendir;
                    117:        pglob->gl_readdir = (struct dirent *(*)(void *))fudge_readdir;
                    118:        pglob->gl_closedir = (void (*)(void *))fudge_closedir;
1.1       djm       119:        pglob->gl_lstat = fudge_lstat;
                    120:        pglob->gl_stat = fudge_stat;
1.9       deraadt   121:
1.1       djm       122:        memset(&cur, 0, sizeof(cur));
1.10      djm       123:        cur.conn = conn;
1.1       djm       124:
1.10      djm       125:        return(glob(pattern, flags | GLOB_ALTDIRFUNC, errfunc, pglob));
1.1       djm       126: }