home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / clib / progs / utilslib / c / Mktemp < prev    next >
Encoding:
Text File  |  1990-06-10  |  3.5 KB  |  196 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)
  28. {
  29.     char *dir;
  30.     char *name;
  31.     char buf[11];
  32.     int len = strlen(file);
  33.     _kernel_osfile_block blk;
  34.     _kernel_swi_regs regs;
  35.  
  36.     /* Is the supplied filename a pure file name? */
  37.     if (len > 10)
  38.         return 0;
  39.  
  40.     /* Pad out the supplied filename on the left with a unique ID
  41.      * (Based on the program start time)
  42.      */
  43.     if (len < 10 && _kernel_swi(OS_GetEnv,®s,®s) == NULL)
  44.     {
  45.         int i;
  46.         char *time = (char *)regs.r[2];
  47.  
  48.         strcpy(buf,file);
  49.  
  50.         for (i = len; i < 10; ++i)
  51.         {
  52.             char c = time[(9 - i) >> 1];
  53.  
  54.             if (i & 1)
  55.                 c >>= 4;
  56.  
  57.             c &= 0x0F;
  58.             buf[i] = "abcdefghijklmnop"[c];
  59.         }
  60.  
  61.         buf[10] = 0;
  62.  
  63.         file = buf;
  64.     }
  65.  
  66.     /* First, try the supplied directory */
  67.     if ( temp_dir )
  68.     {
  69.         if ( _kernel_osfile(ReadCat,temp_dir,&blk) == 2 )
  70.             return concat(temp_dir,file);
  71.         else
  72.         {
  73.             /* Is it a filing system name only? */
  74.             len = strlen(temp_dir);
  75.  
  76.             if (temp_dir[len-1] != ':')
  77.                 return 0;
  78.  
  79.             /* One extra, just in case file == "", for the '@' */
  80.             name = malloc(len + strlen(file) + 2);
  81.  
  82.             if (name == 0)
  83.                 return 0;
  84.  
  85.             strcpy(name,temp_dir);
  86.             name[len] = '@';
  87.             name[len+1] = '\0';
  88.  
  89.             if (_kernel_osfile(ReadCat,name,&blk) != 2)
  90.             {
  91.                 free(name);
  92.                 return 0;
  93.             }
  94.  
  95.             strcpy(&name[len],file);
  96.             return name;
  97.         }
  98.     }
  99.  
  100.     /* Otherwise, go through the list... */
  101.  
  102.     /* First of all, try <Tmp$Dir> */
  103.     if ((dir = getenv("Tmp$Dir")) != 0)
  104.     {
  105.         if (_kernel_osfile(ReadCat,dir,&blk) == 2)
  106.             return concat(dir,file);
  107.         else
  108.         {
  109.             /* Is it a filing system name only? */
  110.             len = strlen(dir);
  111.  
  112.             if (dir[len-1] != ':')
  113.                 goto no_go;
  114.  
  115.             /* One extra, just in case file == "", for the '@' */
  116.             name = malloc(len + strlen(file) + 2);
  117.  
  118.             if (name == 0)
  119.                 goto no_go;
  120.  
  121.             strcpy(name,dir);
  122.             name[len] = '@';
  123.             name[len+1] = '\0';
  124.  
  125.             if (_kernel_osfile(ReadCat,name,&blk) != 2)
  126.             {
  127.                 free(name);
  128.                 goto no_go;
  129.             }
  130.  
  131.             strcpy(&name[len],file);
  132.             return name;
  133.         }
  134.     }
  135.  
  136. no_go:
  137.     /* No <Tmp$Dir>, so try &.Tmp, if it exists */
  138.     if (_kernel_osfile(ReadCat,"&.Tmp",&blk) == 2)
  139.         return concat("&.Tmp",file);
  140.  
  141.     /* Out of luck - use the current directory */
  142.     name = malloc(strlen(file)+1);
  143.     if ( name )
  144.         strcpy(name,file);
  145.  
  146.     return name;
  147. }
  148.  
  149. static char *concat (const char *dir, const char *file)
  150. {
  151.     char *result = malloc(strlen(dir)+strlen(file)+2);
  152.     char *p = result;
  153.  
  154.     if ( result == 0 )
  155.         return 0;
  156.  
  157.     while ( *dir )
  158.         *p++ = *dir++;
  159.  
  160.     *p++ = '.';
  161.     while ( *file )
  162.         *p++ = *file++;
  163.  
  164.     *p = '\0';
  165.  
  166.     return result;
  167. }
  168.  
  169. /* ----------------------------------------------------------------- */
  170.  
  171. #ifdef test
  172.  
  173. #include <stdio.h>
  174.  
  175. int main (int argc, char *argv[])
  176. {
  177.     char *tmp;
  178.  
  179.     if ( argc != 2 && argc != 3 )
  180.     {
  181.         fprintf(stderr,"Usage: %s file [dir]\n",argv[0]);
  182.         return 1;
  183.     }
  184.  
  185.     if ( argc == 3 )
  186.         temp_dir = argv[2];
  187.  
  188.     tmp = mktemp (argv[1]);
  189.  
  190.     printf("Temp file = %s\n", tmp ? tmp : "<Not possible>");
  191.  
  192.     return 0;
  193. }
  194.  
  195. #endif
  196.