本文整理汇总了C++中wordexp函数的典型用法代码示例。如果您正苦于以下问题:C++ wordexp函数的具体用法?C++ wordexp怎么用?C++ wordexp使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wordexp函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: _ifparser_source
static void
_ifparser_source (const char *path, const char *en_dir, int quiet)
{
char *abs_path;
wordexp_t we;
uint i;
if (g_path_is_absolute (path))
abs_path = g_strdup (path);
else
abs_path = g_build_filename (en_dir, path, NULL);
if (!quiet)
nm_log_info (LOGD_SETTINGS, " interface-parser: source line includes interfaces file(s) %s\n", abs_path);
/* ifupdown uses WRDE_NOCMD for wordexp. */
if (wordexp (abs_path, &we, WRDE_NOCMD)) {
if (!quiet)
nm_log_warn (LOGD_SETTINGS, "word expansion for %s failed\n", abs_path);
} else {
for (i = 0; i < we.we_wordc; i++)
_recursive_ifparser (we.we_wordv[i], quiet);
wordfree (&we);
}
g_free (abs_path);
}
示例2: word_expansion
char * word_expansion(char * line)
{
//on traite les caractères spéciaux
char * new_line = replace_char(line, "|");
char * new_line_2 = replace_char(new_line, "<");
char * new_line_3 = replace_char(new_line_2, ">");
char * new_line_4 = replace_char(new_line_3, "&");
wordexp_t result;
wordexp(new_line_4, &result, 0);
free(new_line_4);
int cmd_offset = 0;
//on calcule la longueur de la nouvelle commande
for(int i=0; i<result.we_wordc; i++)
{
cmd_offset += strlen(result.we_wordv[i]);
cmd_offset++; //derrière chaque commande, il y a un espace
}
//cmd_offset--;
char * max_cmd = malloc(sizeof(char)*cmd_offset);
//initialisation à '\0'
for(int i=0; i<cmd_offset; i++)
max_cmd[i] = '\0';
int tmp_cmd_offset = 0;
//on fait la recopie
for(int i=0; tmp_cmd_offset<cmd_offset; i++)
{
strcat(max_cmd, result.we_wordv[i]);
tmp_cmd_offset += (int)strlen(result.we_wordv[i]);
max_cmd[tmp_cmd_offset++] = ' ';
}
max_cmd[cmd_offset] = '\0';
wordfree(&result);
return max_cmd;
}
示例3: assert
g_vector<g_string> PinCmd::getFullCmdArgs(uint32_t procIdx, const char** inputFile) {
assert(procIdx < procInfo.size()); //must be one of the topmost processes
g_vector<g_string> res = getPinCmdArgs(procIdx);
g_string cmd = procInfo[procIdx].cmd;
/* Loader injection: Turns out that Pin mingles with the simulated binary, which decides the loader used,
* even when PIN_VM_LIBRARY_PATH is used. This kill the invariance on libzsim.so's loaded address, because
* loaders in different children have different sizes. So, if specified, we prefix the program with the
* given loader. This is optional because it won't work with statically linked binaries.
*
* BTW, thinking of running pin under a specific loaderto fix this instead? Nope, it gets into an infinite loop.
*/
if (procInfo[procIdx].loader != "") {
cmd = procInfo[procIdx].loader + " " + cmd;
info("Injected loader on process%d, command line: %s", procIdx, cmd.c_str());
warn("Loader injection makes Pin unaware of symbol routines, so things like routine patching"
"will not work! You can homogeneize the loaders instead by editing the .interp ELF section");
}
//Parse command -- use glibc's wordexp to parse things like quotes, handle argument expansion, etc correctly
wordexp_t p;
wordexp(cmd.c_str(), &p, 0);
for (uint32_t i = 0; i < p.we_wordc; i++) {
res.push_back(g_string(p.we_wordv[i]));
}
wordfree(&p);
//Input redirect
*inputFile = (procInfo[procIdx].input == "")? nullptr : procInfo[procIdx].input.c_str();
return res;
}
示例4: expandPath
std::string expandPath(const char* file)
{
if (!file)
return "";
std::string f;
size_t size = strlen(file);
f.reserve(size);
for (size_t x=0; x<size; x++)
{
if (file[x] == ' ')
f.push_back('\\');
f.push_back(file[x]);
}
wordexp_t exp_result;
memset(&exp_result, 0, sizeof(wordexp_t));
int res = wordexp(f.c_str(), &exp_result, 0);
if (res != 0)
return "";
std::string r;
if (exp_result.we_wordv[0])
r = exp_result.we_wordv[0];
wordfree(&exp_result);
return r;
}
示例5: wordexp
const char *az_get_app_data_directory(void) {
static char path_buffer[PATH_MAX];
if (path_buffer[0] == '\0') {
// First, do tilde-expansion so we get a path in the user's homedir.
wordexp_t words;
wordexp("~/.azimuth-game", &words, 0);
if (words.we_wordc < 1) {
wordfree(&words);
return NULL;
}
strncpy(path_buffer, words.we_wordv[0], sizeof(path_buffer) - 1);
wordfree(&words);
struct stat stat_buffer;
// Try to stat the desired path.
if (stat(path_buffer, &stat_buffer) == 0) {
// If the path exists but isn't a directory, we fail.
if (!S_ISDIR(stat_buffer.st_mode)) {
path_buffer[0] = '\0';
return NULL;
}
}
// If the directory doesn't exist, try to create it. If we can't create
// it, or if stat failed for some other reason, we fail.
else if (errno != ENOENT || mkdir(path_buffer, 0700) != 0) {
path_buffer[0] = '\0';
return NULL;
}
}
return path_buffer;
}
示例6: do_wordexp
static char*
do_wordexp (const char *path)
{
#ifdef HAVE_WORDEXP_H
wordexp_t wexp;
char *dir;
if (!path) {
/* g_debug ("%s: path is empty", __FUNCTION__); */
return NULL;
}
if (wordexp (path, &wexp, 0) != 0) {
/* g_debug ("%s: expansion failed for %s", __FUNCTION__, path); */
return NULL;
}
/* we just pick the first one */
dir = g_strdup (wexp.we_wordv[0]);
/* strangely, below seems to lead to a crash on MacOS (BSD);
so we have to allow for a tiny leak here on that
platform... maybe instead of __APPLE__ it should be
__BSD__?*/
#ifndef __APPLE__
wordfree (&wexp);
#endif /*__APPLE__*/
return dir;
# else /*!HAVE_WORDEXP_H*/
/* E.g. OpenBSD does not have wordexp.h, so we ignore it */
return path ? g_strdup (path) : NULL;
#endif /*HAVE_WORDEXP_H*/
}
示例7: mame_expand_string
static void
mame_expand_string (gchar **p_string)
{
#if HAVE_WORDEXP_H
wordexp_t words;
g_return_if_fail (p_string != NULL);
g_return_if_fail (*p_string != NULL);
/* MAME configurations often use shell variables like $HOME
* in its search and output paths, so we need to expand them. */
if (wordexp (*p_string, &words, WRDE_NOCMD) == 0)
{
GString *buffer;
gsize ii;
buffer = g_string_sized_new (strlen (*p_string));
for (ii = 0; ii < words.we_wordc; ii++)
{
if (ii > 0)
g_string_append_c (buffer, ' ');
g_string_append (buffer, words.we_wordv[ii]);
}
g_free (*p_string);
*p_string = g_string_free (buffer, FALSE);
wordfree (&words);
}
#endif
}
示例8: time
void NaiveAlgorithm::logPopulationStatistics()
{
if (!currentLogFile.is_open()) {
time_t now = time(NULL);
std::stringstream s;
wordexp_t directory;
memset(&directory, 0, sizeof(wordexp_t));
wordexp("~/temp/machines/", &directory, 0);
s << directory.we_wordv[0];
s << "log" << now << ".log";
std::string filename = s.str();
currentLogFile.open(filename.c_str());
}
// write generation #: <list of fitnesses>
currentLogFile << generations << ": ";
std::vector<SystemInfo *>::iterator popIter = population.begin();
while (popIter != population.end()) {
currentLogFile << (*popIter)->fitness << " ";
popIter++;
}
currentLogFile << "\n";
currentLogFile.flush();
}
示例9: draw_image
/* @brief Draw an image at the given offset.
*
* @param cr A cairo context for drawing to the screen.
* @param file The image to be drawn.
* @return The advance in the x direction.
*/
static uint32_t draw_image(cairo_t *cr, const char *file, offset_t offset) {
wordexp_t expanded_file;
if (wordexp(file, &expanded_file, 0)) {
fprintf(stderr, "Error expanding file %s\n", file);
} else {
file = expanded_file.we_wordv[0];
}
if (access(file, F_OK) == -1) {
fprintf(stderr, "Cannot open image file %s\n", file);
return 0;
}
cairo_surface_t *img;
img = cairo_image_surface_create_from_png(file);
int w = cairo_image_surface_get_width(img);
int h = cairo_image_surface_get_height(img);
int neww = (int)(((float)(settings.height) * ((float)(w) / (float)(h))) + 0.49999999);
img = scale_surface (img, w, h, neww, settings.height);
h = settings.height;
w = neww;
/* Attempt to center the image if it is not the height of the line. */
int image_offset = (h - settings.height) / 2;
cairo_set_source_surface(cr, img, offset.x, offset.image_y - h + image_offset);
cairo_mask_surface(cr, img, offset.x, offset.image_y - h + image_offset);
return w;
}
示例10: ExpandEnvironmentStrings
std::string DataDirLocater::SubstEnvVars(const std::string& in) const
{
std::string out;
#ifdef _WIN32
const size_t maxSize = 32 * 1024;
char out_c[maxSize];
ExpandEnvironmentStrings(in.c_str(), out_c, maxSize); // expands %HOME% etc.
out = out_c;
#else
std::string previous = in;
for (int i = 0; i < 10; ++i) { // repeat substitution till we got a pure absolute path
wordexp_t pwordexp;
int r = wordexp(previous.c_str(), &pwordexp, WRDE_NOCMD); // expands $FOO, ${FOO}, ${FOO-DEF} ~/, etc.
if (r == EXIT_SUCCESS) {
if (pwordexp.we_wordc > 0) {
out = pwordexp.we_wordv[0];;
for (unsigned int w = 1; w < pwordexp.we_wordc; ++w) {
out += " ";
out += pwordexp.we_wordv[w];
}
}
wordfree(&pwordexp);
} else {
out = in;
}
if (previous == out) {
break;
}
previous.swap(out);
}
#endif
return out;
}
示例11: xdg_open_selection_cb
static void
xdg_open_selection_cb (GtkClipboard *clipboard, const char *string, gpointer data)
{
char *command;
wordexp_t result;
gboolean spawn;
GError *spawn_error = NULL;
command = g_strconcat ("xdg-open ", string, NULL);
switch (wordexp (command, &result, WRDE_NOCMD)) {
case 0:
break;
case WRDE_BADCHAR:
fprintf (stderr, "'%s' contains an invalid character\n", string);
goto finalize;
case WRDE_CMDSUB:
fprintf (stderr, "'%s' uses command substitution, which is not allowed\n", string);
goto finalize;
case WRDE_NOSPACE:
fprintf (stderr, "Could not allocate enough memory when parsing '%s'\n", string);
goto finalize;
case WRDE_SYNTAX:
fprintf (stderr, "Syntax error in '%s'\n", string);
goto finalize;
}
spawn = g_spawn_async (NULL, result.we_wordv, NULL, G_SPAWN_SEARCH_PATH,
NULL, NULL, NULL, &spawn_error);
if (!spawn) {
fprintf (stderr, "%s\n", spawn_error->message);
g_error_free (spawn_error);
}
finalize:
wordfree (&result);
}
示例12: getenv
static char *get_config_path(void) {
static const char *config_paths[] = {
"$HOME/.sway/config",
"$XDG_CONFIG_HOME/sway/config",
"$HOME/.i3/config",
"$XDG_CONFIG_HOME/i3/config",
FALLBACK_CONFIG_DIR "/config",
"/etc/i3/config",
};
if (!getenv("XDG_CONFIG_HOME")) {
char *home = getenv("HOME");
char *config_home = malloc(strlen("home") + strlen("/.config") + 1);
strcpy(config_home, home);
strcat(config_home, "/.config");
setenv("XDG_CONFIG_HOME", config_home, 1);
sway_log(L_DEBUG, "Set XDG_CONFIG_HOME to %s", config_home);
}
wordexp_t p;
char *path;
int i;
for (i = 0; i < (int)(sizeof(config_paths) / sizeof(char *)); ++i) {
if (wordexp(config_paths[i], &p, 0) == 0) {
path = p.we_wordv[0];
if (file_exists(path)) {
return path;
}
}
}
return NULL; // Not reached
}
示例13: glob_for_cachedir
static int
glob_for_cachedir(char *path)
{
int ret = 1;
if (!str_endswith(path, "XXXXXX"))
return ret;
wordexp_t word_vector;
char *p = solv_strdup(path);
const int len = strlen(p);
struct stat s;
ret = 2;
p[len-6] = '*';
p[len-5] = '\0';
if (wordexp(p, &word_vector, 0)) {
solv_free(p);
return ret;
}
for (int i = 0; i < word_vector.we_wordc; ++i) {
char *entry = word_vector.we_wordv[i];
if (stat(entry, &s))
continue;
if (S_ISDIR(s.st_mode) &&
s.st_uid == getuid()) {
assert(strlen(path) == strlen(entry));
strcpy(path, entry);
ret = 0;
break;
}
}
wordfree(&word_vector);
solv_free(p);
return ret;
}
示例14: wordexp
std::string ConfigurationManager::retrieveAsPath(std::string namespaceName, std::string identifier, std::string defaultValue)
{
std::string result = defaultValue;
EntriesMap::const_iterator iter = entries->find(std::make_pair(namespaceName, identifier));
if(iter != entries->end())
{
wordexp_t expansion;
int expansionResult = 0;
expansionResult = wordexp(iter->second.c_str(), &expansion, WRDE_NOCMD);
if(expansionResult == 0 && expansion.we_wordc > 0)
{
result.clear();
for(int i=0;i<expansion.we_wordc;i++)
{
result += expansion.we_wordv[i];
}
}
wordfree(&expansion);
if(result[0] != '/') // relative path, need to prefix with workingDir
{
result = workingDir + result;
}
}
return result;
}
示例15: get_dclick_time
static int get_dclick_time (void)
{
FILE *fp;
char buf[256];
int val;
wordexp_t exp;
struct stat st;
// check to see if there is a gtkrc file in the home directory
wordexp ("~/.gtkrc-2.0", &exp, 0);
if (stat (exp.we_wordv[0], &st) != 0)
{
// there isn't - create one with default value
sprintf (buf, "echo gtk-double-click-time=250 > %s", exp.we_wordv[0]);
system (buf);
return 250;
}
// there is a file - does it contain a value?
fp = popen ("grep gtk-double-click-time ~/.gtkrc-2.0", "r");
if (fp == NULL) return 0; // should never happen...
if (fgets (buf, sizeof (buf) - 1, fp) != NULL)
{
if (sscanf (buf, "gtk-double-click-time=%d", &val) == 1) return val;
}
// no matching parameter in file - prepend one
sprintf (buf, "sed -i \"1i gtk-double-click-time=250\" %s", exp.we_wordv[0]);
system (buf);
return 250;
}