[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.21

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