Blame libmisc/quote.c

rpm-build 0a0c83
/*
rpm-build 0a0c83
  File: quote.c
rpm-build 0a0c83
rpm-build 0a0c83
  Copyright (C) 2003 Andreas Gruenbacher <a.gruenbacher@bestbits.at>
rpm-build 0a0c83
rpm-build 0a0c83
  This program is free software; you can redistribute it and/or modify it under
rpm-build 0a0c83
  the terms of the GNU Lesser General Public License as published by the
rpm-build 0a0c83
  Free Software Foundation; either version 2.1 of the License, or (at
rpm-build 0a0c83
  your option) any later version.
rpm-build 0a0c83
rpm-build 0a0c83
  This program is distributed in the hope that it will be useful, but WITHOUT
rpm-build 0a0c83
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
rpm-build 0a0c83
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
rpm-build 0a0c83
  License for more details.
rpm-build 0a0c83
rpm-build 0a0c83
  You should have received a copy of the GNU Lesser General Public
rpm-build 0a0c83
  License along with this program.  If not, see <http://www.gnu.org/licenses/>.
rpm-build 0a0c83
*/
rpm-build 0a0c83
rpm-build 0a0c83
#include "config.h"
rpm-build 0a0c83
#include <stdio.h>
rpm-build 0a0c83
#include <stdlib.h>
rpm-build 0a0c83
#include <ctype.h>
rpm-build 0a0c83
#include <string.h>
rpm-build 0a0c83
#include "misc.h"
rpm-build 0a0c83
rpm-build 0a0c83
const char *__acl_quote(const char *str, const char *quote_chars)
rpm-build 0a0c83
{
rpm-build 0a0c83
	static char *quoted_str;
rpm-build 0a0c83
	static size_t quoted_str_len;
rpm-build 0a0c83
	const unsigned char *s;
rpm-build 0a0c83
	char *q;
rpm-build 0a0c83
	size_t nonpr;
rpm-build 0a0c83
rpm-build 0a0c83
	if (!str)
rpm-build 0a0c83
		return str;
rpm-build 0a0c83
rpm-build 0a0c83
	for (nonpr = 0, s = (unsigned char *)str; *s != '\0'; s++)
rpm-build 0a0c83
		if (*s == '\\' || strchr(quote_chars, *s))
rpm-build 0a0c83
			nonpr++;
rpm-build 0a0c83
	if (nonpr == 0)
rpm-build 0a0c83
		return str;
rpm-build 0a0c83
rpm-build 0a0c83
	if (__acl_high_water_alloc((void **)&quoted_str, &quoted_str_len,
rpm-build 0a0c83
			     (s - (unsigned char *)str) + nonpr * 3 + 1))
rpm-build 0a0c83
		return NULL;
rpm-build 0a0c83
	for (s = (unsigned char *)str, q = quoted_str; *s != '\0'; s++) {
rpm-build 0a0c83
		if (strchr(quote_chars, *s)) {
rpm-build 0a0c83
			*q++ = '\\';
rpm-build 0a0c83
			*q++ = '0' + ((*s >> 6)    );
rpm-build 0a0c83
			*q++ = '0' + ((*s >> 3) & 7);
rpm-build 0a0c83
			*q++ = '0' + ((*s     ) & 7);
rpm-build 0a0c83
		} else if (*s == '\\') {
rpm-build 0a0c83
			*q++ = '\\';
rpm-build 0a0c83
			*q++ = '\\';
rpm-build 0a0c83
		} else
rpm-build 0a0c83
			*q++ = *s;
rpm-build 0a0c83
	}
rpm-build 0a0c83
	*q++ = '\0';
rpm-build 0a0c83
rpm-build 0a0c83
	return quoted_str;
rpm-build 0a0c83
}