aboutsummaryrefslogtreecommitdiff
path: root/sys/fio/fgetfd.x
blob: 7669421ff6e8c567868cd05257fed61aa3ac6d26 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include <syserr.h>
include <config.h>
include <fio.h>

# FGETFD -- Allocate a file descriptor.  Called by all OPEN routines.
# Search static part of file descriptor storage for an open file descriptor.
# Allocate memory for rest of file descriptor, initialize all fields.

int procedure fgetfd (filename, mode, type)

char    filename[ARB]           # name of file to be assigned a descriptor
int     mode                    # access mode
int     type                    # file type

int     fd
int     fsetfd()
include	<fio.com>

begin
        for (fd=FIRST_FD;  fd <= LAST_FD && fiodes[fd] != NULL;  fd=fd+1)
            ;
        if (fd > LAST_FD)                               # out of descriptors
            call syserr (SYS_FTOOMANYFILES)

        return (fsetfd (fd, filename, mode, type))
end


# FSETFD -- Initialize the file descriptor FD.

int procedure fsetfd (fd, filename, mode, type)

int     fd                      # fd to be initialized
char    filename[ARB]           # name of file to be assigned to FD
int     mode                    # access mode
int     type                    # file type

int	or()
errchk  calloc, filerr, syserr
include <fio.com>
include "mmap.inc"

begin
        # Allocate descriptor.
        call calloc (fp, LEN_FIODES, TY_STRUCT)

        iop[fd]      = NULL
        itop[fd]     = NULL
        otop[fd]     = NULL
        bufptr[fd]   = NULL
        buftop[fd]   = NULL
        boffset[fd]  = 1
        redir_fd[fd] = NULL
        fflags[fd]   = 0
        fiodes[fd]   = fp                               # set ptr to fildes
        FCD(fp)      = FLCD(fp)                         # set ptr to chandes

	# Set the file permission bits for the given mode.  Note that read
	# permission is required in append mode on a binary file since the
	# partial block at the end of the file has to be read in before we
	# can append to it.

        switch (mode) {
        case STRING_FILE, SPOOL_FILE:
            # (neither read or write perm, disable flushnl)
        case READ_ONLY:
            fflags[fd] = FF_READ
            FILSIZE(fp) = -1                            # file size unknown
        case WRITE_ONLY:
            fflags[fd] = FF_WRITE
            FILSIZE(fp) = -1                            # file size unknown
        case READ_WRITE, APPEND:
            fflags[fd] = FF_READ + FF_WRITE
            FILSIZE(fp) = -1
        case NEW_FILE, TEMP_FILE:
	    if (type == STATIC_FILE) {
		fiodes[fd] = NULL
		call mfree (fp, TY_STRUCT)
		call filerr (filename, SYS_FSFOPNF)
	    }
            fflags[fd] = FF_READ + FF_WRITE
            FILSIZE(fp) = 0                             # zero length file
        default:
	    fiodes[fd] = NULL
	    call mfree (fp, TY_STRUCT)
            call filerr (filename, SYS_FILLEGMODE)
        }

        switch (type) {
        case STRING_FILE, SPOOL_FILE:
            # Allocate an (improper) device for string "files".  Since there
            # is no channel for a string file, any improper i/o on a string
            # file will result in an error return.

            FDEV(fp) = TX_DRIVER

	    # Spool files have all read and write permissions turned off
	    # so that they never try to write to a device driver - the file
	    # consists of only the buffered data.  Spool files are considered
	    # to be streaming binary files, so also set the blk size to 0.

	    if (type == SPOOL_FILE) {
		fflags[fd] = 0
		FBLKSIZE(fp) = 0
	    }

        case TEXT_FILE:
            fflags[fd] = or (FF_FLUSH, fflags[fd])
            FDEV(fp) = TX_DRIVER
        case BINARY_FILE:
            FDEV(fp) = BF_DRIVER
        case STATIC_FILE:
            FDEV(fp) = SF_DRIVER
        default:
	    fiodes[fd] = NULL
	    call mfree (fp, TY_STRUCT)
            call filerr (filename, SYS_FILLEGTYPE)
        }

	# A static file is equivalent to a binary file at the VOS level.
        FMODE(fp) = mmap[mode]
	if (type == STATIC_FILE)
	    FTYPE(fp) = BINARY_FILE
	else
	    FTYPE(fp) = type

        FCHAN(fp) = -1
        FNBUFS(fp) = 1
        FREFCNT(fp) = 1                                 # no. fd active on chan
        call strcpy (filename, FNAME(fp), SZ_FFNAME)

        return (fd)
end