Sat Aug 6 00:39:29 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 - 2006, 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  */
00022 
00023 #include "asterisk.h"
00024 
00025 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 95470 $")
00026 
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 #include <sys/types.h>
00031 #include <sys/stat.h>
00032 
00033 #include "asterisk/module.h"
00034 #include "asterisk/channel.h"
00035 #include "asterisk/pbx.h"
00036 #include "asterisk/logger.h"
00037 #include "asterisk/utils.h"
00038 #include "asterisk/app.h"
00039 
00040 static int env_read(struct ast_channel *chan, char *cmd, char *data,
00041           char *buf, size_t len)
00042 {
00043    char *ret = NULL;
00044 
00045    *buf = '\0';
00046 
00047    if (data)
00048       ret = getenv(data);
00049 
00050    if (ret)
00051       ast_copy_string(buf, ret, len);
00052 
00053    return 0;
00054 }
00055 
00056 static int env_write(struct ast_channel *chan, char *cmd, char *data,
00057            const char *value)
00058 {
00059    if (!ast_strlen_zero(data)) {
00060       if (!ast_strlen_zero(value)) {
00061          setenv(data, value, 1);
00062       } else {
00063          unsetenv(data);
00064       }
00065    }
00066 
00067    return 0;
00068 }
00069 
00070 static int stat_read(struct ast_channel *chan, char *cmd, char *data,
00071            char *buf, size_t len)
00072 {
00073    char *action;
00074    struct stat s;
00075 
00076    ast_copy_string(buf, "0", len);
00077 
00078    action = strsep(&data, "|");
00079    if (stat(data, &s)) {
00080       return 0;
00081    } else {
00082       switch (*action) {
00083       case 'e':
00084          strcpy(buf, "1");
00085          break;
00086       case 's':
00087          snprintf(buf, len, "%d", (unsigned int) s.st_size);
00088          break;
00089       case 'f':
00090          snprintf(buf, len, "%d", S_ISREG(s.st_mode) ? 1 : 0);
00091          break;
00092       case 'd':
00093          snprintf(buf, len, "%d", S_ISDIR(s.st_mode) ? 1 : 0);
00094          break;
00095       case 'M':
00096          snprintf(buf, len, "%d", (int) s.st_mtime);
00097          break;
00098       case 'A':
00099          snprintf(buf, len, "%d", (int) s.st_mtime);
00100          break;
00101       case 'C':
00102          snprintf(buf, len, "%d", (int) s.st_ctime);
00103          break;
00104       case 'm':
00105          snprintf(buf, len, "%o", (int) s.st_mode);
00106          break;
00107       }
00108    }
00109 
00110    return 0;
00111 }
00112 
00113 static struct ast_custom_function env_function = {
00114    .name = "ENV",
00115    .synopsis = "Gets or sets the environment variable specified",
00116    .syntax = "ENV(<envname>)",
00117    .read = env_read,
00118    .write = env_write,
00119 };
00120 
00121 static struct ast_custom_function stat_function = {
00122    .name = "STAT",
00123    .synopsis = "Does a check on the specified file",
00124    .syntax = "STAT(<flag>,<filename>)",
00125    .read = stat_read,
00126    .desc =
00127       "flag may be one of the following:\n"
00128       "  d - Checks if the file is a directory\n"
00129       "  e - Checks if the file exists\n"
00130       "  f - Checks if the file is a regular file\n"
00131       "  m - Returns the file mode (in octal)\n"
00132       "  s - Returns the size (in bytes) of the file\n"
00133       "  A - Returns the epoch at which the file was last accessed\n"
00134       "  C - Returns the epoch at which the inode was last changed\n"
00135       "  M - Returns the epoch at which the file was last modified\n",
00136 };
00137 
00138 static int unload_module(void)
00139 {
00140    int res = 0;
00141 
00142    res |= ast_custom_function_unregister(&env_function);
00143    res |= ast_custom_function_unregister(&stat_function);
00144 
00145    return res;
00146 }
00147 
00148 static int load_module(void)
00149 {
00150    int res = 0;
00151 
00152    res |= ast_custom_function_register(&env_function);
00153    res |= ast_custom_function_register(&stat_function);
00154 
00155    return res;
00156 }
00157 
00158 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Environment/filesystem dialplan functions");

Generated on Sat Aug 6 00:39:29 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7