25 #include "pips_config.h"
38 #include <sys/param.h>
39 #include <sys/types.h>
58 FILE * fd = fopen(file,
mode);
62 file,
mode, strerror(errno));
67 FILE *
safe_fopen(
const char *filename,
const char *what)
70 if((
f = fopen( filename, what)) == (FILE *) NULL) {
72 filename, strerror(errno));
79 if(fclose(stream) == EOF) {
82 filename, strerror(errno));
85 filename, strerror(errno));
92 if (fflush(stream) == EOF)
103 if((
f = freopen( filename, what, stream)) == (FILE *) NULL) {
114 if( fseek( stream,
offset, wherefrom) != 0) {
127 if((pt == -1L) && (errno != 0)) {
150 if((
value = fgetc( stream)) == EOF) {
152 filename, strerror(errno));
161 if((
value = getc( stream)) == EOF ) {
172 if (fgets(s, n, stream) == (
char *) NULL) {
174 filename, strerror(errno));
184 if(fputc( c, stream) == EOF) {
186 filename, strerror(errno));
196 if(putc( c, stream) == EOF) {
198 filename, strerror(errno));
204 char * s, * filename;
207 if(fputs( s, stream) == EOF) {
209 filename, strerror(errno));
215 char * ptr, * filename;
220 if(((
int)fread(ptr, element_size,
count, stream)) !=
count) {
222 filename, strerror(errno));
228 char * ptr, * filename;
233 if(((
int)fwrite(ptr, element_size,
count, stream)) !=
count) {
254 bool (*file_name_predicate)(
const char *) )
268 if (regcomp(&re_compiled, re, REG_ICASE))
271 while((dp = readdir(dirp)) != NULL) {
272 if (!regexec(&re_compiled, dp->d_name, 0, NULL, 0))
274 char * full_file_name =
276 if (file_name_predicate(full_file_name))
278 free(full_file_name);
282 regfree(&re_compiled);
303 bool (* file_name_predicate)(
const char *))
308 if (return_code == -1)
310 dir, strerror(errno));
317 return (stat(name, &
buf) == 0) && S_ISDIR(
buf.st_mode);
324 return (stat(name, &
buf) == 0) && S_ISREG(
buf.st_mode);
327 #define to_escape(c) \
328 !isalnum(c) && c != '_' && c != '/' && c != '.'
337 for (
const char *iter=source; *iter; ++iter,++new_size)
339 char *escaped =
malloc(
sizeof(
char)*new_size);
340 char *eiter = escaped;
341 for (
const char *iter=source; *iter; ++iter,++eiter) {
366 if (path_list == NULL)
370 while (*path_list && n > 0)
371 if (*path_list++ ==
COLON)
379 for(len = 0; path_list[len] && path_list[len] !=
COLON; len++)
382 char *unescaped =
strndup(path_list, len);
390 if (name[0]==
'/' || name[0]==
'.')
return strdup(name);
409 return (
string) NULL;
424 return (
string) NULL;
431 return !stat(name, &
buf) && (S_IRUSR &
buf.st_mode);
443 if (mkdir(name, 0777) == -1) {
445 name, strerror(errno));
458 if(system(
concatenate(
"/bin/rm -r ", name, (
char*) NULL))) {
475 #if !defined(PATH_MAX)
476 #if defined(_POSIX_PATH_MAX)
477 #define PATH_MAX _POSIX_PATH_MAX
499 int i=0, size = 20, c;
500 char *
buf = (
char*)
malloc(
sizeof(
char)*size), * res;
502 while((c=getc(file)) && c!=EOF && c!=
'\n')
506 size+=20;
buf = (
char*) realloc((
char*)
buf,
sizeof(char)*size);
511 if (c==EOF && i==0) { res = NULL;
free(
buf); }
527 #define MAX_INPUT_LINE_SIZE (200)
548 asprintf(&msg,
"<empty interval on \"%s\": %d-%d>\n", fn,
f, l);
557 for (i = 1; i <
f ; i++) {
564 for (i =
f; i <= l; i++)
568 if (
line == NULL)
break;
573 while (*c && (*c ==
' ' || *c ==
'\t')) c++;
605 #define MAX_INPUT_LINE_SIZE (200)
608 pips_assert(
"The interval is strictly positive and non empty",
622 if((
char) n ==
'#') {
629 else if((
char) n ==
'\n') {
658 realloc(
buf,
sizeof(
char)*(strlen(
buf)+strlen(
line)+2));
672 while ((c=getc(in))!=EOF)
673 if (putc(c,
out)==EOF)
688 while ((c=getc(in))!=EOF)
690 if (first && (!but_comments || (c!=
'C' && c!=
'c' && c!=
'*' && c!=
'!')))
692 for (i=0; i<margin; i++)
693 if (putc(
' ',
out)==EOF)
699 if (putc(c,
out)==EOF)
730 if (isatty(fileno(stdout)))
736 waitpid(pgpid, &
status, 0);
741 char *pager = getenv(
"PIPS_MORE");
743 pager = getenv(
"PAGER");
746 execlp(pager, pager, fname, NULL);
748 pager, fname, strerror(errno));
777 int len = strlen(fullpath)-1, i, j;
782 int ls = strlen(suffix) - 1, le = len;
783 while (suffix[ls] == fullpath[le]) {
786 if (ls < 0 || le < 0)
795 for (i=len; i>=0; i--)
if (fullpath[i]==
'/')
break;
797 result = (
char*)
malloc(
sizeof(
char)*(len-i+1));
798 for (i++, j=0; i<=len; i++, j++)
799 result[j] = fullpath[i];
806 if(fullpath[0]==
'.' && fullpath[1]==
'/')
807 result =
strndup(fullpath+2, len-1);
809 result =
strndup(fullpath, len+1);
812 #define SLASH_SUBSTITUTION_CHARACTER '_'
814 for(cc=result; *cc!=
'\000'; cc++) {
839 char *result =
strdup(fullpath);
840 int len = strlen(result);
841 while (result[--len]!=
'/' && len>=0);
856 perror(
"[safe_unlink] ");
864 if (symlink(topath, frompath))
866 perror(
"[safe_symlink] ");
880 if (link(frompath, topath))
882 perror(
"[safe_link] ");
895 in = popen(what,
"r");
898 perror(
"[safe_system_output] ");
907 perror(
"[safe_system_output] ");
909 if (result)
free(result), result = NULL;
938 int desc = mkstemp(name);
939 pips_assert(
"could create temporary name", desc!=-1);
949 #define DEFAULT_CONFIG_DIR "etc"
950 #define CONFIG_DEFAULT_RIGHT "r"
953 const char* cproperty,
968 string senv = getenv(cenv);
974 string pipsenv = getenv(
"PIPS_ROOT");
980 sdefault =
concatenate(CONFIG_DIR
"/", canonical_name, NULL);
void gen_array_dupaddto(gen_array_t a, size_t i, void *what)
void gen_array_sort(gen_array_t a)
struct _newgen_struct_status_ * status
void safe_copy(char *source, char *target)
char * pips_filename(char *fullpath, char *suffix, bool short_p)
Some OS do not define basename and dirname.
char * safe_fgets(char *s, int n, FILE *stream, char *filename)
bool create_directory(char *name)
FILE * safe_fopen(const char *filename, const char *what)
#define CONFIG_DEFAULT_RIGHT
bool file_exists_p(const char *name)
char * get_string_property(const char *)
char * pips_initial_filename(char *fullpath, char *suffix)
The source file name access path is shortened or not depending on the property.
int safe_fputc(char c, FILE *stream, char *filename)
string nth_path(const char *path_list, int n)
Returns the allocated nth path from colon-separated path string.
#define DEFAULT_CONFIG_DIR
utility to open configuration file, (read only!) its name can be found using various ways property an...
void safe_symlink(const char *topath, const char *frompath)
char * find_file_in_directories(const char *file_name, const char *dir_path)
returns an allocated string pointing to the file, possibly with an additional path taken from colon-s...
int safe_fread(char *ptr, int element_size, int count, FILE *stream, char *filename)
int safe_display(char *fname)
Display a file through $PIPS_MORE (or $PAGER) if stdout is a TTY, on stdout otherwise.
string safe_get_line_interval(const string fn, int f, int l)
return lines f-l from file fn as a string
char * get_cwd(void)
returns the current working directory name.
int safe_fclose(FILE *stream, const char *filename)
int safe_fseek(FILE *stream, long int offset, int wherefrom, char *filename)
bool file_readable_p(char *name)
char * safe_readfile(FILE *file)
returns the file as an allocated string.
static char * relative_name_if_necessary(const char *name)
void list_files_in_directory(gen_array_t files, string dir, string re, bool(*file_name_predicate)(const char *))
The same as the previous safe_list_files_in_directory() but with no return code and a call to user er...
bool directory_exists_p(const char *name)
int safe_fflush(FILE *stream, char *filename)
char * strescape(const char *source)
protect a string, for example for use in a system call list of non escaped characters in the macro ab...
int safe_fputs(char *s, FILE *stream, char *filename)
some path to file suffix some path to *char * pips_dirname(char *fullpath)
void safe_append(FILE *out, char *file, int margin, bool but_comments)
#define MAX_INPUT_LINE_SIZE
int safe_fgetc(FILE *stream, char *filename)
char * safe_new_tmp_file(char *prefix)
SunOS forgets to declare this one.
char * safe_readline(FILE *file)
returns the allocated line read, whatever its length.
int safe_list_files_in_directory(gen_array_t files, string dir, string re, bool(*file_name_predicate)(const char *))
returns a sorted arg list of files matching regular expression re in directory 'dir' and with file_na...
int safe_getc(FILE *stream, char *filename)
char * pips_basename(char *fullpath, char *suffix)
FILE * fopen_config(const char *canonical_name, const char *cproperty, const char *cenv)
void safe_link(const char *topath, const char *frompath)
Create a hard link to topath.
char * safe_system_substitute(char *what)
returns what after variable, command and file substitutions.
long int safe_ftell(FILE *stream, char *filename)
FILE * check_fopen(const char *file, const char *mode)
file.c
int safe_putc(char c, FILE *stream, char *filename)
void safe_rewind(FILE *stream, char *filename)
bool purge_directory(char *name)
FILE * safe_freopen(char *filename, char *what, FILE *stream)
void safe_unlink(const char *file_name)
Delete the given file.
char * safe_system_output(char *what)
attempt shell substitutions to what.
int safe_fwrite(char *ptr, int element_size, int count, FILE *stream, char *filename)
#define SLASH_SUBSTITUTION_CHARACTER
bool get_bool_property(const string)
FC 2015-07-20: yuk, moved out to prevent an include cycle dependency include "properties....
void safe_copy_line_interval(string fn, int f, int l, FILE *out)
Assumes "fn" is the name of a text file, opens it, and copies the lines whose number belongs to [f....
void safe_cat(FILE *out, FILE *in)
string safe_read_nth_line(string fn, int n)
Assumes "fn" is the name of a text file, opens it, and returns its nth line (starting with 1) in a ne...
static int current_line
Poor attempt at associating physical line numbers to statement.
#define pips_user_warning
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
#define pips_internal_error
#define pips_user_irrecoverable_error
string concatenate(const char *,...)
Return the concatenation of the given strings.
void string_buffer_append(string_buffer, const string)
append string s (if non empty) to string buffer sb, the duplication is done if needed according to th...
string string_buffer_to_string(const string_buffer)
return malloc'ed string from string buffer sb
void string_buffer_free(string_buffer *)
free string buffer structure, also free string contents according to the dup field
string_buffer string_buffer_make(bool dup)
allocate a new string buffer
int f(int off1, int off2, int n, float r[n], float a[n], float b[n])
static const char * prefix
struct _newgen_struct_value_ * value
static int line
FLEX_SCANNER.
char * strndup(char const *s, size_t n)
A replacement function, for systems that lack strndup.
internally defined structure.