Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gm_map.c
Go to the documentation of this file.
1 
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <errno.h>
6 #include <sys/types.h>
7 #include <sys/file.h>
8 #include <sys/fcntl.h>
9 #include <sys/mman.h>
10 
11 #include "global.h"
12 #include "object.h"
13 
14 #include "linuxlib_proto.h"
15 
16 struct GLOBAL_MEMORY *gm_map(name, iswrite)
17 char *name;
18 int iswrite;
19 {
20  int fd = -1, sz, isrd, iswr;
21  char zero = 1;
22  struct GLOBAL_MEMORY *map;
23 
24  sz = 0;
25 
26  /* Try to open the file */
27 
28  if(access(name, F_OK)) /* access failed, attempt to create it */
29  {
30  if(!iswrite)
31  {
32  st_report("gm_map can't create a file when called with no write access");
33  return(0);
34  }
35 
36  if(( fd = open( name, O_CREAT | O_RDWR, 0664 )) < 0)
37  {
38  st_report("gm_map(): File creation failed");
39  return(0);
40  }
41 
42  /* fill new file with zeros with unix lseek trick */
43  lseek(fd, sizeof(struct GLOBAL_MEMORY)-1, SEEK_SET );
44  zero = 0;
45  write(fd, &zero, 1 );
46  }
47  else /* File already exists */
48  {
49  isrd = access(name, R_OK)==0;
50  iswr = iswrite && access(name, W_OK)==0;
51 
52  if(iswr && isrd)
53  {
54  fd = open(name, O_RDWR,0);
55  }
56  else
57  if(isrd)
58  {
59  fd = open(name, O_RDONLY,0);
60  }
61 
62  if(fd < 0)
63  {
64  st_report("gm_map could not open file %s",name);
65  return(0);
66  }
67 
68  sz = lseek(fd, 0l, SEEK_END );
69 
70  /* Sanity check. The file ought to be the expected size. */
71  if( sz > 0 && sz != sizeof(struct GLOBAL_MEMORY))
72  {
73 
74  char *pwd;
75 
76  if( !(pwd = getenv("PWD")))
77  {
78  pwd = "no pwd";
79  }
80 
81  st_report("gm_map: %s (pwd %s) has %d bytes but expected %d",
82  name, pwd, sz, sizeof(struct GLOBAL_MEMORY));
83 
84  return(0);
85  }
86  }
87 
88  /* Map to the file */
89 
90  map = (struct GLOBAL_MEMORY *)mmap(0, sizeof(struct GLOBAL_MEMORY), PROT_READ | (iswr?PROT_WRITE:0), MAP_SHARED, fd, 0);
91 
92  close(fd);
93 
94  if(map == NULL)
95  {
96  st_report("gm_map could not map to %s", name);
97  perror("mmap");
98 
99  return 0;
100  }
101 
102  /* Result is also saved in a static pointer */
103 
104  CGM = map;
105 
106  return(map);
107 }
108 
109 
110 struct OBJECT_GLOBAL_MEMORY *obj_map(name, iswrite)
111 char *name;
112 int iswrite;
113 {
114  int fd = -1, sz, isrd, iswr;
115  char zero = 1;
116  struct OBJECT_GLOBAL_MEMORY *map;
117 
118  sz = 0;
119 
120  /* Try to open the file */
121 
122  if(access(name, F_OK)) /* access failed, attempt to create it */
123  {
124  if(!iswrite)
125  {
126  st_report("obj_map() can't create a file when called with no write access");
127  return(0);
128  }
129 
130  if(( fd = open( name, O_CREAT | O_RDWR, 0664 )) < 0)
131  {
132  st_report("obj_map(): File creation failed");
133  return(0);
134  }
135 
136  /* fill new file with zeros with unix lseek trick */
137  lseek(fd, sizeof(struct OBJECT_GLOBAL_MEMORY)-1, SEEK_SET );
138  zero = 0;
139  write(fd, &zero, 1 );
140  }
141  else /* File already exists */
142  {
143  isrd = access(name, R_OK)==0;
144  iswr = iswrite && access(name, W_OK)==0;
145 
146  if(iswr && isrd)
147  {
148  fd = open(name, O_RDWR,0);
149  }
150  else
151  if(isrd)
152  {
153  fd = open(name, O_RDONLY,0);
154  }
155 
156  if(fd < 0)
157  {
158  st_report("obj_map() could not open file %s",name);
159  return(0);
160  }
161 
162  sz = lseek(fd, 0l, SEEK_END );
163 
164  /* Sanity check. The file ought to be the expected size. */
165  if( sz > 0 && sz != sizeof(struct OBJECT_GLOBAL_MEMORY))
166  {
167  char *pwd;
168 
169  if( !(pwd = getenv("PWD")))
170  {
171  pwd = "no pwd";
172  }
173 
174  st_report("obj_map(): %s (pwd %s) has %d bytes but expected %d", name, pwd, sz, sizeof(struct OBJECT_GLOBAL_MEMORY));
175 
176  if(!iswrite)
177  {
178  st_report("obj_map() abort: Called with no write access");
179  return(0); /* Can't create a file if not allowed write access */
180  }
181 
182  st_report("obj_map(): Erasing this file to create another with the correct length");
183 
184  if(( fd = open( name, O_CREAT | O_TRUNC | O_RDWR, 0664 )) < 0)
185  {
186  st_report("obj_map(): File creation failed");
187  return(0);
188  }
189 
190  /* fill new file with zeros with unix lseek trick */
191 
192  lseek(fd, sizeof(struct OBJECT_GLOBAL_MEMORY)-1, SEEK_SET );
193  zero = 0;
194  write(fd, &zero, 1 );
195  }
196  }
197 
198  /* Map to the file */
199 
200  map = (struct OBJECT_GLOBAL_MEMORY *)mmap(0, sizeof(struct OBJECT_GLOBAL_MEMORY), PROT_READ | (iswr?PROT_WRITE:0), MAP_SHARED, fd, 0);
201 
202  close(fd);
203 
204  if(map == NULL)
205  {
206  st_report("obj_map() could not map to %s", name);
207  perror("mmap");
208 
209  return 0;
210  }
211 
212  /* Result is also saved in a static pointer */
213 
214  OGM = map;
215 
216  return(map);
217 }
struct OBJECT_GLOBAL_MEMORY * obj_map()
struct GLOBAL_MEMORY * gm_map()
struct GLOBAL_MEMORY * CGM
Definition: global.h:66
struct OBJECT_GLOBAL_MEMORY * OGM
Definition: object.h:115
void st_report()