NCEPLIBS-bacio  2.5.0
bacio.c
Go to the documentation of this file.
1 
38 #include <stdio.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <fcntl.h>
42 #include <unistd.h>
43 #include <stdlib.h>
44 #include <ctype.h>
45 #include <string.h>
46 
47 #include "clib.h"
48 
100 int
101 baciol(int mode, long int start, int size, long int no,
102  long int *nactual, int *fdes, const char *fname, void *datary)
103 {
104  /* Initialization. */
105  *nactual = 0;
106 
107  /* Check for illegal combinations of options */
108  if ((BAOPEN_RONLY & mode) &&
109  ((BAOPEN_WONLY & mode) || (BAOPEN_WONLY_TRUNC & mode) || (BAOPEN_WONLY_APPEND & mode)))
110  return BA_EROANDWO;
111 
112  if ((BAREAD & mode) && (BAWRITE & mode))
113  return BA_ERANDW;
114 
115  /* Open files with correct read/write and file permission. */
116  if (BAOPEN_RONLY & mode)
117  {
118  *fdes = open(fname, O_RDONLY , S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP);
119  }
120  else if (BAOPEN_WONLY & mode)
121  {
122  *fdes = open(fname, O_WRONLY | O_CREAT , S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP);
123  }
124  else if (BAOPEN_WONLY_TRUNC & mode)
125  {
126  *fdes = open(fname, O_WRONLY | O_CREAT | O_TRUNC , S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP);
127  }
128  else if (BAOPEN_WONLY_APPEND & mode)
129  {
130  *fdes = open(fname, O_WRONLY | O_CREAT | O_APPEND , S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP);
131  }
132  else if (BAOPEN_RW & mode)
133  {
134  *fdes = open(fname, O_RDWR | O_CREAT , S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP);
135  }
136 
137  /* If the file open didn't work, or a bad fdes was passed in,
138  * return error. */
139  if (*fdes < 0)
140  return BA_EFILEOPEN;
141 
142  /* Check for bad mode flags. */
143  if (BAREAD & mode &&
144  ((BAOPEN_WONLY & mode) || (BAOPEN_WONLY_TRUNC & mode) || (BAOPEN_WONLY_APPEND & mode)))
145  return BA_ERONWO;
146 
147  /* Read data as requested. */
148  if (BAREAD & mode )
149  {
150  /* Seek the right part of the file. */
151  if (!(mode & NOSEEK))
152  if (lseek(*fdes, start, SEEK_SET) == -1)
153  return BA_ERNOSTART;
154 
155  if (datary == NULL)
156  {
157  printf("Massive catastrophe -- datary pointer is NULL\n");
158  return BA_EDATANULL;
159  }
160  *nactual = read(*fdes, (void *)datary, (size_t)no);
161  }
162 
163  /* Check for bad mode flag. */
164  if (BAWRITE & mode && BAOPEN_RONLY & mode)
165  return BA_EWANDRO;
166 
167  /* See if we should be writing. */
168  if (BAWRITE & mode)
169  {
170  if (!(mode & NOSEEK))
171  if (lseek(*fdes, start, SEEK_SET) == -1)
172  return BA_EWNOSTART;
173 
174  if (datary == NULL)
175  {
176  printf("Massive catastrophe -- datary pointer is NULL\n");
177  return BA_EDATANULL;
178  }
179  *nactual = write(*fdes, (void *) datary, (size_t)no);
180  }
181 
182  /* Close file if requested */
183  if (BACLOSE & mode )
184  if (close(*fdes) != 0)
185  return BA_ECLOSE;
186 
187  /* Check that if we were reading or writing, that we actually got
188  what we expected. Return 0 (success) if we're here and weren't
189  reading or writing. */
190  if ((mode & BAREAD || mode & BAWRITE) && (*nactual != no))
191  return BA_EFEWDATA;
192  else
193  return BA_NOERROR;
194 }
195 
196 
int baciol(int mode, long int start, int size, long int no, long int *nactual, int *fdes, const char *fname, void *datary)
Do a bacio operation.
Definition: bacio.c:101
Include file to define variables for Fortran to C interface(s) revision history.
#define BA_ECLOSE
Error in close.
Definition: clib.h:38
#define BA_NOERROR
No error.
Definition: clib.h:29
#define BA_EFEWDATA
Read or wrote fewer data than requested.
Definition: clib.h:39
#define BA_EDATANULL
Data pointer is NULL.
Definition: clib.h:40
#define BA_EROANDWO
Tried to open read only and write only.
Definition: clib.h:30
#define BA_EFILEOPEN
Failure in opening file.
Definition: clib.h:33
#define BA_EWANDRO
Tried to write to a read only file.
Definition: clib.h:36
#define BA_ERANDW
Tried to read and write in the same call.
Definition: clib.h:31
#define BAOPEN_WONLY
Open or create file for Write only.
Definition: clib.h:18
#define BAOPEN_WONLY_APPEND
Open or create a file for write only append.
Definition: clib.h:25
#define BAOPEN_RONLY
Open file read only.
Definition: clib.h:17
#define BA_ERNOSTART
Failed in read to find the 'start' location.
Definition: clib.h:35
#define BAWRITE
Write to an open file.
Definition: clib.h:22
#define BAREAD
Read from an open file.
Definition: clib.h:21
#define BAOPEN_WONLY_TRUNC
Open or create a file for write only, truncating existing contents.
Definition: clib.h:24
#define BAOPEN_RW
Open or create file for read/write.
Definition: clib.h:19
#define BA_ERONWO
Tried to read on a write-only file.
Definition: clib.h:34
#define NOSEEK
No seek ignore start parameter and do not call lseek().
Definition: clib.h:23
#define BACLOSE
Close an open file.
Definition: clib.h:20
#define BA_EWNOSTART
Failed in write to find the 'start' location.
Definition: clib.h:37