More shell quoting: this time for C

Saturday, 21 May 2005

A few days ago, I wrote about reliable shell quoting in shell. Now I happened to write some code for the same thing in C.

char *
ashellquote( char * unquot )
{
  int i, j;
  char * quot;

  for( i = 0, j = 0 ; unquot[ i ] ; ++i )
    if( unquot[ i ] == '\'' ) ++j;

  /* original length
     + 3 extra chars per escaped quote
     + 2 chars for surrounding quotes
     + NUL byte */
  quot = malloc( i + j * 3 + 3 );

  if( ! quot ) return NULL;

  quot[ 0 ] = '\'';

  for( i = 1, j = 0; ( quot[ i ] = unquot[ j ] ); ++i, ++j )
    if( '\'' == quot[ i ] ) {
      quot[ ++i ] = '\\';
      quot[ ++i ] = '\'';
      quot[ ++i ] = '\'';
    }

  quot[ i++ ] = '\'';
  quot[ i++ ] = '\0';

  return quot;
}

The name has a leading “a” for “allocating” as in asprintf().

It assumes 8-bit characters, obviously; my experience with C traditions is too limited so far for me to know how I should go about writing a Unicode-aware version. For wchar strings it’s obvious, but what about UTF-8? In any case, the code obviously wouldn’t need to change significantly to accomodate any of these options.

Preemptive legalese: the piece of code in this entry is in the public domain.