aboutsummaryrefslogtreecommitdiff
path: root/sys/libc/fseek.c
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2015-07-08 20:46:52 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2015-07-08 20:46:52 -0400
commitfa080de7afc95aa1c19a6e6fc0e0708ced2eadc4 (patch)
treebdda434976bc09c864f2e4fa6f16ba1952b1e555 /sys/libc/fseek.c
downloadiraf-linux-fa080de7afc95aa1c19a6e6fc0e0708ced2eadc4.tar.gz
Initial commit
Diffstat (limited to 'sys/libc/fseek.c')
-rw-r--r--sys/libc/fseek.c93
1 files changed, 93 insertions, 0 deletions
diff --git a/sys/libc/fseek.c b/sys/libc/fseek.c
new file mode 100644
index 00000000..fa32466c
--- /dev/null
+++ b/sys/libc/fseek.c
@@ -0,0 +1,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);
+}