env.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * $Id: env.c,v 1.6 2007/06/21 18:07:23 cparker Exp $
  3. *
  4. * Copyright (C) 1995,1996,1997 Lars Fenneberg
  5. *
  6. * See the file COPYRIGHT for the respective terms and conditions.
  7. * If the file is missing contact me at lf@elemental.net
  8. * and I'll send you a copy.
  9. *
  10. */
  11. #include <config.h>
  12. #include <includes.h>
  13. #include <freeradius-client.h>
  14. /** Allocate space for a new environment
  15. *
  16. * @param size the maximum size of the environment.
  17. * @return the initialized environment.
  18. */
  19. ENV *rc_new_env(int size)
  20. {
  21. ENV *p;
  22. if (size < 1)
  23. return NULL;
  24. if ((p = malloc(sizeof(*p))) == NULL)
  25. return NULL;
  26. if ((p->env = malloc(size * sizeof(char *))) == NULL)
  27. {
  28. rc_log(LOG_CRIT, "rc_new_env: out of memory");
  29. free(p);
  30. return NULL;
  31. }
  32. p->env[0] = NULL;
  33. p->size = 0;
  34. p->maxsize = size;
  35. return p;
  36. }
  37. /** Free the space used by an env structure
  38. *
  39. * @param env an initialized environment value.
  40. */
  41. void rc_free_env(ENV *env)
  42. {
  43. free(env->env);
  44. free(env);
  45. }
  46. /** Add an environment entry
  47. *
  48. * @param env an initialized environment value.
  49. * @return 0 on success or -1 on error.
  50. */
  51. int rc_add_env(ENV *env, char const *name, char const *value)
  52. {
  53. int i;
  54. size_t len;
  55. char *new_env;
  56. for (i = 0; env->env[i] != NULL; i++)
  57. {
  58. if (strncmp(env->env[i], name, MAX(strchr(env->env[i], '=') - env->env[i], (int)strlen(name))) == 0)
  59. break;
  60. }
  61. if (env->env[i])
  62. {
  63. len = strlen(name)+strlen(value)+2;
  64. if ((new_env = realloc(env->env[i], len)) == NULL)
  65. return -1;
  66. env->env[i] = new_env;
  67. snprintf(env->env[i], len, "%s=%s", name, value);
  68. } else {
  69. if (env->size == (env->maxsize-1)) {
  70. rc_log(LOG_CRIT, "rc_add_env: not enough space for environment (increase ENV_SIZE)");
  71. return -1;
  72. }
  73. len = strlen(name)+strlen(value)+2;
  74. if ((env->env[env->size] = malloc(len)) == NULL) {
  75. rc_log(LOG_CRIT, "rc_add_env: out of memory");
  76. return -1;
  77. }
  78. snprintf(env->env[env->size], len, "%s=%s", name, value);
  79. env->size++;
  80. env->env[env->size] = NULL;
  81. }
  82. return 0;
  83. }
  84. /** Imports an array of null-terminated strings
  85. *
  86. * @param env an initialized environment value.
  87. * @return 0 on success or -1 on error.
  88. */
  89. int rc_import_env(ENV *env, char const **import)
  90. {
  91. char *es;
  92. while (*import)
  93. {
  94. es = strchr(*import, '=');
  95. if (!es)
  96. {
  97. import++;
  98. continue;
  99. }
  100. /* ok, i grant thats not very clean... */
  101. *es = '\0';
  102. if (rc_add_env(env, *import, es+1) < 0)
  103. {
  104. *es = '=';
  105. return -1;
  106. }
  107. *es = '=';
  108. import++;
  109. }
  110. return 0;
  111. }