Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions nifti2/nifti_tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -852,40 +852,43 @@ int verify_opts( nt_opts * opts, const char * prog )
int fill_cmd_string( nt_opts * opts, int argc, const char * argv[])
{
char * cp;
size_t len, c, remain = sizeof(opts->command); /* max command len */
int ac;
size_t remain = sizeof(opts->command); /* max command len */
int has_space; /* arguments containing space must be quoted */
int skip = 0; /* counter to skip some of the arguments */

/* get the first argument separately */
len = snprintf( opts->command, sizeof(opts->command),
"\n command: %s", argv[0] );
if( len >= sizeof(opts->command) ) {
int len = snprintf( opts->command, sizeof(opts->command),
"\n command: %s", argv[0] );
if( len < 0 || (size_t)len >= sizeof(opts->command) ) {
fprintf(stderr,"FCS: no space remaining for command, continuing...\n");
return 1;
}
cp = opts->command + len;
remain -= len;

/* get the rest, with special attention to input files */
for( ac = 1; ac < argc; ac++ )
for( int ac = 1; ac < argc; ac++ )
{
if( skip ){ skip--; continue; } /* then skip these arguments */

len = strlen(argv[ac]);
if( len + 3 >= remain ) { /* extra 3 for space and possible '' */
size_t tmp_len = strlen(argv[ac]);
if( tmp_len + 3 >= remain ) { /* extra 3 for space and possible '' */
fprintf(stderr,"FCS: no space remaining for command, continuing...\n");
return 1;
}

/* put the argument in, possibly with '' */

has_space = 0;
for( c = 0; c < len-1; c++ )
for( size_t c = 0; c < tmp_len-1; c++ )
if( isspace(argv[ac][c]) ){ has_space = 1; break; }
if( has_space ) len = snprintf(cp, remain, " '%s'", argv[ac]);
else len = snprintf(cp, remain, " %s", argv[ac]);

if( len < 0 || len >= remain ) {
fprintf(stderr,"FCS: error parsing command, continuing...\n");
return 1;
}
remain -= len;

/* infiles is okay, but after the *next* argument, we may skip files */
Expand Down
21 changes: 12 additions & 9 deletions niftilib/nifti1_tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -690,40 +690,43 @@ int verify_opts( nt_opts * opts, const char * prog )
int fill_cmd_string( nt_opts * opts, int argc, const char * argv[])
{
char * cp;
size_t len, c, remain = sizeof(opts->command); /* max command len */
int ac;
size_t remain = sizeof(opts->command); /* max command len */
int has_space; /* arguments containing space must be quoted */
int skip = 0; /* counter to skip some of the arguments */

/* get the first argument separately */
len = snprintf( opts->command, sizeof(opts->command),
"\n command: %s", argv[0] );
if( len >= sizeof(opts->command) ) {
int len = snprintf( opts->command, sizeof(opts->command),
"\n command: %s", argv[0] );
if( len < 0 || (size_t)len >= sizeof(opts->command) ) {
fprintf(stderr,"FCS: no space remaining for command, continuing...\n");
return 1;
}
cp = opts->command + len;
remain -= len;

/* get the rest, with special attention to input files */
for( ac = 1; ac < argc; ac++ )
for( int ac = 1; ac < argc; ac++ )
{
if( skip ){ skip--; continue; } /* then skip these arguments */

len = strlen(argv[ac]);
if( len + 3 >= remain ) { /* extra 3 for space and possible '' */
size_t tmp_len = strlen(argv[ac]);
if( tmp_len + 3 >= remain ) { /* extra 3 for space and possible '' */
fprintf(stderr,"FCS: no space remaining for command, continuing...\n");
return 1;
}

/* put the argument in, possibly with '' */

has_space = 0;
for( c = 0; c < len-1; c++ )
for( size_t c = 0; c < tmp_len-1; c++ )
if( isspace(argv[ac][c]) ){ has_space = 1; break; }
if( has_space ) len = snprintf(cp, remain, " '%s'", argv[ac]);
else len = snprintf(cp, remain, " %s", argv[ac]);

if( len < 0 || len >= remain ) {
fprintf(stderr,"FCS: error parsing command, continuing...\n");
return 1;
}
remain -= len;

/* infiles is okay, but after the *next* argument, we may skip files */
Expand Down