~ubuntu-branches/ubuntu/precise/netatalk/precise

« back to all changes in this revision

Viewing changes to libatalk/compat/mktemp.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Rittau
  • Date: 2004-01-19 12:43:49 UTC
  • Revision ID: james.westby@ubuntu.com-20040119124349-es563jbp0hk0ae51
Tags: upstream-1.6.4
ImportĀ upstreamĀ versionĀ 1.6.4

Show diffs side-by-side

added added

removed removed

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