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