-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
114 lines (101 loc) · 2.36 KB
/
util.c
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
/*
* Copyright (c) 1987,1989 IBM Corporation
* Copyright (c) 1989 Linas Vepstas
* Author: Bruce Lucas
* Author: Linas Vepstas
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "util.h"
/*
* Open with possible substitution of extension
* name==0 => stdin
*/
int
Open(const char *name, const char *ext)
{
int fd;
if (ext) {
/* substitute extension */
char *slash, *dot, full[200];
if (!name)
return -1;
strcpy(full, name);
slash = (char *) rindex(full, '/');
dot = (char *) rindex((slash?slash:full), '.');
if (dot)
*dot = '\0';
strcat(full, ext);
fd = open(full, O_RDONLY);
} else if (!name) {
/* stdin */
return 0;
} else {
/* simple name */
fd = open(name, O_RDONLY);
}
return fd;
}
/*
* Linas Vepstas -- 6 July 1989
* STDIO Fopen with possible substitution of extension
* name==0 => stdin
*/
FILE *Fopen(const char *name, const char *ext)
{
FILE *fyle;
if (ext) {
/* substitute extension */
char *slash, *dot, full[200];
if (!name)
return NULL;
strcpy(full, name);
slash = (char *) rindex(full, '/');
dot = (char *) rindex((slash?slash:full), '.');
if (dot)
*dot = '\0';
strcat(full, ext);
fyle = fopen (full, "wb");
} else if (!name) {
/* stdin */
return NULL;
} else {
/* simple name */
fyle = fopen (name, "wb");
}
return fyle;
}
/*
* Linas Vepstas -- 6 July 1989
* STDIO Fopen with possible substitution of extension
* name==0 => stdin
*/
FILE *Fopenr(const char *name, const char *ext)
{
FILE *fyle;
if (ext) {
/* substitute extension */
char *slash, *dot, full[200];
if (!name)
return NULL;
strcpy(full, name);
slash = (char *) rindex(full, '/');
dot = (char *) rindex((slash?slash:full), '.');
if (dot)
*dot = '\0';
strcat(full, ext);
fyle = fopen (full, "rb");
} else if (!name) {
/* stdin */
return NULL;
} else {
/* simple name */
fyle = fopen (name, "rb");
}
return fyle;
}
/* ----------------------- END OF FILE ---------------------- */