diff options
Diffstat (limited to 'function.c')
-rw-r--r-- | function.c | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -2105,6 +2105,45 @@ func_realpath (char *o, char **argv, const char *funcname UNUSED) } static char * +func_file (char *o, char **argv, const char *funcname UNUSED) +{ + char *fn = argv[0]; + + if (fn[0] == '>') + { + FILE *fp; + const char *mode = "w"; + + /* We are writing a file. */ + ++fn; + if (fn[0] == '>') + { + mode = "a"; + ++fn; + } + fn = next_token (fn); + + fp = fopen (fn, mode); + if (fp == NULL) + fatal (reading_file, _("open: %s: %s"), fn, strerror (errno)); + else + { + int l = strlen (argv[1]); + int nl = (l == 0 || argv[1][l-1] != '\n'); + + if (fputs (argv[1], fp) == EOF || (nl && fputc('\n', fp) == EOF)) + fatal (reading_file, _("write: %s: %s"), fn, strerror (errno)); + + fclose (fp); + } + } + else + fatal (reading_file, _("Invalid file operation: %s"), fn); + + return o; +} + +static char * func_abspath (char *o, char **argv, const char *funcname UNUSED) { /* Expand the argument. */ @@ -2191,6 +2230,7 @@ static struct function_table_entry function_table_init[] = { STRING_SIZE_TUPLE("and"), 1, 0, 0, func_and}, { STRING_SIZE_TUPLE("value"), 0, 1, 1, func_value}, { STRING_SIZE_TUPLE("eval"), 0, 1, 1, func_eval}, + { STRING_SIZE_TUPLE("file"), 1, 2, 1, func_file}, #ifdef EXPERIMENTAL { STRING_SIZE_TUPLE("eq"), 2, 2, 1, func_eq}, { STRING_SIZE_TUPLE("not"), 0, 1, 1, func_not}, |