"Fossies" - the Fresh Open Source Software Archive

Member "xarchive-0.2.8-6/src/my_strescape.c" (20 Oct 2005, 1759 Bytes) of package /linux/privat/old/xarchive-0.2.8-6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "my_strescape.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  *  my_strescape.c - escape hazardous characters in a string for bash 
    3  *  Copyright (C) 2005 Lee Bigelow <ligelowbee@yahoo.com> 
    4  *
    5  *  This program is free software; you can redistribute it and/or modify
    6  *  it under the terms of the GNU General Public License as published by
    7  *  the Free Software Foundation; either version 2 of the License, or
    8  *  (at your option) any later version.
    9  *
   10  *  This program is distributed in the hope that it will be useful,
   11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  *  GNU General Public License for more details.
   14  *
   15  *  You should have received a copy of the GNU General Public License
   16  *  along with this program; if not, write to the Free Software
   17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   18  */
   19 #include<string.h>
   20 #include<glib.h>
   21 
   22 gint 
   23 is_escaped_char(gchar c)
   24 {
   25   switch ( c )
   26   {
   27     case ' ':   
   28     case '\'':  
   29     case '"':   
   30     case '(':   
   31     case ')':   
   32     case '$':   
   33     case '\\':  
   34     case ';':   
   35     case '<':   
   36     case '>':   
   37     case '&':   
   38     case '#':   
   39     case '*':   
   40     case '|':   
   41     case '`':   
   42     case '!':   
   43       return 1;
   44     default:    
   45       return 0;
   46   }
   47 }
   48 
   49 extern gchar *
   50 my_strescape(gchar *string)
   51 {
   52   gchar *q;
   53   gchar *escaped;
   54   gint escapechars = 0;
   55   gchar *p = string;
   56 
   57   while (*p != '\000') 
   58   {
   59     if (is_escaped_char(*p)) escapechars++;
   60     p++;
   61   }
   62 
   63   if (!escapechars) return g_strdup(string);
   64 
   65   escaped = (gchar *) g_malloc (strlen(string) + escapechars + 1);
   66 
   67   p = string;
   68   q = escaped;
   69 
   70   while (*p != '\000')
   71   {
   72     if (is_escaped_char(*p)) *q++ = '\\';
   73     *q++ = *p++;
   74   }
   75   *q = '\000';
   76 
   77   return escaped;
   78 }
   79