1 #include <sys/types.h> 2 #include <unistd.h> 3 #include <fcntl.h> 4 #include <string.h> 5 6 int 7 putenv(char *s) 8 { 9 int f, n; 10 char *value; 11 char buf[300]; 12 13 value = strchr(s, '='); 14 if (value) { 15 n = value-s; 16 if(n<=0 || n > sizeof(buf)-1) 17 return -1; 18 strncpy(buf, s, n); 19 buf[n] = 0; 20 f = creat(buf, 0666); 21 if(f < 0) 22 return 1; 23 value++; 24 n = strlen(value); 25 if(write(f, buf, n) != n) 26 return -1; 27 close(f); 28 return 0; 29 } else 30 return -1; 31 } 32