00001
00002
00003
00004
00005
00006
00007
00008 #include <stdlib.h>
00009 #include <stdio.h>
00010 #include <string.h>
00011
00012
00013 extern "C" int setenv(const char *env_var, const char *env_val, int dummy) {
00014 char *buf, *value, *p, *q, *a, *b, quote='\0';
00015 int inquote = 0;
00016
00017
00018 a = (char *) malloc((strlen(env_var) + 1) * sizeof(char));
00019 b = (char *) malloc((strlen(env_val) + 1) * sizeof(char));
00020 if (!a || !b) {
00021 R_Suicide("allocation failure in reading Renviron");
00022 }
00023 strcpy(a, env_var);
00024 strcpy(b, env_val);
00025
00026 buf = (char *) malloc((strlen(a) + strlen(b) + 2) * sizeof(char));
00027 if (!buf) {
00028 R_Suicide("allocation failure in reading Renviron");
00029 }
00030 strcpy(buf, a); strcat(buf, "=");
00031 value = buf+strlen(buf);
00032
00033
00034 for(p = b, q = value; *p; p++) {
00035
00036 if(!inquote && (*p == '"' || *p == '\'')) {
00037 inquote = 1;
00038 quote = *p;
00039 continue;
00040 }
00041
00042 if(inquote && *p == quote && *(p-1) != '\\') {
00043 inquote = 0;
00044 continue;
00045 }
00046
00047 if(!inquote && *p == '\\') {
00048 if(*(p+1) == '\n') p++;
00049 else if(*(p+1) == '\\') *q++ = *p;
00050 continue;
00051 }
00052
00053 if(inquote && *p == '\\' && *(p+1) == quote)
00054 continue;
00055 *q++ = *p;
00056 }
00057 *q = '\0';
00058
00059
00060 return putenv(buf);
00061
00062
00063 }
00064