home *** CD-ROM | disk | FTP | other *** search
- #include <string.h>
- #include <stdio.h>
-
- #include <exec/types.h>
- #include <exec/memory.h>
- #include <libraries/dos.h>
- #include <libraries/dosextens.h>
-
- #ifdef LATTICE_50
- #include <proto/dos.h>
- #include <proto/exec.h>
- #else
- void *AllocMem();
- BPTR Lock();
- long IoErr();
- #endif
-
- static void uns2str(char *str, unsigned short num, unsigned short width, char pad)
- {
- short i;
- if (num<10) {
- for (i=0; i<=width-2; i++) *str++ = pad;
- *str = (unsigned char)num + '0';
- }
- else {
- uns2str(str, num / 10, width - 1, pad);
- str[width-1] = (unsigned char)(num % 10) + '0';
- };
- }
-
- char *mktemp(template)
- char *template;
- {
- char *s;
- BPTR fl;
- int try = 0;
- char alreadythere;
-
- s = strstr(template,"XXXXXX");
- if (s != 0L) {
- *s = 'T';
- s++;
- alreadythere = !0;
- do {
- try++;
- uns2str(s,try,5,'0');
-
- /* If we can Lock it then it exists */
- if ((fl = Lock(template,SHARED_LOCK)) == 0L) {
- if (IoErr()!=0L) alreadythere = 0;
- };
- if (alreadythere) UnLock(fl);
-
- } while (alreadythere);
- }
- else {
- fprintf(stderr,"weird template for mktemp()\n");
- return 0L;
- };
- return template;
- }
-