tmpfile.c 744 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include "sys9.h"
  5. #undef OPEN
  6. #include "../stdio/iolib.h"
  7. #include "lib.h"
  8. #include "dir.h"
  9. FILE *
  10. tmpfile(void){
  11. FILE *f;
  12. static char name[]="/tmp/tf0000000000000";
  13. char *p;
  14. int n;
  15. for(f=_IO_stream;f!=&_IO_stream[FOPEN_MAX];f++)
  16. if(f->state==CLOSED)
  17. break;
  18. if(f==&_IO_stream[FOPEN_MAX])
  19. return NULL;
  20. while(access(name, 0) >= 0){
  21. p = name+7;
  22. while(*p == '9')
  23. *p++ = '0';
  24. if(*p == '\0')
  25. return NULL;
  26. ++*p;
  27. }
  28. n = _CREATE(name, 64|2, 0777); /* remove-on-close */
  29. if(n==-1){
  30. _syserrno();
  31. return NULL;
  32. }
  33. _fdinfo[n].flags = FD_ISOPEN;
  34. _fdinfo[n].oflags = 2;
  35. f->fd=n;
  36. f->flags=0;
  37. f->state=OPEN;
  38. f->buf=0;
  39. f->rp=0;
  40. f->wp=0;
  41. f->lp=0;
  42. return f;
  43. }