home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / prog_c / f2c.lha / f2c_ami.zoo / libI77 / mktemp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-12  |  1.1 KB  |  62 lines

  1. #include <string.h>
  2. #include <stdio.h>
  3.  
  4. #include <exec/types.h>
  5. #include <exec/memory.h>
  6. #include <libraries/dos.h>
  7. #include <libraries/dosextens.h>
  8.  
  9. #ifdef LATTICE_50
  10. #include <proto/dos.h>
  11. #include <proto/exec.h>
  12. #else
  13. void *AllocMem();
  14. BPTR Lock();
  15. long IoErr();
  16. #endif
  17.  
  18. static void uns2str(char *str, unsigned short num, unsigned short width, char pad)
  19. {
  20.   short i;
  21.   if (num<10) {
  22.     for (i=0; i<=width-2; i++) *str++ = pad;
  23.     *str = (unsigned char)num + '0';
  24.   }
  25.   else {
  26.     uns2str(str, num / 10, width - 1, pad);
  27.     str[width-1] = (unsigned char)(num % 10) + '0';
  28.   };
  29. }
  30.  
  31. char *mktemp(template)
  32. char *template;
  33. {
  34.   char *s;
  35.   BPTR fl;
  36.   int try = 0;
  37.   char alreadythere;
  38.  
  39.   s = strstr(template,"XXXXXX");
  40.   if (s != 0L) {
  41.     *s = 'T';
  42.     s++;
  43.     alreadythere = !0;
  44.     do {
  45.       try++;
  46.       uns2str(s,try,5,'0');
  47.  
  48.       /* If we can Lock it then it exists */
  49.       if ((fl = Lock(template,SHARED_LOCK)) == 0L) {
  50.         if (IoErr()!=0L) alreadythere = 0;
  51.       };
  52.       if (alreadythere) UnLock(fl);
  53.  
  54.     } while (alreadythere);
  55.   }
  56.   else {
  57.     fprintf(stderr,"weird template for mktemp()\n");
  58.     return 0L;
  59.   };
  60.   return template;
  61. }
  62.