OSDN Git Service

* aclocal.m4 (gcc_version_trigger): Use robust path construction.
[pf3gnuchains/gcc-fork.git] / libchill / writerecord.c
1 /* Implement Input/Output runtime actions for CHILL.
2    Copyright (C) 1992,1993 Free Software Foundation, Inc.
3    Author: Wilfried Moser, et al
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 /* As a special exception, if you link this library with other files,
23    some of which are compiled with GCC, to produce an executable,
24    this library does not by itself cause the resulting executable
25    to be covered by the GNU General Public License.
26    This exception does not however invalidate any other reasons why
27    the executable file might be covered by the GNU General Public License.  */
28
29 #include <setjmp.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/types.h>
34
35 #include "fileio.h"
36
37 static
38 void
39 doWrite( Access_Mode* the_access, void* buf, size_t nbyte )
40 {
41   size_t nwrit;
42
43   nwrit = write( the_access->association->handle, buf, nbyte );
44
45   if( nwrit < nbyte )
46   {
47     the_access->association->syserrno = errno;
48     RWEXCEPTION( WRITEFAIL, OS_IO_ERROR );
49   }
50 }
51
52
53 void
54 __writerecord( Access_Mode*  the_access,
55                signed long   the_index,
56                char*         the_val_addr,
57                unsigned long the_val_len,
58                char*         file,
59                int           line )
60
61 {
62   Association_Mode* the_assoc;
63   unsigned long  info;
64   char*          actaddr;
65   unsigned short actlen;
66   off_t          filepos;
67
68   if( !the_access )
69     CHILLEXCEPTION( file, line, EMPTY, NULL_ACCESS );
70
71   if( !(the_assoc = the_access->association) )
72     CHILLEXCEPTION( file, line, NOTCONNECTED, IS_NOT_CONNECTED );
73
74   /* Usage must no be ReadOnly */
75   if( the_assoc->usage == ReadOnly )
76     CHILLEXCEPTION( file, line, WRITEFAIL, BAD_USAGE );
77
78   /*
79    *  Positioning
80    */
81   if( TEST_FLAG( the_access, IO_INDEXED ) )
82   {
83     /* index expression must be within bounds of index mode */
84     if( the_index < the_access->lowindex
85         || the_access->highindex < the_index )
86       CHILLEXCEPTION( file, line, RANGEFAIL, BAD_INDEX );
87     filepos = the_access->base + 
88               (the_index - the_access->lowindex) * the_access->reclength;
89
90     if( lseek( the_assoc->handle, filepos, SEEK_SET ) == -1L )
91       CHILLEXCEPTION( file, line, WRITEFAIL, LSEEK_FAILS );
92   }
93
94   if( (info = setjmp( __rw_exception )) )
95     CHILLEXCEPTION( file, line, info>>16, info & 0xffff );
96
97   if( TEST_FLAG( the_access, IO_TEXTIO ) )
98   {
99     if( TEST_FLAG( the_access, IO_INDEXED ) )
100     {
101       int nspace = the_access->reclength - the_val_len;
102       memset( the_val_addr + 2 + the_val_len, ' ', nspace );
103       actlen = the_access->reclength - 2;
104       MOV2(the_val_addr,&actlen);
105       doWrite( the_access, the_val_addr, the_access->reclength );
106     }
107     else
108     { 
109       if( the_assoc->ctl_pre )
110         write( the_assoc->handle, &the_assoc->ctl_pre, 1 );
111       MOV2(&actlen,the_val_addr);
112       write( the_assoc->handle, the_val_addr + 2, actlen );
113       if( the_assoc->ctl_post )
114         write( the_assoc->handle, &the_assoc->ctl_post, 1 );
115       the_assoc->ctl_pre  = '\0';
116       the_assoc->ctl_post = '\n';
117     }
118   }
119   else
120   {
121     switch( the_access->rectype )
122     {
123     case Fixed:
124       if( TEST_FLAG( the_assoc, IO_VARIABLE ) )
125       {
126         actlen = the_access->reclength;
127         doWrite( the_access, &actlen, sizeof(actlen) );
128       }
129       doWrite( the_access, the_val_addr, the_val_len );
130       break;
131     case VaryingChars:
132       MOV2(&actlen,the_val_addr);
133       if( actlen > the_access->reclength - 2 )
134         CHILLEXCEPTION( file, line, RANGEFAIL, RECORD_TOO_LONG );
135       actlen = TEST_FLAG( the_access, IO_INDEXED ) 
136                ? the_access->reclength : actlen + 2;
137       doWrite( the_access, the_val_addr, actlen );
138       break;
139     }
140   }
141 }