home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / dev / gcc / libnixV0_8.lha / gnu / libnix-sources.lha / sources / nix / stdio / tmpfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-12  |  920 b   |  37 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <dos/dos.h>
  4. #ifdef __GNUC__
  5. #include <inline/exec.h>
  6. #include <inline/dos.h>
  7. #endif
  8.  
  9. FILE *tmpfile(void)
  10. { char *name;
  11.   FILE *file;
  12.   BPTR dirlock,filelock,cd;
  13.   static unsigned long filecount=0;
  14.   name=malloc(32); /* buffer for filename */
  15.   if(name!=NULL)
  16.   { dirlock=Lock("T:",ACCESS_READ); /* lock T: directory */
  17.     if(dirlock!=0)
  18.     { cd=CurrentDir(dirlock); /* cd T: */
  19.       do /* generate a filename that doesn't exist */
  20.       { sprintf(name,"tempfile_1_%p_%lu",FindTask(NULL),filecount++);
  21.         filelock=Lock(name,ACCESS_WRITE);
  22.         if(filelock!=0)
  23.           UnLock(filelock);
  24.       }while(filelock!=0||IoErr()==ERROR_OBJECT_IN_USE);
  25.       file=fopen(name,"wb+");
  26.       CurrentDir(cd);
  27.       if(file!=NULL)
  28.       { file->tmpdir=dirlock;
  29.         file->name=name;
  30.         return file; }
  31.       UnLock(dirlock);
  32.     }
  33.     free(name);
  34.   }
  35.   return NULL;
  36. }
  37.