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
25 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
30 #include <sys/types.h>
33 #include "asterisk/module.h"
34 #include "asterisk/channel.h"
35 #include "asterisk/pbx.h"
36 #include "asterisk/logger.h"
37 #include "asterisk/utils.h"
38 #include "asterisk/app.h"
40 static int env_read(struct ast_channel
*chan
, char *cmd
, char *data
,
41 char *buf
, size_t len
)
51 ast_copy_string(buf
, ret
, len
);
56 static int env_write(struct ast_channel
*chan
, char *cmd
, char *data
,
59 if (!ast_strlen_zero(data
)) {
60 if (!ast_strlen_zero(value
)) {
61 setenv(data
, value
, 1);
70 static int stat_read(struct ast_channel
*chan
, char *cmd
, char *data
,
71 char *buf
, size_t len
)
76 ast_copy_string(buf
, "0", len
);
78 action
= strsep(&data
, "|");
87 snprintf(buf
, len
, "%d", (unsigned int) s
.st_size
);
90 snprintf(buf
, len
, "%d", S_ISREG(s
.st_mode
) ? 1 : 0);
93 snprintf(buf
, len
, "%d", S_ISDIR(s
.st_mode
) ? 1 : 0);
96 snprintf(buf
, len
, "%d", (int) s
.st_mtime
);
99 snprintf(buf
, len
, "%d", (int) s
.st_mtime
);
102 snprintf(buf
, len
, "%d", (int) s
.st_ctime
);
105 snprintf(buf
, len
, "%o", (int) s
.st_mode
);
113 static struct ast_custom_function env_function
= {
115 .synopsis
= "Gets or sets the environment variable specified",
116 .syntax
= "ENV(<envname>)",
121 static struct ast_custom_function stat_function
= {
123 .synopsis
= "Does a check on the specified file",
124 .syntax
= "STAT(<flag>,<filename>)",
127 "flag may be one of the following:\n"
128 " d - Checks if the file is a directory\n"
129 " e - Checks if the file exists\n"
130 " f - Checks if the file is a regular file\n"
131 " m - Returns the file mode (in octal)\n"
132 " s - Returns the size (in bytes) of the file\n"
133 " A - Returns the epoch at which the file was last accessed\n"
134 " C - Returns the epoch at which the inode was last changed\n"
135 " M - Returns the epoch at which the file was last modified\n",
138 static int unload_module(void)
142 res
|= ast_custom_function_unregister(&env_function
);
143 res
|= ast_custom_function_unregister(&stat_function
);
148 static int load_module(void)
152 res
|= ast_custom_function_register(&env_function
);
153 res
|= ast_custom_function_register(&stat_function
);
158 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Environment/filesystem dialplan functions");