2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
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.
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.
19 * \brief Environment related dialplan functions
26 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include "asterisk/module.h"
31 #include "asterisk/channel.h"
32 #include "asterisk/pbx.h"
33 #include "asterisk/utils.h"
34 #include "asterisk/app.h"
36 static int env_read(struct ast_channel *chan, const char *cmd, char *data,
37 char *buf, size_t len)
47 ast_copy_string(buf, ret, len);
52 static int env_write(struct ast_channel *chan, const char *cmd, char *data,
55 if (!ast_strlen_zero(data)) {
56 if (!ast_strlen_zero(value)) {
57 setenv(data, value, 1);
66 static int stat_read(struct ast_channel *chan, const char *cmd, char *data,
67 char *buf, size_t len)
74 action = strsep(&data, ",");
83 snprintf(buf, len, "%d", (unsigned int) s.st_size);
86 snprintf(buf, len, "%d", S_ISREG(s.st_mode) ? 1 : 0);
89 snprintf(buf, len, "%d", S_ISDIR(s.st_mode) ? 1 : 0);
92 snprintf(buf, len, "%d", (int) s.st_mtime);
95 snprintf(buf, len, "%d", (int) s.st_mtime);
98 snprintf(buf, len, "%d", (int) s.st_ctime);
101 snprintf(buf, len, "%o", (int) s.st_mode);
109 static int file_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
111 AST_DECLARE_APP_ARGS(args,
112 AST_APP_ARG(filename);
116 int offset = 0, length;
119 AST_STANDARD_APP_ARGS(args, data);
121 offset = atoi(args.offset);
124 if ((length = atoi(args.length)) < 1) {
125 ast_log(LOG_WARNING, "Invalid length '%s'. Returning the max (%d)\n", args.length, (int)len);
127 } else if (length > len) {
128 ast_log(LOG_WARNING, "Length %d is greater than the max (%d). Truncating output.\n", length, (int)len);
134 if (!(contents = ast_read_textfile(args.filename)))
138 ast_copy_string(buf, &contents[offset], length);
140 size_t tmp = strlen(contents);
141 if (offset * -1 > tmp) {
142 ast_log(LOG_WARNING, "Offset is larger than the file size.\n");
145 ast_copy_string(buf, &contents[tmp + offset], length);
152 static struct ast_custom_function env_function = {
154 .synopsis = "Gets or sets the environment variable specified",
155 .syntax = "ENV(<envname>)",
160 static struct ast_custom_function stat_function = {
162 .synopsis = "Does a check on the specified file",
163 .syntax = "STAT(<flag>,<filename>)",
166 "flag may be one of the following:\n"
167 " d - Checks if the file is a directory\n"
168 " e - Checks if the file exists\n"
169 " f - Checks if the file is a regular file\n"
170 " m - Returns the file mode (in octal)\n"
171 " s - Returns the size (in bytes) of the file\n"
172 " A - Returns the epoch at which the file was last accessed\n"
173 " C - Returns the epoch at which the inode was last changed\n"
174 " M - Returns the epoch at which the file was last modified\n",
177 static struct ast_custom_function file_function = {
179 .synopsis = "Obtains the contents of a file",
180 .syntax = "FILE(<filename>,<offset>,<length>)",
183 * Some enterprising programmer could probably add write functionality
184 * to FILE(), although I'm not sure how useful it would be. Hence why
185 * it's called FILE and not READFILE (like the app was).
188 "<offset> may be specified as any number. If negative, <offset> specifies\n"
189 " the number of bytes back from the end of the file.\n"
190 "<length>, if specified, will limit the length of the data read to that size.\n",
193 static int unload_module(void)
197 res |= ast_custom_function_unregister(&env_function);
198 res |= ast_custom_function_unregister(&stat_function);
199 res |= ast_custom_function_unregister(&file_function);
204 static int load_module(void)
208 res |= ast_custom_function_register(&env_function);
209 res |= ast_custom_function_register(&stat_function);
210 res |= ast_custom_function_register(&file_function);
215 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Environment/filesystem dialplan functions");