env.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
  3. * All rights reserved.
  4. * This component and the accompanying materials are made available
  5. * under the terms of the License "Eclipse Public License v1.0"
  6. * which accompanies this distribution, and is available
  7. * at the URL "http://www.eclipse.org/legal/epl-v10.html".
  8. *
  9. * Initial Contributors:
  10. * Nokia Corporation - initial contribution.
  11. *
  12. * Contributors:
  13. *
  14. * Description: functions for manipulating environment variables.
  15. *
  16. */
  17. #include "../config.h"
  18. #ifdef HAS_WINSOCK2
  19. #include <winsock2.h>
  20. #include <ws2tcpip.h>
  21. #define WIN32_LEAN_AND_MEAN
  22. #endif
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include <string.h>
  26. #include <stdio.h>
  27. #include <fcntl.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <stdarg.h>
  31. #include "env.h"
  32. #include "log.h"
  33. void talon_setenv(char name[], char val[])
  34. {
  35. #if defined(HAS_GETENVIRONMENTVARIABLE)
  36. SetEnvironmentVariableA(name,val);
  37. #elif defined(HAS_GETENV)
  38. setenv(name,val, 1);
  39. #else
  40. # error "Need a function for setting environment variables"
  41. #endif
  42. }
  43. char * talon_getenv(char name[])
  44. {
  45. #if defined(HAS_SETENV)
  46. char *val = getenv(name);
  47. char *dest = NULL;
  48. if (val)
  49. {
  50. dest = malloc(strlen(val) + 1);
  51. if (dest)
  52. {
  53. strcpy(dest,val);
  54. }
  55. }
  56. return dest;
  57. #elif defined(HAS_SETENVIRONMENTVARIABLE)
  58. char *val = malloc(TALON_MAXENV);
  59. if (0 != GetEnvironmentVariableA(name,val,TALON_MAXENV-1))
  60. return val;
  61. else
  62. return NULL;
  63. #else
  64. # error "Need a function for setting environment variables"
  65. #endif
  66. }
  67. int talon_pathprepend(char value[])
  68. {
  69. int extra_length = strlen(value);
  70. int path_length;
  71. char *path = talon_getenv("PATH");
  72. char *new_path;
  73. if(path != NULL)
  74. {
  75. path_length = strlen(path);
  76. new_path = malloc(sizeof(char) * (1 + 1 + path_length + extra_length));
  77. if(new_path != NULL)
  78. {
  79. new_path[0] = '\0';
  80. new_path = strcat(new_path, value);
  81. new_path = strcat(new_path, ";");
  82. new_path = strcat(new_path, path);
  83. talon_setenv("PATH", new_path);
  84. DEBUG(("talon_pathprepend: PATH set to %s\n", new_path));
  85. free(new_path);
  86. free(path);
  87. return 0;
  88. }
  89. else
  90. {
  91. return -1;
  92. }
  93. }
  94. else
  95. {
  96. return -1;
  97. }
  98. }
  99. int talon_pathappend(char name[])
  100. {
  101. return 0;
  102. }