1 /* $Id: $ */
2 
3 /* Copyright (C) 1997 Sverre Hvammen Johansen,
4  * Department of Informatics, University of Oslo.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 
20 #include "file.h"
21 #include "simfile.h"
22 
23 
24 /******************************************************************************
25                                          BOOLEAN PROCEDURE OPEN              */
26 
27 char
__rdopen(p,t)28 __rdopen (p, t)
29      __bs5FILE *p;
30      __txtvp t;
31 {
32   long loc;
33   if (!((__bs1FILE *) p)->open)
34     {
35       ((__bs1FILE *) p)->file
36 	= fopen (((__bs1FILE *) p)->filename.obj->string, "r");
37       if (((__bs1FILE *) p)->file != __NULL)
38 	(void) fclose (((__bs1FILE *) p)->file);
39       if (((__bs1FILE *) p)->create == __CREATE
40 	  && ((__bs1FILE *) p)->file != __NULL)
41 	return (__FALSE);
42       if (((__bs1FILE *) p)->create == __NOCREATE
43 	  && ((__bs1FILE *) p)->file == __NULL)
44 	return (__FALSE);
45 #if VMS_DIRECTFILE
46       if (((__bs1FILE *) p)->file == __NULL)
47 	{
48 	  if ((((__bs1FILE *) p)->file
49 	       = fopen (((__bs1FILE *) p)->filename.obj->string
50 			,"w")) == __NULL)
51 	    return (__FALSE);
52 	  (void) fclose (((__bs1FILE *) p)->file);
53 	}
54       if ((((__bs1FILE *) p)->file
55 	   = fopen (((__bs1FILE *) p)->filename.obj->string
56 					    ,"r+")) == __NULL)
57 	return (__FALSE);
58 #else
59       if ((((__bs1FILE *) p)->file
60 	   = fopen (((__bs1FILE *) p)->filename.obj->string
61 				 ,((__bs1FILE *) p)->file == __NULL ? "w+" :
62 	((__bs1FILE *) p)->readwrite == __READONLY ? "r" : "r+")) == __NULL)
63 	return (__FALSE);
64 #endif
65       if (((__bs1FILE *) p)->append == __APPEND)
66 	{
67 	  if (fseek (((__bs1FILE *) p)->file, 0L, 2) == __EOF)
68 	    {
69 	      (void) fclose (((__bs1FILE *) p)->file);
70 	      return (__FALSE);
71 	    }
72 	  loc = ftell (((__bs1FILE *) p)->file);
73 	}
74       else
75 	loc = 0;
76       p->loc = loc / (t->length + 1) + 1;
77       if (loc % (t->length + 1))
78 	__rerror ("Open: Illegal size on direct-file");
79       p->minwriteloc = p->loc;
80       p->lastop = __SEEK;
81       p->maxloc = (MAX_INT - 1) / (t->length + 1);
82       if (((__bs1FILE *) p)->readwrite == __READONLY)
83 	p->minwriteloc = MAX_INT;
84       if (((__bs1FILE *) p)->readwrite == __WRITEONLY)
85 	p->writeonly = __TRUE;
86       ((__bs2FILE *) p)->IMAGE.obj = t->obj;
87       ((__bs2FILE *) p)->IMAGE.length = t->length;
88       ((__bs2FILE *) p)->IMAGE.pos = 1;
89       ((__bs2FILE *) p)->IMAGE.start = t->start;
90       ((__bs5FILE *) p)->endfile = __FALSE;
91       ((__bs5FILE *) p)->imagelength = t->length;
92       return (((__bs1FILE *) p)->open = __TRUE);
93     }
94   else
95     return (__FALSE);
96 }
97