Asterisk - The Open Source Telephony Project GIT-master-8924258
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
func_env.c
Go to the documentation of this file.
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#include <sys/stat.h> /* stat(2) */
31#include <libgen.h> /* dirname and basename */
32
33#include "asterisk/module.h"
34#include "asterisk/channel.h"
35#include "asterisk/pbx.h"
36#include "asterisk/utils.h"
37#include "asterisk/app.h"
38#include "asterisk/file.h"
39
40/*** DOCUMENTATION
41 <function name="ENV" language="en_US">
42 <since>
43 <version>1.2.0</version>
44 </since>
45 <synopsis>
46 Gets or sets the environment variable specified.
47 </synopsis>
48 <syntax>
49 <parameter name="varname" required="true">
50 <para>Environment variable name</para>
51 </parameter>
52 </syntax>
53 <description>
54 <para>Variables starting with <literal>AST_</literal> are reserved to the system and may not be set.</para>
55 <para>Additionally, the following system variables are available as special built-in dialplan variables.
56 These variables cannot be set or modified and are read-only.</para>
57 <variablelist>
58 <variable name="EPOCH">
59 <para>Current unix style epoch</para>
60 </variable>
61 <variable name="SYSTEMNAME">
62 <para>value of the <literal>systemname</literal> option from <literal>asterisk.conf</literal></para>
63 </variable>
64 <variable name="ASTCACHEDIR">
65 <para>value of the <literal>astcachedir</literal> option from <literal>asterisk.conf</literal></para>
66 </variable>
67 <variable name="ASTETCDIR">
68 <para>value of the <literal>astetcdir</literal> option from <literal>asterisk.conf</literal></para>
69 </variable>
70 <variable name="ASTMODDIR">
71 <para>value of the <literal>astmoddir</literal> option from <literal>asterisk.conf</literal></para>
72 </variable>
73 <variable name="ASTVARLIBDIR">
74 <para>value of the <literal>astvarlib</literal> option from <literal>asterisk.conf</literal></para>
75 </variable>
76 <variable name="ASTDBDIR">
77 <para>value of the <literal>astdbdir</literal> option from <literal>asterisk.conf</literal></para>
78 </variable>
79 <variable name="ASTKEYDIR">
80 <para>value of the <literal>astkeydir</literal> option from <literal>asterisk.conf</literal></para>
81 </variable>
82 <variable name="ASTDATADIR">
83 <para>value of the <literal>astdatadir</literal> option from <literal>asterisk.conf</literal></para>
84 </variable>
85 <variable name="ASTAGIDIR">
86 <para>value of the <literal>astagidir</literal> option from <literal>asterisk.conf</literal></para>
87 </variable>
88 <variable name="ASTSPOOLDIR">
89 <para>value of the <literal>astspooldir</literal> option from <literal>asterisk.conf</literal></para>
90 </variable>
91 <variable name="ASTRUNDIR">
92 <para>value of the <literal>astrundir</literal> option from <literal>asterisk.conf</literal></para>
93 </variable>
94 <variable name="ASTLOGDIR">
95 <para>value of the <literal>astlogdir</literal> option from <literal>asterisk.conf</literal></para>
96 </variable>
97 <variable name="ASTSBINDIR">
98 <para>value of the <literal>astsbindir</literal> option from <literal>asterisk.conf</literal></para>
99 </variable>
100 <variable name="ENTITYID">
101 <para>Global Entity ID set automatically, or from <literal>asterisk.conf</literal></para>
102 </variable>
103 </variablelist>
104 </description>
105 </function>
106 <function name="STAT" language="en_US">
107 <since>
108 <version>1.4.0</version>
109 </since>
110 <synopsis>
111 Does a check on the specified file.
112 </synopsis>
113 <syntax>
114 <parameter name="flag" required="true">
115 <para>Flag may be one of the following:</para>
116 <para>d - Checks if the file is a directory.</para>
117 <para>e - Checks if the file exists.</para>
118 <para>f - Checks if the file is a regular file.</para>
119 <para>m - Returns the file mode (in octal)</para>
120 <para>s - Returns the size (in bytes) of the file</para>
121 <para>A - Returns the epoch at which the file was last accessed.</para>
122 <para>C - Returns the epoch at which the inode was last changed.</para>
123 <para>M - Returns the epoch at which the file was last modified.</para>
124 </parameter>
125 <parameter name="filename" required="true" />
126 </syntax>
127 <description>
128 <note>
129 <para>If <literal>live_dangerously</literal> in <literal>asterisk.conf</literal>
130 is set to <literal>no</literal>, this function can only be executed from the
131 dialplan, and not directly from external protocols.</para>
132 </note>
133 </description>
134 </function>
135 <function name="FILE" language="en_US">
136 <since>
137 <version>1.6.0</version>
138 </since>
139 <synopsis>
140 Read or write text file.
141 </synopsis>
142 <syntax>
143 <parameter name="filename" required="true" />
144 <parameter name="offset">
145 <para>Maybe specified as any number. If negative, <replaceable>offset</replaceable> specifies the number
146 of bytes back from the end of the file.</para>
147 </parameter>
148 <parameter name="length">
149 <para>If specified, will limit the length of the data read to that size. If negative,
150 trims <replaceable>length</replaceable> bytes from the end of the file.</para>
151 </parameter>
152 <parameter name="options">
153 <optionlist>
154 <option name="l">
155 <para>Line mode: offset and length are assumed to be
156 measured in lines, instead of byte offsets.</para>
157 </option>
158 <option name="a">
159 <para>In write mode only, the append option is used to
160 append to the end of the file, instead of overwriting
161 the existing file.</para>
162 </option>
163 <option name="d">
164 <para>In write mode and line mode only, this option does
165 not automatically append a newline string to the end of
166 a value. This is useful for deleting lines, instead of
167 setting them to blank.</para>
168 </option>
169 </optionlist>
170 </parameter>
171 <parameter name="format">
172 <para>The <replaceable>format</replaceable> parameter may be
173 used to delimit the type of line terminators in line mode.</para>
174 <optionlist>
175 <option name="u">
176 <para>Unix newline format.</para>
177 </option>
178 <option name="d">
179 <para>DOS newline format.</para>
180 </option>
181 <option name="m">
182 <para>Macintosh newline format.</para>
183 </option>
184 </optionlist>
185 </parameter>
186 </syntax>
187 <description>
188 <para>Read and write text file in character and line mode.</para>
189 <para>Examples:</para>
190 <para>Read mode (byte):</para>
191 <example title="Reads the entire content of the file">
192 same => n,Set(foo=${FILE(/tmp/test.txt)})
193 </example>
194 <example title="Reads from the 11th byte to the end of the file (i.e. skips the first 10)">
195 same => n,Set(foo=${FILE(/tmp/test.txt,10)})
196 </example>
197 <example title="Reads from the 11th to 20th byte in the file (i.e. skip the first 10, then read 10 bytes)">
198 same => n,Set(foo=${FILE(/tmp/test.txt,10,10)})
199 </example>
200 <para>Read mode (line):</para>
201 <example title="Reads the 3rd line of the file">
202 same => n,Set(foo=${FILE(/tmp/test.txt,3,1,l)})
203 </example>
204 <example title="Reads the 3rd and 4th lines of the file">
205 same => n,Set(foo=${FILE(/tmp/test.txt,3,2,l)})
206 </example>
207 <example title="Reads from the third line to the end of the file">
208 same => n,Set(foo=${FILE(/tmp/test.txt,3,,l)})
209 </example>
210 <example title="Reads the last three lines of the file">
211 same => n,Set(foo=${FILE(/tmp/test.txt,-3,,l)})
212 </example>
213 <example title="Reads the 3rd line of a DOS-formatted file">
214 same => n,Set(foo=${FILE(/tmp/test.txt,3,1,l,d)})
215 </example>
216 <para>Write mode (byte):</para>
217 <example title="Truncate the file and write bar">
218 same => n,Set(FILE(/tmp/test.txt)=bar)
219 </example>
220 <example title="Append bar">
221 same => n,Set(FILE(/tmp/test.txt,,,a)=bar)
222 </example>
223 <example title="Replace the first byte with bar (replaces 1 character with 3)">
224 same => n,Set(FILE(/tmp/test.txt,0,1)=bar)
225 </example>
226 <example title="Replace 10 bytes beginning at the 21st byte of the file with bar">
227 same => n,Set(FILE(/tmp/test.txt,20,10)=bar)
228 </example>
229 <example title="Replace all bytes from the 21st with bar">
230 same => n,Set(FILE(/tmp/test.txt,20)=bar)
231 </example>
232 <example title="Insert bar after the 4th character">
233 same => n,Set(FILE(/tmp/test.txt,4,0)=bar)
234 </example>
235 <para>Write mode (line):</para>
236 <example title="Replace the first line of the file with bar">
237 same => n,Set(FILE(/tmp/foo.txt,0,1,l)=bar)
238 </example>
239 <example title="Replace the last line of the file with bar">
240 same => n,Set(FILE(/tmp/foo.txt,-1,,l)=bar)
241 </example>
242 <example title="Append bar to the file with a newline">
243 same => n,Set(FILE(/tmp/foo.txt,,,al)=bar)
244 </example>
245 <note>
246 <para>If <literal>live_dangerously</literal> in <literal>asterisk.conf</literal>
247 is set to <literal>no</literal>, this function can only be executed from the
248 dialplan, and not directly from external protocols.</para>
249 </note>
250 </description>
251 <see-also>
252 <ref type="function">FILE_COUNT_LINE</ref>
253 <ref type="function">FILE_FORMAT</ref>
254 </see-also>
255 </function>
256 <function name="FILE_COUNT_LINE" language="en_US">
257 <since>
258 <version>1.8.0</version>
259 </since>
260 <synopsis>
261 Obtains the number of lines of a text file.
262 </synopsis>
263 <syntax>
264 <parameter name="filename" required="true" />
265 <parameter name="format">
266 <para>Format may be one of the following:</para>
267 <optionlist>
268 <option name="u">
269 <para>Unix newline format.</para>
270 </option>
271 <option name="d">
272 <para>DOS newline format.</para>
273 </option>
274 <option name="m">
275 <para>Macintosh newline format.</para>
276 </option>
277 </optionlist>
278 <note><para>If not specified, an attempt will be made to determine the newline format type.</para></note>
279 </parameter>
280 </syntax>
281 <description>
282 <para>Returns the number of lines, or <literal>-1</literal> on error.</para>
283 <note>
284 <para>If <literal>live_dangerously</literal> in <literal>asterisk.conf</literal>
285 is set to <literal>no</literal>, this function can only be executed from the
286 dialplan, and not directly from external protocols.</para>
287 </note>
288 </description>
289 <see-also>
290 <ref type="function">FILE</ref>
291 <ref type="function">FILE_FORMAT</ref>
292 </see-also>
293 </function>
294 <function name="FILE_FORMAT" language="en_US">
295 <since>
296 <version>1.8.0</version>
297 </since>
298 <synopsis>
299 Return the newline format of a text file.
300 </synopsis>
301 <syntax>
302 <parameter name="filename" required="true" />
303 </syntax>
304 <description>
305 <para>Return the line terminator type:</para>
306 <para>'u' - Unix "\n" format</para>
307 <para>'d' - DOS "\r\n" format</para>
308 <para>'m' - Macintosh "\r" format</para>
309 <para>'x' - Cannot be determined</para>
310 <note>
311 <para>If <literal>live_dangerously</literal> in <literal>asterisk.conf</literal>
312 is set to <literal>no</literal>, this function can only be executed from the
313 dialplan, and not directly from external protocols.</para>
314 </note>
315 </description>
316 <see-also>
317 <ref type="function">FILE</ref>
318 <ref type="function">FILE_COUNT_LINE</ref>
319 </see-also>
320 </function>
321 <function name="BASENAME" language="en_US">
322 <since>
323 <version>16.21.0</version>
324 <version>18.7.0</version>
325 </since>
326 <synopsis>
327 Return the name of a file.
328 </synopsis>
329 <syntax>
330 <parameter name="filename" required="true" />
331 </syntax>
332 <description>
333 <para>Return the base file name, given a full file path.</para>
334 <example title="Directory name">
335 same => n,Set(basename=${BASENAME(/etc/asterisk/extensions.conf)})
336 same => n,NoOp(${basename}) ; outputs extensions.conf
337 </example>
338 </description>
339 <see-also>
340 <ref type="function">DIRNAME</ref>
341 </see-also>
342 </function>
343 <function name="DIRNAME" language="en_US">
344 <since>
345 <version>16.21.0</version>
346 <version>18.7.0</version>
347 </since>
348 <synopsis>
349 Return the directory of a file.
350 </synopsis>
351 <syntax>
352 <parameter name="filename" required="true" />
353 </syntax>
354 <description>
355 <para>Return the directory of a file, given a full file path.</para>
356 <example title="Directory name">
357 same => n,Set(dirname=${DIRNAME(/etc/asterisk/extensions.conf)})
358 same => n,NoOp(${dirname}) ; outputs /etc/asterisk
359 </example>
360 </description>
361 <see-also>
362 <ref type="function">BASENAME</ref>
363 </see-also>
364 </function>
365 ***/
366
367static int env_read(struct ast_channel *chan, const char *cmd, char *data,
368 char *buf, size_t len)
369{
370 char *ret = NULL;
371
372 *buf = '\0';
373
374 if (data)
375 ret = getenv(data);
376
377 if (ret)
378 ast_copy_string(buf, ret, len);
379
380 return 0;
381}
382
383static int env_write(struct ast_channel *chan, const char *cmd, char *data,
384 const char *value)
385{
386 if (!ast_strlen_zero(data) && strncmp(data, "AST_", 4)) {
387 if (!ast_strlen_zero(value)) {
388 setenv(data, value, 1);
389 } else {
390 unsetenv(data);
391 }
392 }
393
394 return 0;
395}
396
397static int stat_read(struct ast_channel *chan, const char *cmd, char *data,
398 char *buf, size_t len)
399{
400 char *action;
401 struct stat s;
402
403 ast_copy_string(buf, "0", len);
404
405 action = strsep(&data, ",");
406 if (stat(data, &s)) {
407 return 0;
408 } else {
409 switch (*action) {
410 case 'e':
411 strcpy(buf, "1");
412 break;
413 case 's':
414 snprintf(buf, len, "%u", (unsigned int) s.st_size);
415 break;
416 case 'f':
417 snprintf(buf, len, "%d", S_ISREG(s.st_mode) ? 1 : 0);
418 break;
419 case 'd':
420 snprintf(buf, len, "%d", S_ISDIR(s.st_mode) ? 1 : 0);
421 break;
422 case 'M':
423 snprintf(buf, len, "%d", (int) s.st_mtime);
424 break;
425 case 'A':
426 snprintf(buf, len, "%d", (int) s.st_mtime);
427 break;
428 case 'C':
429 snprintf(buf, len, "%d", (int) s.st_ctime);
430 break;
431 case 'm':
432 snprintf(buf, len, "%o", (unsigned int) s.st_mode);
433 break;
434 }
435 }
436
437 return 0;
438}
439
445};
446
447static int64_t count_lines(const char *filename, enum file_format newline_format)
448{
449 int count = 0;
450 char fbuf[4096];
451 FILE *ff;
452
453 if (!(ff = fopen(filename, "r"))) {
454 ast_log(LOG_ERROR, "Unable to open '%s': %s\n", filename, strerror(errno));
455 return -1;
456 }
457
458 while (fgets(fbuf, sizeof(fbuf), ff)) {
459 char *next = fbuf, *first_cr = NULL, *first_nl = NULL;
460
461 /* Must do it this way, because if the fileformat is FF_MAC, then Unix
462 * assumptions about line-format will not come into play. */
463 while (next) {
464 if (newline_format == FF_DOS || newline_format == FF_MAC || newline_format == FF_UNKNOWN) {
465 first_cr = strchr(next, '\r');
466 }
467 if (newline_format == FF_UNIX || newline_format == FF_UNKNOWN) {
468 first_nl = strchr(next, '\n');
469 }
470
471 /* No terminators found in buffer */
472 if (!first_cr && !first_nl) {
473 break;
474 }
475
476 if (newline_format == FF_UNKNOWN) {
477 if ((first_cr && !first_nl) || (first_cr && first_cr < first_nl)) {
478 if (first_nl && first_nl == first_cr + 1) {
479 newline_format = FF_DOS;
480 } else if (first_cr && first_cr == &fbuf[sizeof(fbuf) - 2]) {
481 /* Get it on the next pass */
482 fseek(ff, -1, SEEK_CUR);
483 break;
484 } else {
485 newline_format = FF_MAC;
486 first_nl = NULL;
487 }
488 } else {
489 newline_format = FF_UNIX;
490 first_cr = NULL;
491 }
492 /* Jump down into next section */
493 }
494
495 if (newline_format == FF_DOS) {
496 if (first_nl && first_cr && first_nl == first_cr + 1) {
497 next = first_nl + 1;
498 count++;
499 } else if (first_cr == &fbuf[sizeof(fbuf) - 2]) {
500 /* Get it on the next pass */
501 fseek(ff, -1, SEEK_CUR);
502 break;
503 }
504 } else if (newline_format == FF_MAC) {
505 if (first_cr) {
506 next = first_cr + 1;
507 count++;
508 }
509 } else if (newline_format == FF_UNIX) {
510 if (first_nl) {
511 next = first_nl + 1;
512 count++;
513 }
514 }
515 }
516 }
517 fclose(ff);
518
519 return count;
520}
521
522static int file_count_line(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
523{
524 enum file_format newline_format = FF_UNKNOWN;
525 int64_t count;
527 AST_APP_ARG(filename);
528 AST_APP_ARG(format);
529 );
530
532 if (args.argc > 1) {
533 if (tolower(args.format[0]) == 'd') {
534 newline_format = FF_DOS;
535 } else if (tolower(args.format[0]) == 'm') {
536 newline_format = FF_MAC;
537 } else if (tolower(args.format[0]) == 'u') {
538 newline_format = FF_UNIX;
539 }
540 }
541
542 count = count_lines(args.filename, newline_format);
543 ast_str_set(buf, len, "%" PRId64, count);
544 return 0;
545}
546
547#define LINE_COUNTER(cptr, term, counter) \
548 if (*cptr == '\n' && term == FF_UNIX) { \
549 counter++; \
550 } else if (*cptr == '\n' && term == FF_DOS && dos_state == 0) { \
551 dos_state = 1; \
552 } else if (*cptr == '\r' && term == FF_DOS && dos_state == 1) { \
553 dos_state = 0; \
554 counter++; \
555 } else if (*cptr == '\r' && term == FF_MAC) { \
556 counter++; \
557 } else if (term == FF_DOS) { \
558 dos_state = 0; \
559 }
560
561static enum file_format file2format(const char *filename)
562{
563 FILE *ff;
564 char fbuf[4096];
565 char *first_cr, *first_nl;
566 enum file_format newline_format = FF_UNKNOWN;
567
568 if (!(ff = fopen(filename, "r"))) {
569 ast_log(LOG_ERROR, "Cannot open '%s': %s\n", filename, strerror(errno));
570 return -1;
571 }
572
573 while (fgets(fbuf, sizeof(fbuf), ff)) {
574 first_cr = strchr(fbuf, '\r');
575 first_nl = strchr(fbuf, '\n');
576
577 if (!first_cr && !first_nl) {
578 continue;
579 }
580
581 if ((first_cr && !first_nl) || (first_cr && first_cr < first_nl)) {
582
583 if (first_nl && first_nl == first_cr + 1) {
584 newline_format = FF_DOS;
585 } else if (first_cr && first_cr == &fbuf[sizeof(fbuf) - 2]) {
586 /* Edge case: get it on the next pass */
587 fseek(ff, -1, SEEK_CUR);
588 continue;
589 } else {
590 newline_format = FF_MAC;
591 }
592 } else {
593 newline_format = FF_UNIX;
594 }
595 break;
596 }
597 fclose(ff);
598 return newline_format;
599}
600
601static int file_format(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
602{
603 enum file_format newline_format = file2format(data);
604 ast_str_set(buf, len, "%c", newline_format == FF_UNIX ? 'u' : newline_format == FF_DOS ? 'd' : newline_format == FF_MAC ? 'm' : 'x');
605 return 0;
606}
607
608static int file_dirname(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
609{
610 char *ret = NULL;
611
612 *buf = '\0';
613
614 if (data) {
615 ret = dirname(data);
616 }
617
618 if (ret) {
619 ast_copy_string(buf, ret, len);
620 }
621
622 return 0;
623}
624
625static int file_basename(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
626{
627 char *ret = NULL;
628
629 *buf = '\0';
630
631 if (data) {
632 ret = basename(data);
633 }
634
635 if (ret) {
636 ast_copy_string(buf, ret, len);
637 }
638
639 return 0;
640}
641
642static int file_read(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
643{
644 FILE *ff;
645 int64_t offset = 0, length = LLONG_MAX;
646 enum file_format format = FF_UNKNOWN;
647 char fbuf[4096];
648 int64_t flength, i; /* iterator needs to be signed, so it can go negative and terminate the loop */
649 int64_t offset_offset = -1, length_offset = -1;
650 char dos_state = 0;
652 AST_APP_ARG(filename);
653 AST_APP_ARG(offset);
654 AST_APP_ARG(length);
656 AST_APP_ARG(fileformat);
657 );
658
660
661 if (args.argc > 1) {
662 sscanf(args.offset, "%" SCNd64, &offset);
663 }
664 if (args.argc > 2) {
665 sscanf(args.length, "%" SCNd64, &length);
666 }
667
668 if (args.argc < 4 || !strchr(args.options, 'l')) {
669 /* Character-based mode */
670 off_t off_i;
671
672 if (!(ff = fopen(args.filename, "r"))) {
673 ast_log(LOG_WARNING, "Cannot open file '%s' for reading: %s\n", args.filename, strerror(errno));
674 return 0;
675 }
676
677 if (fseeko(ff, 0, SEEK_END) < 0) {
678 ast_log(LOG_ERROR, "Cannot seek to end of '%s': %s\n", args.filename, strerror(errno));
679 fclose(ff);
680 return -1;
681 }
682 flength = ftello(ff);
683
684 if (offset < 0) {
685 fseeko(ff, offset, SEEK_END);
686 if ((offset = ftello(ff)) < 0) {
687 ast_log(AST_LOG_ERROR, "Cannot determine offset position of '%s': %s\n", args.filename, strerror(errno));
688 fclose(ff);
689 return -1;
690 }
691 }
692 if (length < 0) {
693 fseeko(ff, length, SEEK_END);
694 if ((length = ftello(ff)) - offset < 0) {
695 /* Eliminates all results */
696 fclose(ff);
697 return -1;
698 }
699 } else if (length == LLONG_MAX) {
700 fseeko(ff, 0, SEEK_END);
701 length = ftello(ff);
702 }
703
705
706 fseeko(ff, offset, SEEK_SET);
707 for (off_i = ftello(ff); off_i < flength && off_i < offset + length; off_i += sizeof(fbuf)) {
708 /* Calculate if we need to retrieve just a portion of the file in memory */
709 size_t toappend = sizeof(fbuf);
710
711 if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
712 ast_log(LOG_ERROR, "Short read?!!\n");
713 break;
714 }
715
716 /* Don't go past the length requested */
717 if (off_i + toappend > offset + length) {
718 toappend = MIN(offset + length - off_i, flength - off_i);
719 }
720
721 ast_str_append_substr(buf, len, fbuf, toappend);
722 }
723 fclose(ff);
724 return 0;
725 }
726
727 /* Line-based read */
728 if (args.argc == 5) {
729 if (tolower(args.fileformat[0]) == 'd') {
730 format = FF_DOS;
731 } else if (tolower(args.fileformat[0]) == 'm') {
732 format = FF_MAC;
733 } else if (tolower(args.fileformat[0]) == 'u') {
734 format = FF_UNIX;
735 }
736 }
737
738 if (format == FF_UNKNOWN) {
739 if ((format = file2format(args.filename)) == FF_UNKNOWN) {
740 ast_log(LOG_WARNING, "'%s' is not a line-based file\n", args.filename);
741 return -1;
742 }
743 }
744
745 if (offset < 0 && length <= offset) {
746 /* Length eliminates all content */
747 return -1;
748 } else if (offset == 0) {
749 offset_offset = 0;
750 }
751
752 if (!(ff = fopen(args.filename, "r"))) {
753 ast_log(LOG_ERROR, "Cannot open '%s': %s\n", args.filename, strerror(errno));
754 return -1;
755 }
756
757 if (fseek(ff, 0, SEEK_END)) {
758 ast_log(LOG_ERROR, "Cannot seek to end of file '%s': %s\n", args.filename, strerror(errno));
759 fclose(ff);
760 return -1;
761 }
762
763 flength = ftello(ff);
764
765 if (length == LLONG_MAX) {
766 length_offset = flength;
767 }
768
769 /* For negative offset and/or negative length */
770 if (offset < 0 || length < 0) {
771 int64_t count = 0;
772 /* Start with an even multiple of fbuf, so at the end of reading with a
773 * 0 offset, we don't try to go past the beginning of the file. */
774 for (i = (flength / sizeof(fbuf)) * sizeof(fbuf); i >= 0; i -= sizeof(fbuf)) {
775 size_t end;
776 char *pos;
777 if (fseeko(ff, i, SEEK_SET)) {
778 ast_log(LOG_ERROR, "Cannot seek to offset %" PRId64 ": %s\n", i, strerror(errno));
779 }
780 end = fread(fbuf, 1, sizeof(fbuf), ff);
781 for (pos = (end < sizeof(fbuf) ? fbuf + end - 1 : fbuf + sizeof(fbuf) - 1); pos >= fbuf; pos--) {
782 LINE_COUNTER(pos, format, count);
783
784 if (length < 0 && count * -1 == length) {
785 length_offset = i + (pos - fbuf);
786 } else if (offset < 0 && count * -1 == (offset - 1)) {
787 /* Found our initial offset. We're done with reverse motion! */
788 if (format == FF_DOS) {
789 offset_offset = i + (pos - fbuf) + 2;
790 } else {
791 offset_offset = i + (pos - fbuf) + 1;
792 }
793 break;
794 }
795 }
796 if ((offset < 0 && offset_offset >= 0) || (offset >= 0 && length_offset >= 0)) {
797 break;
798 }
799 }
800 /* We're at the beginning, and the negative offset indicates the exact number of lines in the file */
801 if (offset < 0 && offset_offset < 0 && offset == count * -1) {
802 offset_offset = 0;
803 }
804 }
805
806 /* Positve line offset */
807 if (offset > 0) {
808 int64_t count = 0;
809 fseek(ff, 0, SEEK_SET);
810 for (i = 0; i < flength; i += sizeof(fbuf)) {
811 char *pos;
812 if (i + sizeof(fbuf) <= flength) {
813 /* Don't let previous values influence current counts, due to short reads */
814 memset(fbuf, 0, sizeof(fbuf));
815 }
816 if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
817 ast_log(LOG_ERROR, "Short read?!!\n");
818 fclose(ff);
819 return -1;
820 }
821 for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
822 LINE_COUNTER(pos, format, count);
823
824 if (count == offset) {
825 offset_offset = i + (pos - fbuf) + 1;
826 break;
827 }
828 }
829 if (offset_offset >= 0) {
830 break;
831 }
832 }
833 }
834
835 if (offset_offset < 0) {
836 ast_log(LOG_ERROR, "Offset '%s' refers to before the beginning of the file!\n", args.offset);
837 fclose(ff);
838 return -1;
839 }
840
842 if (fseeko(ff, offset_offset, SEEK_SET)) {
843 ast_log(LOG_ERROR, "fseeko failed: %s\n", strerror(errno));
844 }
845
846 /* If we have both offset_offset and length_offset, then grabbing the
847 * buffer is simply a matter of just retrieving the file and adding it
848 * to buf. Otherwise, we need to run byte-by-byte forward until the
849 * length is complete. */
850 if (length_offset >= 0) {
851 ast_debug(3, "offset=%" PRId64 ", length=%" PRId64 ", offset_offset=%" PRId64 ", length_offset=%" PRId64 "\n", offset, length, offset_offset, length_offset);
852 for (i = offset_offset; i < length_offset; i += sizeof(fbuf)) {
853 if (fread(fbuf, 1, i + sizeof(fbuf) > flength ? flength - i : sizeof(fbuf), ff) < (i + sizeof(fbuf) > flength ? flength - i : sizeof(fbuf))) {
854 ast_log(LOG_ERROR, "Short read?!!\n");
855 }
856 ast_debug(3, "Appending first %" PRId64" bytes of fbuf=%s\n", (int64_t)(i + sizeof(fbuf) > length_offset ? length_offset - i : sizeof(fbuf)), fbuf);
857 ast_str_append_substr(buf, len, fbuf, i + sizeof(fbuf) > length_offset ? length_offset - i : sizeof(fbuf));
858 }
859 } else if (length == 0) {
860 /* Nothing to do */
861 } else {
862 /* Positive line offset */
863 int64_t current_length = 0;
864 char dos_state = 0;
865 ast_debug(3, "offset=%" PRId64 ", length=%" PRId64 ", offset_offset=%" PRId64 ", length_offset=%" PRId64 "\n", offset, length, offset_offset, length_offset);
866 for (i = offset_offset; i < flength; i += sizeof(fbuf)) {
867 char *pos;
868 size_t bytes_read;
869 if ((bytes_read = fread(fbuf, 1, sizeof(fbuf), ff)) < sizeof(fbuf) && !feof(ff)) {
870 ast_log(LOG_ERROR, "Short read?!!\n");
871 fclose(ff);
872 return -1;
873 }
874 for (pos = fbuf; pos < fbuf + bytes_read; pos++) {
875 LINE_COUNTER(pos, format, current_length);
876
877 if (current_length == length) {
878 length_offset = i + (pos - fbuf) + 1;
879 break;
880 }
881 }
882 ast_debug(3, "length_offset=%" PRId64 ", length_offset - i=%" PRId64 "\n", length_offset, length_offset - i);
883 ast_str_append_substr(buf, len, fbuf, (length_offset >= 0) ? length_offset - i : (flength > i + sizeof(fbuf)) ? sizeof(fbuf) : flength - i);
884
885 if (length_offset >= 0) {
886 break;
887 }
888 }
889 }
890
891 fclose(ff);
892 return 0;
893}
894
895const char *format2term(enum file_format f) __attribute__((const));
896const char *format2term(enum file_format f)
897{
898 const char *term[] = { "", "\n", "\r\n", "\r" };
899 return term[f + 1];
900}
901
902static int file_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
903{
905 AST_APP_ARG(filename);
906 AST_APP_ARG(offset);
907 AST_APP_ARG(length);
909 AST_APP_ARG(format);
910 );
911 int64_t offset = 0, length = LLONG_MAX;
912 off_t flength, vlength;
913 size_t foplen = 0;
914 FILE *ff;
915
917
918 if (args.argc > 1) {
919 sscanf(args.offset, "%" SCNd64, &offset);
920 }
921 if (args.argc > 2) {
922 sscanf(args.length, "%" SCNd64, &length);
923 }
924
925 vlength = strlen(value);
926
927 if (args.argc < 4 || !strchr(args.options, 'l')) {
928 /* Character-based mode */
929
930 if (args.argc > 3 && strchr(args.options, 'a')) {
931 /* Append mode */
932 if (!(ff = fopen(args.filename, "a"))) {
933 ast_log(LOG_WARNING, "Cannot open file '%s' for appending: %s\n", args.filename, strerror(errno));
934 return 0;
935 }
936 if (fwrite(value, 1, vlength, ff) < vlength) {
937 ast_log(LOG_ERROR, "Short write?!!\n");
938 }
939 fclose(ff);
940 return 0;
941 } else if (offset == 0 && length == LLONG_MAX) {
942 if (!(ff = fopen(args.filename, "w"))) {
943 ast_log(LOG_WARNING, "Cannot open file '%s' for writing: %s\n", args.filename, strerror(errno));
944 return 0;
945 }
946 if (fwrite(value, 1, vlength, ff) < vlength) {
947 ast_log(LOG_ERROR, "Short write?!!\n");
948 }
949 fclose(ff);
950 return 0;
951 }
952
953 if (!(ff = fopen(args.filename, "r+"))) {
954 ast_log(LOG_WARNING, "Cannot open file '%s' for modification: %s\n", args.filename, strerror(errno));
955 return 0;
956 }
957 fseeko(ff, 0, SEEK_END);
958 flength = ftello(ff);
959
960 if (offset < 0) {
961 if (fseeko(ff, offset, SEEK_END)) {
962 ast_log(LOG_ERROR, "Cannot seek to offset of '%s': %s\n", args.filename, strerror(errno));
963 fclose(ff);
964 return -1;
965 }
966 if ((offset = ftello(ff)) < 0) {
967 ast_log(AST_LOG_ERROR, "Cannot determine offset position of '%s': %s\n", args.filename, strerror(errno));
968 fclose(ff);
969 return -1;
970 }
971 }
972
973 if (length < 0) {
974 length = flength - offset + length;
975 if (length < 0) {
976 ast_log(LOG_ERROR, "Length '%s' exceeds the file length. No data will be written.\n", args.length);
977 fclose(ff);
978 return -1;
979 }
980 }
981
982 fseeko(ff, offset, SEEK_SET);
983
984 ast_debug(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 ", vlength=%" PRId64 ", flength=%" PRId64 "\n",
985 S_OR(args.offset, "(null)"), offset, S_OR(args.length, "(null)"), length, vlength, flength);
986
987 if (length == vlength) {
988 /* Simplest case, a straight replace */
989 if (fwrite(value, 1, vlength, ff) < vlength) {
990 ast_log(LOG_ERROR, "Short write?!!\n");
991 }
992 fclose(ff);
993 } else if (length == LLONG_MAX) {
994 /* Simple truncation */
995 if (fwrite(value, 1, vlength, ff) < vlength) {
996 ast_log(LOG_ERROR, "Short write?!!\n");
997 }
998 fclose(ff);
999 if (truncate(args.filename, offset + vlength)) {
1000 ast_log(LOG_ERROR, "Unable to truncate the file: %s\n", strerror(errno));
1001 }
1002 } else if (length > vlength) {
1003 /* More complex -- need to close a gap */
1004 char fbuf[4096];
1005 off_t cur;
1006 if (fwrite(value, 1, vlength, ff) < vlength) {
1007 ast_log(LOG_ERROR, "Short write?!!\n");
1008 }
1009 fseeko(ff, length - vlength, SEEK_CUR);
1010 while ((cur = ftello(ff)) < flength) {
1011 if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
1012 ast_log(LOG_ERROR, "Short read?!!\n");
1013 }
1014 fseeko(ff, cur + vlength - length, SEEK_SET);
1015 if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
1016 ast_log(LOG_ERROR, "Short write?!!\n");
1017 }
1018 /* Seek to where we stopped reading */
1019 if (fseeko(ff, cur + sizeof(fbuf), SEEK_SET) < 0) {
1020 /* Only reason for seek to fail is EOF */
1021 break;
1022 }
1023 }
1024 fclose(ff);
1025 if (truncate(args.filename, flength - (length - vlength))) {
1026 ast_log(LOG_ERROR, "Unable to truncate the file: %s\n", strerror(errno));
1027 }
1028 } else {
1029 /* Most complex -- need to open a gap */
1030 char fbuf[4096];
1031 off_t lastwritten = flength + vlength - length;
1032
1033 /* Start reading exactly the buffer size back from the end. */
1034 fseeko(ff, flength - sizeof(fbuf), SEEK_SET);
1035 while (offset < ftello(ff)) {
1036 if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
1037 ast_log(LOG_ERROR, "Short read?!!\n");
1038 fclose(ff);
1039 return -1;
1040 }
1041 /* Since the read moved our file ptr forward, we reverse, but
1042 * seek an offset equal to the amount we want to extend the
1043 * file by */
1044 fseeko(ff, vlength - length - sizeof(fbuf), SEEK_CUR);
1045
1046 /* Note the location of this buffer -- we must not overwrite this position. */
1047 lastwritten = ftello(ff);
1048
1049 if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
1050 ast_log(LOG_ERROR, "Short write?!!\n");
1051 fclose(ff);
1052 return -1;
1053 }
1054
1055 if (lastwritten < offset + sizeof(fbuf)) {
1056 break;
1057 }
1058 /* Our file pointer is now either pointing to the end of the
1059 * file (new position) or a multiple of the fbuf size back from
1060 * that point. Move back to where we want to start reading
1061 * again. We never actually try to read beyond the end of the
1062 * file, so we don't have do deal with short reads, as we would
1063 * when we're shortening the file. */
1064 fseeko(ff, 2 * sizeof(fbuf) + vlength - length, SEEK_CUR);
1065 }
1066
1067 /* Last part of the file that we need to preserve */
1068 if (fseeko(ff, offset + length, SEEK_SET)) {
1069 ast_log(LOG_WARNING, "Unable to seek to %" PRId64 " + %" PRId64 " != %" PRId64 "?)\n", offset, length, ftello(ff));
1070 }
1071
1072 /* Doesn't matter how much we read -- just need to restrict the write */
1073 ast_debug(1, "Reading at %" PRId64 "\n", ftello(ff));
1074 if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
1075 ast_log(LOG_ERROR, "Short read?!!\n");
1076 }
1077 fseek(ff, offset, SEEK_SET);
1078 /* Write out the value, then write just up until where we last moved some data */
1079 if (fwrite(value, 1, vlength, ff) < vlength) {
1080 ast_log(LOG_ERROR, "Short write?!!\n");
1081 } else {
1082 off_t curpos = ftello(ff);
1083 foplen = lastwritten - curpos;
1084 if (fwrite(fbuf, 1, foplen, ff) < foplen) {
1085 ast_log(LOG_ERROR, "Short write?!!\n");
1086 }
1087 }
1088 fclose(ff);
1089 }
1090 } else {
1091 enum file_format newline_format = FF_UNKNOWN;
1092
1093 /* Line mode */
1094 if (args.argc == 5) {
1095 if (tolower(args.format[0]) == 'u') {
1096 newline_format = FF_UNIX;
1097 } else if (tolower(args.format[0]) == 'm') {
1098 newline_format = FF_MAC;
1099 } else if (tolower(args.format[0]) == 'd') {
1100 newline_format = FF_DOS;
1101 }
1102 }
1103 if (newline_format == FF_UNKNOWN && (newline_format = file2format(args.filename)) == FF_UNKNOWN) {
1104 ast_log(LOG_ERROR, "File '%s' not in line format\n", args.filename);
1105 return -1;
1106 }
1107
1108 if (strchr(args.options, 'a')) {
1109 /* Append to file */
1110 if (!(ff = fopen(args.filename, "a"))) {
1111 ast_log(LOG_ERROR, "Unable to open '%s' for appending: %s\n", args.filename, strerror(errno));
1112 return -1;
1113 }
1114 if (fwrite(value, 1, vlength, ff) < vlength) {
1115 ast_log(LOG_ERROR, "Short write?!!\n");
1116 } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
1117 ast_log(LOG_ERROR, "Short write?!!\n");
1118 }
1119 fclose(ff);
1120 } else if (offset == 0 && length == LLONG_MAX) {
1121 /* Overwrite file */
1122 off_t truncsize;
1123 if (!(ff = fopen(args.filename, "w"))) {
1124 ast_log(LOG_ERROR, "Unable to open '%s' for writing: %s\n", args.filename, strerror(errno));
1125 return -1;
1126 }
1127 if (fwrite(value, 1, vlength, ff) < vlength) {
1128 ast_log(LOG_ERROR, "Short write?!!\n");
1129 } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
1130 ast_log(LOG_ERROR, "Short write?!!\n");
1131 }
1132 if ((truncsize = ftello(ff)) < 0) {
1133 ast_log(AST_LOG_ERROR, "Unable to determine truncate position of '%s': %s\n", args.filename, strerror(errno));
1134 }
1135 fclose(ff);
1136 if (truncsize >= 0 && truncate(args.filename, truncsize)) {
1137 ast_log(LOG_ERROR, "Unable to truncate file '%s': %s\n", args.filename, strerror(errno));
1138 return -1;
1139 }
1140 } else {
1141 int64_t offset_offset = (offset == 0 ? 0 : -1), length_offset = -1, flength, i, current_length = 0;
1142 char dos_state = 0, fbuf[4096];
1143
1144 if (offset < 0 && length < offset) {
1145 /* Nonsense! */
1146 ast_log(LOG_ERROR, "Length cannot specify a position prior to the offset\n");
1147 return -1;
1148 }
1149
1150 if (!(ff = fopen(args.filename, "r+"))) {
1151 ast_log(LOG_ERROR, "Cannot open '%s' for modification: %s\n", args.filename, strerror(errno));
1152 return -1;
1153 }
1154
1155 if (fseek(ff, 0, SEEK_END)) {
1156 ast_log(LOG_ERROR, "Cannot seek to end of file '%s': %s\n", args.filename, strerror(errno));
1157 fclose(ff);
1158 return -1;
1159 }
1160 if ((flength = ftello(ff)) < 0) {
1161 ast_log(AST_LOG_ERROR, "Cannot determine end position of file '%s': %s\n", args.filename, strerror(errno));
1162 fclose(ff);
1163 return -1;
1164 }
1165
1166 /* For negative offset and/or negative length */
1167 if (offset < 0 || length < 0) {
1168 int64_t count = 0;
1169 for (i = (flength / sizeof(fbuf)) * sizeof(fbuf); i >= 0; i -= sizeof(fbuf)) {
1170 char *pos;
1171 if (fseeko(ff, i, SEEK_SET)) {
1172 ast_log(LOG_ERROR, "Cannot seek to offset %" PRId64 ": %s\n", i, strerror(errno));
1173 }
1174 if (i + sizeof(fbuf) >= flength) {
1175 memset(fbuf, 0, sizeof(fbuf));
1176 }
1177 if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
1178 ast_log(LOG_ERROR, "Short read: %s\n", strerror(errno));
1179 fclose(ff);
1180 return -1;
1181 }
1182 for (pos = fbuf + sizeof(fbuf) - 1; pos >= fbuf; pos--) {
1183 LINE_COUNTER(pos, newline_format, count);
1184
1185 if (length < 0 && count * -1 == length) {
1186 length_offset = i + (pos - fbuf);
1187 } else if (offset < 0 && count * -1 == (offset - 1)) {
1188 /* Found our initial offset. We're done with reverse motion! */
1189 if (newline_format == FF_DOS) {
1190 offset_offset = i + (pos - fbuf) + 2;
1191 } else {
1192 offset_offset = i + (pos - fbuf) + 1;
1193 }
1194 break;
1195 }
1196 }
1197 if ((offset < 0 && offset_offset >= 0) || (offset >= 0 && length_offset >= 0)) {
1198 break;
1199 }
1200 }
1201 /* We're at the beginning, and the negative offset indicates the exact number of lines in the file */
1202 if (offset < 0 && offset_offset < 0 && offset == count * -1) {
1203 offset_offset = 0;
1204 }
1205 }
1206
1207 /* Positve line offset */
1208 if (offset > 0) {
1209 int64_t count = 0;
1210 fseek(ff, 0, SEEK_SET);
1211 for (i = 0; i < flength; i += sizeof(fbuf)) {
1212 char *pos;
1213 if (i + sizeof(fbuf) >= flength) {
1214 memset(fbuf, 0, sizeof(fbuf));
1215 }
1216 if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
1217 ast_log(LOG_ERROR, "Short read?!!\n");
1218 fclose(ff);
1219 return -1;
1220 }
1221 for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
1222 LINE_COUNTER(pos, newline_format, count);
1223
1224 if (count == offset) {
1225 offset_offset = i + (pos - fbuf) + 1;
1226 break;
1227 }
1228 }
1229 if (offset_offset >= 0) {
1230 break;
1231 }
1232 }
1233 }
1234
1235 if (offset_offset < 0) {
1236 ast_log(LOG_ERROR, "Offset '%s' refers to before the beginning of the file!\n", args.offset);
1237 fclose(ff);
1238 return -1;
1239 }
1240
1241 if (length == 0) {
1242 length_offset = offset_offset;
1243 } else if (length == LLONG_MAX) {
1244 length_offset = flength;
1245 }
1246
1247 /* Positive line length */
1248 if (length_offset < 0) {
1249 fseeko(ff, offset_offset, SEEK_SET);
1250 for (i = offset_offset; i < flength; i += sizeof(fbuf)) {
1251 char *pos;
1252 if (i + sizeof(fbuf) >= flength) {
1253 memset(fbuf, 0, sizeof(fbuf));
1254 }
1255 if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
1256 ast_log(LOG_ERROR, "Short read?!!\n");
1257 fclose(ff);
1258 return -1;
1259 }
1260 for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
1261 LINE_COUNTER(pos, newline_format, current_length);
1262
1263 if (current_length == length) {
1264 length_offset = i + (pos - fbuf) + 1;
1265 break;
1266 }
1267 }
1268 if (length_offset >= 0) {
1269 break;
1270 }
1271 }
1272 if (length_offset < 0) {
1273 /* Exceeds length of file */
1274 ast_debug(3, "Exceeds length of file? length=%" PRId64 ", count=%" PRId64 ", flength=%" PRId64 "\n", length, current_length, flength);
1275 length_offset = flength;
1276 }
1277 }
1278
1279 /* Have offset_offset and length_offset now */
1280 if (length_offset - offset_offset == vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)))) {
1281 /* Simple case - replacement of text inline */
1282 fseeko(ff, offset_offset, SEEK_SET);
1283 if (fwrite(value, 1, vlength, ff) < vlength) {
1284 ast_log(LOG_ERROR, "Short write?!!\n");
1285 } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
1286 ast_log(LOG_ERROR, "Short write?!!\n");
1287 }
1288 fclose(ff);
1289 } else if (length_offset - offset_offset > vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)))) {
1290 /* More complex case - need to shorten file */
1291 off_t cur;
1292 int64_t length_length = length_offset - offset_offset;
1293 size_t vlen = vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)));
1294
1295 ast_debug(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 " (%" PRId64 "), vlength=%" PRId64 ", flength=%" PRId64 "\n",
1296 args.offset, offset_offset, args.length, length_offset, length_length, vlength, flength);
1297
1298 fseeko(ff, offset_offset, SEEK_SET);
1299 if (fwrite(value, 1, vlength, ff) < vlength) {
1300 ast_log(LOG_ERROR, "Short write?!!\n");
1301 fclose(ff);
1302 return -1;
1303 } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, vlen - vlength, ff) < vlen - vlength) {
1304 ast_log(LOG_ERROR, "Short write?!!\n");
1305 fclose(ff);
1306 return -1;
1307 }
1308 while ((cur = ftello(ff)) < flength) {
1309 if (cur < 0) {
1310 ast_log(AST_LOG_ERROR, "Unable to determine last write position for '%s': %s\n", args.filename, strerror(errno));
1311 fclose(ff);
1312 return -1;
1313 }
1314 fseeko(ff, length_length - vlen, SEEK_CUR);
1315 if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
1316 ast_log(LOG_ERROR, "Short read?!!\n");
1317 fclose(ff);
1318 return -1;
1319 }
1320 /* Seek to where we last stopped writing */
1321 fseeko(ff, cur, SEEK_SET);
1322 if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
1323 ast_log(LOG_ERROR, "Short write?!!\n");
1324 fclose(ff);
1325 return -1;
1326 }
1327 }
1328 fclose(ff);
1329 if (truncate(args.filename, flength - (length_length - vlen))) {
1330 ast_log(LOG_ERROR, "Truncation of file failed: %s\n", strerror(errno));
1331 }
1332 } else {
1333 /* Most complex case - need to lengthen file */
1334 size_t vlen = vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)));
1335 int64_t origlen = length_offset - offset_offset;
1336 off_t lastwritten = flength + vlen - origlen;
1337
1338 ast_debug(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 ", vlength=%" PRId64 ", flength=%" PRId64 "\n",
1339 args.offset, offset_offset, args.length, length_offset, vlength, flength);
1340
1341 fseeko(ff, flength - sizeof(fbuf), SEEK_SET);
1342 while (offset_offset + sizeof(fbuf) < ftello(ff)) {
1343 if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
1344 ast_log(LOG_ERROR, "Short read?!!\n");
1345 fclose(ff);
1346 return -1;
1347 }
1348 fseeko(ff, sizeof(fbuf) - vlen - origlen, SEEK_CUR);
1349 if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
1350 ast_log(LOG_ERROR, "Short write?!!\n");
1351 fclose(ff);
1352 return -1;
1353 }
1354 if ((lastwritten = ftello(ff) - sizeof(fbuf)) < offset_offset + sizeof(fbuf)) {
1355 break;
1356 }
1357 fseeko(ff, 2 * sizeof(fbuf) + vlen - origlen, SEEK_CUR);
1358 }
1359 fseek(ff, length_offset, SEEK_SET);
1360 if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
1361 ast_log(LOG_ERROR, "Short read?!!\n");
1362 fclose(ff);
1363 return -1;
1364 }
1365 fseek(ff, offset_offset, SEEK_SET);
1366 if (fwrite(value, 1, vlength, ff) < vlength) {
1367 ast_log(LOG_ERROR, "Short write?!!\n");
1368 fclose(ff);
1369 return -1;
1370 } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
1371 ast_log(LOG_ERROR, "Short write?!!\n");
1372 fclose(ff);
1373 return -1;
1374 } else {
1375 off_t curpos = ftello(ff);
1376 foplen = lastwritten - curpos;
1377 if (fwrite(fbuf, 1, foplen, ff) < foplen) {
1378 ast_log(LOG_ERROR, "Short write?!!\n");
1379 }
1380 }
1381 fclose(ff);
1382 }
1383 }
1384 }
1385
1386 return 0;
1387}
1388
1390 .name = "ENV",
1391 .read = env_read,
1392 .write = env_write
1393};
1394
1396 .name = "STAT",
1397 .read = stat_read,
1398 .read_max = 12,
1399};
1400
1402 .name = "FILE",
1403 .read2 = file_read,
1404 .write = file_write,
1405};
1406
1408 .name = "FILE_COUNT_LINE",
1409 .read2 = file_count_line,
1410 .read_max = 12,
1411};
1412
1414 .name = "FILE_FORMAT",
1415 .read2 = file_format,
1416 .read_max = 2,
1417};
1418
1420 .name = "DIRNAME",
1421 .read = file_dirname,
1422 .read_max = 12,
1423};
1424
1426 .name = "BASENAME",
1427 .read = file_basename,
1428 .read_max = 12,
1429};
1430
1431static int unload_module(void)
1432{
1433 int res = 0;
1434
1442
1443 return res;
1444}
1445
1446static int load_module(void)
1447{
1448 int res = 0;
1449
1457
1458 return res;
1459}
1460
1461AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Environment/filesystem dialplan functions");
int unsetenv(const char *name)
int setenv(const char *name, const char *value, int overwrite)
#define LLONG_MAX
char * strsep(char **str, const char *delims)
Asterisk main include file. File version handling, generic pbx functions.
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
char * end
Definition: eagi_proxy.c:73
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
Generic File Format Support. Should be included by clients of the file handling routines....
static int env_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
Definition: func_env.c:383
static struct ast_custom_function file_format_function
Definition: func_env.c:1413
static int file_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
Definition: func_env.c:902
static struct ast_custom_function file_count_line_function
Definition: func_env.c:1407
static int64_t count_lines(const char *filename, enum file_format newline_format)
Definition: func_env.c:447
static enum file_format file2format(const char *filename)
Definition: func_env.c:561
static int env_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_env.c:367
static struct ast_custom_function env_function
Definition: func_env.c:1389
static int file_count_line(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
Definition: func_env.c:522
const char * format2term(enum file_format f)
Definition: func_env.c:896
static int stat_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_env.c:397
static struct ast_custom_function file_function
Definition: func_env.c:1401
static struct ast_custom_function file_basename_function
Definition: func_env.c:1425
static int file_basename(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_env.c:625
file_format
Definition: func_env.c:440
@ FF_UNKNOWN
Definition: func_env.c:441
@ FF_UNIX
Definition: func_env.c:442
@ FF_DOS
Definition: func_env.c:443
@ FF_MAC
Definition: func_env.c:444
static struct ast_custom_function stat_function
Definition: func_env.c:1395
static int load_module(void)
Definition: func_env.c:1446
static int unload_module(void)
Definition: func_env.c:1431
#define LINE_COUNTER(cptr, term, counter)
Definition: func_env.c:547
static int file_read(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
Definition: func_env.c:642
static struct ast_custom_function file_dirname_function
Definition: func_env.c:1419
static int file_dirname(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_env.c:608
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define AST_LOG_ERROR
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define LOG_WARNING
int errno
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Core PBX routines and definitions.
#define ast_custom_function_register_escalating(acf, escalation)
Register a custom function which requires escalated privileges.
Definition: pbx.h:1568
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1559
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
@ AST_CFE_READ
Definition: pbx.h:1551
@ AST_CFE_BOTH
Definition: pbx.h:1553
#define NULL
Definition: resample.c:96
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
Definition: strings.h:80
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
char * ast_str_append_substr(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc)
Append a non-NULL terminated substring to the end of a dynamic string.
Definition: strings.h:1062
void ast_str_reset(struct ast_str *buf)
Reset the content of a dynamic string. Useful before a series of ast_str_append.
Definition: strings.h:693
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
Main Channel structure associated with a channel.
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
const char * name
Definition: pbx.h:119
Support for dynamic strings.
Definition: strings.h:623
int value
Definition: syslog.c:37
const char * args
static struct test_options options
Utility functions.
#define MIN(a, b)
Definition: utils.h:231