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