aboutsummaryrefslogtreecommitdiff
path: root/sys/libc/fseek.c
blob: fa32466cd9f89f2116ad8cd418d235d2f6d5652b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
*/

#define	import_spp
#define	import_libc
#define	import_stdio
#define	import_fset
#include <iraf.h>


/* FSEEK -- Seek on a file opened under stdio.  The seek functions do not
** completely emulate the UNIX seek functions.  The following restrictions
** apply:
**
**	- It is only permissible to seek to the beginning of a line if the
**	  actual file is a text file.  The seek offset must have been
**	  obtained by a prior call to FTELL, which returns the seek offset
**	  of the next line to be read or written.  Seek offsets cannot be
**	  manufactured, e.g., as physical char offsets in the file, as is
**	  the case for binary files.  These restrictions apply because text
**	  files are record structured on many systems, rather than simple
**	  byte stream files as in UNIX.
**
**	- It is permissible to randomly seek about on a binary file, but
**	  seeks must be aligned on XCHAR word boundaries in the file.  This
**	  can be guaranteed by structuring the application so that it always
**	  reads and writes binary data records that are an integral number
**	  of integers in size.  If this is done the program is portable to
**	  any IRAF machine as well as to UNIX.  Seek offsets are specified
**	  in units of bytes and are zero-indexed, as in C.
*/
int
fseek (
  FILE	*fp,			/* operand file				*/
  long	offset,			/* offset in file			*/
  int	mode			/* 0=absolute, 1=relative, 2=from EOF	*/
)
{
	register XINT	fd = fileno(fp);
	int	text_file, stat;
	long	c_note();


	text_file = (c_fstati (fd, F_TYPE) == TEXT_FILE);
	fp->_fflags &= ~_FEOF;

	if (text_file) {
	    switch (mode) {
	    case 0:
		if (offset == 0L)
		    stat = c_seek (fd, BOFL);
		else
		    stat = c_seek (fd, offset);
		break;
	    case 2:
		if (offset == 0L) {
		    stat = c_seek (fd, EOFL);
		    fp->_fflags |= _FEOF;
		} else
		    stat = ERR;
		break;
	    default:
		stat = ERR;
		break;
	    }

	} else {
	    /* Random seeks on (non-streaming) binary files are permitted,
	     * but the seek must be to an XCHAR offset.  This is checked
	     * by c_seek, which takes a zero-indexed byte offset as argument.
	     */
	    switch (mode) {
	    case 0:
		stat = c_seek (fd, offset);
		break;
	    case 1:
		stat = c_seek (fd, c_note(fd) + offset);
		break;
	    case 2:
		if ((stat = c_seek (fd, EOFL)) != ERR) {
		    if (offset == 0L)
			fp->_fflags |= _FEOF;
		    else
			stat = c_seek (fd, c_note(fd) - offset);
		}
		break;
	    default:
		stat = ERR;
	    }
	}

	return (stat);
}