Let's also include aclocal.m4
[asterisk-bristuff.git] / funcs / func_env.c
blob01ec4958debcad1821db2673aba64bc540bede0a
1 /*
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.
17 /*! \file
19 * \brief Environment related dialplan functions
23 #include "asterisk.h"
25 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/stat.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)
43 char *ret = NULL;
45 *buf = '\0';
47 if (data)
48 ret = getenv(data);
50 if (ret)
51 ast_copy_string(buf, ret, len);
53 return 0;
56 static int env_write(struct ast_channel *chan, char *cmd, char *data,
57 const char *value)
59 if (!ast_strlen_zero(data)) {
60 if (!ast_strlen_zero(value)) {
61 setenv(data, value, 1);
62 } else {
63 unsetenv(data);
67 return 0;
70 static int stat_read(struct ast_channel *chan, char *cmd, char *data,
71 char *buf, size_t len)
73 char *action;
74 struct stat s;
76 ast_copy_string(buf, "0", len);
78 action = strsep(&data, "|");
79 if (stat(data, &s)) {
80 return 0;
81 } else {
82 switch (*action) {
83 case 'e':
84 strcpy(buf, "1");
85 break;
86 case 's':
87 snprintf(buf, len, "%d", (unsigned int) s.st_size);
88 break;
89 case 'f':
90 snprintf(buf, len, "%d", S_ISREG(s.st_mode) ? 1 : 0);
91 break;
92 case 'd':
93 snprintf(buf, len, "%d", S_ISDIR(s.st_mode) ? 1 : 0);
94 break;
95 case 'M':
96 snprintf(buf, len, "%d", (int) s.st_mtime);
97 break;
98 case 'A':
99 snprintf(buf, len, "%d", (int) s.st_mtime);
100 break;
101 case 'C':
102 snprintf(buf, len, "%d", (int) s.st_ctime);
103 break;
104 case 'm':
105 snprintf(buf, len, "%o", (int) s.st_mode);
106 break;
110 return 0;
113 static struct ast_custom_function env_function = {
114 .name = "ENV",
115 .synopsis = "Gets or sets the environment variable specified",
116 .syntax = "ENV(<envname>)",
117 .read = env_read,
118 .write = env_write,
121 static struct ast_custom_function stat_function = {
122 .name = "STAT",
123 .synopsis = "Does a check on the specified file",
124 .syntax = "STAT(<flag>,<filename>)",
125 .read = stat_read,
126 .desc =
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)
140 int res = 0;
142 res |= ast_custom_function_unregister(&env_function);
143 res |= ast_custom_function_unregister(&stat_function);
145 return res;
148 static int load_module(void)
150 int res = 0;
152 res |= ast_custom_function_register(&env_function);
153 res |= ast_custom_function_register(&stat_function);
155 return res;
158 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Environment/filesystem dialplan functions");