Wed Apr 6 11:29:45 2011

Asterisk developer's documentation


func_env.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2010, Digium, Inc.
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  */
00016 
00017 /*! \file
00018  *
00019  * \brief Environment related dialplan functions
00020  *
00021  * \ingroup functions
00022  */
00023 
00024 #include "asterisk.h"
00025 
00026 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 289581 $")
00027 
00028 #include <sys/stat.h>   /* stat(2) */
00029 
00030 #include "asterisk/module.h"
00031 #include "asterisk/channel.h"
00032 #include "asterisk/pbx.h"
00033 #include "asterisk/utils.h"
00034 #include "asterisk/app.h"
00035 #include "asterisk/file.h"
00036 
00037 /*** DOCUMENTATION
00038    <function name="ENV" language="en_US">
00039       <synopsis>
00040          Gets or sets the environment variable specified.
00041       </synopsis>
00042       <syntax>
00043          <parameter name="varname" required="true">
00044             <para>Environment variable name</para>
00045          </parameter>
00046       </syntax>
00047       <description>
00048          <para>Variables starting with <literal>AST_</literal> are reserved to the system and may not be set.</para>
00049       </description>
00050    </function>
00051    <function name="STAT" language="en_US">
00052       <synopsis>
00053          Does a check on the specified file.
00054       </synopsis>
00055       <syntax>
00056          <parameter name="flag" required="true">
00057             <para>Flag may be one of the following:</para>
00058             <para>d - Checks if the file is a directory.</para>
00059             <para>e - Checks if the file exists.</para>
00060             <para>f - Checks if the file is a regular file.</para>
00061             <para>m - Returns the file mode (in octal)</para>
00062             <para>s - Returns the size (in bytes) of the file</para>
00063             <para>A - Returns the epoch at which the file was last accessed.</para>
00064             <para>C - Returns the epoch at which the inode was last changed.</para>
00065             <para>M - Returns the epoch at which the file was last modified.</para>
00066          </parameter>
00067          <parameter name="filename" required="true" />
00068       </syntax>
00069       <description>
00070       </description>
00071    </function>
00072    <function name="FILE" language="en_US">
00073       <synopsis>
00074          Read or write text file.
00075       </synopsis>
00076       <syntax>
00077          <parameter name="filename" required="true" />
00078          <parameter name="offset">
00079             <para>Maybe specified as any number. If negative, <replaceable>offset</replaceable> specifies the number
00080             of bytes back from the end of the file.</para>
00081          </parameter>
00082          <parameter name="length">
00083             <para>If specified, will limit the length of the data read to that size. If negative,
00084             trims <replaceable>length</replaceable> bytes from the end of the file.</para>
00085          </parameter>
00086          <parameter name="options">
00087             <optionlist>
00088                <option name="l">
00089                   <para>Line mode:  offset and length are assumed to be
00090                   measured in lines, instead of byte offsets.</para>
00091                </option>
00092                <option name="a">
00093                   <para>In write mode only, the append option is used to
00094                   append to the end of the file, instead of overwriting
00095                   the existing file.</para>
00096                </option>
00097                <option name="d">
00098                   <para>In write mode and line mode only, this option does
00099                   not automatically append a newline string to the end of
00100                   a value.  This is useful for deleting lines, instead of
00101                   setting them to blank.</para>
00102                </option>
00103             </optionlist>
00104          </parameter>
00105          <parameter name="format">
00106             <para>The <replaceable>format</replaceable> parameter may be
00107             used to delimit the type of line terminators in line mode.</para>
00108             <optionlist>
00109                <option name="u">
00110                   <para>Unix newline format.</para>
00111                </option>
00112                <option name="d">
00113                   <para>DOS newline format.</para>
00114                </option>
00115                <option name="m">
00116                   <para>Macintosh newline format.</para>
00117                </option>
00118             </optionlist>
00119          </parameter>
00120       </syntax>
00121       <description>
00122          <para>Read and write text file in character and line mode.</para>
00123          <para>Examples:</para>
00124          <para/>
00125          <para>Read mode (byte):</para>
00126          <para>    ;reads the entire content of the file.</para>
00127          <para>    Set(foo=${FILE(/tmp/test.txt)})</para>
00128          <para>    ;reads from the 11th byte to the end of the file (i.e. skips the first 10).</para>
00129          <para>    Set(foo=${FILE(/tmp/test.txt,10)})</para>
00130          <para>    ;reads from the 11th to 20th byte in the file (i.e. skip the first 10, then read 10 bytes).</para>
00131          <para>    Set(foo=${FILE(/tmp/test.txt,10,10)})</para>
00132          <para/>
00133          <para>Read mode (line):</para>
00134          <para>    ; reads the 3rd line of the file.</para>
00135          <para>    Set(foo=${FILE(/tmp/test.txt,3,1,l)})</para>
00136          <para>    ; reads the 3rd and 4th lines of the file.</para>
00137          <para>    Set(foo=${FILE(/tmp/test.txt,3,2,l)})</para>
00138          <para>    ; reads from the third line to the end of the file.</para>
00139          <para>    Set(foo=${FILE(/tmp/test.txt,3,,l)})</para>
00140          <para>    ; reads the last three lines of the file.</para>
00141          <para>    Set(foo=${FILE(/tmp/test.txt,-3,,l)})</para>
00142          <para>    ; reads the 3rd line of a DOS-formatted file.</para>
00143          <para>    Set(foo=${FILE(/tmp/test.txt,3,1,l,d)})</para>
00144          <para/>
00145          <para>Write mode (byte):</para>
00146          <para>    ; truncate the file and write "bar"</para>
00147          <para>    Set(FILE(/tmp/test.txt)=bar)</para>
00148          <para>    ; Append "bar"</para>
00149          <para>    Set(FILE(/tmp/test.txt,,,a)=bar)</para>
00150          <para>    ; Replace the first byte with "bar" (replaces 1 character with 3)</para>
00151          <para>    Set(FILE(/tmp/test.txt,0,1)=bar)</para>
00152          <para>    ; Replace 10 bytes beginning at the 21st byte of the file with "bar"</para>
00153          <para>    Set(FILE(/tmp/test.txt,20,10)=bar)</para>
00154          <para>    ; Replace all bytes from the 21st with "bar"</para>
00155          <para>    Set(FILE(/tmp/test.txt,20)=bar)</para>
00156          <para>    ; Insert "bar" after the 4th character</para>
00157          <para>    Set(FILE(/tmp/test.txt,4,0)=bar)</para>
00158          <para/>
00159          <para>Write mode (line):</para>
00160          <para>    ; Replace the first line of the file with "bar"</para>
00161          <para>    Set(FILE(/tmp/foo.txt,0,1,l)=bar)</para>
00162          <para>    ; Replace the last line of the file with "bar"</para>
00163          <para>    Set(FILE(/tmp/foo.txt,-1,,l)=bar)</para>
00164          <para>    ; Append "bar" to the file with a newline</para>
00165          <para>    Set(FILE(/tmp/foo.txt,,,al)=bar)</para>
00166       </description>
00167       <see-also>
00168          <ref type="function">FILE_COUNT_LINE</ref>
00169          <ref type="function">FILE_FORMAT</ref>
00170       </see-also>
00171    </function>
00172    <function name="FILE_COUNT_LINE" language="en_US">
00173       <synopsis>
00174          Obtains the number of lines of a text file.
00175       </synopsis>
00176       <syntax>
00177          <parameter name="filename" required="true" />
00178          <parameter name="format">
00179             <para>Format may be one of the following:</para>
00180             <optionlist>
00181                <option name="u">
00182                   <para>Unix newline format.</para>
00183                </option>
00184                <option name="d">
00185                   <para>DOS newline format.</para>
00186                </option>
00187                <option name="m">
00188                   <para>Macintosh newline format.</para>
00189                </option>
00190             </optionlist>
00191             <note><para>If not specified, an attempt will be made to determine the newline format type.</para></note>
00192          </parameter>
00193       </syntax>
00194       <description>
00195          <para>Returns the number of lines, or <literal>-1</literal> on error.</para>
00196       </description>
00197       <see-also>
00198          <ref type="function">FILE</ref>
00199          <ref type="function">FILE_FORMAT</ref>
00200       </see-also>
00201    </function>
00202    <function name="FILE_FORMAT" language="en_US">
00203       <synopsis>
00204          Return the newline format of a text file.
00205       </synopsis>
00206       <syntax>
00207          <parameter name="filename" required="true" />
00208       </syntax>
00209       <description>
00210          <para>Return the line terminator type:</para>
00211          <para>'u' - Unix "\n" format</para>
00212          <para>'d' - DOS "\r\n" format</para>
00213          <para>'m' - Macintosh "\r" format</para>
00214          <para>'x' - Cannot be determined</para>
00215       </description>
00216       <see-also>
00217          <ref type="function">FILE</ref>
00218          <ref type="function">FILE_COUNT_LINE</ref>
00219       </see-also>
00220    </function>
00221  ***/
00222 
00223 static int env_read(struct ast_channel *chan, const char *cmd, char *data,
00224          char *buf, size_t len)
00225 {
00226    char *ret = NULL;
00227 
00228    *buf = '\0';
00229 
00230    if (data)
00231       ret = getenv(data);
00232 
00233    if (ret)
00234       ast_copy_string(buf, ret, len);
00235 
00236    return 0;
00237 }
00238 
00239 static int env_write(struct ast_channel *chan, const char *cmd, char *data,
00240           const char *value)
00241 {
00242    if (!ast_strlen_zero(data) && strncmp(data, "AST_", 4)) {
00243       if (!ast_strlen_zero(value)) {
00244          setenv(data, value, 1);
00245       } else {
00246          unsetenv(data);
00247       }
00248    }
00249 
00250    return 0;
00251 }
00252 
00253 static int stat_read(struct ast_channel *chan, const char *cmd, char *data,
00254           char *buf, size_t len)
00255 {
00256    char *action;
00257    struct stat s;
00258 
00259    ast_copy_string(buf, "0", len);
00260 
00261    action = strsep(&data, ",");
00262    if (stat(data, &s)) {
00263       return 0;
00264    } else {
00265       switch (*action) {
00266       case 'e':
00267          strcpy(buf, "1");
00268          break;
00269       case 's':
00270          snprintf(buf, len, "%d", (unsigned int) s.st_size);
00271          break;
00272       case 'f':
00273          snprintf(buf, len, "%d", S_ISREG(s.st_mode) ? 1 : 0);
00274          break;
00275       case 'd':
00276          snprintf(buf, len, "%d", S_ISDIR(s.st_mode) ? 1 : 0);
00277          break;
00278       case 'M':
00279          snprintf(buf, len, "%d", (int) s.st_mtime);
00280          break;
00281       case 'A':
00282          snprintf(buf, len, "%d", (int) s.st_mtime);
00283          break;
00284       case 'C':
00285          snprintf(buf, len, "%d", (int) s.st_ctime);
00286          break;
00287       case 'm':
00288          snprintf(buf, len, "%o", (int) s.st_mode);
00289          break;
00290       }
00291    }
00292 
00293    return 0;
00294 }
00295 
00296 enum file_format {
00297    FF_UNKNOWN = -1,
00298    FF_UNIX,
00299    FF_DOS,
00300    FF_MAC,
00301 };
00302 
00303 static int64_t count_lines(const char *filename, enum file_format newline_format)
00304 {
00305    int count = 0;
00306    char fbuf[4096];
00307    FILE *ff;
00308 
00309    if (!(ff = fopen(filename, "r"))) {
00310       ast_log(LOG_ERROR, "Unable to open '%s': %s\n", filename, strerror(errno));
00311       return -1;
00312    }
00313 
00314    while (fgets(fbuf, sizeof(fbuf), ff)) {
00315       char *next = fbuf, *first_cr = NULL, *first_nl = NULL;
00316 
00317       /* Must do it this way, because if the fileformat is FF_MAC, then Unix
00318        * assumptions about line-format will not come into play. */
00319       while (next) {
00320          if (newline_format == FF_DOS || newline_format == FF_MAC || newline_format == FF_UNKNOWN) {
00321             first_cr = strchr(next, '\r');
00322          }
00323          if (newline_format == FF_UNIX || newline_format == FF_UNKNOWN) {
00324             first_nl = strchr(next, '\n');
00325          }
00326 
00327          /* No terminators found in buffer */
00328          if (!first_cr && !first_nl) {
00329             break;
00330          }
00331 
00332          if (newline_format == FF_UNKNOWN) {
00333             if ((first_cr && !first_nl) || (first_cr && first_cr < first_nl)) {
00334                if (first_nl && first_nl == first_cr + 1) {
00335                   newline_format = FF_DOS;
00336                } else if (first_cr && first_cr == &fbuf[sizeof(fbuf) - 2]) {
00337                   /* Get it on the next pass */
00338                   fseek(ff, -1, SEEK_CUR);
00339                   break;
00340                } else {
00341                   newline_format = FF_MAC;
00342                   first_nl = NULL;
00343                }
00344             } else {
00345                newline_format = FF_UNIX;
00346                first_cr = NULL;
00347             }
00348             /* Jump down into next section */
00349          }
00350 
00351          if (newline_format == FF_DOS) {
00352             if (first_nl && first_cr && first_nl == first_cr + 1) {
00353                next = first_nl + 1;
00354                count++;
00355             } else if (first_cr == &fbuf[sizeof(fbuf) - 2]) {
00356                /* Get it on the next pass */
00357                fseek(ff, -1, SEEK_CUR);
00358                break;
00359             }
00360          } else if (newline_format == FF_MAC) {
00361             if (first_cr) {
00362                next = first_cr + 1;
00363                count++;
00364             }
00365          } else if (newline_format == FF_UNIX) {
00366             if (first_nl) {
00367                next = first_nl + 1;
00368                count++;
00369             }
00370          }
00371       }
00372    }
00373    fclose(ff);
00374 
00375    return count;
00376 }
00377 
00378 static int file_count_line(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
00379 {
00380    enum file_format newline_format = FF_UNKNOWN;
00381    int64_t count;
00382    AST_DECLARE_APP_ARGS(args,
00383       AST_APP_ARG(filename);
00384       AST_APP_ARG(format);
00385    );
00386 
00387    AST_STANDARD_APP_ARGS(args, data);
00388    if (args.argc > 1) {
00389       if (tolower(args.format[0]) == 'd') {
00390          newline_format = FF_DOS;
00391       } else if (tolower(args.format[0]) == 'm') {
00392          newline_format = FF_MAC;
00393       } else if (tolower(args.format[0]) == 'u') {
00394          newline_format = FF_UNIX;
00395       }
00396    }
00397 
00398    count = count_lines(args.filename, newline_format);
00399    ast_str_set(buf, len, "%" PRId64, count);
00400    return 0;
00401 }
00402 
00403 #define LINE_COUNTER(cptr, term, counter) \
00404    if (*cptr == '\n' && term == FF_UNIX) { \
00405       counter++; \
00406    } else if (*cptr == '\n' && term == FF_DOS && dos_state == 0) { \
00407       dos_state = 1; \
00408    } else if (*cptr == '\r' && term == FF_DOS && dos_state == 1) { \
00409       dos_state = 0; \
00410       counter++; \
00411    } else if (*cptr == '\r' && term == FF_MAC) { \
00412       counter++; \
00413    } else if (term == FF_DOS) { \
00414       dos_state = 0; \
00415    }
00416 
00417 static enum file_format file2format(const char *filename)
00418 {
00419    FILE *ff;
00420    char fbuf[4096];
00421    char *first_cr, *first_nl;
00422    enum file_format newline_format = FF_UNKNOWN;
00423 
00424    if (!(ff = fopen(filename, "r"))) {
00425       ast_log(LOG_ERROR, "Cannot open '%s': %s\n", filename, strerror(errno));
00426       return -1;
00427    }
00428 
00429    while (fgets(fbuf, sizeof(fbuf), ff)) {
00430       first_cr = strchr(fbuf, '\r');
00431       first_nl = strchr(fbuf, '\n');
00432 
00433       if (!first_cr && !first_nl) {
00434          continue;
00435       }
00436 
00437       if ((first_cr && !first_nl) || (first_cr && first_cr < first_nl)) {
00438 
00439          if (first_nl && first_nl == first_cr + 1) {
00440             newline_format = FF_DOS;
00441          } else if (first_cr && first_cr == &fbuf[sizeof(fbuf) - 2]) {
00442             /* Edge case: get it on the next pass */
00443             fseek(ff, -1, SEEK_CUR);
00444             continue;
00445          } else {
00446             newline_format = FF_MAC;
00447          }
00448       } else {
00449          newline_format = FF_UNIX;
00450       }
00451       break;
00452    }
00453    fclose(ff);
00454    return newline_format;
00455 }
00456 
00457 static int file_format(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
00458 {
00459    enum file_format newline_format = file2format(data);
00460    ast_str_set(buf, len, "%c", newline_format == FF_UNIX ? 'u' : newline_format == FF_DOS ? 'd' : newline_format == FF_MAC ? 'm' : 'x');
00461    return 0;
00462 }
00463 
00464 static int file_read(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
00465 {
00466    FILE *ff;
00467    int64_t offset = 0, length = LLONG_MAX;
00468    enum file_format format = FF_UNKNOWN;
00469    char fbuf[4096];
00470    int64_t flength, i; /* iterator needs to be signed, so it can go negative and terminate the loop */
00471    int64_t offset_offset = -1, length_offset = -1;
00472    char dos_state = 0;
00473    size_t readlen;
00474    AST_DECLARE_APP_ARGS(args,
00475       AST_APP_ARG(filename);
00476       AST_APP_ARG(offset);
00477       AST_APP_ARG(length);
00478       AST_APP_ARG(options);
00479       AST_APP_ARG(fileformat);
00480    );
00481 
00482    AST_STANDARD_APP_ARGS(args, data);
00483 
00484    if (args.argc > 1) {
00485       sscanf(args.offset, "%" SCNd64, &offset);
00486    }
00487    if (args.argc > 2) {
00488       sscanf(args.length, "%" SCNd64, &length);
00489    }
00490 
00491    if (args.argc < 4 || !strchr(args.options, 'l')) {
00492       /* Character-based mode */
00493       off_t off_i;
00494 
00495       if (!(ff = fopen(args.filename, "r"))) {
00496          ast_log(LOG_WARNING, "Cannot open file '%s' for reading: %s\n", args.filename, strerror(errno));
00497          return 0;
00498       }
00499 
00500       if (fseeko(ff, 0, SEEK_END) < 0) {
00501          ast_log(LOG_ERROR, "Cannot seek to end of '%s': %s\n", args.filename, strerror(errno));
00502          return -1;
00503       }
00504       flength = ftello(ff);
00505 
00506       if (offset < 0) {
00507          fseeko(ff, offset, SEEK_END);
00508          offset = ftello(ff);
00509       }
00510       if (length < 0) {
00511          fseeko(ff, length, SEEK_END);
00512          if ((length = ftello(ff)) - offset < 0) {
00513             /* Eliminates all results */
00514             return -1;
00515          }
00516       } else if (length == LLONG_MAX) {
00517          fseeko(ff, 0, SEEK_END);
00518          length = ftello(ff);
00519       }
00520 
00521       ast_str_reset(*buf);
00522 
00523       fseeko(ff, offset, SEEK_SET);
00524       for (off_i = ftello(ff); off_i < flength && off_i < offset + length; off_i += sizeof(fbuf)) {
00525          /* Calculate if we need to retrieve just a portion of the file in memory */
00526          size_t toappend = sizeof(fbuf);
00527 
00528          if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
00529             ast_log(LOG_ERROR, "Short read?!!\n");
00530             break;
00531          }
00532 
00533          /* Don't go past the length requested */
00534          if (off_i + toappend > offset + length) {
00535             toappend = length - off_i;
00536          }
00537 
00538          ast_str_append_substr(buf, len, fbuf, toappend);
00539       }
00540       return 0;
00541    }
00542 
00543    /* Line-based read */
00544    if (args.argc == 5) {
00545       if (tolower(args.fileformat[0]) == 'd') {
00546          format = FF_DOS;
00547       } else if (tolower(args.fileformat[0]) == 'm') {
00548          format = FF_MAC;
00549       } else if (tolower(args.fileformat[0]) == 'u') {
00550          format = FF_UNIX;
00551       }
00552    }
00553 
00554    if (format == FF_UNKNOWN) {
00555       if ((format = file2format(args.filename)) == FF_UNKNOWN) {
00556          ast_log(LOG_WARNING, "'%s' is not a line-based file\n", args.filename);
00557          return -1;
00558       }
00559    }
00560 
00561    if (offset < 0 && length <= offset) {
00562       /* Length eliminates all content */
00563       return -1;
00564    } else if (offset == 0) {
00565       offset_offset = 0;
00566    }
00567 
00568    if (!(ff = fopen(args.filename, "r"))) {
00569       ast_log(LOG_ERROR, "Cannot open '%s': %s\n", args.filename, strerror(errno));
00570       return -1;
00571    }
00572 
00573    if (fseek(ff, 0, SEEK_END)) {
00574       ast_log(LOG_ERROR, "Cannot seek to end of file '%s': %s\n", args.filename, strerror(errno));
00575       fclose(ff);
00576       return -1;
00577    }
00578 
00579    flength = ftello(ff);
00580 
00581    if (length == LLONG_MAX) {
00582       length_offset = flength;
00583    }
00584 
00585    /* For negative offset and/or negative length */
00586    if (offset < 0 || length < 0) {
00587       int64_t count = 0;
00588       /* Start with an even multiple of fbuf, so at the end of reading with a
00589        * 0 offset, we don't try to go past the beginning of the file. */
00590       for (i = (flength / sizeof(fbuf)) * sizeof(fbuf); i >= 0; i -= sizeof(fbuf)) {
00591          size_t end;
00592          char *pos;
00593          if (fseeko(ff, i, SEEK_SET)) {
00594             ast_log(LOG_ERROR, "Cannot seek to offset %" PRId64 ": %s\n", i, strerror(errno));
00595          }
00596          end = fread(fbuf, 1, sizeof(fbuf), ff);
00597          for (pos = end < sizeof(fbuf) ? fbuf + end - 1 : fbuf + sizeof(fbuf) - 1; pos > fbuf - 1; pos--) {
00598             LINE_COUNTER(pos, format, count);
00599 
00600             if (length < 0 && count * -1 == length) {
00601                length_offset = i + (pos - fbuf);
00602             } else if (offset < 0 && count * -1 == (offset - 1)) {
00603                /* Found our initial offset.  We're done with reverse motion! */
00604                if (format == FF_DOS) {
00605                   offset_offset = i + (pos - fbuf) + 2;
00606                } else {
00607                   offset_offset = i + (pos - fbuf) + 1;
00608                }
00609                break;
00610             }
00611          }
00612          if ((offset < 0 && offset_offset >= 0) || (offset >= 0 && length_offset >= 0)) {
00613             break;
00614          }
00615       }
00616       /* We're at the beginning, and the negative offset indicates the exact number of lines in the file */
00617       if (offset < 0 && offset_offset < 0 && offset == count * -1) {
00618          offset_offset = 0;
00619       }
00620    }
00621 
00622    /* Positve line offset */
00623    if (offset > 0) {
00624       int64_t count = 0;
00625       fseek(ff, 0, SEEK_SET);
00626       for (i = 0; i < flength; i += sizeof(fbuf)) {
00627          char *pos;
00628          if (i + sizeof(fbuf) <= flength) {
00629             /* Don't let previous values influence current counts, due to short reads */
00630             memset(fbuf, 0, sizeof(fbuf));
00631          }
00632          if (fread(fbuf, 1, sizeof(fbuf), ff) && !feof(ff)) {
00633             ast_log(LOG_ERROR, "Short read?!!\n");
00634             fclose(ff);
00635             return -1;
00636          }
00637          for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
00638             LINE_COUNTER(pos, format, count);
00639 
00640             if (count == offset) {
00641                offset_offset = i + (pos - fbuf) + 1;
00642                break;
00643             }
00644          }
00645          if (offset_offset >= 0) {
00646             break;
00647          }
00648       }
00649    }
00650 
00651    if (offset_offset < 0) {
00652       ast_log(LOG_ERROR, "Offset '%s' refers to before the beginning of the file!\n", args.offset);
00653       fclose(ff);
00654       return -1;
00655    }
00656 
00657    ast_str_reset(*buf);
00658    if (fseeko(ff, offset_offset, SEEK_SET)) {
00659       ast_log(LOG_ERROR, "fseeko failed: %s\n", strerror(errno));
00660    }
00661 
00662    /* If we have both offset_offset and length_offset, then grabbing the
00663     * buffer is simply a matter of just retrieving the file and adding it
00664     * to buf.  Otherwise, we need to run byte-by-byte forward until the
00665     * length is complete. */
00666    if (length_offset >= 0) {
00667       ast_debug(3, "offset=%" PRId64 ", length=%" PRId64 ", offset_offset=%" PRId64 ", length_offset=%" PRId64 "\n", offset, length, offset_offset, length_offset);
00668       for (i = offset_offset; i < length_offset; i += sizeof(fbuf)) {
00669          if (fread(fbuf, 1, i + sizeof(fbuf) > flength ? flength - i : sizeof(fbuf), ff) < (i + sizeof(fbuf) > flength ? flength - i : sizeof(fbuf))) {
00670             ast_log(LOG_ERROR, "Short read?!!\n");
00671          }
00672          ast_debug(3, "Appending first %" PRId64" bytes of fbuf=%s\n", i + sizeof(fbuf) > length_offset ? length_offset - i : sizeof(fbuf), fbuf);
00673          ast_str_append_substr(buf, len, fbuf, i + sizeof(fbuf) > length_offset ? length_offset - i : sizeof(fbuf));
00674       }
00675    } else if (length == 0) {
00676       /* Nothing to do */
00677    } else {
00678       /* Positive line offset */
00679       int64_t current_length = 0;
00680       char dos_state = 0;
00681       ast_debug(3, "offset=%" PRId64 ", length=%" PRId64 ", offset_offset=%" PRId64 ", length_offset=%" PRId64 "\n", offset, length, offset_offset, length_offset);
00682       for (i = offset_offset; i < flength; i += sizeof(fbuf)) {
00683          char *pos;
00684          if ((readlen = fread(fbuf, 1, sizeof(fbuf), ff)) < sizeof(fbuf) && !feof(ff)) {
00685             ast_log(LOG_ERROR, "Short read?!!\n");
00686          }
00687          for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
00688             LINE_COUNTER(pos, format, current_length);
00689 
00690             if (current_length == length) {
00691                length_offset = i + (pos - fbuf) + 1;
00692                break;
00693             }
00694          }
00695          ast_debug(3, "length_offset=%" PRId64 ", length_offset - i=%" PRId64 "\n", length_offset, length_offset - i);
00696          ast_str_append_substr(buf, len, fbuf, length_offset >= 0 ? length_offset - i : flength > i + sizeof(fbuf)) ? sizeof(fbuf) : flength - i;
00697 
00698          if (length_offset >= 0) {
00699             break;
00700          }
00701       }
00702    }
00703 
00704    return 0;
00705 }
00706 
00707 const char *format2term(enum file_format f) __attribute__((const));
00708 const char *format2term(enum file_format f)
00709 {
00710    const char *term[] = { "", "\n", "\r\n", "\r" };
00711    return term[f + 1];
00712 }
00713 
00714 static int file_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
00715 {
00716    AST_DECLARE_APP_ARGS(args,
00717       AST_APP_ARG(filename);
00718       AST_APP_ARG(offset);
00719       AST_APP_ARG(length);
00720       AST_APP_ARG(options);
00721       AST_APP_ARG(format);
00722    );
00723    int64_t offset = 0, length = LLONG_MAX;
00724    off_t flength, vlength;
00725    size_t foplen = 0;
00726    FILE *ff;
00727 
00728    AST_STANDARD_APP_ARGS(args, data);
00729 
00730    if (args.argc > 1) {
00731       sscanf(args.offset, "%" SCNd64, &offset);
00732    }
00733    if (args.argc > 2) {
00734       sscanf(args.length, "%" SCNd64, &length);
00735    }
00736 
00737    vlength = strlen(value);
00738 
00739    if (args.argc < 4 || !strchr(args.options, 'l')) {
00740       /* Character-based mode */
00741 
00742       if (args.argc > 3 && strchr(args.options, 'a')) {
00743          /* Append mode */
00744          if (!(ff = fopen(args.filename, "a"))) {
00745             ast_log(LOG_WARNING, "Cannot open file '%s' for appending: %s\n", args.filename, strerror(errno));
00746             return 0;
00747          }
00748          if (fwrite(value, 1, vlength, ff) < vlength) {
00749             ast_log(LOG_ERROR, "Short write?!!\n");
00750          }
00751          fclose(ff);
00752          return 0;
00753       } else if (offset == 0 && length == LLONG_MAX) {
00754          if (!(ff = fopen(args.filename, "w"))) {
00755             ast_log(LOG_WARNING, "Cannot open file '%s' for writing: %s\n", args.filename, strerror(errno));
00756             return 0;
00757          }
00758          if (fwrite(value, 1, vlength, ff) < vlength) {
00759             ast_log(LOG_ERROR, "Short write?!!\n");
00760          }
00761          fclose(ff);
00762          return 0;
00763       }
00764 
00765       if (!(ff = fopen(args.filename, "r+"))) {
00766          ast_log(LOG_WARNING, "Cannot open file '%s' for modification: %s\n", args.filename, strerror(errno));
00767          return 0;
00768       }
00769       fseeko(ff, 0, SEEK_END);
00770       flength = ftello(ff);
00771 
00772       if (offset < 0) {
00773          if (fseeko(ff, offset, SEEK_END)) {
00774             ast_log(LOG_ERROR, "Cannot seek to offset: %s\n", strerror(errno));
00775          }
00776          offset = ftello(ff);
00777       }
00778 
00779       if (length < 0) {
00780          length = flength - offset + length;
00781          if (length < 0) {
00782             ast_log(LOG_ERROR, "Length '%s' exceeds the file length.  No data will be written.\n", args.length);
00783             fclose(ff);
00784             return -1;
00785          }
00786       }
00787 
00788       fseeko(ff, offset, SEEK_SET);
00789 
00790       ast_debug(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 ", vlength=%" PRId64 ", flength=%" PRId64 "\n",
00791          S_OR(args.offset, "(null)"), offset, S_OR(args.length, "(null)"), length, vlength, flength);
00792 
00793       if (length == vlength) {
00794          /* Simplest case, a straight replace */
00795          if (fwrite(value, 1, vlength, ff) < vlength) {
00796             ast_log(LOG_ERROR, "Short write?!!\n");
00797          }
00798          fclose(ff);
00799       } else if (length == LLONG_MAX) {
00800          /* Simple truncation */
00801          if (fwrite(value, 1, vlength, ff) < vlength) {
00802             ast_log(LOG_ERROR, "Short write?!!\n");
00803          }
00804          fclose(ff);
00805          if (truncate(args.filename, offset + vlength)) {
00806             ast_log(LOG_ERROR, "Unable to truncate the file: %s\n", strerror(errno));
00807          }
00808       } else if (length > vlength) {
00809          /* More complex -- need to close a gap */
00810          char fbuf[4096];
00811          off_t cur;
00812          if (fwrite(value, 1, vlength, ff) < vlength) {
00813             ast_log(LOG_ERROR, "Short write?!!\n");
00814          }
00815          fseeko(ff, length - vlength, SEEK_CUR);
00816          while ((cur = ftello(ff)) < flength) {
00817             if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
00818                ast_log(LOG_ERROR, "Short read?!!\n");
00819             }
00820             fseeko(ff, cur + vlength - length, SEEK_SET);
00821             if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
00822                ast_log(LOG_ERROR, "Short write?!!\n");
00823             }
00824             /* Seek to where we stopped reading */
00825             if (fseeko(ff, cur + sizeof(fbuf), SEEK_SET) < 0) {
00826                /* Only reason for seek to fail is EOF */
00827                break;
00828             }
00829          }
00830          fclose(ff);
00831          if (truncate(args.filename, flength - (length - vlength))) {
00832             ast_log(LOG_ERROR, "Unable to truncate the file: %s\n", strerror(errno));
00833          }
00834       } else {
00835          /* Most complex -- need to open a gap */
00836          char fbuf[4096];
00837          off_t lastwritten = flength + vlength - length;
00838 
00839          /* Start reading exactly the buffer size back from the end. */
00840          fseeko(ff, flength - sizeof(fbuf), SEEK_SET);
00841          while (offset < ftello(ff)) {
00842             if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
00843                ast_log(LOG_ERROR, "Short read?!!\n");
00844                fclose(ff);
00845                return -1;
00846             }
00847             /* Since the read moved our file ptr forward, we reverse, but
00848              * seek an offset equal to the amount we want to extend the
00849              * file by */
00850             fseeko(ff, vlength - length - sizeof(fbuf), SEEK_CUR);
00851 
00852             /* Note the location of this buffer -- we must not overwrite this position. */
00853             lastwritten = ftello(ff);
00854 
00855             if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
00856                ast_log(LOG_ERROR, "Short write?!!\n");
00857                fclose(ff);
00858                return -1;
00859             }
00860 
00861             if (lastwritten < offset + sizeof(fbuf)) {
00862                break;
00863             }
00864             /* Our file pointer is now either pointing to the end of the
00865              * file (new position) or a multiple of the fbuf size back from
00866              * that point.  Move back to where we want to start reading
00867              * again.  We never actually try to read beyond the end of the
00868              * file, so we don't have do deal with short reads, as we would
00869              * when we're shortening the file. */
00870             fseeko(ff, 2 * sizeof(fbuf) + vlength - length, SEEK_CUR);
00871          }
00872 
00873          /* Last part of the file that we need to preserve */
00874          if (fseeko(ff, offset + length, SEEK_SET)) {
00875             ast_log(LOG_WARNING, "Unable to seek to %" PRId64 " + %" PRId64 " != %" PRId64 "?)\n", offset, length, ftello(ff));
00876          }
00877 
00878          /* Doesn't matter how much we read -- just need to restrict the write */
00879          ast_debug(1, "Reading at %" PRId64 "\n", ftello(ff));
00880          if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
00881             ast_log(LOG_ERROR, "Short read?!!\n");
00882          }
00883          fseek(ff, offset, SEEK_SET);
00884          /* Write out the value, then write just up until where we last moved some data */
00885          if (fwrite(value, 1, vlength, ff) < vlength) {
00886             ast_log(LOG_ERROR, "Short write?!!\n");
00887          } else {
00888             off_t curpos = ftello(ff);
00889             foplen = lastwritten - curpos;
00890             if (fwrite(fbuf, 1, foplen, ff) < foplen) {
00891                ast_log(LOG_ERROR, "Short write?!!\n");
00892             }
00893          }
00894          fclose(ff);
00895       }
00896    } else {
00897       enum file_format newline_format = FF_UNKNOWN;
00898 
00899       /* Line mode */
00900       if (args.argc == 5) {
00901          if (tolower(args.format[0]) == 'u') {
00902             newline_format = FF_UNIX;
00903          } else if (tolower(args.format[0]) == 'm') {
00904             newline_format = FF_MAC;
00905          } else if (tolower(args.format[0]) == 'd') {
00906             newline_format = FF_DOS;
00907          }
00908       }
00909       if (newline_format == FF_UNKNOWN && (newline_format = file2format(args.filename)) == FF_UNKNOWN) {
00910          ast_log(LOG_ERROR, "File '%s' not in line format\n", args.filename);
00911          return -1;
00912       }
00913 
00914       if (strchr(args.options, 'a')) {
00915          /* Append to file */
00916          if (!(ff = fopen(args.filename, "a"))) {
00917             ast_log(LOG_ERROR, "Unable to open '%s' for appending: %s\n", args.filename, strerror(errno));
00918             return -1;
00919          }
00920          if (fwrite(value, 1, vlength, ff) < vlength) {
00921             ast_log(LOG_ERROR, "Short write?!!\n");
00922          } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
00923             ast_log(LOG_ERROR, "Short write?!!\n");
00924          }
00925          fclose(ff);
00926       } else if (offset == 0 && length == LLONG_MAX) {
00927          /* Overwrite file */
00928          off_t truncsize;
00929          if (!(ff = fopen(args.filename, "w"))) {
00930             ast_log(LOG_ERROR, "Unable to open '%s' for writing: %s\n", args.filename, strerror(errno));
00931             return -1;
00932          }
00933          if (fwrite(value, 1, vlength, ff) < vlength) {
00934             ast_log(LOG_ERROR, "Short write?!!\n");
00935          } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
00936             ast_log(LOG_ERROR, "Short write?!!\n");
00937          }
00938          truncsize = ftello(ff);
00939          fclose(ff);
00940          if (truncate(args.filename, truncsize)) {
00941             ast_log(LOG_ERROR, "Unable to truncate file: %s\n", strerror(errno));
00942          }
00943       } else {
00944          int64_t offset_offset = (offset == 0 ? 0 : -1), length_offset = -1, flength, i, current_length = 0;
00945          char dos_state = 0, fbuf[4096];
00946 
00947          if (offset < 0 && length < offset) {
00948             /* Nonsense! */
00949             ast_log(LOG_ERROR, "Length cannot specify a position prior to the offset\n");
00950             return -1;
00951          }
00952 
00953          if (!(ff = fopen(args.filename, "r+"))) {
00954             ast_log(LOG_ERROR, "Cannot open '%s' for modification: %s\n", args.filename, strerror(errno));
00955             return -1;
00956          }
00957 
00958          if (fseek(ff, 0, SEEK_END)) {
00959             ast_log(LOG_ERROR, "Cannot seek to end of file '%s': %s\n", args.filename, strerror(errno));
00960             fclose(ff);
00961             return -1;
00962          }
00963          flength = ftello(ff);
00964 
00965          /* For negative offset and/or negative length */
00966          if (offset < 0 || length < 0) {
00967             int64_t count = 0;
00968             for (i = (flength / sizeof(fbuf)) * sizeof(fbuf); i >= 0; i -= sizeof(fbuf)) {
00969                char *pos;
00970                if (fseeko(ff, i, SEEK_SET)) {
00971                   ast_log(LOG_ERROR, "Cannot seek to offset %" PRId64 ": %s\n", i, strerror(errno));
00972                }
00973                if (i + sizeof(fbuf) >= flength) {
00974                   memset(fbuf, 0, sizeof(fbuf));
00975                }
00976                if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
00977                   ast_log(LOG_ERROR, "Short read: %s\n", strerror(errno));
00978                   fclose(ff);
00979                   return -1;
00980                }
00981                for (pos = fbuf + sizeof(fbuf) - 1; pos > fbuf - 1; pos--) {
00982                   LINE_COUNTER(pos, newline_format, count);
00983 
00984                   if (length < 0 && count * -1 == length) {
00985                      length_offset = i + (pos - fbuf);
00986                   } else if (offset < 0 && count * -1 == (offset - 1)) {
00987                      /* Found our initial offset.  We're done with reverse motion! */
00988                      if (newline_format == FF_DOS) {
00989                         offset_offset = i + (pos - fbuf) + 2;
00990                      } else {
00991                         offset_offset = i + (pos - fbuf) + 1;
00992                      }
00993                      break;
00994                   }
00995                }
00996                if ((offset < 0 && offset_offset >= 0) || (offset >= 0 && length_offset >= 0)) {
00997                   break;
00998                }
00999             }
01000             /* We're at the beginning, and the negative offset indicates the exact number of lines in the file */
01001             if (offset < 0 && offset_offset < 0 && offset == count * -1) {
01002                offset_offset = 0;
01003             }
01004          }
01005 
01006          /* Positve line offset */
01007          if (offset > 0) {
01008             int64_t count = 0;
01009             fseek(ff, 0, SEEK_SET);
01010             for (i = 0; i < flength; i += sizeof(fbuf)) {
01011                char *pos;
01012                if (i + sizeof(fbuf) >= flength) {
01013                   memset(fbuf, 0, sizeof(fbuf));
01014                }
01015                if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
01016                   ast_log(LOG_ERROR, "Short read?!!\n");
01017                   fclose(ff);
01018                   return -1;
01019                }
01020                for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
01021                   LINE_COUNTER(pos, newline_format, count);
01022 
01023                   if (count == offset) {
01024                      offset_offset = i + (pos - fbuf) + 1;
01025                      break;
01026                   }
01027                }
01028                if (offset_offset >= 0) {
01029                   break;
01030                }
01031             }
01032          }
01033 
01034          if (offset_offset < 0) {
01035             ast_log(LOG_ERROR, "Offset '%s' refers to before the beginning of the file!\n", args.offset);
01036             fclose(ff);
01037             return -1;
01038          }
01039 
01040          if (length == 0) {
01041             length_offset = offset_offset;
01042          } else if (length == LLONG_MAX) {
01043             length_offset = flength;
01044          }
01045 
01046          /* Positive line length */
01047          if (length_offset < 0) {
01048             fseeko(ff, offset_offset, SEEK_SET);
01049             for (i = offset_offset; i < flength; i += sizeof(fbuf)) {
01050                char *pos;
01051                if (i + sizeof(fbuf) >= flength) {
01052                   memset(fbuf, 0, sizeof(fbuf));
01053                }
01054                if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
01055                   ast_log(LOG_ERROR, "Short read?!!\n");
01056                   fclose(ff);
01057                   return -1;
01058                }
01059                for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
01060                   LINE_COUNTER(pos, newline_format, current_length);
01061 
01062                   if (current_length == length) {
01063                      length_offset = i + (pos - fbuf) + 1;
01064                      break;
01065                   }
01066                }
01067                if (length_offset >= 0) {
01068                   break;
01069                }
01070             }
01071             if (length_offset < 0) {
01072                /* Exceeds length of file */
01073                ast_debug(3, "Exceeds length of file? length=%" PRId64 ", count=%" PRId64 ", flength=%" PRId64 "\n", length, current_length, flength);
01074                length_offset = flength;
01075             }
01076          }
01077 
01078          /* Have offset_offset and length_offset now */
01079          if (length_offset - offset_offset == vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)))) {
01080             /* Simple case - replacement of text inline */
01081             fseeko(ff, offset_offset, SEEK_SET);
01082             if (fwrite(value, 1, vlength, ff) < vlength) {
01083                ast_log(LOG_ERROR, "Short write?!!\n");
01084             } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
01085                ast_log(LOG_ERROR, "Short write?!!\n");
01086             }
01087             fclose(ff);
01088          } else if (length_offset - offset_offset > vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)))) {
01089             /* More complex case - need to shorten file */
01090             off_t cur;
01091             int64_t length_length = length_offset - offset_offset;
01092             size_t vlen = vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)));
01093 
01094             ast_debug(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 " (%" PRId64 "), vlength=%" PRId64 ", flength=%" PRId64 "\n",
01095                args.offset, offset_offset, args.length, length_offset, length_length, vlength, flength);
01096 
01097             fseeko(ff, offset_offset, SEEK_SET);
01098             if (fwrite(value, 1, vlength, ff) < vlength) {
01099                ast_log(LOG_ERROR, "Short write?!!\n");
01100                fclose(ff);
01101                return -1;
01102             } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, vlen - vlength, ff) < vlen - vlength) {
01103                ast_log(LOG_ERROR, "Short write?!!\n");
01104                fclose(ff);
01105                return -1;
01106             }
01107             while ((cur = ftello(ff)) < flength) {
01108                fseeko(ff, length_length - vlen, SEEK_CUR);
01109                if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
01110                   ast_log(LOG_ERROR, "Short read?!!\n");
01111                   fclose(ff);
01112                   return -1;
01113                }
01114                /* Seek to where we last stopped writing */
01115                fseeko(ff, cur, SEEK_SET);
01116                if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
01117                   ast_log(LOG_ERROR, "Short write?!!\n");
01118                   fclose(ff);
01119                   return -1;
01120                }
01121             }
01122             fclose(ff);
01123             if (truncate(args.filename, flength - (length_length - vlen))) {
01124                ast_log(LOG_ERROR, "Truncation of file failed: %s\n", strerror(errno));
01125             }
01126          } else {
01127             /* Most complex case - need to lengthen file */
01128             size_t vlen = vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)));
01129             int64_t origlen = length_offset - offset_offset;
01130             off_t lastwritten = flength + vlen - origlen;
01131 
01132             ast_debug(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 ", vlength=%" PRId64 ", flength=%" PRId64 "\n",
01133                args.offset, offset_offset, args.length, length_offset, vlength, flength);
01134 
01135             fseeko(ff, flength - sizeof(fbuf), SEEK_SET);
01136             while (offset_offset + sizeof(fbuf) < ftello(ff)) {
01137                if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
01138                   ast_log(LOG_ERROR, "Short read?!!\n");
01139                   fclose(ff);
01140                   return -1;
01141                }
01142                fseeko(ff, sizeof(fbuf) - vlen - origlen, SEEK_CUR);
01143                if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
01144                   ast_log(LOG_ERROR, "Short write?!!\n");
01145                   fclose(ff);
01146                   return -1;
01147                }
01148                if ((lastwritten = ftello(ff) - sizeof(fbuf)) < offset_offset + sizeof(fbuf)) {
01149                   break;
01150                }
01151                fseeko(ff, 2 * sizeof(fbuf) + vlen - origlen, SEEK_CUR);
01152             }
01153             fseek(ff, length_offset, SEEK_SET);
01154             if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
01155                ast_log(LOG_ERROR, "Short read?!!\n");
01156                fclose(ff);
01157                return -1;
01158             }
01159             fseek(ff, offset_offset, SEEK_SET);
01160             if (fwrite(value, 1, vlength, ff) < vlength) {
01161                ast_log(LOG_ERROR, "Short write?!!\n");
01162                fclose(ff);
01163                return -1;
01164             } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
01165                ast_log(LOG_ERROR, "Short write?!!\n");
01166                fclose(ff);
01167                return -1;
01168             } else {
01169                off_t curpos = ftello(ff);
01170                foplen = lastwritten - curpos;
01171                if (fwrite(fbuf, 1, foplen, ff) < foplen) {
01172                   ast_log(LOG_ERROR, "Short write?!!\n");
01173                }
01174             }
01175             fclose(ff);
01176          }
01177       }
01178    }
01179 
01180    return 0;
01181 }
01182 
01183 static struct ast_custom_function env_function = {
01184    .name = "ENV",
01185    .read = env_read,
01186    .write = env_write
01187 };
01188 
01189 static struct ast_custom_function stat_function = {
01190    .name = "STAT",
01191    .read = stat_read,
01192    .read_max = 12,
01193 };
01194 
01195 static struct ast_custom_function file_function = {
01196    .name = "FILE",
01197    .read2 = file_read,
01198    .write = file_write,
01199 };
01200 
01201 static struct ast_custom_function file_count_line_function = {
01202    .name = "FILE_COUNT_LINE",
01203    .read2 = file_count_line,
01204    .read_max = 12,
01205 };
01206 
01207 static struct ast_custom_function file_format_function = {
01208    .name = "FILE_FORMAT",
01209    .read2 = file_format,
01210    .read_max = 2,
01211 };
01212 
01213 static int unload_module(void)
01214 {
01215    int res = 0;
01216 
01217    res |= ast_custom_function_unregister(&env_function);
01218    res |= ast_custom_function_unregister(&stat_function);
01219    res |= ast_custom_function_unregister(&file_function);
01220    res |= ast_custom_function_unregister(&file_count_line_function);
01221    res |= ast_custom_function_unregister(&file_format_function);
01222 
01223    return res;
01224 }
01225 
01226 static int load_module(void)
01227 {
01228    int res = 0;
01229 
01230    res |= ast_custom_function_register(&env_function);
01231    res |= ast_custom_function_register(&stat_function);
01232    res |= ast_custom_function_register(&file_function);
01233    res |= ast_custom_function_register(&file_count_line_function);
01234    res |= ast_custom_function_register(&file_format_function);
01235 
01236    return res;
01237 }
01238 
01239 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Environment/filesystem dialplan functions");

Generated on Wed Apr 6 11:29:45 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7