Merged revisions 328247 via svnmerge from
[asterisk/asterisk.git] / funcs / func_env.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2010, Digium, Inc.
5  *
6  * See http://www.asterisk.org for more information about
7  * the Asterisk project. Please do not directly contact
8  * any of the maintainers of this project for assistance;
9  * the project provides a web site, mailing lists and IRC
10  * channels for your use.
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License Version 2. See the LICENSE file
14  * at the top of the source tree.
15  */
16
17 /*! \file
18  *
19  * \brief Environment related dialplan functions
20  *
21  * \ingroup functions
22  */
23
24 /*** MODULEINFO
25         <support_level>core</support_level>
26  ***/
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include <sys/stat.h>   /* stat(2) */
33
34 #include "asterisk/module.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/utils.h"
38 #include "asterisk/app.h"
39 #include "asterisk/file.h"
40
41 /*** DOCUMENTATION
42         <function name="ENV" language="en_US">
43                 <synopsis>
44                         Gets or sets the environment variable specified.
45                 </synopsis>
46                 <syntax>
47                         <parameter name="varname" required="true">
48                                 <para>Environment variable name</para>
49                         </parameter>
50                 </syntax>
51                 <description>
52                         <para>Variables starting with <literal>AST_</literal> are reserved to the system and may not be set.</para>
53                 </description>
54         </function>
55         <function name="STAT" language="en_US">
56                 <synopsis>
57                         Does a check on the specified file.
58                 </synopsis>
59                 <syntax>
60                         <parameter name="flag" required="true">
61                                 <para>Flag may be one of the following:</para>
62                                 <para>d - Checks if the file is a directory.</para>
63                                 <para>e - Checks if the file exists.</para>
64                                 <para>f - Checks if the file is a regular file.</para>
65                                 <para>m - Returns the file mode (in octal)</para>
66                                 <para>s - Returns the size (in bytes) of the file</para>
67                                 <para>A - Returns the epoch at which the file was last accessed.</para>
68                                 <para>C - Returns the epoch at which the inode was last changed.</para>
69                                 <para>M - Returns the epoch at which the file was last modified.</para>
70                         </parameter>
71                         <parameter name="filename" required="true" />
72                 </syntax>
73                 <description>
74                 </description>
75         </function>
76         <function name="FILE" language="en_US">
77                 <synopsis>
78                         Read or write text file.
79                 </synopsis>
80                 <syntax>
81                         <parameter name="filename" required="true" />
82                         <parameter name="offset">
83                                 <para>Maybe specified as any number. If negative, <replaceable>offset</replaceable> specifies the number
84                                 of bytes back from the end of the file.</para>
85                         </parameter>
86                         <parameter name="length">
87                                 <para>If specified, will limit the length of the data read to that size. If negative,
88                                 trims <replaceable>length</replaceable> bytes from the end of the file.</para>
89                         </parameter>
90                         <parameter name="options">
91                                 <optionlist>
92                                         <option name="l">
93                                                 <para>Line mode:  offset and length are assumed to be
94                                                 measured in lines, instead of byte offsets.</para>
95                                         </option>
96                                         <option name="a">
97                                                 <para>In write mode only, the append option is used to
98                                                 append to the end of the file, instead of overwriting
99                                                 the existing file.</para>
100                                         </option>
101                                         <option name="d">
102                                                 <para>In write mode and line mode only, this option does
103                                                 not automatically append a newline string to the end of
104                                                 a value.  This is useful for deleting lines, instead of
105                                                 setting them to blank.</para>
106                                         </option>
107                                 </optionlist>
108                         </parameter>
109                         <parameter name="format">
110                                 <para>The <replaceable>format</replaceable> parameter may be
111                                 used to delimit the type of line terminators in line mode.</para>
112                                 <optionlist>
113                                         <option name="u">
114                                                 <para>Unix newline format.</para>
115                                         </option>
116                                         <option name="d">
117                                                 <para>DOS newline format.</para>
118                                         </option>
119                                         <option name="m">
120                                                 <para>Macintosh newline format.</para>
121                                         </option>
122                                 </optionlist>
123                         </parameter>
124                 </syntax>
125                 <description>
126                         <para>Read and write text file in character and line mode.</para>
127                         <para>Examples:</para>
128                         <para/>
129                         <para>Read mode (byte):</para>
130                         <para>    ;reads the entire content of the file.</para>
131                         <para>    Set(foo=${FILE(/tmp/test.txt)})</para>
132                         <para>    ;reads from the 11th byte to the end of the file (i.e. skips the first 10).</para>
133                         <para>    Set(foo=${FILE(/tmp/test.txt,10)})</para>
134                         <para>    ;reads from the 11th to 20th byte in the file (i.e. skip the first 10, then read 10 bytes).</para>
135                         <para>    Set(foo=${FILE(/tmp/test.txt,10,10)})</para>
136                         <para/>
137                         <para>Read mode (line):</para>
138                         <para>    ; reads the 3rd line of the file.</para>
139                         <para>    Set(foo=${FILE(/tmp/test.txt,3,1,l)})</para>
140                         <para>    ; reads the 3rd and 4th lines of the file.</para>
141                         <para>    Set(foo=${FILE(/tmp/test.txt,3,2,l)})</para>
142                         <para>    ; reads from the third line to the end of the file.</para>
143                         <para>    Set(foo=${FILE(/tmp/test.txt,3,,l)})</para>
144                         <para>    ; reads the last three lines of the file.</para>
145                         <para>    Set(foo=${FILE(/tmp/test.txt,-3,,l)})</para>
146                         <para>    ; reads the 3rd line of a DOS-formatted file.</para>
147                         <para>    Set(foo=${FILE(/tmp/test.txt,3,1,l,d)})</para>
148                         <para/>
149                         <para>Write mode (byte):</para>
150                         <para>    ; truncate the file and write "bar"</para>
151                         <para>    Set(FILE(/tmp/test.txt)=bar)</para>
152                         <para>    ; Append "bar"</para>
153                         <para>    Set(FILE(/tmp/test.txt,,,a)=bar)</para>
154                         <para>    ; Replace the first byte with "bar" (replaces 1 character with 3)</para>
155                         <para>    Set(FILE(/tmp/test.txt,0,1)=bar)</para>
156                         <para>    ; Replace 10 bytes beginning at the 21st byte of the file with "bar"</para>
157                         <para>    Set(FILE(/tmp/test.txt,20,10)=bar)</para>
158                         <para>    ; Replace all bytes from the 21st with "bar"</para>
159                         <para>    Set(FILE(/tmp/test.txt,20)=bar)</para>
160                         <para>    ; Insert "bar" after the 4th character</para>
161                         <para>    Set(FILE(/tmp/test.txt,4,0)=bar)</para>
162                         <para/>
163                         <para>Write mode (line):</para>
164                         <para>    ; Replace the first line of the file with "bar"</para>
165                         <para>    Set(FILE(/tmp/foo.txt,0,1,l)=bar)</para>
166                         <para>    ; Replace the last line of the file with "bar"</para>
167                         <para>    Set(FILE(/tmp/foo.txt,-1,,l)=bar)</para>
168                         <para>    ; Append "bar" to the file with a newline</para>
169                         <para>    Set(FILE(/tmp/foo.txt,,,al)=bar)</para>
170                 </description>
171                 <see-also>
172                         <ref type="function">FILE_COUNT_LINE</ref>
173                         <ref type="function">FILE_FORMAT</ref>
174                 </see-also>
175         </function>
176         <function name="FILE_COUNT_LINE" language="en_US">
177                 <synopsis>
178                         Obtains the number of lines of a text file.
179                 </synopsis>
180                 <syntax>
181                         <parameter name="filename" required="true" />
182                         <parameter name="format">
183                                 <para>Format may be one of the following:</para>
184                                 <optionlist>
185                                         <option name="u">
186                                                 <para>Unix newline format.</para>
187                                         </option>
188                                         <option name="d">
189                                                 <para>DOS newline format.</para>
190                                         </option>
191                                         <option name="m">
192                                                 <para>Macintosh newline format.</para>
193                                         </option>
194                                 </optionlist>
195                                 <note><para>If not specified, an attempt will be made to determine the newline format type.</para></note>
196                         </parameter>
197                 </syntax>
198                 <description>
199                         <para>Returns the number of lines, or <literal>-1</literal> on error.</para>
200                 </description>
201                 <see-also>
202                         <ref type="function">FILE</ref>
203                         <ref type="function">FILE_FORMAT</ref>
204                 </see-also>
205         </function>
206         <function name="FILE_FORMAT" language="en_US">
207                 <synopsis>
208                         Return the newline format of a text file.
209                 </synopsis>
210                 <syntax>
211                         <parameter name="filename" required="true" />
212                 </syntax>
213                 <description>
214                         <para>Return the line terminator type:</para>
215                         <para>'u' - Unix "\n" format</para>
216                         <para>'d' - DOS "\r\n" format</para>
217                         <para>'m' - Macintosh "\r" format</para>
218                         <para>'x' - Cannot be determined</para>
219                 </description>
220                 <see-also>
221                         <ref type="function">FILE</ref>
222                         <ref type="function">FILE_COUNT_LINE</ref>
223                 </see-also>
224         </function>
225  ***/
226
227 static int env_read(struct ast_channel *chan, const char *cmd, char *data,
228                         char *buf, size_t len)
229 {
230         char *ret = NULL;
231
232         *buf = '\0';
233
234         if (data)
235                 ret = getenv(data);
236
237         if (ret)
238                 ast_copy_string(buf, ret, len);
239
240         return 0;
241 }
242
243 static int env_write(struct ast_channel *chan, const char *cmd, char *data,
244                          const char *value)
245 {
246         if (!ast_strlen_zero(data) && strncmp(data, "AST_", 4)) {
247                 if (!ast_strlen_zero(value)) {
248                         setenv(data, value, 1);
249                 } else {
250                         unsetenv(data);
251                 }
252         }
253
254         return 0;
255 }
256
257 static int stat_read(struct ast_channel *chan, const char *cmd, char *data,
258                          char *buf, size_t len)
259 {
260         char *action;
261         struct stat s;
262
263         ast_copy_string(buf, "0", len);
264
265         action = strsep(&data, ",");
266         if (stat(data, &s)) {
267                 return 0;
268         } else {
269                 switch (*action) {
270                 case 'e':
271                         strcpy(buf, "1");
272                         break;
273                 case 's':
274                         snprintf(buf, len, "%d", (unsigned int) s.st_size);
275                         break;
276                 case 'f':
277                         snprintf(buf, len, "%d", S_ISREG(s.st_mode) ? 1 : 0);
278                         break;
279                 case 'd':
280                         snprintf(buf, len, "%d", S_ISDIR(s.st_mode) ? 1 : 0);
281                         break;
282                 case 'M':
283                         snprintf(buf, len, "%d", (int) s.st_mtime);
284                         break;
285                 case 'A':
286                         snprintf(buf, len, "%d", (int) s.st_mtime);
287                         break;
288                 case 'C':
289                         snprintf(buf, len, "%d", (int) s.st_ctime);
290                         break;
291                 case 'm':
292                         snprintf(buf, len, "%o", (int) s.st_mode);
293                         break;
294                 }
295         }
296
297         return 0;
298 }
299
300 enum file_format {
301         FF_UNKNOWN = -1,
302         FF_UNIX,
303         FF_DOS,
304         FF_MAC,
305 };
306
307 static int64_t count_lines(const char *filename, enum file_format newline_format)
308 {
309         int count = 0;
310         char fbuf[4096];
311         FILE *ff;
312
313         if (!(ff = fopen(filename, "r"))) {
314                 ast_log(LOG_ERROR, "Unable to open '%s': %s\n", filename, strerror(errno));
315                 return -1;
316         }
317
318         while (fgets(fbuf, sizeof(fbuf), ff)) {
319                 char *next = fbuf, *first_cr = NULL, *first_nl = NULL;
320
321                 /* Must do it this way, because if the fileformat is FF_MAC, then Unix
322                  * assumptions about line-format will not come into play. */
323                 while (next) {
324                         if (newline_format == FF_DOS || newline_format == FF_MAC || newline_format == FF_UNKNOWN) {
325                                 first_cr = strchr(next, '\r');
326                         }
327                         if (newline_format == FF_UNIX || newline_format == FF_UNKNOWN) {
328                                 first_nl = strchr(next, '\n');
329                         }
330
331                         /* No terminators found in buffer */
332                         if (!first_cr && !first_nl) {
333                                 break;
334                         }
335
336                         if (newline_format == FF_UNKNOWN) {
337                                 if ((first_cr && !first_nl) || (first_cr && first_cr < first_nl)) {
338                                         if (first_nl && first_nl == first_cr + 1) {
339                                                 newline_format = FF_DOS;
340                                         } else if (first_cr && first_cr == &fbuf[sizeof(fbuf) - 2]) {
341                                                 /* Get it on the next pass */
342                                                 fseek(ff, -1, SEEK_CUR);
343                                                 break;
344                                         } else {
345                                                 newline_format = FF_MAC;
346                                                 first_nl = NULL;
347                                         }
348                                 } else {
349                                         newline_format = FF_UNIX;
350                                         first_cr = NULL;
351                                 }
352                                 /* Jump down into next section */
353                         }
354
355                         if (newline_format == FF_DOS) {
356                                 if (first_nl && first_cr && first_nl == first_cr + 1) {
357                                         next = first_nl + 1;
358                                         count++;
359                                 } else if (first_cr == &fbuf[sizeof(fbuf) - 2]) {
360                                         /* Get it on the next pass */
361                                         fseek(ff, -1, SEEK_CUR);
362                                         break;
363                                 }
364                         } else if (newline_format == FF_MAC) {
365                                 if (first_cr) {
366                                         next = first_cr + 1;
367                                         count++;
368                                 }
369                         } else if (newline_format == FF_UNIX) {
370                                 if (first_nl) {
371                                         next = first_nl + 1;
372                                         count++;
373                                 }
374                         }
375                 }
376         }
377         fclose(ff);
378
379         return count;
380 }
381
382 static int file_count_line(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
383 {
384         enum file_format newline_format = FF_UNKNOWN;
385         int64_t count;
386         AST_DECLARE_APP_ARGS(args,
387                 AST_APP_ARG(filename);
388                 AST_APP_ARG(format);
389         );
390
391         AST_STANDARD_APP_ARGS(args, data);
392         if (args.argc > 1) {
393                 if (tolower(args.format[0]) == 'd') {
394                         newline_format = FF_DOS;
395                 } else if (tolower(args.format[0]) == 'm') {
396                         newline_format = FF_MAC;
397                 } else if (tolower(args.format[0]) == 'u') {
398                         newline_format = FF_UNIX;
399                 }
400         }
401
402         count = count_lines(args.filename, newline_format);
403         ast_str_set(buf, len, "%" PRId64, count);
404         return 0;
405 }
406
407 #define LINE_COUNTER(cptr, term, counter) \
408         if (*cptr == '\n' && term == FF_UNIX) { \
409                 counter++; \
410         } else if (*cptr == '\n' && term == FF_DOS && dos_state == 0) { \
411                 dos_state = 1; \
412         } else if (*cptr == '\r' && term == FF_DOS && dos_state == 1) { \
413                 dos_state = 0; \
414                 counter++; \
415         } else if (*cptr == '\r' && term == FF_MAC) { \
416                 counter++; \
417         } else if (term == FF_DOS) { \
418                 dos_state = 0; \
419         }
420
421 static enum file_format file2format(const char *filename)
422 {
423         FILE *ff;
424         char fbuf[4096];
425         char *first_cr, *first_nl;
426         enum file_format newline_format = FF_UNKNOWN;
427
428         if (!(ff = fopen(filename, "r"))) {
429                 ast_log(LOG_ERROR, "Cannot open '%s': %s\n", filename, strerror(errno));
430                 return -1;
431         }
432
433         while (fgets(fbuf, sizeof(fbuf), ff)) {
434                 first_cr = strchr(fbuf, '\r');
435                 first_nl = strchr(fbuf, '\n');
436
437                 if (!first_cr && !first_nl) {
438                         continue;
439                 }
440
441                 if ((first_cr && !first_nl) || (first_cr && first_cr < first_nl)) {
442
443                         if (first_nl && first_nl == first_cr + 1) {
444                                 newline_format = FF_DOS;
445                         } else if (first_cr && first_cr == &fbuf[sizeof(fbuf) - 2]) {
446                                 /* Edge case: get it on the next pass */
447                                 fseek(ff, -1, SEEK_CUR);
448                                 continue;
449                         } else {
450                                 newline_format = FF_MAC;
451                         }
452                 } else {
453                         newline_format = FF_UNIX;
454                 }
455                 break;
456         }
457         fclose(ff);
458         return newline_format;
459 }
460
461 static int file_format(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
462 {
463         enum file_format newline_format = file2format(data);
464         ast_str_set(buf, len, "%c", newline_format == FF_UNIX ? 'u' : newline_format == FF_DOS ? 'd' : newline_format == FF_MAC ? 'm' : 'x');
465         return 0;
466 }
467
468 static int file_read(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
469 {
470         FILE *ff;
471         int64_t offset = 0, length = LLONG_MAX;
472         enum file_format format = FF_UNKNOWN;
473         char fbuf[4096];
474         int64_t flength, i; /* iterator needs to be signed, so it can go negative and terminate the loop */
475         int64_t offset_offset = -1, length_offset = -1;
476         char dos_state = 0;
477         size_t readlen;
478         AST_DECLARE_APP_ARGS(args,
479                 AST_APP_ARG(filename);
480                 AST_APP_ARG(offset);
481                 AST_APP_ARG(length);
482                 AST_APP_ARG(options);
483                 AST_APP_ARG(fileformat);
484         );
485
486         AST_STANDARD_APP_ARGS(args, data);
487
488         if (args.argc > 1) {
489                 sscanf(args.offset, "%" SCNd64, &offset);
490         }
491         if (args.argc > 2) {
492                 sscanf(args.length, "%" SCNd64, &length);
493         }
494
495         if (args.argc < 4 || !strchr(args.options, 'l')) {
496                 /* Character-based mode */
497                 off_t off_i;
498
499                 if (!(ff = fopen(args.filename, "r"))) {
500                         ast_log(LOG_WARNING, "Cannot open file '%s' for reading: %s\n", args.filename, strerror(errno));
501                         return 0;
502                 }
503
504                 if (fseeko(ff, 0, SEEK_END) < 0) {
505                         ast_log(LOG_ERROR, "Cannot seek to end of '%s': %s\n", args.filename, strerror(errno));
506                         fclose(ff);
507                         return -1;
508                 }
509                 flength = ftello(ff);
510
511                 if (offset < 0) {
512                         fseeko(ff, offset, SEEK_END);
513                         offset = ftello(ff);
514                 }
515                 if (length < 0) {
516                         fseeko(ff, length, SEEK_END);
517                         if ((length = ftello(ff)) - offset < 0) {
518                                 /* Eliminates all results */
519                                 fclose(ff);
520                                 return -1;
521                         }
522                 } else if (length == LLONG_MAX) {
523                         fseeko(ff, 0, SEEK_END);
524                         length = ftello(ff);
525                 }
526
527                 ast_str_reset(*buf);
528
529                 fseeko(ff, offset, SEEK_SET);
530                 for (off_i = ftello(ff); off_i < flength && off_i < offset + length; off_i += sizeof(fbuf)) {
531                         /* Calculate if we need to retrieve just a portion of the file in memory */
532                         size_t toappend = sizeof(fbuf);
533
534                         if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
535                                 ast_log(LOG_ERROR, "Short read?!!\n");
536                                 break;
537                         }
538
539                         /* Don't go past the length requested */
540                         if (off_i + toappend > offset + length) {
541                                 toappend = length - off_i;
542                         }
543
544                         ast_str_append_substr(buf, len, fbuf, toappend);
545                 }
546                 fclose(ff);
547                 return 0;
548         }
549
550         /* Line-based read */
551         if (args.argc == 5) {
552                 if (tolower(args.fileformat[0]) == 'd') {
553                         format = FF_DOS;
554                 } else if (tolower(args.fileformat[0]) == 'm') {
555                         format = FF_MAC;
556                 } else if (tolower(args.fileformat[0]) == 'u') {
557                         format = FF_UNIX;
558                 }
559         }
560
561         if (format == FF_UNKNOWN) {
562                 if ((format = file2format(args.filename)) == FF_UNKNOWN) {
563                         ast_log(LOG_WARNING, "'%s' is not a line-based file\n", args.filename);
564                         return -1;
565                 }
566         }
567
568         if (offset < 0 && length <= offset) {
569                 /* Length eliminates all content */
570                 return -1;
571         } else if (offset == 0) {
572                 offset_offset = 0;
573         }
574
575         if (!(ff = fopen(args.filename, "r"))) {
576                 ast_log(LOG_ERROR, "Cannot open '%s': %s\n", args.filename, strerror(errno));
577                 return -1;
578         }
579
580         if (fseek(ff, 0, SEEK_END)) {
581                 ast_log(LOG_ERROR, "Cannot seek to end of file '%s': %s\n", args.filename, strerror(errno));
582                 fclose(ff);
583                 return -1;
584         }
585
586         flength = ftello(ff);
587
588         if (length == LLONG_MAX) {
589                 length_offset = flength;
590         }
591
592         /* For negative offset and/or negative length */
593         if (offset < 0 || length < 0) {
594                 int64_t count = 0;
595                 /* Start with an even multiple of fbuf, so at the end of reading with a
596                  * 0 offset, we don't try to go past the beginning of the file. */
597                 for (i = (flength / sizeof(fbuf)) * sizeof(fbuf); i >= 0; i -= sizeof(fbuf)) {
598                         size_t end;
599                         char *pos;
600                         if (fseeko(ff, i, SEEK_SET)) {
601                                 ast_log(LOG_ERROR, "Cannot seek to offset %" PRId64 ": %s\n", i, strerror(errno));
602                         }
603                         end = fread(fbuf, 1, sizeof(fbuf), ff);
604                         for (pos = end < sizeof(fbuf) ? fbuf + end - 1 : fbuf + sizeof(fbuf) - 1; pos > fbuf - 1; pos--) {
605                                 LINE_COUNTER(pos, format, count);
606
607                                 if (length < 0 && count * -1 == length) {
608                                         length_offset = i + (pos - fbuf);
609                                 } else if (offset < 0 && count * -1 == (offset - 1)) {
610                                         /* Found our initial offset.  We're done with reverse motion! */
611                                         if (format == FF_DOS) {
612                                                 offset_offset = i + (pos - fbuf) + 2;
613                                         } else {
614                                                 offset_offset = i + (pos - fbuf) + 1;
615                                         }
616                                         break;
617                                 }
618                         }
619                         if ((offset < 0 && offset_offset >= 0) || (offset >= 0 && length_offset >= 0)) {
620                                 break;
621                         }
622                 }
623                 /* We're at the beginning, and the negative offset indicates the exact number of lines in the file */
624                 if (offset < 0 && offset_offset < 0 && offset == count * -1) {
625                         offset_offset = 0;
626                 }
627         }
628
629         /* Positve line offset */
630         if (offset > 0) {
631                 int64_t count = 0;
632                 fseek(ff, 0, SEEK_SET);
633                 for (i = 0; i < flength; i += sizeof(fbuf)) {
634                         char *pos;
635                         if (i + sizeof(fbuf) <= flength) {
636                                 /* Don't let previous values influence current counts, due to short reads */
637                                 memset(fbuf, 0, sizeof(fbuf));
638                         }
639                         if (fread(fbuf, 1, sizeof(fbuf), ff) && !feof(ff)) {
640                                 ast_log(LOG_ERROR, "Short read?!!\n");
641                                 fclose(ff);
642                                 return -1;
643                         }
644                         for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
645                                 LINE_COUNTER(pos, format, count);
646
647                                 if (count == offset) {
648                                         offset_offset = i + (pos - fbuf) + 1;
649                                         break;
650                                 }
651                         }
652                         if (offset_offset >= 0) {
653                                 break;
654                         }
655                 }
656         }
657
658         if (offset_offset < 0) {
659                 ast_log(LOG_ERROR, "Offset '%s' refers to before the beginning of the file!\n", args.offset);
660                 fclose(ff);
661                 return -1;
662         }
663
664         ast_str_reset(*buf);
665         if (fseeko(ff, offset_offset, SEEK_SET)) {
666                 ast_log(LOG_ERROR, "fseeko failed: %s\n", strerror(errno));
667         }
668
669         /* If we have both offset_offset and length_offset, then grabbing the
670          * buffer is simply a matter of just retrieving the file and adding it
671          * to buf.  Otherwise, we need to run byte-by-byte forward until the
672          * length is complete. */
673         if (length_offset >= 0) {
674                 ast_debug(3, "offset=%" PRId64 ", length=%" PRId64 ", offset_offset=%" PRId64 ", length_offset=%" PRId64 "\n", offset, length, offset_offset, length_offset);
675                 for (i = offset_offset; i < length_offset; i += sizeof(fbuf)) {
676                         if (fread(fbuf, 1, i + sizeof(fbuf) > flength ? flength - i : sizeof(fbuf), ff) < (i + sizeof(fbuf) > flength ? flength - i : sizeof(fbuf))) {
677                                 ast_log(LOG_ERROR, "Short read?!!\n");
678                         }
679                         ast_debug(3, "Appending first %" PRId64" bytes of fbuf=%s\n", i + sizeof(fbuf) > length_offset ? length_offset - i : sizeof(fbuf), fbuf);
680                         ast_str_append_substr(buf, len, fbuf, i + sizeof(fbuf) > length_offset ? length_offset - i : sizeof(fbuf));
681                 }
682         } else if (length == 0) {
683                 /* Nothing to do */
684         } else {
685                 /* Positive line offset */
686                 int64_t current_length = 0;
687                 char dos_state = 0;
688                 ast_debug(3, "offset=%" PRId64 ", length=%" PRId64 ", offset_offset=%" PRId64 ", length_offset=%" PRId64 "\n", offset, length, offset_offset, length_offset);
689                 for (i = offset_offset; i < flength; i += sizeof(fbuf)) {
690                         char *pos;
691                         if ((readlen = fread(fbuf, 1, sizeof(fbuf), ff)) < sizeof(fbuf) && !feof(ff)) {
692                                 ast_log(LOG_ERROR, "Short read?!!\n");
693                         }
694                         for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
695                                 LINE_COUNTER(pos, format, current_length);
696
697                                 if (current_length == length) {
698                                         length_offset = i + (pos - fbuf) + 1;
699                                         break;
700                                 }
701                         }
702                         ast_debug(3, "length_offset=%" PRId64 ", length_offset - i=%" PRId64 "\n", length_offset, length_offset - i);
703                         ast_str_append_substr(buf, len, fbuf, length_offset >= 0 ? length_offset - i : flength > i + sizeof(fbuf)) ? sizeof(fbuf) : flength - i;
704
705                         if (length_offset >= 0) {
706                                 break;
707                         }
708                 }
709         }
710
711         fclose(ff);
712         return 0;
713 }
714
715 const char *format2term(enum file_format f) __attribute__((const));
716 const char *format2term(enum file_format f)
717 {
718         const char *term[] = { "", "\n", "\r\n", "\r" };
719         return term[f + 1];
720 }
721
722 static int file_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
723 {
724         AST_DECLARE_APP_ARGS(args,
725                 AST_APP_ARG(filename);
726                 AST_APP_ARG(offset);
727                 AST_APP_ARG(length);
728                 AST_APP_ARG(options);
729                 AST_APP_ARG(format);
730         );
731         int64_t offset = 0, length = LLONG_MAX;
732         off_t flength, vlength;
733         size_t foplen = 0;
734         FILE *ff;
735
736         AST_STANDARD_APP_ARGS(args, data);
737
738         if (args.argc > 1) {
739                 sscanf(args.offset, "%" SCNd64, &offset);
740         }
741         if (args.argc > 2) {
742                 sscanf(args.length, "%" SCNd64, &length);
743         }
744
745         vlength = strlen(value);
746
747         if (args.argc < 4 || !strchr(args.options, 'l')) {
748                 /* Character-based mode */
749
750                 if (args.argc > 3 && strchr(args.options, 'a')) {
751                         /* Append mode */
752                         if (!(ff = fopen(args.filename, "a"))) {
753                                 ast_log(LOG_WARNING, "Cannot open file '%s' for appending: %s\n", args.filename, strerror(errno));
754                                 return 0;
755                         }
756                         if (fwrite(value, 1, vlength, ff) < vlength) {
757                                 ast_log(LOG_ERROR, "Short write?!!\n");
758                         }
759                         fclose(ff);
760                         return 0;
761                 } else if (offset == 0 && length == LLONG_MAX) {
762                         if (!(ff = fopen(args.filename, "w"))) {
763                                 ast_log(LOG_WARNING, "Cannot open file '%s' for writing: %s\n", args.filename, strerror(errno));
764                                 return 0;
765                         }
766                         if (fwrite(value, 1, vlength, ff) < vlength) {
767                                 ast_log(LOG_ERROR, "Short write?!!\n");
768                         }
769                         fclose(ff);
770                         return 0;
771                 }
772
773                 if (!(ff = fopen(args.filename, "r+"))) {
774                         ast_log(LOG_WARNING, "Cannot open file '%s' for modification: %s\n", args.filename, strerror(errno));
775                         return 0;
776                 }
777                 fseeko(ff, 0, SEEK_END);
778                 flength = ftello(ff);
779
780                 if (offset < 0) {
781                         if (fseeko(ff, offset, SEEK_END)) {
782                                 ast_log(LOG_ERROR, "Cannot seek to offset: %s\n", strerror(errno));
783                                 fclose(ff);
784                                 return -1;
785                         }
786                         offset = ftello(ff);
787                 }
788
789                 if (length < 0) {
790                         length = flength - offset + length;
791                         if (length < 0) {
792                                 ast_log(LOG_ERROR, "Length '%s' exceeds the file length.  No data will be written.\n", args.length);
793                                 fclose(ff);
794                                 return -1;
795                         }
796                 }
797
798                 fseeko(ff, offset, SEEK_SET);
799
800                 ast_debug(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 ", vlength=%" PRId64 ", flength=%" PRId64 "\n",
801                         S_OR(args.offset, "(null)"), offset, S_OR(args.length, "(null)"), length, vlength, flength);
802
803                 if (length == vlength) {
804                         /* Simplest case, a straight replace */
805                         if (fwrite(value, 1, vlength, ff) < vlength) {
806                                 ast_log(LOG_ERROR, "Short write?!!\n");
807                         }
808                         fclose(ff);
809                 } else if (length == LLONG_MAX) {
810                         /* Simple truncation */
811                         if (fwrite(value, 1, vlength, ff) < vlength) {
812                                 ast_log(LOG_ERROR, "Short write?!!\n");
813                         }
814                         fclose(ff);
815                         if (truncate(args.filename, offset + vlength)) {
816                                 ast_log(LOG_ERROR, "Unable to truncate the file: %s\n", strerror(errno));
817                         }
818                 } else if (length > vlength) {
819                         /* More complex -- need to close a gap */
820                         char fbuf[4096];
821                         off_t cur;
822                         if (fwrite(value, 1, vlength, ff) < vlength) {
823                                 ast_log(LOG_ERROR, "Short write?!!\n");
824                         }
825                         fseeko(ff, length - vlength, SEEK_CUR);
826                         while ((cur = ftello(ff)) < flength) {
827                                 if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
828                                         ast_log(LOG_ERROR, "Short read?!!\n");
829                                 }
830                                 fseeko(ff, cur + vlength - length, SEEK_SET);
831                                 if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
832                                         ast_log(LOG_ERROR, "Short write?!!\n");
833                                 }
834                                 /* Seek to where we stopped reading */
835                                 if (fseeko(ff, cur + sizeof(fbuf), SEEK_SET) < 0) {
836                                         /* Only reason for seek to fail is EOF */
837                                         break;
838                                 }
839                         }
840                         fclose(ff);
841                         if (truncate(args.filename, flength - (length - vlength))) {
842                                 ast_log(LOG_ERROR, "Unable to truncate the file: %s\n", strerror(errno));
843                         }
844                 } else {
845                         /* Most complex -- need to open a gap */
846                         char fbuf[4096];
847                         off_t lastwritten = flength + vlength - length;
848
849                         /* Start reading exactly the buffer size back from the end. */
850                         fseeko(ff, flength - sizeof(fbuf), SEEK_SET);
851                         while (offset < ftello(ff)) {
852                                 if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
853                                         ast_log(LOG_ERROR, "Short read?!!\n");
854                                         fclose(ff);
855                                         return -1;
856                                 }
857                                 /* Since the read moved our file ptr forward, we reverse, but
858                                  * seek an offset equal to the amount we want to extend the
859                                  * file by */
860                                 fseeko(ff, vlength - length - sizeof(fbuf), SEEK_CUR);
861
862                                 /* Note the location of this buffer -- we must not overwrite this position. */
863                                 lastwritten = ftello(ff);
864
865                                 if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
866                                         ast_log(LOG_ERROR, "Short write?!!\n");
867                                         fclose(ff);
868                                         return -1;
869                                 }
870
871                                 if (lastwritten < offset + sizeof(fbuf)) {
872                                         break;
873                                 }
874                                 /* Our file pointer is now either pointing to the end of the
875                                  * file (new position) or a multiple of the fbuf size back from
876                                  * that point.  Move back to where we want to start reading
877                                  * again.  We never actually try to read beyond the end of the
878                                  * file, so we don't have do deal with short reads, as we would
879                                  * when we're shortening the file. */
880                                 fseeko(ff, 2 * sizeof(fbuf) + vlength - length, SEEK_CUR);
881                         }
882
883                         /* Last part of the file that we need to preserve */
884                         if (fseeko(ff, offset + length, SEEK_SET)) {
885                                 ast_log(LOG_WARNING, "Unable to seek to %" PRId64 " + %" PRId64 " != %" PRId64 "?)\n", offset, length, ftello(ff));
886                         }
887
888                         /* Doesn't matter how much we read -- just need to restrict the write */
889                         ast_debug(1, "Reading at %" PRId64 "\n", ftello(ff));
890                         if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
891                                 ast_log(LOG_ERROR, "Short read?!!\n");
892                         }
893                         fseek(ff, offset, SEEK_SET);
894                         /* Write out the value, then write just up until where we last moved some data */
895                         if (fwrite(value, 1, vlength, ff) < vlength) {
896                                 ast_log(LOG_ERROR, "Short write?!!\n");
897                         } else {
898                                 off_t curpos = ftello(ff);
899                                 foplen = lastwritten - curpos;
900                                 if (fwrite(fbuf, 1, foplen, ff) < foplen) {
901                                         ast_log(LOG_ERROR, "Short write?!!\n");
902                                 }
903                         }
904                         fclose(ff);
905                 }
906         } else {
907                 enum file_format newline_format = FF_UNKNOWN;
908
909                 /* Line mode */
910                 if (args.argc == 5) {
911                         if (tolower(args.format[0]) == 'u') {
912                                 newline_format = FF_UNIX;
913                         } else if (tolower(args.format[0]) == 'm') {
914                                 newline_format = FF_MAC;
915                         } else if (tolower(args.format[0]) == 'd') {
916                                 newline_format = FF_DOS;
917                         }
918                 }
919                 if (newline_format == FF_UNKNOWN && (newline_format = file2format(args.filename)) == FF_UNKNOWN) {
920                         ast_log(LOG_ERROR, "File '%s' not in line format\n", args.filename);
921                         return -1;
922                 }
923
924                 if (strchr(args.options, 'a')) {
925                         /* Append to file */
926                         if (!(ff = fopen(args.filename, "a"))) {
927                                 ast_log(LOG_ERROR, "Unable to open '%s' for appending: %s\n", args.filename, strerror(errno));
928                                 return -1;
929                         }
930                         if (fwrite(value, 1, vlength, ff) < vlength) {
931                                 ast_log(LOG_ERROR, "Short write?!!\n");
932                         } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
933                                 ast_log(LOG_ERROR, "Short write?!!\n");
934                         }
935                         fclose(ff);
936                 } else if (offset == 0 && length == LLONG_MAX) {
937                         /* Overwrite file */
938                         off_t truncsize;
939                         if (!(ff = fopen(args.filename, "w"))) {
940                                 ast_log(LOG_ERROR, "Unable to open '%s' for writing: %s\n", args.filename, strerror(errno));
941                                 return -1;
942                         }
943                         if (fwrite(value, 1, vlength, ff) < vlength) {
944                                 ast_log(LOG_ERROR, "Short write?!!\n");
945                         } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
946                                 ast_log(LOG_ERROR, "Short write?!!\n");
947                         }
948                         truncsize = ftello(ff);
949                         fclose(ff);
950                         if (truncate(args.filename, truncsize)) {
951                                 ast_log(LOG_ERROR, "Unable to truncate file: %s\n", strerror(errno));
952                         }
953                 } else {
954                         int64_t offset_offset = (offset == 0 ? 0 : -1), length_offset = -1, flength, i, current_length = 0;
955                         char dos_state = 0, fbuf[4096];
956
957                         if (offset < 0 && length < offset) {
958                                 /* Nonsense! */
959                                 ast_log(LOG_ERROR, "Length cannot specify a position prior to the offset\n");
960                                 return -1;
961                         }
962
963                         if (!(ff = fopen(args.filename, "r+"))) {
964                                 ast_log(LOG_ERROR, "Cannot open '%s' for modification: %s\n", args.filename, strerror(errno));
965                                 return -1;
966                         }
967
968                         if (fseek(ff, 0, SEEK_END)) {
969                                 ast_log(LOG_ERROR, "Cannot seek to end of file '%s': %s\n", args.filename, strerror(errno));
970                                 fclose(ff);
971                                 return -1;
972                         }
973                         flength = ftello(ff);
974
975                         /* For negative offset and/or negative length */
976                         if (offset < 0 || length < 0) {
977                                 int64_t count = 0;
978                                 for (i = (flength / sizeof(fbuf)) * sizeof(fbuf); i >= 0; i -= sizeof(fbuf)) {
979                                         char *pos;
980                                         if (fseeko(ff, i, SEEK_SET)) {
981                                                 ast_log(LOG_ERROR, "Cannot seek to offset %" PRId64 ": %s\n", i, strerror(errno));
982                                         }
983                                         if (i + sizeof(fbuf) >= flength) {
984                                                 memset(fbuf, 0, sizeof(fbuf));
985                                         }
986                                         if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
987                                                 ast_log(LOG_ERROR, "Short read: %s\n", strerror(errno));
988                                                 fclose(ff);
989                                                 return -1;
990                                         }
991                                         for (pos = fbuf + sizeof(fbuf) - 1; pos > fbuf - 1; pos--) {
992                                                 LINE_COUNTER(pos, newline_format, count);
993
994                                                 if (length < 0 && count * -1 == length) {
995                                                         length_offset = i + (pos - fbuf);
996                                                 } else if (offset < 0 && count * -1 == (offset - 1)) {
997                                                         /* Found our initial offset.  We're done with reverse motion! */
998                                                         if (newline_format == FF_DOS) {
999                                                                 offset_offset = i + (pos - fbuf) + 2;
1000                                                         } else {
1001                                                                 offset_offset = i + (pos - fbuf) + 1;
1002                                                         }
1003                                                         break;
1004                                                 }
1005                                         }
1006                                         if ((offset < 0 && offset_offset >= 0) || (offset >= 0 && length_offset >= 0)) {
1007                                                 break;
1008                                         }
1009                                 }
1010                                 /* We're at the beginning, and the negative offset indicates the exact number of lines in the file */
1011                                 if (offset < 0 && offset_offset < 0 && offset == count * -1) {
1012                                         offset_offset = 0;
1013                                 }
1014                         }
1015
1016                         /* Positve line offset */
1017                         if (offset > 0) {
1018                                 int64_t count = 0;
1019                                 fseek(ff, 0, SEEK_SET);
1020                                 for (i = 0; i < flength; i += sizeof(fbuf)) {
1021                                         char *pos;
1022                                         if (i + sizeof(fbuf) >= flength) {
1023                                                 memset(fbuf, 0, sizeof(fbuf));
1024                                         }
1025                                         if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
1026                                                 ast_log(LOG_ERROR, "Short read?!!\n");
1027                                                 fclose(ff);
1028                                                 return -1;
1029                                         }
1030                                         for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
1031                                                 LINE_COUNTER(pos, newline_format, count);
1032
1033                                                 if (count == offset) {
1034                                                         offset_offset = i + (pos - fbuf) + 1;
1035                                                         break;
1036                                                 }
1037                                         }
1038                                         if (offset_offset >= 0) {
1039                                                 break;
1040                                         }
1041                                 }
1042                         }
1043
1044                         if (offset_offset < 0) {
1045                                 ast_log(LOG_ERROR, "Offset '%s' refers to before the beginning of the file!\n", args.offset);
1046                                 fclose(ff);
1047                                 return -1;
1048                         }
1049
1050                         if (length == 0) {
1051                                 length_offset = offset_offset;
1052                         } else if (length == LLONG_MAX) {
1053                                 length_offset = flength;
1054                         }
1055
1056                         /* Positive line length */
1057                         if (length_offset < 0) {
1058                                 fseeko(ff, offset_offset, SEEK_SET);
1059                                 for (i = offset_offset; i < flength; i += sizeof(fbuf)) {
1060                                         char *pos;
1061                                         if (i + sizeof(fbuf) >= flength) {
1062                                                 memset(fbuf, 0, sizeof(fbuf));
1063                                         }
1064                                         if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
1065                                                 ast_log(LOG_ERROR, "Short read?!!\n");
1066                                                 fclose(ff);
1067                                                 return -1;
1068                                         }
1069                                         for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
1070                                                 LINE_COUNTER(pos, newline_format, current_length);
1071
1072                                                 if (current_length == length) {
1073                                                         length_offset = i + (pos - fbuf) + 1;
1074                                                         break;
1075                                                 }
1076                                         }
1077                                         if (length_offset >= 0) {
1078                                                 break;
1079                                         }
1080                                 }
1081                                 if (length_offset < 0) {
1082                                         /* Exceeds length of file */
1083                                         ast_debug(3, "Exceeds length of file? length=%" PRId64 ", count=%" PRId64 ", flength=%" PRId64 "\n", length, current_length, flength);
1084                                         length_offset = flength;
1085                                 }
1086                         }
1087
1088                         /* Have offset_offset and length_offset now */
1089                         if (length_offset - offset_offset == vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)))) {
1090                                 /* Simple case - replacement of text inline */
1091                                 fseeko(ff, offset_offset, SEEK_SET);
1092                                 if (fwrite(value, 1, vlength, ff) < vlength) {
1093                                         ast_log(LOG_ERROR, "Short write?!!\n");
1094                                 } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
1095                                         ast_log(LOG_ERROR, "Short write?!!\n");
1096                                 }
1097                                 fclose(ff);
1098                         } else if (length_offset - offset_offset > vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)))) {
1099                                 /* More complex case - need to shorten file */
1100                                 off_t cur;
1101                                 int64_t length_length = length_offset - offset_offset;
1102                                 size_t vlen = vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)));
1103
1104                                 ast_debug(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 " (%" PRId64 "), vlength=%" PRId64 ", flength=%" PRId64 "\n",
1105                                         args.offset, offset_offset, args.length, length_offset, length_length, vlength, flength);
1106
1107                                 fseeko(ff, offset_offset, SEEK_SET);
1108                                 if (fwrite(value, 1, vlength, ff) < vlength) {
1109                                         ast_log(LOG_ERROR, "Short write?!!\n");
1110                                         fclose(ff);
1111                                         return -1;
1112                                 } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, vlen - vlength, ff) < vlen - vlength) {
1113                                         ast_log(LOG_ERROR, "Short write?!!\n");
1114                                         fclose(ff);
1115                                         return -1;
1116                                 }
1117                                 while ((cur = ftello(ff)) < flength) {
1118                                         fseeko(ff, length_length - vlen, SEEK_CUR);
1119                                         if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
1120                                                 ast_log(LOG_ERROR, "Short read?!!\n");
1121                                                 fclose(ff);
1122                                                 return -1;
1123                                         }
1124                                         /* Seek to where we last stopped writing */
1125                                         fseeko(ff, cur, SEEK_SET);
1126                                         if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
1127                                                 ast_log(LOG_ERROR, "Short write?!!\n");
1128                                                 fclose(ff);
1129                                                 return -1;
1130                                         }
1131                                 }
1132                                 fclose(ff);
1133                                 if (truncate(args.filename, flength - (length_length - vlen))) {
1134                                         ast_log(LOG_ERROR, "Truncation of file failed: %s\n", strerror(errno));
1135                                 }
1136                         } else {
1137                                 /* Most complex case - need to lengthen file */
1138                                 size_t vlen = vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)));
1139                                 int64_t origlen = length_offset - offset_offset;
1140                                 off_t lastwritten = flength + vlen - origlen;
1141
1142                                 ast_debug(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 ", vlength=%" PRId64 ", flength=%" PRId64 "\n",
1143                                         args.offset, offset_offset, args.length, length_offset, vlength, flength);
1144
1145                                 fseeko(ff, flength - sizeof(fbuf), SEEK_SET);
1146                                 while (offset_offset + sizeof(fbuf) < ftello(ff)) {
1147                                         if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
1148                                                 ast_log(LOG_ERROR, "Short read?!!\n");
1149                                                 fclose(ff);
1150                                                 return -1;
1151                                         }
1152                                         fseeko(ff, sizeof(fbuf) - vlen - origlen, SEEK_CUR);
1153                                         if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
1154                                                 ast_log(LOG_ERROR, "Short write?!!\n");
1155                                                 fclose(ff);
1156                                                 return -1;
1157                                         }
1158                                         if ((lastwritten = ftello(ff) - sizeof(fbuf)) < offset_offset + sizeof(fbuf)) {
1159                                                 break;
1160                                         }
1161                                         fseeko(ff, 2 * sizeof(fbuf) + vlen - origlen, SEEK_CUR);
1162                                 }
1163                                 fseek(ff, length_offset, SEEK_SET);
1164                                 if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
1165                                         ast_log(LOG_ERROR, "Short read?!!\n");
1166                                         fclose(ff);
1167                                         return -1;
1168                                 }
1169                                 fseek(ff, offset_offset, SEEK_SET);
1170                                 if (fwrite(value, 1, vlength, ff) < vlength) {
1171                                         ast_log(LOG_ERROR, "Short write?!!\n");
1172                                         fclose(ff);
1173                                         return -1;
1174                                 } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
1175                                         ast_log(LOG_ERROR, "Short write?!!\n");
1176                                         fclose(ff);
1177                                         return -1;
1178                                 } else {
1179                                         off_t curpos = ftello(ff);
1180                                         foplen = lastwritten - curpos;
1181                                         if (fwrite(fbuf, 1, foplen, ff) < foplen) {
1182                                                 ast_log(LOG_ERROR, "Short write?!!\n");
1183                                         }
1184                                 }
1185                                 fclose(ff);
1186                         }
1187                 }
1188         }
1189
1190         return 0;
1191 }
1192
1193 static struct ast_custom_function env_function = {
1194         .name = "ENV",
1195         .read = env_read,
1196         .write = env_write
1197 };
1198
1199 static struct ast_custom_function stat_function = {
1200         .name = "STAT",
1201         .read = stat_read,
1202         .read_max = 12,
1203 };
1204
1205 static struct ast_custom_function file_function = {
1206         .name = "FILE",
1207         .read2 = file_read,
1208         .write = file_write,
1209 };
1210
1211 static struct ast_custom_function file_count_line_function = {
1212         .name = "FILE_COUNT_LINE",
1213         .read2 = file_count_line,
1214         .read_max = 12,
1215 };
1216
1217 static struct ast_custom_function file_format_function = {
1218         .name = "FILE_FORMAT",
1219         .read2 = file_format,
1220         .read_max = 2,
1221 };
1222
1223 static int unload_module(void)
1224 {
1225         int res = 0;
1226
1227         res |= ast_custom_function_unregister(&env_function);
1228         res |= ast_custom_function_unregister(&stat_function);
1229         res |= ast_custom_function_unregister(&file_function);
1230         res |= ast_custom_function_unregister(&file_count_line_function);
1231         res |= ast_custom_function_unregister(&file_format_function);
1232
1233         return res;
1234 }
1235
1236 static int load_module(void)
1237 {
1238         int res = 0;
1239
1240         res |= ast_custom_function_register(&env_function);
1241         res |= ast_custom_function_register(&stat_function);
1242         res |= ast_custom_function_register(&file_function);
1243         res |= ast_custom_function_register(&file_count_line_function);
1244         res |= ast_custom_function_register(&file_format_function);
1245
1246         return res;
1247 }
1248
1249 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Environment/filesystem dialplan functions");