File: xopen.c

package info (click to toggle)
diablo 1.16.test2-1
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 1,504 kB
  • ctags: 1,603
  • sloc: ansic: 17,654; perl: 2,054; sh: 260; csh: 118; makefile: 73
file content (149 lines) | stat: -rw-r--r-- 3,008 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

/*
 * LIB/XOPEN.C
 *
 * (c)Copyright 1997, Matthew Dillon, All Rights Reserved.  Refer to
 *    the COPYRIGHT file in the base directory of this distribution 
 *    for specific rights granted.
 *
 */

#include "defs.h"

Prototype int xopen(int modes, int perms, const char *ctl, ...);
Prototype FILE *xfopen(const char *modes, const char *ctl, ...);
Prototype int xfprintf(FILE *fo, const char *ctl, ...);
Prototype void ddprintf(const char *ctl, ...);
Prototype void vddprintf(const char *ctl, va_list va);
Prototype FILE *fcdopen(const char *path, const char *modes);
Prototype int cdopen(const char *path, int modes, int perms);
Prototype const char *cdcache(const char *path);

int
xopen(int modes, int perms, const char *ctl, ...)
{
    char path[1024];
    va_list va;

    va_start(va, ctl);
    vsnprintf(path, sizeof(path), ctl, va);
    va_end(va);
    return(open(path, modes, perms));
}


FILE *
xfopen(const char *modes, const char *ctl, ...)
{
    char path[1024];
    va_list va;

    va_start(va, ctl);
    vsnprintf(path, sizeof(path), ctl, va);
    va_end(va);
    return(fopen(path, modes));
}

int
xfprintf(FILE *fo, const char *ctl, ...)
{
    va_list va;
    int r;

    if (fo == NULL) {
	syslog(LOG_CRIT, "xfprintf() - fo NULL!");
	exit(1);
    }

    va_start(va, ctl);
    r = vfprintf(fo, ctl, va);
    va_end(va);
    if (DebugOpt) {
	ddprintf("%d >> ", (int)getpid());
	va_start(va, ctl);
	vddprintf(ctl, va);
	va_end(va);
    }
    return(r);
}

void 
ddprintf(const char *ctl, ...)
{
    va_list va;

    if (DebugOpt) {
	va_start(va, ctl);
	vddprintf(ctl, va);
	va_end(va);
    }
}

void 
vddprintf(const char *ctl, va_list va)
{
    if (DebugOpt) {
	vprintf(ctl, va);
    }
}

FILE *
fcdopen(const char *path, const char *modes)
{
    const char *p = cdcache(path);
    FILE *fi;

    fi = fopen(p, modes);
    if (DebugOpt && strstr(path, "/feeds/") == NULL) {
        printf("%*.*s^%s\t%s\n", p - path, p - path, path, p,
            (fi == NULL) ? "open failure" : "open ok"
        );
    }
    return(fi);
}

int
cdopen(const char *path, int modes, int perms)
{
    const char *p = cdcache(path);
    int fd;

    fd = open(p, modes, perms);
    if (DebugOpt) {
        printf("%*.*s^%s\t%s\n", p - path, p - path, path, p,
            (fd < 0) ? "open failure" : "open ok"
        );
    }
    return(fd);
}

const char *
cdcache(const char *path)
{
    static char DirCache[256];
    const char *p;

    if ((p = strrchr(path, '/')) == NULL)
	return(path);

    ++p;

    if (strncmp(DirCache, path, p - path) != 0 || DirCache[p - path] != 0) {
        bcopy(path, DirCache, p - path);
        DirCache[p - path] = 0;
        if (chdir(DirCache) != 0) {
#ifdef NOTDEF
	    /*
	     * this is actually ok for diablo, because it tries to write the
	     * file before creating the directory.
	     */
	    syslog(LOG_CRIT, "fcdopen() - unable to chdir(\"%s\")\n", DirCache);
#endif
            DirCache[0] = 0;
            p = path;
        }
    }
    return(p);
}