-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfunc_env.c
1461 lines (1333 loc) · 44.4 KB
/
func_env.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 1999 - 2010, Digium, Inc.
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief Environment related dialplan functions
*
* \ingroup functions
*/
/*** MODULEINFO
<support_level>core</support_level>
***/
#include "asterisk.h"
#include <sys/stat.h> /* stat(2) */
#include <libgen.h> /* dirname and basename */
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
#include "asterisk/file.h"
/*** DOCUMENTATION
<function name="ENV" language="en_US">
<since>
<version>1.2.0</version>
</since>
<synopsis>
Gets or sets the environment variable specified.
</synopsis>
<syntax>
<parameter name="varname" required="true">
<para>Environment variable name</para>
</parameter>
</syntax>
<description>
<para>Variables starting with <literal>AST_</literal> are reserved to the system and may not be set.</para>
<para>Additionally, the following system variables are available as special built-in dialplan variables.
These variables cannot be set or modified and are read-only.</para>
<variablelist>
<variable name="EPOCH">
<para>Current unix style epoch</para>
</variable>
<variable name="SYSTEMNAME">
<para>value of the <literal>systemname</literal> option from <literal>asterisk.conf</literal></para>
</variable>
<variable name="ASTCACHEDIR">
<para>value of the <literal>astcachedir</literal> option from <literal>asterisk.conf</literal></para>
</variable>
<variable name="ASTETCDIR">
<para>value of the <literal>astetcdir</literal> option from <literal>asterisk.conf</literal></para>
</variable>
<variable name="ASTMODDIR">
<para>value of the <literal>astmoddir</literal> option from <literal>asterisk.conf</literal></para>
</variable>
<variable name="ASTVARLIBDIR">
<para>value of the <literal>astvarlib</literal> option from <literal>asterisk.conf</literal></para>
</variable>
<variable name="ASTDBDIR">
<para>value of the <literal>astdbdir</literal> option from <literal>asterisk.conf</literal></para>
</variable>
<variable name="ASTKEYDIR">
<para>value of the <literal>astkeydir</literal> option from <literal>asterisk.conf</literal></para>
</variable>
<variable name="ASTDATADIR">
<para>value of the <literal>astdatadir</literal> option from <literal>asterisk.conf</literal></para>
</variable>
<variable name="ASTAGIDIR">
<para>value of the <literal>astagidir</literal> option from <literal>asterisk.conf</literal></para>
</variable>
<variable name="ASTSPOOLDIR">
<para>value of the <literal>astspooldir</literal> option from <literal>asterisk.conf</literal></para>
</variable>
<variable name="ASTRUNDIR">
<para>value of the <literal>astrundir</literal> option from <literal>asterisk.conf</literal></para>
</variable>
<variable name="ASTLOGDIR">
<para>value of the <literal>astlogdir</literal> option from <literal>asterisk.conf</literal></para>
</variable>
<variable name="ASTSBINDIR">
<para>value of the <literal>astsbindir</literal> option from <literal>asterisk.conf</literal></para>
</variable>
<variable name="ENTITYID">
<para>Global Entity ID set automatically, or from <literal>asterisk.conf</literal></para>
</variable>
</variablelist>
</description>
</function>
<function name="STAT" language="en_US">
<since>
<version>1.4.0</version>
</since>
<synopsis>
Does a check on the specified file.
</synopsis>
<syntax>
<parameter name="flag" required="true">
<para>Flag may be one of the following:</para>
<para>d - Checks if the file is a directory.</para>
<para>e - Checks if the file exists.</para>
<para>f - Checks if the file is a regular file.</para>
<para>m - Returns the file mode (in octal)</para>
<para>s - Returns the size (in bytes) of the file</para>
<para>A - Returns the epoch at which the file was last accessed.</para>
<para>C - Returns the epoch at which the inode was last changed.</para>
<para>M - Returns the epoch at which the file was last modified.</para>
</parameter>
<parameter name="filename" required="true" />
</syntax>
<description>
<note>
<para>If <literal>live_dangerously</literal> in <literal>asterisk.conf</literal>
is set to <literal>no</literal>, this function can only be executed from the
dialplan, and not directly from external protocols.</para>
</note>
</description>
</function>
<function name="FILE" language="en_US">
<since>
<version>1.6.0</version>
</since>
<synopsis>
Read or write text file.
</synopsis>
<syntax>
<parameter name="filename" required="true" />
<parameter name="offset">
<para>Maybe specified as any number. If negative, <replaceable>offset</replaceable> specifies the number
of bytes back from the end of the file.</para>
</parameter>
<parameter name="length">
<para>If specified, will limit the length of the data read to that size. If negative,
trims <replaceable>length</replaceable> bytes from the end of the file.</para>
</parameter>
<parameter name="options">
<optionlist>
<option name="l">
<para>Line mode: offset and length are assumed to be
measured in lines, instead of byte offsets.</para>
</option>
<option name="a">
<para>In write mode only, the append option is used to
append to the end of the file, instead of overwriting
the existing file.</para>
</option>
<option name="d">
<para>In write mode and line mode only, this option does
not automatically append a newline string to the end of
a value. This is useful for deleting lines, instead of
setting them to blank.</para>
</option>
</optionlist>
</parameter>
<parameter name="format">
<para>The <replaceable>format</replaceable> parameter may be
used to delimit the type of line terminators in line mode.</para>
<optionlist>
<option name="u">
<para>Unix newline format.</para>
</option>
<option name="d">
<para>DOS newline format.</para>
</option>
<option name="m">
<para>Macintosh newline format.</para>
</option>
</optionlist>
</parameter>
</syntax>
<description>
<para>Read and write text file in character and line mode.</para>
<para>Examples:</para>
<para>Read mode (byte):</para>
<example title="Reads the entire content of the file">
same => n,Set(foo=${FILE(/tmp/test.txt)})
</example>
<example title="Reads from the 11th byte to the end of the file (i.e. skips the first 10)">
same => n,Set(foo=${FILE(/tmp/test.txt,10)})
</example>
<example title="Reads from the 11th to 20th byte in the file (i.e. skip the first 10, then read 10 bytes)">
same => n,Set(foo=${FILE(/tmp/test.txt,10,10)})
</example>
<para>Read mode (line):</para>
<example title="Reads the 3rd line of the file">
same => n,Set(foo=${FILE(/tmp/test.txt,3,1,l)})
</example>
<example title="Reads the 3rd and 4th lines of the file">
same => n,Set(foo=${FILE(/tmp/test.txt,3,2,l)})
</example>
<example title="Reads from the third line to the end of the file">
same => n,Set(foo=${FILE(/tmp/test.txt,3,,l)})
</example>
<example title="Reads the last three lines of the file">
same => n,Set(foo=${FILE(/tmp/test.txt,-3,,l)})
</example>
<example title="Reads the 3rd line of a DOS-formatted file">
same => n,Set(foo=${FILE(/tmp/test.txt,3,1,l,d)})
</example>
<para>Write mode (byte):</para>
<example title="Truncate the file and write bar">
same => n,Set(FILE(/tmp/test.txt)=bar)
</example>
<example title="Append bar">
same => n,Set(FILE(/tmp/test.txt,,,a)=bar)
</example>
<example title="Replace the first byte with bar (replaces 1 character with 3)">
same => n,Set(FILE(/tmp/test.txt,0,1)=bar)
</example>
<example title="Replace 10 bytes beginning at the 21st byte of the file with bar">
same => n,Set(FILE(/tmp/test.txt,20,10)=bar)
</example>
<example title="Replace all bytes from the 21st with bar">
same => n,Set(FILE(/tmp/test.txt,20)=bar)
</example>
<example title="Insert bar after the 4th character">
same => n,Set(FILE(/tmp/test.txt,4,0)=bar)
</example>
<para>Write mode (line):</para>
<example title="Replace the first line of the file with bar">
same => n,Set(FILE(/tmp/foo.txt,0,1,l)=bar)
</example>
<example title="Replace the last line of the file with bar">
same => n,Set(FILE(/tmp/foo.txt,-1,,l)=bar)
</example>
<example title="Append bar to the file with a newline">
same => n,Set(FILE(/tmp/foo.txt,,,al)=bar)
</example>
<note>
<para>If <literal>live_dangerously</literal> in <literal>asterisk.conf</literal>
is set to <literal>no</literal>, this function can only be executed from the
dialplan, and not directly from external protocols.</para>
</note>
</description>
<see-also>
<ref type="function">FILE_COUNT_LINE</ref>
<ref type="function">FILE_FORMAT</ref>
</see-also>
</function>
<function name="FILE_COUNT_LINE" language="en_US">
<since>
<version>1.8.0</version>
</since>
<synopsis>
Obtains the number of lines of a text file.
</synopsis>
<syntax>
<parameter name="filename" required="true" />
<parameter name="format">
<para>Format may be one of the following:</para>
<optionlist>
<option name="u">
<para>Unix newline format.</para>
</option>
<option name="d">
<para>DOS newline format.</para>
</option>
<option name="m">
<para>Macintosh newline format.</para>
</option>
</optionlist>
<note><para>If not specified, an attempt will be made to determine the newline format type.</para></note>
</parameter>
</syntax>
<description>
<para>Returns the number of lines, or <literal>-1</literal> on error.</para>
<note>
<para>If <literal>live_dangerously</literal> in <literal>asterisk.conf</literal>
is set to <literal>no</literal>, this function can only be executed from the
dialplan, and not directly from external protocols.</para>
</note>
</description>
<see-also>
<ref type="function">FILE</ref>
<ref type="function">FILE_FORMAT</ref>
</see-also>
</function>
<function name="FILE_FORMAT" language="en_US">
<since>
<version>1.8.0</version>
</since>
<synopsis>
Return the newline format of a text file.
</synopsis>
<syntax>
<parameter name="filename" required="true" />
</syntax>
<description>
<para>Return the line terminator type:</para>
<para>'u' - Unix "\n" format</para>
<para>'d' - DOS "\r\n" format</para>
<para>'m' - Macintosh "\r" format</para>
<para>'x' - Cannot be determined</para>
<note>
<para>If <literal>live_dangerously</literal> in <literal>asterisk.conf</literal>
is set to <literal>no</literal>, this function can only be executed from the
dialplan, and not directly from external protocols.</para>
</note>
</description>
<see-also>
<ref type="function">FILE</ref>
<ref type="function">FILE_COUNT_LINE</ref>
</see-also>
</function>
<function name="BASENAME" language="en_US">
<since>
<version>16.21.0</version>
<version>18.7.0</version>
</since>
<synopsis>
Return the name of a file.
</synopsis>
<syntax>
<parameter name="filename" required="true" />
</syntax>
<description>
<para>Return the base file name, given a full file path.</para>
<example title="Directory name">
same => n,Set(basename=${BASENAME(/etc/asterisk/extensions.conf)})
same => n,NoOp(${basename}) ; outputs extensions.conf
</example>
</description>
<see-also>
<ref type="function">DIRNAME</ref>
</see-also>
</function>
<function name="DIRNAME" language="en_US">
<since>
<version>16.21.0</version>
<version>18.7.0</version>
</since>
<synopsis>
Return the directory of a file.
</synopsis>
<syntax>
<parameter name="filename" required="true" />
</syntax>
<description>
<para>Return the directory of a file, given a full file path.</para>
<example title="Directory name">
same => n,Set(dirname=${DIRNAME(/etc/asterisk/extensions.conf)})
same => n,NoOp(${dirname}) ; outputs /etc/asterisk
</example>
</description>
<see-also>
<ref type="function">BASENAME</ref>
</see-also>
</function>
***/
static int env_read(struct ast_channel *chan, const char *cmd, char *data,
char *buf, size_t len)
{
char *ret = NULL;
*buf = '\0';
if (data)
ret = getenv(data);
if (ret)
ast_copy_string(buf, ret, len);
return 0;
}
static int env_write(struct ast_channel *chan, const char *cmd, char *data,
const char *value)
{
if (!ast_strlen_zero(data) && strncmp(data, "AST_", 4)) {
if (!ast_strlen_zero(value)) {
setenv(data, value, 1);
} else {
unsetenv(data);
}
}
return 0;
}
static int stat_read(struct ast_channel *chan, const char *cmd, char *data,
char *buf, size_t len)
{
char *action;
struct stat s;
ast_copy_string(buf, "0", len);
action = strsep(&data, ",");
if (stat(data, &s)) {
return 0;
} else {
switch (*action) {
case 'e':
strcpy(buf, "1");
break;
case 's':
snprintf(buf, len, "%u", (unsigned int) s.st_size);
break;
case 'f':
snprintf(buf, len, "%d", S_ISREG(s.st_mode) ? 1 : 0);
break;
case 'd':
snprintf(buf, len, "%d", S_ISDIR(s.st_mode) ? 1 : 0);
break;
case 'M':
snprintf(buf, len, "%d", (int) s.st_mtime);
break;
case 'A':
snprintf(buf, len, "%d", (int) s.st_mtime);
break;
case 'C':
snprintf(buf, len, "%d", (int) s.st_ctime);
break;
case 'm':
snprintf(buf, len, "%o", (unsigned int) s.st_mode);
break;
}
}
return 0;
}
enum file_format {
FF_UNKNOWN = -1,
FF_UNIX,
FF_DOS,
FF_MAC,
};
static int64_t count_lines(const char *filename, enum file_format newline_format)
{
int count = 0;
char fbuf[4096];
FILE *ff;
if (!(ff = fopen(filename, "r"))) {
ast_log(LOG_ERROR, "Unable to open '%s': %s\n", filename, strerror(errno));
return -1;
}
while (fgets(fbuf, sizeof(fbuf), ff)) {
char *next = fbuf, *first_cr = NULL, *first_nl = NULL;
/* Must do it this way, because if the fileformat is FF_MAC, then Unix
* assumptions about line-format will not come into play. */
while (next) {
if (newline_format == FF_DOS || newline_format == FF_MAC || newline_format == FF_UNKNOWN) {
first_cr = strchr(next, '\r');
}
if (newline_format == FF_UNIX || newline_format == FF_UNKNOWN) {
first_nl = strchr(next, '\n');
}
/* No terminators found in buffer */
if (!first_cr && !first_nl) {
break;
}
if (newline_format == FF_UNKNOWN) {
if ((first_cr && !first_nl) || (first_cr && first_cr < first_nl)) {
if (first_nl && first_nl == first_cr + 1) {
newline_format = FF_DOS;
} else if (first_cr && first_cr == &fbuf[sizeof(fbuf) - 2]) {
/* Get it on the next pass */
fseek(ff, -1, SEEK_CUR);
break;
} else {
newline_format = FF_MAC;
first_nl = NULL;
}
} else {
newline_format = FF_UNIX;
first_cr = NULL;
}
/* Jump down into next section */
}
if (newline_format == FF_DOS) {
if (first_nl && first_cr && first_nl == first_cr + 1) {
next = first_nl + 1;
count++;
} else if (first_cr == &fbuf[sizeof(fbuf) - 2]) {
/* Get it on the next pass */
fseek(ff, -1, SEEK_CUR);
break;
}
} else if (newline_format == FF_MAC) {
if (first_cr) {
next = first_cr + 1;
count++;
}
} else if (newline_format == FF_UNIX) {
if (first_nl) {
next = first_nl + 1;
count++;
}
}
}
}
fclose(ff);
return count;
}
static int file_count_line(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
{
enum file_format newline_format = FF_UNKNOWN;
int64_t count;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(filename);
AST_APP_ARG(format);
);
AST_STANDARD_APP_ARGS(args, data);
if (args.argc > 1) {
if (tolower(args.format[0]) == 'd') {
newline_format = FF_DOS;
} else if (tolower(args.format[0]) == 'm') {
newline_format = FF_MAC;
} else if (tolower(args.format[0]) == 'u') {
newline_format = FF_UNIX;
}
}
count = count_lines(args.filename, newline_format);
ast_str_set(buf, len, "%" PRId64, count);
return 0;
}
#define LINE_COUNTER(cptr, term, counter) \
if (*cptr == '\n' && term == FF_UNIX) { \
counter++; \
} else if (*cptr == '\n' && term == FF_DOS && dos_state == 0) { \
dos_state = 1; \
} else if (*cptr == '\r' && term == FF_DOS && dos_state == 1) { \
dos_state = 0; \
counter++; \
} else if (*cptr == '\r' && term == FF_MAC) { \
counter++; \
} else if (term == FF_DOS) { \
dos_state = 0; \
}
static enum file_format file2format(const char *filename)
{
FILE *ff;
char fbuf[4096];
char *first_cr, *first_nl;
enum file_format newline_format = FF_UNKNOWN;
if (!(ff = fopen(filename, "r"))) {
ast_log(LOG_ERROR, "Cannot open '%s': %s\n", filename, strerror(errno));
return -1;
}
while (fgets(fbuf, sizeof(fbuf), ff)) {
first_cr = strchr(fbuf, '\r');
first_nl = strchr(fbuf, '\n');
if (!first_cr && !first_nl) {
continue;
}
if ((first_cr && !first_nl) || (first_cr && first_cr < first_nl)) {
if (first_nl && first_nl == first_cr + 1) {
newline_format = FF_DOS;
} else if (first_cr && first_cr == &fbuf[sizeof(fbuf) - 2]) {
/* Edge case: get it on the next pass */
fseek(ff, -1, SEEK_CUR);
continue;
} else {
newline_format = FF_MAC;
}
} else {
newline_format = FF_UNIX;
}
break;
}
fclose(ff);
return newline_format;
}
static int file_format(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
{
enum file_format newline_format = file2format(data);
ast_str_set(buf, len, "%c", newline_format == FF_UNIX ? 'u' : newline_format == FF_DOS ? 'd' : newline_format == FF_MAC ? 'm' : 'x');
return 0;
}
static int file_dirname(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
char *ret = NULL;
*buf = '\0';
if (data) {
ret = dirname(data);
}
if (ret) {
ast_copy_string(buf, ret, len);
}
return 0;
}
static int file_basename(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
char *ret = NULL;
*buf = '\0';
if (data) {
ret = basename(data);
}
if (ret) {
ast_copy_string(buf, ret, len);
}
return 0;
}
static int file_read(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
{
FILE *ff;
int64_t offset = 0, length = LLONG_MAX;
enum file_format format = FF_UNKNOWN;
char fbuf[4096];
int64_t flength, i; /* iterator needs to be signed, so it can go negative and terminate the loop */
int64_t offset_offset = -1, length_offset = -1;
char dos_state = 0;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(filename);
AST_APP_ARG(offset);
AST_APP_ARG(length);
AST_APP_ARG(options);
AST_APP_ARG(fileformat);
);
AST_STANDARD_APP_ARGS(args, data);
if (args.argc > 1) {
sscanf(args.offset, "%" SCNd64, &offset);
}
if (args.argc > 2) {
sscanf(args.length, "%" SCNd64, &length);
}
if (args.argc < 4 || !strchr(args.options, 'l')) {
/* Character-based mode */
off_t off_i;
if (!(ff = fopen(args.filename, "r"))) {
ast_log(LOG_WARNING, "Cannot open file '%s' for reading: %s\n", args.filename, strerror(errno));
return 0;
}
if (fseeko(ff, 0, SEEK_END) < 0) {
ast_log(LOG_ERROR, "Cannot seek to end of '%s': %s\n", args.filename, strerror(errno));
fclose(ff);
return -1;
}
flength = ftello(ff);
if (offset < 0) {
fseeko(ff, offset, SEEK_END);
if ((offset = ftello(ff)) < 0) {
ast_log(AST_LOG_ERROR, "Cannot determine offset position of '%s': %s\n", args.filename, strerror(errno));
fclose(ff);
return -1;
}
}
if (length < 0) {
fseeko(ff, length, SEEK_END);
if ((length = ftello(ff)) - offset < 0) {
/* Eliminates all results */
fclose(ff);
return -1;
}
} else if (length == LLONG_MAX) {
fseeko(ff, 0, SEEK_END);
length = ftello(ff);
}
ast_str_reset(*buf);
fseeko(ff, offset, SEEK_SET);
for (off_i = ftello(ff); off_i < flength && off_i < offset + length; off_i += sizeof(fbuf)) {
/* Calculate if we need to retrieve just a portion of the file in memory */
size_t toappend = sizeof(fbuf);
if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
ast_log(LOG_ERROR, "Short read?!!\n");
break;
}
/* Don't go past the length requested */
if (off_i + toappend > offset + length) {
toappend = MIN(offset + length - off_i, flength - off_i);
}
ast_str_append_substr(buf, len, fbuf, toappend);
}
fclose(ff);
return 0;
}
/* Line-based read */
if (args.argc == 5) {
if (tolower(args.fileformat[0]) == 'd') {
format = FF_DOS;
} else if (tolower(args.fileformat[0]) == 'm') {
format = FF_MAC;
} else if (tolower(args.fileformat[0]) == 'u') {
format = FF_UNIX;
}
}
if (format == FF_UNKNOWN) {
if ((format = file2format(args.filename)) == FF_UNKNOWN) {
ast_log(LOG_WARNING, "'%s' is not a line-based file\n", args.filename);
return -1;
}
}
if (offset < 0 && length <= offset) {
/* Length eliminates all content */
return -1;
} else if (offset == 0) {
offset_offset = 0;
}
if (!(ff = fopen(args.filename, "r"))) {
ast_log(LOG_ERROR, "Cannot open '%s': %s\n", args.filename, strerror(errno));
return -1;
}
if (fseek(ff, 0, SEEK_END)) {
ast_log(LOG_ERROR, "Cannot seek to end of file '%s': %s\n", args.filename, strerror(errno));
fclose(ff);
return -1;
}
flength = ftello(ff);
if (length == LLONG_MAX) {
length_offset = flength;
}
/* For negative offset and/or negative length */
if (offset < 0 || length < 0) {
int64_t count = 0;
/* Start with an even multiple of fbuf, so at the end of reading with a
* 0 offset, we don't try to go past the beginning of the file. */
for (i = (flength / sizeof(fbuf)) * sizeof(fbuf); i >= 0; i -= sizeof(fbuf)) {
size_t end;
char *pos;
if (fseeko(ff, i, SEEK_SET)) {
ast_log(LOG_ERROR, "Cannot seek to offset %" PRId64 ": %s\n", i, strerror(errno));
}
end = fread(fbuf, 1, sizeof(fbuf), ff);
for (pos = (end < sizeof(fbuf) ? fbuf + end - 1 : fbuf + sizeof(fbuf) - 1); pos >= fbuf; pos--) {
LINE_COUNTER(pos, format, count);
if (length < 0 && count * -1 == length) {
length_offset = i + (pos - fbuf);
} else if (offset < 0 && count * -1 == (offset - 1)) {
/* Found our initial offset. We're done with reverse motion! */
if (format == FF_DOS) {
offset_offset = i + (pos - fbuf) + 2;
} else {
offset_offset = i + (pos - fbuf) + 1;
}
break;
}
}
if ((offset < 0 && offset_offset >= 0) || (offset >= 0 && length_offset >= 0)) {
break;
}
}
/* We're at the beginning, and the negative offset indicates the exact number of lines in the file */
if (offset < 0 && offset_offset < 0 && offset == count * -1) {
offset_offset = 0;
}
}
/* Positve line offset */
if (offset > 0) {
int64_t count = 0;
fseek(ff, 0, SEEK_SET);
for (i = 0; i < flength; i += sizeof(fbuf)) {
char *pos;
if (i + sizeof(fbuf) <= flength) {
/* Don't let previous values influence current counts, due to short reads */
memset(fbuf, 0, sizeof(fbuf));
}
if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
ast_log(LOG_ERROR, "Short read?!!\n");
fclose(ff);
return -1;
}
for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
LINE_COUNTER(pos, format, count);
if (count == offset) {
offset_offset = i + (pos - fbuf) + 1;
break;
}
}
if (offset_offset >= 0) {
break;
}
}
}
if (offset_offset < 0) {
ast_log(LOG_ERROR, "Offset '%s' refers to before the beginning of the file!\n", args.offset);
fclose(ff);
return -1;
}
ast_str_reset(*buf);
if (fseeko(ff, offset_offset, SEEK_SET)) {
ast_log(LOG_ERROR, "fseeko failed: %s\n", strerror(errno));
}
/* If we have both offset_offset and length_offset, then grabbing the
* buffer is simply a matter of just retrieving the file and adding it
* to buf. Otherwise, we need to run byte-by-byte forward until the
* length is complete. */
if (length_offset >= 0) {
ast_debug(3, "offset=%" PRId64 ", length=%" PRId64 ", offset_offset=%" PRId64 ", length_offset=%" PRId64 "\n", offset, length, offset_offset, length_offset);
for (i = offset_offset; i < length_offset; i += sizeof(fbuf)) {
if (fread(fbuf, 1, i + sizeof(fbuf) > flength ? flength - i : sizeof(fbuf), ff) < (i + sizeof(fbuf) > flength ? flength - i : sizeof(fbuf))) {
ast_log(LOG_ERROR, "Short read?!!\n");
}
ast_debug(3, "Appending first %" PRId64" bytes of fbuf=%s\n", (int64_t)(i + sizeof(fbuf) > length_offset ? length_offset - i : sizeof(fbuf)), fbuf);
ast_str_append_substr(buf, len, fbuf, i + sizeof(fbuf) > length_offset ? length_offset - i : sizeof(fbuf));
}
} else if (length == 0) {
/* Nothing to do */
} else {
/* Positive line offset */
int64_t current_length = 0;
char dos_state = 0;
ast_debug(3, "offset=%" PRId64 ", length=%" PRId64 ", offset_offset=%" PRId64 ", length_offset=%" PRId64 "\n", offset, length, offset_offset, length_offset);
for (i = offset_offset; i < flength; i += sizeof(fbuf)) {
char *pos;
size_t bytes_read;
if ((bytes_read = fread(fbuf, 1, sizeof(fbuf), ff)) < sizeof(fbuf) && !feof(ff)) {
ast_log(LOG_ERROR, "Short read?!!\n");
fclose(ff);
return -1;
}
for (pos = fbuf; pos < fbuf + bytes_read; pos++) {
LINE_COUNTER(pos, format, current_length);
if (current_length == length) {
length_offset = i + (pos - fbuf) + 1;
break;
}
}
ast_debug(3, "length_offset=%" PRId64 ", length_offset - i=%" PRId64 "\n", length_offset, length_offset - i);
ast_str_append_substr(buf, len, fbuf, (length_offset >= 0) ? length_offset - i : (flength > i + sizeof(fbuf)) ? sizeof(fbuf) : flength - i);
if (length_offset >= 0) {
break;
}
}
}
fclose(ff);
return 0;
}
const char *format2term(enum file_format f) __attribute__((const));
const char *format2term(enum file_format f)
{
const char *term[] = { "", "\n", "\r\n", "\r" };
return term[f + 1];
}
static int file_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
{
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(filename);
AST_APP_ARG(offset);
AST_APP_ARG(length);
AST_APP_ARG(options);
AST_APP_ARG(format);
);
int64_t offset = 0, length = LLONG_MAX;
off_t flength, vlength;
size_t foplen = 0;
FILE *ff;
AST_STANDARD_APP_ARGS(args, data);
if (args.argc > 1) {
sscanf(args.offset, "%" SCNd64, &offset);
}
if (args.argc > 2) {
sscanf(args.length, "%" SCNd64, &length);
}
vlength = strlen(value);
if (args.argc < 4 || !strchr(args.options, 'l')) {
/* Character-based mode */
if (args.argc > 3 && strchr(args.options, 'a')) {
/* Append mode */
if (!(ff = fopen(args.filename, "a"))) {
ast_log(LOG_WARNING, "Cannot open file '%s' for appending: %s\n", args.filename, strerror(errno));
return 0;
}
if (fwrite(value, 1, vlength, ff) < vlength) {
ast_log(LOG_ERROR, "Short write?!!\n");
}
fclose(ff);
return 0;
} else if (offset == 0 && length == LLONG_MAX) {
if (!(ff = fopen(args.filename, "w"))) {
ast_log(LOG_WARNING, "Cannot open file '%s' for writing: %s\n", args.filename, strerror(errno));
return 0;
}
if (fwrite(value, 1, vlength, ff) < vlength) {
ast_log(LOG_ERROR, "Short write?!!\n");
}
fclose(ff);
return 0;
}
if (!(ff = fopen(args.filename, "r+"))) {
ast_log(LOG_WARNING, "Cannot open file '%s' for modification: %s\n", args.filename, strerror(errno));
return 0;
}
fseeko(ff, 0, SEEK_END);
flength = ftello(ff);
if (offset < 0) {
if (fseeko(ff, offset, SEEK_END)) {
ast_log(LOG_ERROR, "Cannot seek to offset of '%s': %s\n", args.filename, strerror(errno));
fclose(ff);
return -1;
}
if ((offset = ftello(ff)) < 0) {
ast_log(AST_LOG_ERROR, "Cannot determine offset position of '%s': %s\n", args.filename, strerror(errno));
fclose(ff);
return -1;
}
}
if (length < 0) {
length = flength - offset + length;
if (length < 0) {
ast_log(LOG_ERROR, "Length '%s' exceeds the file length. No data will be written.\n", args.length);
fclose(ff);
return -1;
}
}
fseeko(ff, offset, SEEK_SET);
ast_debug(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 ", vlength=%" PRId64 ", flength=%" PRId64 "\n",
S_OR(args.offset, "(null)"), offset, S_OR(args.length, "(null)"), length, vlength, flength);
if (length == vlength) {
/* Simplest case, a straight replace */
if (fwrite(value, 1, vlength, ff) < vlength) {
ast_log(LOG_ERROR, "Short write?!!\n");
}
fclose(ff);
} else if (length == LLONG_MAX) {
/* Simple truncation */
if (fwrite(value, 1, vlength, ff) < vlength) {
ast_log(LOG_ERROR, "Short write?!!\n");
}
fclose(ff);
if (truncate(args.filename, offset + vlength)) {
ast_log(LOG_ERROR, "Unable to truncate the file: %s\n", strerror(errno));