putenv.c 483 B

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