func_env.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  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.
  11. *
  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.
  15. */
  16. /*! \file
  17. *
  18. * \brief Environment related dialplan functions
  19. *
  20. */
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/types.h>
  24. #include "asterisk.h"
  25. /* ASTERISK_FILE_VERSION(__FILE__, "$Revision$") */
  26. #include "asterisk/channel.h"
  27. #include "asterisk/pbx.h"
  28. #include "asterisk/logger.h"
  29. #include "asterisk/utils.h"
  30. #include "asterisk/app.h"
  31. static char *builtin_function_env_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
  32. {
  33. char *ret = "";
  34. if (data) {
  35. ret = getenv(data);
  36. if (!ret)
  37. ret = "";
  38. }
  39. ast_copy_string(buf, ret, len);
  40. return buf;
  41. }
  42. static void builtin_function_env_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
  43. {
  44. if (!ast_strlen_zero(data)) {
  45. if (!ast_strlen_zero(value)) {
  46. setenv(data, value, 1);
  47. } else {
  48. unsetenv(data);
  49. }
  50. }
  51. }
  52. #ifndef BUILTIN_FUNC
  53. static
  54. #endif
  55. struct ast_custom_function env_function = {
  56. .name = "ENV",
  57. .synopsis = "Gets or sets the environment variable specified",
  58. .syntax = "ENV(<envname>)",
  59. .read = builtin_function_env_read,
  60. .write = builtin_function_env_write,
  61. };