home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnuc / gen_library / rcs / mktemp.c,v < prev    next >
Encoding:
Text File  |  1992-07-04  |  2.7 KB  |  130 lines

  1. head    1.1;
  2. access;
  3. symbols
  4.     version39-41:1.1;
  5. locks;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.1
  10. date    92.06.08.18.31.20;    author mwild;    state Exp;
  11. branches;
  12. next    ;
  13.  
  14.  
  15. desc
  16. @initial checkin
  17. @
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/*
  26.  * Copyright (c) 1987 Regents of the University of California.
  27.  * All rights reserved.
  28.  *
  29.  * Redistribution and use in source and binary forms are permitted
  30.  * provided that: (1) source distributions retain this entire copyright
  31.  * notice and comment, and (2) distributions including binaries display
  32.  * the following acknowledgement:  ``This product includes software
  33.  * developed by the University of California, Berkeley and its contributors''
  34.  * in the documentation or other materials provided with the distribution
  35.  * and in all advertising materials mentioning features or use of this
  36.  * software. Neither the name of the University nor the names of its
  37.  * contributors may be used to endorse or promote products derived
  38.  * from this software without specific prior written permission.
  39.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  40.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  41.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  42.  */
  43.  
  44. #if defined(LIBC_SCCS) && !defined(lint)
  45. static char sccsid[] = "@@(#)mktemp.c    5.9 (Berkeley) 6/1/90";
  46. #endif /* LIBC_SCCS and not lint */
  47.  
  48. #define KERNEL
  49. #include "ixemul.h"
  50.  
  51. #include <ctype.h>
  52.  
  53. static int
  54. _gettemp(char *path, int *doopen)
  55. {
  56.     register char *start, *trv;
  57.     struct stat sbuf;
  58.     u_int pid;
  59.  
  60.     pid = syscall (SYS_getpid);
  61.     for (trv = path; *trv; ++trv);        /* extra X's get set to 0's */
  62.     while (*--trv == 'X') {
  63.         *trv = (pid % 10) + '0';
  64.         pid /= 10;
  65.     }
  66.  
  67.     /*
  68.      * check the target directory; if you have six X's and it
  69.      * doesn't exist this runs for a *very* long time.
  70.      */
  71.     for (start = trv + 1;; --trv) {
  72.         if (trv <= path)
  73.             break;
  74.         if (*trv == '/') {
  75.             *trv = '\0';
  76.             if (syscall (SYS_stat, path, &sbuf))
  77.                 return(0);
  78.             if (!S_ISDIR(sbuf.st_mode)) {
  79.                 errno = ENOTDIR;
  80.                 return(0);
  81.             }
  82.             *trv = '/';
  83.             break;
  84.         }
  85.     }
  86.  
  87.     for (;;) {
  88.         if (doopen) {
  89.             if ((*doopen =
  90.                 syscall (SYS_open, path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
  91.                 return(1);
  92.             if (errno != EEXIST)
  93.                 return(0);
  94.         }
  95.         else if (syscall (SYS_stat, path, &sbuf))
  96.             return(errno == ENOENT ? 1 : 0);
  97.  
  98.         /* tricky little algorithm for backward compatibility */
  99.         for (trv = start;;) {
  100.             if (!*trv)
  101.                 return(0);
  102.             if (*trv == 'z')
  103.                 *trv++ = 'a';
  104.             else {
  105.                 if (isdigit(*trv))
  106.                     *trv = 'a';
  107.                 else
  108.                     ++*trv;
  109.                 break;
  110.             }
  111.         }
  112.     }
  113.     /*NOTREACHED*/
  114. }
  115.  
  116. int
  117. mkstemp(char *path)
  118. {
  119.     int fd;
  120.  
  121.     return (_gettemp(path, &fd) ? fd : -1);
  122. }
  123.  
  124. char *
  125. mktemp(char *path)
  126. {
  127.     return(_gettemp(path, (int *)NULL) ? path : (char *)NULL);
  128. }
  129. @
  130.