Hello,
In C programming language, this code below is
working well. It is fine.
It allows to convert. I need sthg compatible and Posix. So it is posix
"file with spaces.txt" to "file\ with\ spaces.txt"
It does convert the string (array of chars) to *nix.
However this code is really
not elegant at all.
Would you know
Qu(1) how to avoid:
Code:
j = snprintf( charo, 5 , "%c", aoption[i]);
The above use of snprintf is a sort of trick to avoid the error which occurs with :
Code:
strncat( buffer , aoption[i] , PATH_MAX - strlen(buffer) - 1);
because aoption is a pointer.
I would rather use strncat but no idea how.
snprintf is not sexy, and it is not a reliable work-around.
The use of strncat is more reliable.
How to use strncat with this line above?
Qu(2)
The whole code is not elegant and it may crash depending
on the file names which are passed to aoption.
Code:
char *filename_c_to_unix( char aoption[PATH_MAX] ){
char buffer[PATH_MAX];
char charo[PATH_MAX];
int i, chd, cx , j ;
strncpy( buffer, "" , PATH_MAX);
werase( stdscr);
for ( i=0; ( i <= strlen( aoption ) ) ; i++ )
{
// printw( ">%c ", aoption[i]);
// refresh();
// strncat( buffer , "-" , PATH_MAX - strlen(buffer) - 1);
j = snprintf( charo, 5 , "%c", aoption[i]);
switch ( charo[0] ) {
case '&':
strncat( buffer , "\\" , PATH_MAX - strlen(buffer) - 1);
strncat( buffer , "&" , PATH_MAX - strlen(buffer) - 1);
break;
case '\'':
strncat( buffer , "\\'" , PATH_MAX - strlen(buffer) - 1);
break;
case ' ':
strncat( buffer , "\\ " , PATH_MAX - strlen(buffer) - 1);
break;
default:
strncat( buffer , charo , PATH_MAX - strlen(buffer) - 1);
break;
}
}
size_t len = strlen(buffer);
char *r = malloc(len+1);
return r ? memcpy(r, buffer, len+1) : NULL;
}
The most important question to me is to learn how to use
strncat with converting/using a pointer to my regular array of chars (knowing that I all the time use a regular array of chars). Would you have a better line for question (1)?
This question/issue qu(1) is the most important to me.
Looking forward to reading you !
Many thanks