1 /* Machine-independent I/O routines for gcov.
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 Contributed by Bob Manson <manson@cygnus.com>.
5 This file is part of GNU CC.
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)
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.
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. */
25 #include <sys/types.h>
27 /* These routines only work for signed values. */
29 /* Store a portable representation of VALUE in DEST using BYTES*8-1 bits.
30 Return a non-zero value if VALUE requires more than BYTES*8-1 bits
34 __store_long (value
, dest
, bytes
)
39 int upper_bit
= (value
< 0 ? 128 : 0);
44 long oldvalue
= value
;
46 if (oldvalue
!= -value
)
50 for(i
= 0 ; i
< (sizeof (value
) < bytes
? sizeof (value
) : bytes
) ; i
++) {
51 dest
[i
] = value
& (i
== (bytes
- 1) ? 127 : 255);
55 if (value
&& value
!= -1)
58 for(; i
< bytes
; i
++)
60 dest
[bytes
- 1] |= upper_bit
;
64 /* Retrieve a quantity containing BYTES*8-1 bits from SOURCE and store
65 the result in DEST. Returns a non-zero value if the value in SOURCE
66 will not fit in DEST. */
69 __fetch_long (dest
, source
, bytes
)
77 for (i
= bytes
- 1; i
> (sizeof (*dest
) - 1); i
--)
78 if (source
[i
] & (i
== (bytes
- 1) ? 127 : 255 ))
82 value
= value
* 256 + (source
[i
] & (i
== (bytes
- 1) ? 127 : 255));
84 if ((source
[bytes
- 1] & 128) && (value
> 0))
91 /* Write a BYTES*8-bit quantity to FILE, portably. Returns a non-zero
92 value if the write fails, or if VALUE can't be stored in BYTES*8
95 Note that VALUE may not actually be large enough to hold BYTES*8
96 bits, but BYTES characters will be written anyway.
98 BYTES may be a maximum of 10. */
101 __write_long (value
, file
, bytes
)
108 if (bytes
> 10 || __store_long (value
, c
, bytes
))
111 return fwrite(c
, 1, bytes
, file
) != bytes
;
114 /* Read a quantity containing BYTES bytes from FILE, portably. Return
115 a non-zero value if the read fails or if the value will not fit
118 Note that DEST may not be large enough to hold all of the requested
119 data, but the function will read BYTES characters anyway.
121 BYTES may be a maximum of 10. */
124 __read_long (dest
, file
, bytes
)
131 if (bytes
> 10 || fread(c
, 1, bytes
, file
) != bytes
)
134 return __fetch_long (dest
, c
, bytes
);