Это можно сделать с помощью трюка LD_PRELOAD . Вот реализация, которая отображает пути, начинающиеся с определенного префикса, в другое место. Код также находится на гитхабе .
Например, вы можете подделать существование файла в / etc /
, не будучи пользователем root. Это было необходимо для клиента owncloud, который отказывается работать, когда файл /etc/ownCloud/sync-exclude.list
не существует.
Он работает путем переопределения функций open ()
и open64 ()
для сопоставления одного каталога с другим, например, все вызовы open ()
для / etc / ownCloud / ...
можно перенаправить на /home/user1/.etc/ownCloud / ...
.
Просто настройте path_map
, затем скомпилируйте и запустите вашу программу с предварительно загруженной библиотекой:
gcc -std=c99 -Wall -shared -fPIC path-mapping.c -o path-mapping.so -ldl
LD_PRELOAD=/path/to/my/path-mapping.so someprogram
Исходный код path-mapping.c
:
#define _GNU_SOURCE
#include
#include
#include
#include
#include
#include
#include
#include
#include
// List of path pairs. Paths beginning with the first item will be
// translated by replacing the matching part with the second item.
static const char *path_map[][2] = {
{ "/etc/ownCloud/", "/home/user1/.etc/ownCloud/" },
};
__thread char *buffer = NULL;
__thread int buffer_size = -1;
typedef FILE* (*orig_fopen_func_type)(const char *path, const char *mode);
typedef int (*orig_open_func_type)(const char *pathname, int flags, ...);
static int starts_with(const char *str, const char *prefix) {
return (strncmp(prefix, str, strlen(prefix)) == 0);
}
static char *get_buffer(int min_size) {
int step = 63;
if (min_size < 1) {
min_size = 1;
}
if (min_size > buffer_size) {
if (buffer != NULL) {
free(buffer);
buffer = NULL;
buffer_size = -1;
}
buffer = malloc(min_size + step);
if (buffer != NULL) {
buffer_size = min_size + step;
}
}
return buffer;
}
static const char *fix_path(const char *path)
{
int count = (sizeof path_map) / (sizeof *path_map); // Array length
for (int i = 0; i < count; i++) {
const char *prefix = path_map[i][0];
const char *replace = path_map[i][1];
if (starts_with(path, prefix)) {
const char *rest = path + strlen(prefix);
char *new_path = get_buffer(strlen(path) + strlen(replace) - strlen(prefix));
strcpy(new_path, replace);
strcat(new_path, rest);
printf("Mapped Path: %s ==> %s\n", path, new_path);
return new_path;
}
}
return path;
}
int open(const char *pathname, int flags, ...)
{
const char *new_path = fix_path(pathname);
orig_open_func_type orig_func;
orig_func = (orig_open_func_type)dlsym(RTLD_NEXT, "open");
// If O_CREAT is used to create a file, the file access mode must be given.
if (flags & O_CREAT) {
va_list args;
va_start(args, flags);
int mode = va_arg(args, int);
va_end(args);
return orig_func(new_path, flags, mode);
} else {
return orig_func(new_path, flags);
}
}
int open64(const char *pathname, int flags, ...)
{
const char *new_path = fix_path(pathname);
orig_open_func_type orig_func;
orig_func = (orig_open_func_type)dlsym(RTLD_NEXT, "open64");
// If O_CREAT is used to create a file, the file access mode must be given.
if (flags & O_CREAT) {
va_list args;
va_start(args, flags);
int mode = va_arg(args, int);
va_end(args);
return orig_func(new_path, flags, mode);
} else {
return orig_func(new_path, flags);
}
}
(как обсуждалось в комментариях, вместо этого используйте tput
, если он у вас есть)
Использование оболочки bourne иecho
(встроенной -в )команды, которая понимает escape-последовательность ANSI \e
с опцией -e
:
black() { IFS= ; while read -r line; do echo -e '\e[30m'$line'\e[0m'; done; }
red() { IFS= ; while read -r line; do echo -e '\e[31m'$line'\e[0m'; done; }
green() { IFS= ; while read -r line; do echo -e '\e[32m'$line'\e[0m'; done; }
yellow() { IFS= ; while read -r line; do echo -e '\e[33m'$line'\e[0m'; done; }
blue() { IFS= ; while read -r line; do echo -e '\e[34m'$line'\e[0m'; done; }
purple() { IFS= ; while read -r line; do echo -e '\e[35m'$line'\e[0m'; done; }
cyan() { IFS= ; while read -r line; do echo -e '\e[36m'$line'\e[0m'; done; }
white() { IFS= ; while read -r line; do echo -e '\e[37m'$line'\e[0m'; done; }
echo ' foo\n bar' | red
или более общий сценарий оболочки (, скажем,/usr/local/bin/colorize
):
#!/bin/sh
usage() {
echo 'usage:' >&2
echo ' some-command | colorize {black, red, green, yellow, blue, purple, cyan, white}' >&2
exit 1
}
[ -z "$1" ] && usage
case $1 in
black) color='\e[30m' ;;
red) color='\e[31m' ;;
green) color='\e[32m' ;;
yellow) color='\e[33m' ;;
blue) color='\e[34m' ;;
purple) color='\e[35m' ;;
cyan) color='\e[36m' ;;
white) color='\e[36m' ;;
*) usage ;;
esac
IFS=
while read -r line; do
echo -e $color$line'\e[0m'
done
IFS=
необходим для предотвращения обрезки пробелов (подробности см. в POSIX ).