update comment to match the state of the code
[asterisk-bristuff.git] / apps / app_readfile.c
blob7e43a38064c89fc4059d9a771133547dd003580e
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Matt O'Gorman <mogorman@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
19 /*! \file
21 * \brief ReadFile application -- Reads in a File for you.
23 * \author Matt O'Gorman <mogorman@digium.com>
25 * \ingroup applications
28 #include "asterisk.h"
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <string.h>
37 #include "asterisk/file.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/options.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/app.h"
43 #include "asterisk/module.h"
45 static char *app_readfile = "ReadFile";
47 static char *readfile_synopsis = "ReadFile(varname=file,length)";
49 static char *readfile_descrip =
50 "ReadFile(varname=file,length)\n"
51 " Varname - Result stored here.\n"
52 " File - The name of the file to read.\n"
53 " Length - Maximum number of characters to capture.\n";
56 static int readfile_exec(struct ast_channel *chan, void *data)
58 int res=0;
59 struct ast_module_user *u;
60 char *s, *varname=NULL, *file=NULL, *length=NULL, *returnvar=NULL;
61 int len=0;
63 if (ast_strlen_zero(data)) {
64 ast_log(LOG_WARNING, "ReadFile require an argument!\n");
65 return -1;
68 u = ast_module_user_add(chan);
70 s = ast_strdupa(data);
72 varname = strsep(&s, "=");
73 file = strsep(&s, "|");
74 length = s;
76 if (!varname || !file) {
77 ast_log(LOG_ERROR, "No file or variable specified!\n");
78 ast_module_user_remove(u);
79 return -1;
82 if (length) {
83 if ((sscanf(length, "%d", &len) != 1) || (len < 0)) {
84 ast_log(LOG_WARNING, "%s is not a positive number, defaulting length to max\n", length);
85 len = 0;
89 if ((returnvar = ast_read_textfile(file))) {
90 if (len > 0) {
91 if (len < strlen(returnvar))
92 returnvar[len]='\0';
93 else
94 ast_log(LOG_WARNING, "%s is longer than %d, and %d \n", file, len, (int)strlen(returnvar));
96 pbx_builtin_setvar_helper(chan, varname, returnvar);
97 free(returnvar);
99 ast_module_user_remove(u);
100 return res;
104 static int unload_module(void)
106 int res;
108 res = ast_unregister_application(app_readfile);
110 ast_module_user_hangup_all();
112 return res;
115 static int load_module(void)
117 return ast_register_application(app_readfile, readfile_exec, readfile_synopsis, readfile_descrip);
120 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Stores output of file into a variable");