home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / unix / unixtools / util / c / mktemp < prev    next >
Encoding:
Text File  |  1992-07-21  |  2.5 KB  |  145 lines

  1. /* C.Mktemp: Make a temporary file name */
  2.  
  3. #include "kernel.h"
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. #include "utils.h"
  8.  
  9. #define ReadCat    5
  10.  
  11. /* Create a temporary file name by adding a directory prefix to file.
  12.  * If the external variable temp_dir is not zero, this directory will be
  13.  * used. Otherwise, the following are used, in order.
  14.  *   1. <Tmp$Dir>
  15.  *   2. &.Tmp
  16.  *   3. The current directory.
  17.  * The function returns zero on an error (temp_dir is not a directory, or
  18.  * malloc() failed), otherwise it returns a malloc-ed string containing
  19.  * the required name.
  20.  */
  21.  
  22. static char *concat (const char *dir, const char *file);
  23.  
  24. static char *dirs[] =
  25. {
  26.     "<Tmp$Dir>",
  27.     "&.Tmp",
  28.     "",
  29.     0
  30. };
  31.  
  32. char *temp_dir = 0;
  33.  
  34. char *mktemp (const char *file)
  35. {
  36.     char **d;
  37.         _kernel_osfile_block blk;
  38.  
  39.     /* First, try the supplied directory */
  40.     if ( temp_dir )
  41.     {
  42.         if ( _kernel_osfile(ReadCat,temp_dir,&blk) != 2 )
  43.         {
  44.             /* Is it a filing system name only? */
  45.             int len = strlen(temp_dir);
  46.             int res;
  47.             char *name;
  48.  
  49.             if (temp_dir[len-1] != ':')
  50.                 return 0;
  51.  
  52.             /* One extra, just in case file == "", for the '@' */
  53.             name = malloc(len + strlen(file) + 2);
  54.  
  55.             if (name == 0)
  56.                 return 0;
  57.  
  58.             strcpy(name,temp_dir);
  59.             name[len] = '@';
  60.             name[len+1] = '\0';
  61.  
  62.             res = _kernel_osfile(ReadCat,name,&blk);
  63.  
  64.             if (res != 2)
  65.             {
  66.                 free(name);
  67.                 return 0;
  68.             }
  69.  
  70.             strcpy(&name[len],file);
  71.             return name;
  72.         }
  73.  
  74.         return concat(temp_dir,file);
  75.     }
  76.  
  77.     /* Otherwise, go through the list... */
  78.     for ( d = dirs; *d; ++d )
  79.     {
  80.         /* Is this the current directory? */
  81.         if ( *d[0] == '\0' )
  82.         {
  83.             char *res = malloc(strlen(file)+1);
  84.             if ( res )
  85.                 strcpy(res,file);
  86.             return res;
  87.         }
  88.  
  89.         /* Is this a valid directory? */
  90.         if ( _kernel_osfile(ReadCat,*d,&blk) == 2 )
  91.             return concat(*d,file);
  92.     }
  93.  
  94.     /* Couldn't find anything... */
  95.     return 0;
  96. }
  97.  
  98. static char *concat (const char *dir, const char *file)
  99. {
  100.     char *result = malloc(strlen(dir)+strlen(file)+2);
  101.     char *p = result;
  102.  
  103.     if ( result == 0 )
  104.         return 0;
  105.  
  106.     while ( *dir )
  107.         *p++ = *dir++;
  108.  
  109.     *p++ = '.';
  110.     while ( *file )
  111.         *p++ = *file++;
  112.  
  113.     *p = '\0';
  114.  
  115.     return result;
  116. }
  117.  
  118. /* ----------------------------------------------------------------- */
  119.  
  120. #ifdef test
  121.  
  122. #include <stdio.h>
  123.  
  124. int main (int argc, char *argv[])
  125. {
  126.     char *tmp;
  127.  
  128.     if ( argc != 2 && argc != 3 )
  129.     {
  130.         fprintf(stderr,"Usage: %s file [dir]\n",argv[0]);
  131.         return 1;
  132.     }
  133.  
  134.     if ( argc == 3 )
  135.         temp_dir = argv[2];
  136.  
  137.     tmp = mktemp (argv[1]);
  138.  
  139.     printf("Temp file = %s\n", tmp ? tmp : "<Not possible>");
  140.  
  141.     return 0;
  142. }
  143.  
  144. #endif
  145.