1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
/*
Copyright (C) 2010- The University of Notre Dame
This software is distributed under the GNU General Public License.
See the file COPYING for details.
*/
#include "debug.h"
#include "stringtools.h"
#include "xxmalloc.h"
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int find_executable(const char *exe_name, const char *env_path_var, char *exe_path, int max_length)
{
char *env_paths;
char *cur_path;
if(access(exe_name, R_OK | X_OK) == 0) {
snprintf(exe_path, max_length, "%s", exe_name);
return 1;
}
if(!getenv(env_path_var))
return 0;
env_paths = xxstrdup(getenv(env_path_var));
for(cur_path = strtok(env_paths, ":"); cur_path; cur_path = strtok(NULL, ":")) {
snprintf(exe_path, max_length, "%s/%s", cur_path, exe_name);
if(access(exe_path, R_OK | X_OK) == 0)
goto fe_cleanup;
}
fe_cleanup:
free(env_paths);
return cur_path != NULL;
}
int env_replace( const char *infile, const char *outfile ){
FILE *INPUT = fopen(infile, "r");
if (INPUT == NULL) {
debug(D_ERROR, "unable to open %s: %s", infile, strerror(errno));
return 1;
}
FILE *OUTPUT = fopen(outfile, "w");
if (OUTPUT == NULL) {
debug(D_ERROR, "unable to open %s: %s", outfile, strerror(errno));
return 1;
}
char variable[1024];
int var_index = 0;
int valid_var = 0;
char c = fgetc(INPUT);
while (c != EOF)
{
if (c == '$') {
valid_var = 1;
} else if (valid_var && (isalpha(c) || (c == '_') || (var_index > 1 && isdigit(c)))) {
variable[var_index] = c;
var_index++;
} else if (valid_var && var_index > 0) {
variable[var_index] = '\0';
const char *var = getenv(variable);
if (var) {
fprintf(OUTPUT, "%s", var);
} else {
debug(D_NOTICE, "failed to resolve %s environment variable, restoring string", variable);
}
valid_var = 0;
var_index = 0;
} else {
if (valid_var) {
fprintf(OUTPUT, "$");
}
valid_var = 0;
var_index = 0;
}
if (!valid_var) {
fprintf(OUTPUT, "%c", c);
}
c = fgetc(INPUT);
}
fclose(OUTPUT);
fclose(INPUT);
return 0;
}
/* vim: set noexpandtab tabstop=4: */
|