1 /*
2 * Copyright (c) 1991-1994 Sony Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL SONY CORPORATION BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Except as contained in this notice, the name of Sony Corporation
24 * shall not be used in advertising or otherwise to promote the sale, use
25 * or other dealings in this Software without prior written authorization
26 * from Sony Corporation.
27 *
28 */
29
30 /*
31 * $SonyRCSfile: file.c,v $
32 * $SonyRevision: 1.2 $
33 * $SonyDate: 1995/02/03 07:38:32 $
34 */
35
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42
43 #include "sj3mkdic.h"
44
45 typedef struct filelist {
46 FILE *fp;
47 char *fname;
48 struct filelist *next;
49 } FileList;
50
51 static FileList *flist = NULL;
52
53 static char *
get_fname(FILE * fp)54 get_fname(FILE *fp)
55 {
56 FileList *p;
57
58 for (p = flist ; p ; p = p -> next)
59 if (p -> fp == fp) return p -> fname;
60 return "some file";
61 }
62
63 FILE *
Fopen(char * filename,char * type)64 Fopen(char *filename, char *type)
65 {
66 FILE *fp;
67 char *p;
68 FileList *fl;
69 size_t len;
70
71 if (!(fp = fopen(filename, type))) {
72 fprintf(stderr, "Can't open %s mode \"%s\"\n", filename, type);
73 exit(1);
74 }
75
76 fl = (FileList *)Malloc(sizeof(*fl));
77 len = strlen(filename) + 1;
78 fl -> fname = Malloc(len);
79 strlcpy(fl -> fname, filename, len);
80 fl -> fp = fp;
81 fl -> next = flist;
82
83 return fp;
84 }
85
86 void
Fclose(FILE * fp)87 Fclose(FILE *fp)
88 {
89 FileList *p, *q;
90
91 if (fclose(fp) == EOF) {
92 fprintf(stderr, "Close error in %s\n", get_fname(fp));
93 exit(1);
94 }
95
96 for (p = flist, q = NULL ; p ; q = p, p = p -> next) {
97 if (p -> fp == fp) {
98 if (q)
99 q -> next = p -> next;
100 else
101 flist = p -> next;
102 free(p -> fname);
103 free(p);
104 break;
105 }
106 }
107 }
108
109 int
Fseek(FILE * fp,long ofs,int ptr)110 Fseek(FILE *fp, long ofs, int ptr)
111 {
112 if (fseek(fp, ofs, ptr) == 0) return 0;
113
114 fprintf(stderr, "Seek error in %s\n", get_fname(fp));
115 exit(1);
116 }
117
118 long
Ftell(FILE * fp)119 Ftell(FILE *fp)
120 {
121 return ftell(fp);
122 }
123
124 long
Fsize(char * filename)125 Fsize(char *filename)
126 {
127 struct stat buf;
128
129 if (stat(filename, &buf) == 0) return (long)buf.st_size;
130
131 fprintf(stderr, "Stat error in %s\n", filename);
132 exit(1);
133 }
134
135 size_t
Fread(char * p,int s,int n,FILE * fp)136 Fread(char *p, int s, int n, FILE *fp)
137 {
138 if (fread(p, s, n, fp) == n) return n;
139
140 fprintf(stderr, "Read error in %s\n", get_fname(fp));
141 exit(1);
142 }
143
144 size_t
Fwrite(char * p,int s,int n,FILE * fp)145 Fwrite(char *p, int s, int n, FILE *fp)
146 {
147 if (fwrite(p, s, n, fp) == n) return n;
148
149 fprintf(stderr, "Write error in %s\n", get_fname(fp));
150 exit(1);
151 }
152
153 int
Fgetc(FILE * fp)154 Fgetc(FILE *fp)
155 {
156 return fgetc(fp);
157 }
158
159 int
Fflush(FILE * fp)160 Fflush(FILE *fp)
161 {
162 return fflush(fp);
163 }
164