home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / clib / progs / utilslib / utils_old / c / Mktemp < prev    next >
Encoding:
Text File  |  1994-02-22  |  3.5 KB  |  198 lines

  1. /* C.Mktemp: Make a temporary file name */
  2.  
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include "kernel.h"
  6. #include "swis.h"
  7.  
  8. #include "utils.h"
  9.  
  10. #define ReadCat    5
  11.  
  12. /* Create a temporary file name by adding a directory prefix to file.
  13.  * If the external variable temp_dir is not zero, this directory will be
  14.  * used. Otherwise, the following are used, in order.
  15.  *   1. <Tmp$Dir>
  16.  *   2. &.Tmp
  17.  *   3. The current directory.
  18.  * The function returns zero on an error (temp_dir is not a directory, or
  19.  * malloc() failed), otherwise it returns a malloc-ed string containing
  20.  * the required name.
  21.  */
  22.  
  23. static char *concat (const char *dir, const char *file);
  24.  
  25. char *temp_dir = 0;
  26.  
  27. char *mktemp (const char *file_name)
  28. {
  29.     char *file = (char *)file_name;  /* Sorry Paul - can't assign file = ... */
  30.     char *dir;
  31.     char *name;
  32.     char buf[11];
  33.     int len = strlen(file);
  34.     _kernel_osfile_block blk;
  35.     _kernel_swi_regs regs;
  36.  
  37.     /* Is the supplied filename a pure file name? */
  38.     if (len > 10)
  39.         return 0;
  40.  
  41.     /* Pad out the supplied filename on the left with a unique ID
  42.      * (Based on the program start time)
  43.      */
  44.     if (len < 10 && _kernel_swi(OS_GetEnv,®s,®s) == NULL)
  45.     {
  46.         int i;
  47.         char *time = (char *)regs.r[2];
  48.  
  49.         strcpy(buf,file);
  50.  
  51.         for (i = len; i < 10; ++i)
  52.         {
  53.             char c = time[(9 - i) >> 1];
  54.  
  55.             if (i & 1)
  56.                 c >>= 4;
  57.  
  58.             c &= 0x0F;
  59.             buf[i] = "abcdefghijklmnop"[c];
  60.         }
  61.  
  62.         buf[10] = 0;
  63.  
  64.         file = buf;
  65.     }
  66.  
  67.     /* First, try the supplied directory */
  68.     if ( temp_dir )
  69.     {
  70.         if ( _kernel_osfile(ReadCat,temp_dir,&blk) == 2 )
  71.             return concat(temp_dir,file);
  72.         else
  73.         {
  74.             /* Is it a filing system name only? */
  75.             len = strlen(temp_dir);
  76.  
  77.             if (temp_dir[len-1] != ':')
  78.                 return 0;
  79.  
  80.             /* One extra, just in case file == "", for the '@' */
  81.             name = malloc(len + strlen(file) + 2);
  82.  
  83.             if (name == 0)
  84.                 return 0;
  85.  
  86.             strcpy(name,temp_dir);
  87.             name[len] = '@';
  88.             name[len+1] = '\0';
  89.  
  90.             if (_kernel_osfile(ReadCat,name,&blk) != 2)
  91.             {
  92.                 free(name);
  93.                 return 0;
  94.             }
  95.  
  96.             strcpy(&name[len],file);
  97.             return name;
  98.         }
  99.     }
  100.  
  101.     /* Otherwise, go through the list... */
  102.  
  103.     /* First of all, try <Tmp$Dir> */
  104.     if ((dir = getenv("Tmp$Dir")) != 0)
  105.     {
  106.         if (_kernel_osfile(ReadCat,dir,&blk) == 2)
  107.             return concat(dir,file);
  108.         else
  109.         {
  110.             /* Is it a filing system name only? */
  111.             len = strlen(dir);
  112.  
  113.             if (dir[len-1] != ':')
  114.                 goto no_go;
  115.  
  116.             /* One extra, just in case file == "", for the '@' */
  117.             name = malloc(len + strlen(file) + 2);
  118.  
  119.             if (name == 0)
  120.                 goto no_go;
  121.  
  122.             strcpy(name,dir);
  123.             name[len] = '@';
  124.             name[len+1] = '\0';
  125.  
  126.             if (_kernel_osfile(ReadCat,name,&blk) != 2)
  127.             {
  128.                 free(name);
  129.                 goto no_go;
  130.             }
  131.  
  132.             strcpy(&name[len],file);
  133.             return name;
  134.         }
  135.     }
  136.  
  137. no_go:
  138.     /* No <Tmp$Dir>, so try &.Tmp, if it exists */
  139.     if (_kernel_osfile(ReadCat,"&.Tmp",&blk) == 2) {
  140.         return concat("&.Tmp",file);
  141.     }
  142.  
  143.     /* Out of luck - use the current directory */
  144.     name = malloc(strlen(file)+1);
  145.     if ( name )
  146.         strcpy(name,file);
  147.  
  148.     return name;
  149. }
  150.  
  151. static char *concat (const char *dir, const char *file)
  152. {
  153.     char *result = malloc(strlen(dir)+strlen(file)+2);
  154.     char *p = result;
  155.  
  156.     if ( result == 0 )
  157.         return 0;
  158.  
  159.     while ( *dir )
  160.         *p++ = *dir++;
  161.  
  162.     *p++ = '.';
  163.     while ( *file )
  164.         *p++ = *file++;
  165.  
  166.     *p = '\0';
  167.  
  168.     return result;
  169. }
  170.  
  171. /* ----------------------------------------------------------------- */
  172.  
  173. #ifdef test
  174.  
  175. #include <stdio.h>
  176.  
  177. int main (int argc, char *argv[])
  178. {
  179.     char *tmp;
  180.  
  181.     if ( argc != 2 && argc != 3 )
  182.     {
  183.         fprintf(stderr,"Usage: %s file [dir]\n",argv[0]);
  184.         return 1;
  185.     }
  186.  
  187.     if ( argc == 3 )
  188.         temp_dir = argv[2];
  189.  
  190.     tmp = mktemp (argv[1]);
  191.  
  192.     printf("Temp file = %s\n", tmp ? tmp : "<Not possible>");
  193.  
  194.     return 0;
  195. }
  196.  
  197. #endif
  198.