NCEPLIBS-bacio  2.6.0
bacio.c
Go to the documentation of this file.
1 
8 #include <stdio.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <stdlib.h>
14 #include <ctype.h>
15 #include <string.h>
16 
17 #include "clib.h"
18 
70 int
71 baciol(int mode, long int start, int size, long int no,
72  long int *nactual, int *fdes, const char *fname, void *datary)
73 {
74  /* Initialization. */
75  *nactual = 0;
76 
77  /* Check for illegal combinations of options */
78  if ((BAOPEN_RONLY & mode) &&
79  ((BAOPEN_WONLY & mode) || (BAOPEN_WONLY_TRUNC & mode) || (BAOPEN_WONLY_APPEND & mode)))
80  return BA_EROANDWO;
81 
82  if ((BAREAD & mode) && (BAWRITE & mode))
83  return BA_ERANDW;
84 
85  /* Open files with correct read/write and file permission. */
86  if (BAOPEN_RONLY & mode)
87  {
88  *fdes = open(fname, O_RDONLY , S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP);
89  }
90  else if (BAOPEN_WONLY & mode)
91  {
92  *fdes = open(fname, O_WRONLY | O_CREAT , S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP);
93  }
94  else if (BAOPEN_WONLY_TRUNC & mode)
95  {
96  *fdes = open(fname, O_WRONLY | O_CREAT | O_TRUNC , S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP);
97  }
98  else if (BAOPEN_WONLY_APPEND & mode)
99  {
100  *fdes = open(fname, O_WRONLY | O_CREAT | O_APPEND , S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP);
101  }
102  else if (BAOPEN_RW & mode)
103  {
104  *fdes = open(fname, O_RDWR | O_CREAT , S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP);
105  }
106 
107  /* If the file open didn't work, or a bad fdes was passed in,
108  * return error. */
109  if (*fdes < 0)
110  return BA_EFILEOPEN;
111 
112  /* Check for bad mode flags. */
113  if (BAREAD & mode &&
114  ((BAOPEN_WONLY & mode) || (BAOPEN_WONLY_TRUNC & mode) || (BAOPEN_WONLY_APPEND & mode)))
115  return BA_ERONWO;
116 
117  /* Read data as requested. */
118  if (BAREAD & mode )
119  {
120  /* Seek the right part of the file. */
121  if (!(mode & NOSEEK))
122  if (lseek(*fdes, start, SEEK_SET) == -1)
123  return BA_ERNOSTART;
124 
125  if (datary == NULL)
126  {
127  printf("Massive catastrophe -- datary pointer is NULL\n");
128  return BA_EDATANULL;
129  }
130  *nactual = read(*fdes, (void *)datary, (size_t)no);
131  }
132 
133  /* Check for bad mode flag. */
134  if (BAWRITE & mode && BAOPEN_RONLY & mode)
135  return BA_EWANDRO;
136 
137  /* See if we should be writing. */
138  if (BAWRITE & mode)
139  {
140  if (!(mode & NOSEEK))
141  if (lseek(*fdes, start, SEEK_SET) == -1)
142  return BA_EWNOSTART;
143 
144  if (datary == NULL)
145  {
146  printf("Massive catastrophe -- datary pointer is NULL\n");
147  return BA_EDATANULL;
148  }
149  *nactual = write(*fdes, (void *) datary, (size_t)no);
150  }
151 
152  /* Close file if requested */
153  if (BACLOSE & mode )
154  if (close(*fdes) != 0)
155  return BA_ECLOSE;
156 
157  /* Check that if we were reading or writing, that we actually got
158  what we expected. Return 0 (success) if we're here and weren't
159  reading or writing. */
160  if ((mode & BAREAD || mode & BAWRITE) && (*nactual != no))
161  return BA_EFEWDATA;
162  else
163  return BA_NOERROR;
164 }
165 
166 
BA_EDATANULL
#define BA_EDATANULL
Data pointer is NULL.
Definition: clib.h:33
BAOPEN_WONLY
#define BAOPEN_WONLY
Open or create file for Write only.
Definition: clib.h:11
clib.h
BAWRITE
#define BAWRITE
Write to an open file.
Definition: clib.h:15
BAOPEN_WONLY_TRUNC
#define BAOPEN_WONLY_TRUNC
Open or create a file for write only, truncating existing contents.
Definition: clib.h:17
BA_ERONWO
#define BA_ERONWO
Tried to read on a write-only file.
Definition: clib.h:27
BAOPEN_RONLY
#define BAOPEN_RONLY
Open file read only.
Definition: clib.h:10
BA_EFILEOPEN
#define BA_EFILEOPEN
Failure in opening file.
Definition: clib.h:26
BA_EROANDWO
#define BA_EROANDWO
Tried to open read only and write only.
Definition: clib.h:23
BA_NOERROR
#define BA_NOERROR
No error.
Definition: clib.h:22
BA_EWANDRO
#define BA_EWANDRO
Tried to write to a read only file.
Definition: clib.h:29
BA_ERANDW
#define BA_ERANDW
Tried to read and write in the same call.
Definition: clib.h:24
BA_ECLOSE
#define BA_ECLOSE
Error in close.
Definition: clib.h:31
BA_EFEWDATA
#define BA_EFEWDATA
Read or wrote fewer data than requested.
Definition: clib.h:32
BA_EWNOSTART
#define BA_EWNOSTART
Failed in write to find the 'start' location.
Definition: clib.h:30
baciol
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:71
BA_ERNOSTART
#define BA_ERNOSTART
Failed in read to find the 'start' location.
Definition: clib.h:28
BACLOSE
#define BACLOSE
Close an open file.
Definition: clib.h:13
BAOPEN_RW
#define BAOPEN_RW
Open or create file for read/write.
Definition: clib.h:12
BAREAD
#define BAREAD
Read from an open file.
Definition: clib.h:14
NOSEEK
#define NOSEEK
No seek ignore start parameter and do not call lseek().
Definition: clib.h:16
BAOPEN_WONLY_APPEND
#define BAOPEN_WONLY_APPEND
Open or create a file for write only append.
Definition: clib.h:18