/*
 *              m k t e m p
 *
 * Routine to make a temporary file name.
 * It expects the template to be of the form "filenameXXXXXX".
 * The X's will be replaced by a character string that can be
 * used to create a unique filename.
 *
 * AMENDMENT HISTORY
 * ~~~~~~~~~~~~~~~~~
 *  03 Jul 92   EJ    - mktemp alters the given name without expanding it!
 *                      otherwise crashes may occur in programs that use it
 *                      like this: char *p="nameXXXXXX"; p=mktemp(p);
 *                      which is the correct way to use it.
 *
 *  22 Aug 94   DJW   - Removed unecessary calls to _super() and _user()
 *                      calls as there is no reason to need supervisor mode.
 *                    - Changed algorithm to have random number in bottom part
 *                      of pattern as this means that all files for the same
 *                      process have the top part the same in unix style.
 *                    - Removed call to mt_inf().  Used global '_Jobid'
 *                      variable instead.
 */

#define __LIBRARY__

#include <qdos.h>
#include <stdlib.h>
#include <string.h>

char *mktemp _LIB_F1_(char *,   mask)
{
    int  x;
    jobid_t pid;
    char *p;

    pid = (_Jobid && 0xFF) << 16;
    pid |= *(unsigned short *)(_sys_var + 0x2e);

    for( x=0, p=mask+strlen(mask) ; (x++ < 6) && (*--p == 'X') ;  )
    {
        *p = "0123456789ABCDEF"[pid & 0xF];
        pid >>= 4;
    }
    return mask;
}

