Debian Codegraph @

libsrc/st/fsyunix.c

raw
1 /*===========================================================================
2 Copyright (C) 1995-2010 European Southern Observatory (ESO)
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
17 MA 02139, USA.
18
19 Correspondence concerning ESO-MIDAS should be addressed as follows:
20 Internet e-mail: [email protected]
21 Postal address: European Southern Observatory
22 Data Management Division
23 Karl-Schwarzschild-Strasse 2
24 D 85748 Garching bei Muenchen
25 GERMANY
26 ===========================================================================*/
27
28 /*++++++++++++++++++++++++++ Module FSY ++++++++++++++++++++++++++++++++
29 .TYPE Module
30 .NAME FSY
31 .LANGUAGE C
32 .AUTHOR
33 K. Banse ESO - Garching
34 .CATEGORY Image files handling.
35 .COMMENTS
36 holds FSY_EXTBDF, FSY_MODBDF
37 .VERSION
38 100222 last modif
39
40 ------------------------------------------------------------------------ */
41
42 #include <sys/types.h> /* to be used in extbdf !!! temporarily */
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <errno.h>
46
47
48 #include <osyparms.h>
49 #include <osparms.h>
50 #include <fileexts.h>
51
52
53 static struct stat buf;
54 static off_t filsize;
55
56 /*
57
58 */
59
60 int FSY_EXTBDF(fileid,exalq,nvb)
61 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
62 .PURPOSE Extend Bulk Data File
63 .METHOD This routine is used to extend an EXISTING Bulk Data File. The
64 file is opened using osdseek and extended by the specified number
65 of blocks; the new file size is then
66 calculated and returned to the calling program.
67 .RETURNS status (0 if all o.k. (VMS heritage), else 1,2,3)
68 -------------------------------------------------------------------------*/
69 int fileid /* file identification */;
70 int exalq /* extension allocation quantity */;
71 int *nvb /* number of virtual blocks actually allocated */;
72
73 {
74 off_t addr;
75
76 char c = ' ';
77
78
79
80 /* the file is supposed to have previously been opened */
81
82 addr = ((off_t)exalq * OUR_BLOCK_SIZE) - 1;
83 if (osdseek(fileid,addr,FILE_START) == -1L) /* position to `nobyt-1' */
84 return 1;
85
86 if (write(fileid,&c,1) != 1) return 2; /* write only last byte */
87
88
89 fstat(fileid,&buf); /* get new file size */
90 filsize = buf.st_size / OUR_BLOCK_SIZE;
91 *nvb = (int) filsize;
92 return 0;
93 }
94 /*
95 ^L
96 */
97
98 void FSY_OPNFIL(name, len, iochan, status)
99 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
100 .PURPOSE Open Binary File
101 .METHOD
102 This routine is used to open an EXISTING Data File for
103 subsequent processing. The filename, logical or physical, is
104 supplied by the calling program and the file is opened
105 via Unix open call
106 .RETURNS nothing
107 -------------------------------------------------------------------------*/
108
109 char *name /* file name */;
110 int len /* length of file name, not used in Unix! */;
111 int *iochan /* iochannel (file identifier in UNIX) */;
112 int *status /* return status (1 if correct, 0 elsewhere )!**/;
113
114 {
115
116 int filid;
117
118
119 filid = open(name,O_RDWR);
120
121
122 /* if no permission for attempted operation, try to open it for read only */
123
124 if (filid == -1) filid = open(name,O_RDONLY);
125
126
127 *iochan = filid;
128 if (filid < 0) /* open has failed anyway */
129 *status = 0;
130 else
131 *status = 1;
132
133 }
134
135 /*
136
137 */
138
139 int FSY_MODBDF(fileid, nobyt, nvb)
140 int fileid; /* file identification */
141 long int nobyt; /* no. (position) of byte to be written */
142 int *nvb; /* number of blocks allocated for file*/
143
144 {
145 char c = ' ';
146
147 off_t addr;
148
149
150
151 addr = (off_t) (nobyt-1);
152
153 if (osdseek(fileid,addr,FILE_START) == -1L)
154 { /* position to `nobyt-1' */
155 oserror = errno;
156 close(fileid);
157 return(-1);
158 }
159
160 if (write(fileid,&c,1) != 1)
161 { /* Write last byte */
162 oserror = errno;
163 close(fileid);
164 return(-1);
165 }
166
167 fstat(fileid,&buf);
168
169 filsize = buf.st_size;
170 filsize /= OUR_BLOCK_SIZE;
171 *nvb = (int) filsize;
172 return 0;
173 }
174