Wed Jan 8 2020 09:49:47

Asterisk developer's documentation


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