SCP: Add environment variable file expansion options for file size, date/time

This commit is contained in:
Mark Pizzolato 2019-10-28 17:40:59 -07:00
parent f56753162b
commit 9fe781cb7e
2 changed files with 31 additions and 1 deletions

4
scp.c
View file

@ -1651,6 +1651,8 @@ static const char simh_help[] =
"++%%~pI%% - expands the value of %%I%% to a path only\n" "++%%~pI%% - expands the value of %%I%% to a path only\n"
"++%%~nI%% - expands the value of %%I%% to a file name only\n" "++%%~nI%% - expands the value of %%I%% to a file name only\n"
"++%%~xI%% - expands the value of %%I%% to a file extension only\n\n" "++%%~xI%% - expands the value of %%I%% to a file extension only\n\n"
"++%%~tI%% - expands the value of %%I%% to date/time of file\n\n"
"++%%~zI%% - expands the value of %%I%% to size of file\n\n"
" The modifiers can be combined to get compound results:\n\n" " The modifiers can be combined to get compound results:\n\n"
"++%%~pnI%% - expands the value of %%I%% to a path and name only\n" "++%%~pnI%% - expands the value of %%I%% to a path and name only\n"
"++%%~nxI%% - expands the value of %%I%% to a file name and extension only\n\n" "++%%~nxI%% - expands the value of %%I%% to a file name and extension only\n\n"
@ -4210,7 +4212,7 @@ for (; *ip && (op < oend); ) {
if (*ip == '~') { if (*ip == '~') {
expand_it = TRUE; expand_it = TRUE;
++ip; ++ip;
for (i=0; (i < (sizeof (parts) - 1)) && (strchr ("fpnx", *ip)); i++, ip++) { for (i=0; (i < (sizeof (parts) - 1)) && (strchr ("fpnxtz", *ip)); i++, ip++) {
parts[i] = *ip; parts[i] = *ip;
parts[i + 1] = '\0'; parts[i + 1] = '\0';
} }

View file

@ -742,6 +742,8 @@ char *fullpath = NULL, *result = NULL;
char *c, *name, *ext; char *c, *name, *ext;
char chr; char chr;
const char *p; const char *p;
char filesizebuf[32] = "";
char filedatetimebuf[32] = "";
if (((*filepath == '\'') || (*filepath == '"')) && if (((*filepath == '\'') || (*filepath == '"')) &&
(filepath[strlen (filepath) - 1] == *filepath)) { (filepath[strlen (filepath) - 1] == *filepath)) {
@ -812,6 +814,20 @@ if (ext == NULL)
tot_size = 0; tot_size = 0;
if (*parts == '\0') /* empty part specifier means strip only quotes */ if (*parts == '\0') /* empty part specifier means strip only quotes */
tot_size = strlen (tempfilepath); tot_size = strlen (tempfilepath);
if (strchr (parts, 't') || strchr (parts, 'z')) {
struct stat filestat;
struct tm *tm;
memset (&filestat, 0, sizeof (filestat));
(void)stat (fullpath, &filestat);
if (sizeof (filestat.st_size) == 4)
sprintf (filesizebuf, "%ld ", filestat.st_size);
else
sprintf (filesizebuf, "%" LL_FMT "d ", (LL_TYPE)filestat.st_size);
tm = localtime (&filestat.st_mtime);
sprintf (filedatetimebuf, "%02d/%02d/%04d %02d:%02d %cM ", 1 + tm->tm_mon, tm->tm_mday, 1900 + tm->tm_year,
tm->tm_hour % 12, tm->tm_min, (0 == (tm->tm_hour % 12)) ? 'A' : 'P');
}
for (p = parts; *p; p++) { for (p = parts; *p; p++) {
switch (*p) { switch (*p) {
case 'f': case 'f':
@ -826,6 +842,12 @@ for (p = parts; *p; p++) {
case 'x': case 'x':
tot_size += strlen (ext); tot_size += strlen (ext);
break; break;
case 't':
tot_size += strlen (filedatetimebuf);
break;
case 'z':
tot_size += strlen (filesizebuf);
break;
} }
} }
result = (char *)malloc (1 + tot_size); result = (char *)malloc (1 + tot_size);
@ -852,6 +874,12 @@ for (p = parts; *p; p++) {
case 'x': case 'x':
strlcat (result, ext, 1 + tot_size); strlcat (result, ext, 1 + tot_size);
break; break;
case 't':
strlcat (result, filedatetimebuf, 1 + tot_size);
break;
case 'z':
strlcat (result, filesizebuf, 1 + tot_size);
break;
} }
} }
free (fullpath); free (fullpath);