procps/proc/escape.c

109 lines
2.9 KiB
C
Raw Normal View History

2002-02-02 06:47:29 +08:00
/*
* Copyright 1998-2002 by Albert Cahalan; all rights resered.
2002-02-02 06:47:29 +08:00
* This file may be used subject to the terms and conditions of the
* GNU Library General Public License Version 2, or any later version
* at your option, as published by the Free Software Foundation.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*/
#include <sys/types.h>
2002-11-27 08:24:01 +08:00
#include "../proc/procps.h"
#include "common.h"
2002-02-02 06:47:29 +08:00
/* sanitize a string, without the nice BSD library function: */
/* strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH) */
2002-11-27 08:24:01 +08:00
int octal_escape_str(char *restrict dst, const char *restrict src, size_t n){
2002-02-02 06:47:29 +08:00
unsigned char c;
char d;
size_t i;
2002-11-27 08:24:01 +08:00
const char codes[] =
2002-02-02 06:47:29 +08:00
"Z------abtnvfr-------------e----"
" *******************************" /* better: do not print any space */
"****************************\\***"
"*******************************-"
"--------------------------------"
"********************************"
"********************************"
"********************************";
for(i=0; i<n;){
c = (unsigned char) *(src++);
d = codes[c];
switch(d){
case 'Z':
goto leave;
case '*':
i++;
*(dst++) = c;
break;
case '-':
if(i+4 > n) goto leave;
i += 4;
*(dst++) = '\\';
*(dst++) = "01234567"[c>>6];
*(dst++) = "01234567"[(c>>3)&07];
*(dst++) = "01234567"[c&07];
break;
default:
if(i+2 > n) goto leave;
i += 2;
*(dst++) = '\\';
*(dst++) = d;
break;
}
}
leave:
*(dst++) = '\0';
return i;
}
/* sanitize a string via one-way mangle */
2002-11-27 08:24:01 +08:00
int simple_escape_str(char *restrict dst, const char *restrict src, size_t n){
2002-02-02 06:47:29 +08:00
unsigned char c;
size_t i;
2002-11-27 08:24:01 +08:00
const char codes[] =
2002-02-02 06:47:29 +08:00
"Z-------------------------------"
"********************************"
"********************************"
"*******************************-"
"--------------------------------"
"********************************"
"********************************"
"********************************";
for(i=0; i<n;){
c = (unsigned char) *(src++);
switch(codes[c]){
case 'Z':
goto leave;
case '*':
i++;
*(dst++) = c;
break;
case '-':
i++;
*(dst++) = '?';
break;
}
}
leave:
*(dst++) = '\0';
return i;
}
/* escape a string as desired */
2002-11-27 08:24:01 +08:00
int escape_str(char *restrict dst, const char *restrict src, size_t n){
2002-02-02 06:47:29 +08:00
return simple_escape_str(dst, src, n);
}
/* escape an argv or environment string array */
2002-11-27 08:24:01 +08:00
int escape_strlist(char *restrict dst, const char *restrict const *restrict src, size_t n){
2002-02-02 06:47:29 +08:00
size_t i = 0;
while(*src){
i += simple_escape_str(dst+i, *src, n-i);
2002-11-27 08:24:01 +08:00
if((n-i > 1) && src[1]) dst[i++] = ' ';
2002-02-02 06:47:29 +08:00
src++;
}
return i;
}