version 1.57, 2003/03/05 22:33:43 |
version 1.57.2.1, 2003/09/16 20:50:43 |
|
|
/* Seperators for interactive commands */ |
/* Seperators for interactive commands */ |
#define WHITESPACE " \t\r\n" |
#define WHITESPACE " \t\r\n" |
|
|
|
/* Define what type of ls view (0 - multi-column) */ |
|
#define LONG_VIEW 1 /* Full view ala ls -l */ |
|
#define SHORT_VIEW 2 /* Single row view ala ls -1 */ |
|
|
/* Commands for interactive mode */ |
/* Commands for interactive mode */ |
#define I_CHDIR 1 |
#define I_CHDIR 1 |
#define I_CHGRP 2 |
#define I_CHGRP 2 |
|
|
for(; strchr(WHITESPACE, *cp) == NULL; cp++) { |
for(; strchr(WHITESPACE, *cp) == NULL; cp++) { |
switch (*cp) { |
switch (*cp) { |
case 'l': |
case 'l': |
*lflag = 1; |
*lflag = LONG_VIEW; |
break; |
break; |
|
case '1': |
|
*lflag = SHORT_VIEW; |
|
break; |
default: |
default: |
error("Invalid flag -%c", *cp); |
error("Invalid flag -%c", *cp); |
return(-1); |
return(-1); |
|
|
{ |
{ |
const char *cp = *cpp, *end; |
const char *cp = *cpp, *end; |
char quot; |
char quot; |
int i; |
int i, j; |
|
|
cp += strspn(cp, WHITESPACE); |
cp += strspn(cp, WHITESPACE); |
if (!*cp) { |
if (!*cp) { |
|
|
return (0); |
return (0); |
} |
} |
|
|
|
*path = xmalloc(strlen(cp) + 1); |
|
|
/* Check for quoted filenames */ |
/* Check for quoted filenames */ |
if (*cp == '\"' || *cp == '\'') { |
if (*cp == '\"' || *cp == '\'') { |
quot = *cp++; |
quot = *cp++; |
|
|
end = strchr(cp, quot); |
/* Search for terminating quote, unescape some chars */ |
if (end == NULL) { |
for (i = j = 0; i <= strlen(cp); i++) { |
error("Unterminated quote"); |
if (cp[i] == quot) { /* Found quote */ |
goto fail; |
(*path)[j] = '\0'; |
|
break; |
|
} |
|
if (cp[i] == '\0') { /* End of string */ |
|
error("Unterminated quote"); |
|
goto fail; |
|
} |
|
if (cp[i] == '\\') { /* Escaped characters */ |
|
i++; |
|
if (cp[i] != '\'' && cp[i] != '\"' && |
|
cp[i] != '\\') { |
|
error("Bad escaped character '\%c'", |
|
cp[i]); |
|
goto fail; |
|
} |
|
} |
|
(*path)[j++] = cp[i]; |
} |
} |
if (cp == end) { |
|
|
if (j == 0) { |
error("Empty quotes"); |
error("Empty quotes"); |
goto fail; |
goto fail; |
} |
} |
*cpp = end + 1 + strspn(end + 1, WHITESPACE); |
*cpp = cp + i + strspn(cp + i, WHITESPACE); |
} else { |
} else { |
/* Read to end of filename */ |
/* Read to end of filename */ |
end = strpbrk(cp, WHITESPACE); |
end = strpbrk(cp, WHITESPACE); |
if (end == NULL) |
if (end == NULL) |
end = strchr(cp, '\0'); |
end = strchr(cp, '\0'); |
*cpp = end + strspn(end, WHITESPACE); |
*cpp = end + strspn(end, WHITESPACE); |
|
|
|
memcpy(*path, cp, end - cp); |
|
(*path)[end - cp] = '\0'; |
} |
} |
|
return (0); |
|
|
i = end - cp; |
|
|
|
*path = xmalloc(i + 1); |
|
memcpy(*path, cp, i); |
|
(*path)[i] = '\0'; |
|
return(0); |
|
|
|
fail: |
fail: |
*path = NULL; |
xfree(*path); |
|
*path = NULL; |
return (-1); |
return (-1); |
} |
} |
|
|
|
|
goto out; |
goto out; |
} |
} |
|
|
/* Only one match, dst may be file, directory or unspecified */ |
/* If multiple matches, dst must be a directory or unspecified */ |
if (g.gl_pathv[0] && g.gl_matchc == 1) { |
if (g.gl_matchc > 1 && dst && !is_dir(dst)) { |
if (dst) { |
|
/* If directory specified, append filename */ |
|
if (is_dir(dst)) { |
|
if (infer_path(g.gl_pathv[0], &tmp)) { |
|
err = 1; |
|
goto out; |
|
} |
|
abs_dst = path_append(dst, tmp); |
|
xfree(tmp); |
|
} else |
|
abs_dst = xstrdup(dst); |
|
} else if (infer_path(g.gl_pathv[0], &abs_dst)) { |
|
err = -1; |
|
goto out; |
|
} |
|
err = do_download(conn, g.gl_pathv[0], abs_dst, pflag); |
|
goto out; |
|
} |
|
|
|
/* Multiple matches, dst may be directory or unspecified */ |
|
if (dst && !is_dir(dst)) { |
|
error("Multiple files match, but \"%s\" is not a directory", |
error("Multiple files match, but \"%s\" is not a directory", |
dst); |
dst); |
err = -1; |
err = -1; |
|
|
err = -1; |
err = -1; |
goto out; |
goto out; |
} |
} |
if (dst) { |
|
|
if (g.gl_matchc == 1 && dst) { |
|
/* If directory specified, append filename */ |
|
if (is_dir(dst)) { |
|
if (infer_path(g.gl_pathv[0], &tmp)) { |
|
err = 1; |
|
goto out; |
|
} |
|
abs_dst = path_append(dst, tmp); |
|
xfree(tmp); |
|
} else |
|
abs_dst = xstrdup(dst); |
|
} else if (dst) { |
abs_dst = path_append(dst, tmp); |
abs_dst = path_append(dst, tmp); |
xfree(tmp); |
xfree(tmp); |
} else |
} else |
|
|
goto out; |
goto out; |
} |
} |
|
|
/* Only one match, dst may be file, directory or unspecified */ |
/* If multiple matches, dst may be directory or unspecified */ |
if (g.gl_pathv[0] && g.gl_matchc == 1) { |
if (g.gl_matchc > 1 && tmp_dst && !remote_is_dir(conn, tmp_dst)) { |
if (!is_reg(g.gl_pathv[0])) { |
|
error("Can't upload %s: not a regular file", |
|
g.gl_pathv[0]); |
|
err = 1; |
|
goto out; |
|
} |
|
if (tmp_dst) { |
|
/* If directory specified, append filename */ |
|
if (remote_is_dir(conn, tmp_dst)) { |
|
if (infer_path(g.gl_pathv[0], &tmp)) { |
|
err = 1; |
|
goto out; |
|
} |
|
abs_dst = path_append(tmp_dst, tmp); |
|
xfree(tmp); |
|
} else |
|
abs_dst = xstrdup(tmp_dst); |
|
} else { |
|
if (infer_path(g.gl_pathv[0], &abs_dst)) { |
|
err = -1; |
|
goto out; |
|
} |
|
abs_dst = make_absolute(abs_dst, pwd); |
|
} |
|
err = do_upload(conn, g.gl_pathv[0], abs_dst, pflag); |
|
goto out; |
|
} |
|
|
|
/* Multiple matches, dst may be directory or unspecified */ |
|
if (tmp_dst && !remote_is_dir(conn, tmp_dst)) { |
|
error("Multiple files match, but \"%s\" is not a directory", |
error("Multiple files match, but \"%s\" is not a directory", |
tmp_dst); |
tmp_dst); |
err = -1; |
err = -1; |
|
|
err = -1; |
err = -1; |
goto out; |
goto out; |
} |
} |
if (tmp_dst) { |
|
|
if (g.gl_matchc == 1 && tmp_dst) { |
|
/* If directory specified, append filename */ |
|
if (remote_is_dir(conn, tmp_dst)) { |
|
if (infer_path(g.gl_pathv[0], &tmp)) { |
|
err = 1; |
|
goto out; |
|
} |
|
abs_dst = path_append(tmp_dst, tmp); |
|
xfree(tmp); |
|
} else |
|
abs_dst = xstrdup(tmp_dst); |
|
|
|
} else if (tmp_dst) { |
abs_dst = path_append(tmp_dst, tmp); |
abs_dst = path_append(tmp_dst, tmp); |
xfree(tmp); |
xfree(tmp); |
} else |
} else |
|
|
xfree(abs_dst); |
xfree(abs_dst); |
if (tmp_dst) |
if (tmp_dst) |
xfree(tmp_dst); |
xfree(tmp_dst); |
|
globfree(&g); |
return(err); |
return(err); |
} |
} |
|
|
|
|
static int |
static int |
do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag) |
do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag) |
{ |
{ |
int n; |
int n, c = 1, colspace = 0, columns = 1; |
SFTP_DIRENT **d; |
SFTP_DIRENT **d; |
|
|
if ((n = do_readdir(conn, path, &d)) != 0) |
if ((n = do_readdir(conn, path, &d)) != 0) |
return (n); |
return (n); |
|
|
/* Count entries for sort */ |
if (!(lflag & SHORT_VIEW)) { |
for (n = 0; d[n] != NULL; n++) |
int m = 0, width = 80; |
; |
struct winsize ws; |
|
|
|
/* Count entries for sort and find longest filename */ |
|
for (n = 0; d[n] != NULL; n++) |
|
m = MAX(m, strlen(d[n]->filename)); |
|
|
|
if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1) |
|
width = ws.ws_col; |
|
|
|
columns = width / (m + 2); |
|
columns = MAX(columns, 1); |
|
colspace = width / columns; |
|
} |
|
|
qsort(d, n, sizeof(*d), sdirent_comp); |
qsort(d, n, sizeof(*d), sdirent_comp); |
|
|
for (n = 0; d[n] != NULL; n++) { |
for (n = 0; d[n] != NULL; n++) { |
|
|
fname = path_strip(tmp, strip_path); |
fname = path_strip(tmp, strip_path); |
xfree(tmp); |
xfree(tmp); |
|
|
if (lflag) { |
if (lflag & LONG_VIEW) { |
char *lname; |
char *lname; |
struct stat sb; |
struct stat sb; |
|
|
|
|
printf("%s\n", lname); |
printf("%s\n", lname); |
xfree(lname); |
xfree(lname); |
} else { |
} else { |
/* XXX - multicolumn display would be nice here */ |
printf("%-*s", colspace, fname); |
printf("%s\n", fname); |
if (c >= columns) { |
|
printf("\n"); |
|
c = 1; |
|
} else |
|
c++; |
} |
} |
|
|
xfree(fname); |
xfree(fname); |
} |
} |
|
|
|
if (!(lflag & LONG_VIEW) && (c != 1)) |
|
printf("\n"); |
|
|
free_sftp_dirents(d); |
free_sftp_dirents(d); |
return (0); |
return (0); |
} |
} |
|
|
int lflag) |
int lflag) |
{ |
{ |
glob_t g; |
glob_t g; |
int i; |
int i, c = 1, colspace = 0, columns = 1; |
Attrib *a; |
Attrib *a; |
struct stat sb; |
|
|
|
memset(&g, 0, sizeof(g)); |
memset(&g, 0, sizeof(g)); |
|
|
|
|
} |
} |
} |
} |
|
|
|
if (!(lflag & SHORT_VIEW)) { |
|
int m = 0, width = 80; |
|
struct winsize ws; |
|
|
|
/* Count entries for sort and find longest filename */ |
|
for (i = 0; g.gl_pathv[i]; i++) |
|
m = MAX(m, strlen(g.gl_pathv[i])); |
|
|
|
if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1) |
|
width = ws.ws_col; |
|
|
|
columns = width / (m + 2); |
|
columns = MAX(columns, 1); |
|
colspace = width / columns; |
|
} |
|
|
for (i = 0; g.gl_pathv[i]; i++) { |
for (i = 0; g.gl_pathv[i]; i++) { |
char *fname, *lname; |
char *fname; |
|
|
fname = path_strip(g.gl_pathv[i], strip_path); |
fname = path_strip(g.gl_pathv[i], strip_path); |
|
|
if (lflag) { |
if (lflag & LONG_VIEW) { |
|
char *lname; |
|
struct stat sb; |
|
|
/* |
/* |
* XXX: this is slow - 1 roundtrip per path |
* XXX: this is slow - 1 roundtrip per path |
* A solution to this is to fork glob() and |
* A solution to this is to fork glob() and |
|
|
printf("%s\n", lname); |
printf("%s\n", lname); |
xfree(lname); |
xfree(lname); |
} else { |
} else { |
/* XXX - multicolumn display would be nice here */ |
printf("%-*s", colspace, fname); |
printf("%s\n", fname); |
if (c >= columns) { |
|
printf("\n"); |
|
c = 1; |
|
} else |
|
c++; |
} |
} |
xfree(fname); |
xfree(fname); |
} |
} |
|
|
|
if (!(lflag & LONG_VIEW) && (c != 1)) |
|
printf("\n"); |
|
|
if (g.gl_pathc) |
if (g.gl_pathc) |
globfree(&g); |
globfree(&g); |