Skip to content

Latest commit

 

History

History
243 lines (209 loc) · 5.11 KB

common.c

File metadata and controls

243 lines (209 loc) · 5.11 KB
 
Oct 21, 1999
Oct 21, 1999
1
2
3
4
5
/*
TiMidity -- Experimental MIDI to WAVE converter
Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
This program is free software; you can redistribute it and/or modify
Dec 31, 2011
Dec 31, 2011
6
7
it under the terms of the Perl Artistic License, available in COPYING.
*/
Oct 21, 1999
Oct 21, 1999
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "config.h"
#include "common.h"
#include "output.h"
May 14, 2006
May 14, 2006
17
#include "ctrlmode.h"
Oct 21, 1999
Oct 21, 1999
18
19
20
21
/* I guess "rb" should be right for any libc */
#define OPEN_MODE "rb"
Oct 12, 2009
Oct 12, 2009
22
char current_filename[PATH_MAX];
Oct 21, 1999
Oct 21, 1999
23
Jul 4, 2005
Jul 4, 2005
24
static PathList *pathlist=NULL;
Oct 21, 1999
Oct 21, 1999
25
26
27
/* Try to open a file for reading. If the filename ends in one of the
defined compressor extensions, pipe the file through the decompressor */
Oct 10, 2009
Oct 10, 2009
28
static FILE *try_to_open(const char *name, int decompress, int noise_mode)
Oct 21, 1999
Oct 21, 1999
29
30
31
32
33
34
35
36
37
38
39
40
41
{
FILE *fp;
fp=fopen(name, OPEN_MODE); /* First just check that the file exists */
if (!fp)
return 0;
#ifdef DECOMPRESSOR_LIST
if (decompress)
{
int l,el;
static char *decompressor_list[] = DECOMPRESSOR_LIST, **dec;
Oct 10, 2009
Oct 10, 2009
42
const char *cp;
Oct 12, 2009
Oct 12, 2009
43
char tmp[PATH_MAX], tmp2[PATH_MAX], *cp2;
Oct 21, 1999
Oct 21, 1999
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
/* Check if it's a compressed file */
l=strlen(name);
for (dec=decompressor_list; *dec; dec+=2)
{
el=strlen(*dec);
if ((el>=l) || (strcmp(name+l-el, *dec)))
continue;
/* Yes. Close the file, open a pipe instead. */
fclose(fp);
/* Quote some special characters in the file name */
cp=name;
cp2=tmp2;
while (*cp)
{
switch(*cp)
{
case '\'':
case '\\':
case ' ':
case '`':
case '!':
case '"':
case '&':
case ';':
*cp2++='\\';
}
*cp2++=*cp++;
}
*cp2=0;
sprintf(tmp, *(dec+1), tmp2);
fp=popen(tmp, "r");
break;
}
}
#endif
return fp;
}
/* This is meant to find and open files for reading, possibly piping
them through a decompressor. */
Oct 4, 2009
Oct 4, 2009
88
FILE *open_file(const char *name, int decompress, int noise_mode)
Oct 21, 1999
Oct 21, 1999
89
90
{
FILE *fp;
May 1, 2006
May 1, 2006
91
PathList *plp;
Oct 21, 1999
Oct 21, 1999
92
93
94
95
96
97
98
99
int l;
if (!name || !(*name))
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Attempted to open nameless file.");
return 0;
}
Nov 28, 2006
Nov 28, 2006
100
if (pathlist==NULL) {
Jul 4, 2005
Jul 4, 2005
101
/* Generate path list */
Oct 10, 2009
Oct 10, 2009
102
#ifdef DEFAULT_PATH
Jul 4, 2005
Jul 4, 2005
103
104
add_to_pathlist(DEFAULT_PATH);
#endif
Oct 10, 2009
Oct 10, 2009
105
106
107
108
109
#ifdef DEFAULT_PATH1
add_to_pathlist(DEFAULT_PATH1);
#endif
#ifdef DEFAULT_PATH2
add_to_pathlist(DEFAULT_PATH2);
Jan 1, 2012
Jan 1, 2012
110
111
112
#endif
#ifdef DEFAULT_PATH3
add_to_pathlist(DEFAULT_PATH3);
Oct 10, 2009
Oct 10, 2009
113
114
#endif
}
Jul 4, 2005
Jul 4, 2005
115
Oct 21, 1999
Oct 21, 1999
116
117
/* First try the given name */
Oct 12, 2009
Oct 12, 2009
118
119
strncpy(current_filename, name, PATH_MAX - 1);
current_filename[PATH_MAX - 1]='\0';
Oct 21, 1999
Oct 21, 1999
120
121
122
123
124
ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Trying to open %s", current_filename);
if ((fp=try_to_open(current_filename, decompress, noise_mode)))
return fp;
May 20, 2002
May 20, 2002
125
#ifdef ENOENT
Oct 21, 1999
Oct 21, 1999
126
127
128
129
130
131
if (noise_mode && (errno != ENOENT))
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s",
current_filename, strerror(errno));
return 0;
}
May 20, 2002
May 20, 2002
132
#endif
May 1, 2006
May 1, 2006
133
134
plp=pathlist;
Oct 21, 1999
Oct 21, 1999
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
if (name[0] != PATH_SEP)
while (plp) /* Try along the path then */
{
*current_filename=0;
l=strlen(plp->path);
if(l)
{
strcpy(current_filename, plp->path);
if(current_filename[l-1]!=PATH_SEP)
strcat(current_filename, PATH_STRING);
}
strcat(current_filename, name);
ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Trying to open %s", current_filename);
if ((fp=try_to_open(current_filename, decompress, noise_mode)))
return fp;
May 20, 2002
May 20, 2002
150
#ifdef ENOENT
Oct 21, 1999
Oct 21, 1999
151
152
153
154
155
156
if (noise_mode && (errno != ENOENT))
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s",
current_filename, strerror(errno));
return 0;
}
May 20, 2002
May 20, 2002
157
#endif
Oct 21, 1999
Oct 21, 1999
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
plp=plp->next;
}
/* Nothing could be opened. */
*current_filename=0;
if (noise_mode>=2)
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s", name, strerror(errno));
return 0;
}
/* This closes files opened with open_file */
void close_file(FILE *fp)
{
#ifdef DECOMPRESSOR_LIST
if (pclose(fp)) /* Any better ideas? */
#endif
fclose(fp);
Jan 1, 2012
Jan 1, 2012
178
179
strncpy(current_filename, "MIDI file", PATH_MAX - 1);
Oct 21, 1999
Oct 21, 1999
180
181
182
183
184
185
}
/* This is meant for skipping a few bytes in a file or fifo. */
void skip(FILE *fp, size_t len)
{
size_t c;
Oct 12, 2009
Oct 12, 2009
186
char tmp[PATH_MAX];
Oct 21, 1999
Oct 21, 1999
187
188
189
while (len>0)
{
c=len;
Oct 12, 2009
Oct 12, 2009
190
if (c>PATH_MAX) c=PATH_MAX;
Oct 21, 1999
Oct 21, 1999
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
len-=c;
if (c!=fread(tmp, 1, c, fp))
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: skip: %s",
current_filename, strerror(errno));
}
}
/* This'll allocate memory or die. */
void *safe_malloc(size_t count)
{
void *p;
if (count > (1<<21))
{
ctl->cmsg(CMSG_FATAL, VERB_NORMAL,
"Strange, I feel like allocating %d bytes. This must be a bug.",
count);
}
else if ((p=malloc(count)))
return p;
else
ctl->cmsg(CMSG_FATAL, VERB_NORMAL, "Sorry. Couldn't malloc %d bytes.", count);
ctl->close();
exit(10);
May 20, 2002
May 20, 2002
215
return(NULL);
Oct 21, 1999
Oct 21, 1999
216
217
218
}
/* This adds a directory to the path list */
Oct 4, 2009
Oct 4, 2009
219
void add_to_pathlist(const char *s)
Oct 21, 1999
Oct 21, 1999
220
221
222
223
224
225
{
PathList *plp=safe_malloc(sizeof(PathList));
strcpy((plp->path=safe_malloc(strlen(s)+1)),s);
plp->next=pathlist;
pathlist=plp;
}
Jul 4, 2005
Jul 4, 2005
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* Free memory associated to path list */
void free_pathlist(void)
{
PathList *plp, *next_plp;
plp = pathlist;
while (plp) {
if (plp->path) {
free(plp->path);
plp->path=NULL;
}
next_plp = plp->next;
free(plp);
plp = next_plp;
}
Nov 28, 2006
Nov 28, 2006
242
pathlist = NULL;
Jul 4, 2005
Jul 4, 2005
243
}