BSD 3 development
[unix-history] / usr / doc / f77
CommitLineData
7fe92b09
BJ
1.de XX
2.ne 3
3.sp .3
4.ti -1i
5.ta 1i
6\\$1 \c
7..
8.EQ
9delim $$
10.EN
11.ND "1 August 1978"
12.RP
13.TL
14A Portable Fortran 77 Compiler
15.AU
16S. I. Feldman
17.AU
18P. J. Weinberger
19.AI
20.MH
21.AB
22.LP
23The Fortran language has just been revised.
24The new language, known as Fortran 77,
25became an official American National Standard on April 3, 1978.
26We report here on a compiler and run-time system for the new extended language.
27This is believed to be the first complete Fortran 77 system to be implemented.
28This compiler is designed to be portable,
29to be correct and complete,
30and to generate code compatible with calling sequences produced by C compilers.
31In particular, this Fortran is quite usable on
32.UX
33systems.
34In this paper, we describe the language compiled,
35interfaces between procedures,
36and file formats assumed by the I/O system.
37An appendix describes the Fortran 77 language.
38.AE
39.CS 9 10 19 0 0 8
40.NH 0
41INTRODUCTION
42.PP
43The Fortran language has just been revised.
44The new language, known as Fortran 77, became an official American National Standard [1] on April 3, 1978.
45for the language, known as Fortran 77, is about to be published.
46Fortran 77 supplants 1966 Standard Fortran [2].
47We report here on a compiler and run-time system for the new extended language.
48The compiler and computation library were written by SIF, the I/O system by PJW.
49We believe ours to be the first complete Fortran 77 system to be implemented.
50This compiler is designed to be portable to a number of different machines,
51to be correct and complete,
52and to generate code compatible with calling sequences produced
53by compilers for the C language [3].
54In particular,
55it is in use on
56.UX
57systems.
58Two families of C compilers are in use at Bell Laboratories,
59those based on D. M. Ritchie's PDP-11 compiler[4]
60and those based on S. C. Johnson's portable C compiler [5].
61This Fortran compiler can drive the second passes of either family.
62In this paper, we describe the language compiled,
63interfaces between procedures,
64and file formats assumed by the I/O system.
65We will describe implementation details in companion papers.
66.PP
67.NH 2
68Usage
69.PP
70At present, versions of the compiler run on and compile for the PDP-11,
71the VAX-11/780,
72and the Interdata 8/32
73.UX
74systems.
75The command to run the compiler is
76.DS C
77f\|77 \fIflags file . . .\fR
78.DE
79.B f\|77
80is a general-purpose command for compiling and loading Fortran and Fortran-related files.
81EFL [6] and Ratfor [7] source files will be preprocessed before being presented to the Fortran compiler.
82C and assembler source files will be compiled by the appropriate programs.
83Object files will be loaded.
84(The
85.B f\|77
86and
87.B cc
88commands cause slightly different loading sequences to be generated,
89since Fortran programs need a few extra libraries and a different startup routine
90than do C programs.)
91The following file name suffixes are understood:
92.DS I
93 .f Fortran source file
94 .e EFL source file
95 .r Ratfor source file
96 .c C source file
97 .s Assembler source file
98 .o Object file
99.DE
100The following flags are understood:
101.in +1i
102.XX \(miS
103Generate assembler output for each source file, but do not assemble it.
104Assembler output for a source file
105.B x.f,
106.B x.e,
107.B x.r,
108or
109.B x.c
110is put on file
111\fBx.s\fR.
112.XX \(mic
113Compile but do not load.
114Output for
115.B x.f,
116.B x.e,
117.B x.r,
118.B x.c,
119or
120.B x.s
121is put on file
122.B x.o.
123.XX \(mim
124Apply the M4 macro preprocessor to each EFL or Ratfor source file before using the appropriate compiler.
125.XX \(mif
126Apply the EFL or Ratfor processor to all
127relevant files, and leave the output from
128.B x.e
129or
130.B x.r
131on
132.B x.f.
133Do not compile the resulting Fortran program.
134.XX \(mip
135Generate code to produce usage profiles.
136.XX "\(mio \fIf\fR"
137Put executable module on file
138.I f.
139(Default is
140\fBa.out\fR).
141.XX \(miw
142Suppress all warning messages.
143.XX \(miw66
144Suppress warnings about Fortran 66 features used.
145.XX \(miO
146Invoke the C object code optimizer.
147.XX \(miC
148Compile code the checks that subscripts are within array bounds.
149.XX \(mionetrip
150Compile code that performs every
151.B do
152loop at least once.
153(see Section 2.10).
154.XX \(miU
155Do not convert upper case letters to lower case.
156The default is to convert Fortran programs to lower case.
157.XX \(miu
158Make the default type of a variable
159.B undefined.
160(see Section 2.3).
161.XX \(miI2
162On machines which support short integers,
163make the default integer constants and variables short.
164(\fB\(miI4\fR is the standard value of this option). (see Section 2.14).
165All logical quantities will be short.
166.XX \(miE
167The remaining characters in the argument are used as an EFL flag argument.
168.XX \(miR
169The remaining characters in the argument are used as a Ratfor flag argument.
170.XX \(miF
171Ratfor and and EFL source programs are pre-processed into Fortran files,
172but those files are not compiled or removed.
173.in -1i
174.LP
175Other flags,
176all library names (arguments beginning \fB\(mil\fR),
177and any names not ending with one of the understood suffixes are passed to the loader.
178.NH 2
179Documentation Conventions
180.PP
181In running text, we write Fortran keywords and other literal strings in boldface lower case.
182Examples will be presented in lightface lower case.
183Names representing a class of values will be printed in italics.
184.NH 2
185Implementation Strategy
186.PP
187The compiler and library are written entirely in C.
188The compiler generates C compiler intermediate code.
189Since there are C compilers running on a variety of machines,
190relatively small changes will make this Fortran compiler generate code for any of them.
191Furthermore, this approach guarantees that the resulting programs are compatible with C usage.
192The runtime computational library is complete.
193The mathematical functions are computed to at least 63 bit precision.
194The runtime I/O library makes use of D. M. Ritchie's Standard C I/O package [8]
195for transferring data.
196With the few exceptions described below, only documented calls are used,
197so it should be relatively easy to modify to run on other operating
198systems.
199.NH 1
200LANGUAGE EXTENSIONS
201.PP
202Fortran 77 includes almost all of Fortran 66 as a subset.
203We describe the differences briefly in the Appendix.
204The most important additions are a character string data type,
205file-oriented input/output statements, and random access I/O.
206Also, the language has been cleaned up considerably.
207.PP
208In addition to implementing the language specified in the new Standard,
209our compiler implements a few extensions described in this section.
210Most are useful additions to the language.
211The remainder are extensions
212to make it easier to communicate with C procedures
213or to permit compilation of
214old (1966 Standard) programs.
215.NH 2
216Double Complex Data Type
217.IP
218The new type
219.B "double complex"
220is defined.
221Each datum is represented by a pair of double precision real variables.
222A double complex version of every
223.B complex
224built-in function is provided.
225The specific function names begin with \fBz\fR instead of \fBc\fR.
226.NH 2
227Internal Files
228.IP
229The Fortran 77 standard introduces ``internal files'' (memory arrays), but
230restricts their use to formatted sequential I/O statements.
231Our I/O system also permits internal files to be used
232in direct and unformatted reads and writes.
233.NH 2
234Implicit Undefined statement
235.IP
236Fortran 66 has a fixed rule that the type of a variable that does not appear in a type statement
237is
238.B integer
239if its first letter is
240\fBi, j, k, l, m\fR or \fBn\fR,
241and
242.B real
243otherwise.
244Fortran 77 has an
245.B implicit
246statement for overriding this rule.
247As an aid to good programming practice, we permit an additional type,
248.B undefined.
249The statement
250.DS
251implicit undefined(a-z)
252.DE
253turns off the automatic data typing mechanism,
254and the compiler will issue a diagnostic for each variable that is used but does
255not appear in a type statement.
256Specifying the
257.B \(miu
258compiler flag is equivalent to beginning each procedure with this statement.
259.NH 2
260Recursion
261.IP
262Procedures may call themselves, directly or through a chain of other procedures.
263.NH 2
264Automatic Storage
265.IP
266Two new keywords are recognized,
267.B static
268and
269.B automatic.
270These keywords may appear as ``types'' in type statements and in
271.B implicit
272statements.
273Local variables are static by default;
274there is exactly one copy of the datum, and its value is retained between calls.
275There is one copy of each variable declared
276.B automatic
277for each invocation of the procedure.
278Automatic variables may not appear in
279.B equivalence,
280.B data,
281or
282.B save
283statements.
284.NH 2
285Source Input Format
286.IP
287The Standard expects input to the compiler to be in 72 column format:
288except in comment lines,
289the first five characters are the statement number, the next is the continuation character,
290and the next sixty-six are the body of the line.
291(If there are fewer than seventy-two characters on a line, the compiler pads it with blanks;
292characters after the seventy-second are ignored).
293.IP
294In order to make it easier to type Fortran programs,
295our compiler also accepts input in variable length lines.
296An ampersand (``&'') in the first position of a line indicates a continuation
297line; the remaining characters form the body of the line.
298A tab character in one of the first six positions of a line signals the
299end of the statement number and continuation part of the line;
300the remaining characters form the body of the line.
301A tab elsewhere on the line is treated as another kind of blank by the
302compiler.
303.IP
304In the Standard, there are only 26 letters \(em Fortran is a one-case language.
305Consistent with ordinary
306.UX
307system usage, our compiler expects lower case input.
308By default, the compiler converts all upper case characters to lower case except those inside character constants.
309However, if the
310.B \(miU
311compiler flag is specified, upper case letters are not transformed.
312In this mode, it is possible to specify external names with upper case letters in them,
313and to have distinct variables differing only in case.
314Regardless of the setting of the flag,
315keywords will only be recognized in lower case.
316.NH 2
317Include Statement
318.IP
319The statement
320.DS
321include \(fmstuff\|\(fm
322.DE
323is replaced by the contents of the file
324.B stuff.
325\fBinclude\fRs may be nested to a reasonable depth, currently ten.
326.NH 2
327Binary Initialization Constants
328.IP
329A
330.B logical,
331.B real,
332or
333.B integer
334variable may be initialized in a
335.B data
336statement
337by a binary constant, denoted by a letter followed by a quoted string.
338If the letter is \fBb\fR, the string is binary, and only zeroes and ones are permitted.
339If the letter is \fBo\fR, the string is octal, with digits \fB0\(mi7\fR.
340If the letter is \fBz\fR or \fBx\fR, the string is hexadecimal, with digits \fB0\(mi9\fR, \fBa\(mif\fR.
341Thus, the statements
342.DS
343integer a(3)
344data a / b\(fm1010\(fm, o\(fm12\(fm, z\(fma\(fm /
345.DE
346initialize all three elements of
347.B a
348to ten.
349.NH 2
350Character Strings
351.IP
352For compatibility with C usage, the following backslash escapes are recognized:
353.DS
354\en newline
355\et tab
356\eb backspace
357\ef form feed
358\e0 null
359\e\(fm apostrophe (does not terminate a string)
360\e" quotation mark (does not terminate a string)
361\e\e \e
362\e\fIx\fR \fIx\fR, where \fIx\fR is any other character
363.DE
364Fortran 77 only has one quoting character, the apostrophe.
365Our compiler and I/O system recognize
366both the apostrophe ( \(fm ) and the double-quote ( " ).
367If a string begins with one variety of quote mark, the other may be embedded within it
368without using the repeated quote or backslash escapes.
369.IP
370Every unequivalenced scalar local character variable and every character string constant is aligned
371on an
372.B integer
373word boundary.
374Each character string constant appearing outside a
375.B data
376statement is followed by a
377null character to ease communication with C routines.
378.NH 2
379Hollerith
380.IP
381Fortran 77 does not have the old Hollerith (\fIn\|\fBh\fR)
382notation,
383though the new Standard recommends implementing the old Hollerith feature
384in order to improve compatibility with old programs.
385In our compiler, Hollerith data may be used in place of character string constants,
386and may also be used to initialize non-character variables in
387.B data
388statements.
389.NH 2
390Equivalence Statements
391.IP
392As a very special and peculiar case,
393Fortran 66 permits an element of a multiply-dimensioned array to be represented by
394a singly-subscripted reference in
395.B equivalence
396statements.
397Fortran 77 does not permit this usage, since
398subscript lower bounds may now be different from 1.
399Our compiler permits single subscripts in
400.B equivalence
401statements,
402under the interpretation that all missing subscripts are equal to 1.
403A warning message is printed for each such incomplete subscript.
404.NH 2
405One-Trip DO Loops
406.IP
407The Fortran 77 Standard requires that the range of a
408.B do
409loop not be performed
410if the initial value is already past the limit value,
411as in
412.DS
413do 10 i = 2, 1
414.DE
415The 1966 Standard stated that the effect of such a statement was undefined,
416but it was common practice that the range of a
417.B do
418loop would be performed
419at least once.
420In order to accommodate old programs, though they were in violation of the 1966 Standard,
421the
422.B \(mionetrip
423compiler flag causes non-standard loops to be generated.
424.NH 2
425Commas in Formatted Input
426.IP
427The I/O system attempts to be more lenient than the
428Standard when it seems worthwhile.
429When doing a formatted read of non-character variables,
430commas may be used as value separators in the input record,
431overriding the field lengths given in the format statement.
432Thus,
433the format
434.DS
435(i10, f20.10, i4)
436.DE
437will read the record
438.DS
439\(mi345,.05e\(mi3,12
440.DE
441correctly.
442.NH 2
443Short Integers
444.IP
445On machines that support halfword integers,
446the compiler accepts declarations of type
447.B integer\(**2.
448(Ordinary integers follow the Fortran rules about occupying the same
449space as a REAL variable; they are assumed to be of C type
450.B "long int" ;
451halfword integers are of C type
452.B "short int" .)
453An expression involving only objects of type
454.B integer\(**2
455is of that type.
456Generic functions return short or long integers depending on the actual types of their arguments.
457If a procedure is compiled using the
458.B \(miI2
459flag, all small integer constants will be
460of type
461.B integer\(**2.
462If the precision of an integer-valued intrinsic function is not determined by the generic function rules,
463one will be chosen that returns the prevailing length
464(\fBinteger\(**2\fR when the \fB\(miI2\fR command flag is in effect).
465When the
466.B \(miI2
467option is in effect, all quantities of type
468.B logical
469will be short.
470Note that these short integer and logical quantities do not obey the standard rules for storage association.
471.NH 2
472Additional Intrinsic Functions
473.IP
474This compiler supports all of the intrinsic functions specified in the Fortran 77 Standard.
475In addition, there are functions for performing bitwise Boolean operations
476(
477.B or,
478.B and,
479.B xor,
480and
481.B not)
482and for accessing the
483.UX
484command arguments
485(
486.B getarg
487and
488.B iargc
489).
490.NH 1
491VIOLATIONS OF THE STANDARD
492.PP
493We know only thre ways in which our Fortran system violates the new standard:
494.NH 2
495Double Precision Alignment
496.IP
497The Fortran standards (both 1966 and 1977)
498permit
499.B common
500or
501.B equivalence
502statements to force a double precision quantity onto an odd word boundary,
503as in the following example:
504.DS I
505real a(4)
506double precision b,c
507.sp .5
508equivalence (a(1),b), (a(4),c)
509.DE
510Some machines (e.g., Honeywell 6000, IBM 360) require that double precision quantities be on double word boundaries;
511other machines (e.g., IBM 370), run inefficiently if this alignment rule is not observed.
512It is possible to tell which equivalenced and common variables suffer from a forced odd
513alignment, but every double precision argument would have to be assumed on a bad boundary.
514To load such a quantity on some machines,
515it would be necessary to use separate operations to move the upper and lower halves
516into the halves of an aligned temporary, then to load that double precision temporary; the reverse would be
517needed to store a result.
518We have chosen to require that all double precision real and complex quantities
519fall on even word boundaries on machines with corresponding hardware requirements,
520and to issue a diagnostic if the source code demands a violation of the rule.
521.NH 2
522Dummy Procedure Arguments
523.IP
524If any argument of a procedure is of type character,
525all dummy procedure arguments of that procedure must be declared
526in an
527.B external
528statement.
529This requirement arises as a subtle corollary of the way we represent character string arguments
530and of the one-pass nature of the compiler.
531A warning is printed if a dummy procedure is not declared
532.B external.
533Code is correct if there are no
534.B character
535arguments.
536.NH 2
537T and TL Formats
538.IP
539The implementation of the
540.B t
541(absolute tab)
542and
543.B tl
544(leftward tab)
545format codes
546is defective.
547These codes allow rereading or rewriting part of the
548record which has already been processed.
549(Section 6.3.2 in the Appendix.)
550The implementation uses seeks,
551so if the unit is not one which allows seeks,
552such as a terminal,
553the program is in error.
554(People who can make a case for using
555.B tl
556should let us know.)
557A benefit of the implementation chosen is
558that there is no upper limit on the length of
559a record,
560nor is it necessary to predeclare any record
561lengths except where specifically required
562by Fortran or the operating system.
563.NH 1
564INTER-PROCEDURE INTERFACE
565.PP
566To be able to write C procedures that call or are called by Fortran procedures,
567it is necessary to know the conventions for procedure names,
568data representation,
569return values,
570and argument lists that the compiled code obeys.
571.NH 2
572Procedure Names
573.PP
574On
575.UX
576systems,
577the name of a common block or a Fortran procedure
578has an underscore appended to it by the compiler
579to distinguish it from a C procedure or external variable
580with the same user-assigned name.
581Fortran library procedure names have embedded underscores to avoid clashes
582with user-assigned subroutine names.
583.NH 2
584Data Representations
585.PP
586The following is a table of corresponding Fortran and C declarations:
587.KS
588.TS
589center;
590c c
591l l.
592Fortran C
593.sp .5
594integer\(**2 x short int x;
595integer x long int x;
596logical x long int x;
597real x float x;
598double precision x double x;
599complex x struct { float r, i; } x;
600double complex x struct { double dr, di; } x;
601character\(**6 x char x[6];
602.TE
603.KE
604(By the rules of Fortran,
605.B integer,
606.B logical,
607and
608.B real
609data occupy the same amount of memory).
610.NH 2
611Return Values
612.PP
613A function of type
614.B integer,
615.B logical,
616.B real,
617or
618.B "double precision"
619declared as a C function that returns the corresponding type.
620A
621.B complex
622or
623.B "double complex"
624function is equivalent to a C routine
625with an additional
626initial argument that points to the place where the return value is to be stored.
627Thus,
628.DS
629complex function f( . . . )
630.DE
631is equivalent to
632.DS
633f_(temp, . . .)
634struct { float r, i; } \(**temp;
635 . . .
636.DE
637.DE
638A character-valued function is equivalent to a C routine with
639two extra initial arguments:
640a data address and a length.
641Thus,
642.DS
643character\(**15 function g( . . . )
644.DE
645is equivalent to
646.DS
647g_(result, length, . . .)
648char result[ ];
649long int length;
650 . . .
651.DE
652and could be invoked in C by
653.DS
654char chars[15];
655 . . .
656g_(chars, 15L, . . . );
657.DE
658Subroutines are invoked as if they were \fBinteger\fR-valued functions
659whose value specifies which alternate return to use.
660Alternate return arguments (statement labels) are not passed to the function,
661but are used to do an indexed branch in the calling procedure.
662(If the subroutine has no entry points with alternate return arguments,
663the returned value is undefined.)
664The statement
665.DS
666call nret(\(**1, \(**2, \(**3)
667.DE
668is treated exactly as if it were the computed
669.B goto
670.DS
671goto (1, 2, 3), nret( )
672.DE
673.NH 2
674Argument Lists
675.PP
676All Fortran arguments are passed by address.
677In addition,
678for every argument that is of type character or
679that is a dummy procedure,
680an argument giving the length of the value is passed.
681(The string lengths are
682.B "long int"
683quantities passed by value).
684The order of arguments is then:
685.DS
686Extra arguments for complex and character functions
687Address for each datum or function
688A \fBlong int\fR for each character or procedure argument
689.DE
690Thus, the call in
691.DS
692external f
693character\(**7 s
694integer b(3)
695 . . .
696call sam(f, b(2), s)
697.DE
698is equivalent to that in
699.DS
700int f();
701char s[7];
702long int b[3];
703 . . .
704sam_(f, &b[1], s, 0L, 7L);
705.DE
706Note that the first element of a C array always has subscript zero,
707but Fortran arrays begin at 1 by default.
708Fortran arrays are stored in column-major order, C arrays are stored in row-major order.
709.NH 1
710FILE FORMATS
711.NH 2
712Structure of Fortran Files
713.PP
714Fortran requires four kinds of external files:
715sequential formatted and unformatted,
716and direct formatted and unformatted.
717On
718.UX
719systems,
720these are all implemented as ordinary files
721which are assumed to have the proper
722internal structure.
723.PP
724Fortran I/O is based on ``records''.
725When a direct file is opened in a Fortran program,
726the record length of the records must be given,
727and this is used by the Fortran I/O system to
728make the file look as if it is made up of records
729of the given length.
730In the special case that the record length is given
731as 1,
732the files are not considered to be divided into records,
733but are treated as byte-addressable byte strings;
734that is,
735as ordinary
736.UX
737file system files.
738(A read or write request on such a file keeps consuming bytes until
739satisfied, rather than being restricted to a single record.)
740.PP
741The peculiar requirements on sequential unformatted files
742make it unlikely that they will ever be read or written by any means except Fortran I/O statements.
743Each record is preceded and followed by
744an integer containing the record's length in bytes.
745.PP
746The Fortran I/O system breaks sequential formatted files
747into records while reading by using each newline
748as a record separator.
749The result of reading off the end of a record is undefined according to the Standard.
750The I/O system is permissive and
751treats the record as being extended by blanks.
752On output,
753the I/O system will write a newline at the end of each
754record.
755It is also possible for programs to write newlines
756for themselves.
757This is an error,
758but the only effect will be that the single record
759the user thought he wrote will be treated as
760more than one record when being read or
761backspaced over.
762.NH 2
763Portability Considerations
764.PP
765The Fortran I/O system uses only the facilities of the
766standard C I/O library,
767a widely available and fairly portable package,
768with the following two nonstandard features:
769The I/O system needs to know whether a file
770can be used for direct I/O,
771and whether or not it is possible to backspace.
772Both of these facilities are implemented
773using the
774.B fseek
775routine,
776so there is a routine
777.B canseek
778which determines if
779.B fseek
780will have the desired effect.
781Also, the
782.B inquire
783statement provides the user
784with the ability to find out if two files are the
785same,
786and to get the name of an already opened file
787in a form which would enable the program to reopen
788it.
789(The
790.UX
791operating system implementation attempts to determine the full pathname.)
792Therefore there are two routines which
793depend on facilities of the operating system
794to provide these two services.
795In any case,
796the I/O system
797runs on the PDP-11, VAX-11/780, and Interdata 8/32
798.UX
799systems.
800.NH 2
801Pre-Connected Files and File Positions
802.PP
803Units 5, 6, and 0 are preconnected when the program starts.
804Unit 5 is connected to the standard input,
805unit 6 is connected to the standard output,
806and unit 0 is connected to the standard error unit.
807All are connected for sequential formatted I/O.
808.PP
809All the other units are also preconnected when execution
810begins.
811Unit
812.I n
813is connected to a file named \fBfort.\fIn\fR.
814These files need not exist,
815nor will they be created unless their units are used
816without first executing an
817.B open.
818The default connection is for sequential formatted I/O.
819.PP
820The Standard does not specify where a file which has been explicitly \fBopen\fRed
821for sequential I/O is initially positioned.
822In fact,
823the I/O system attempts to position the file at the end,
824so a
825.B write
826will append to the file and a
827.B read
828will result in an end-of-file indication.
829To position a file to its beginning,
830use a
831.B rewind
832statement.
833The preconnected units
8340, 5, and 6 are positioned as they come
835from the program's parent process.
836.SG
837.SH
838REFERENCES
839.LP
840.IP 1.
841\fISigplan Notices \fB11\fR, No.3 (1976),
842as amended in X3J3 internal documents through ``/90.1''.
843.IP 2.
844\fIUSA Standard FORTRAN, USAS X3.9-1966\fR,
845New York: United States of America Standards Institute, March 7, 1966.
846Clarified in
847\fIComm. ACM \fB12,\fR 289 (1969)
848and
849\fIComm. ACM \fB14, \fR 628 (1971).
850.IP 3.
851B. W. Kernighan and D. M. Ritchie,
852.I
853The C Programming Language,
854.R
855Englewood Cliffs: Prentice-Hall (1978).
856.IP 4.
857D. M. Ritchie, private communication.
858.IP 5.
859S. C. Johnson,
860``A Portable Compiler: Theory and Practice'',
861Proc. 5th ACM Symp. on Principles of Programming Languages
862(January 1978).
863.IP 6.
864S. I. Feldman,
865``An Informal Description of EFL'',
866internal memorandum.
867.IP 7.
868B. W. Kernighan,
869``RATFOR \(em A Preprocessor for a Rational Fortran'',
870.I
871Bell Laboratories Computing Science Technical Report #55,
872.R
873(January 1977).
874.IP 8.
875D. M. Ritchie, private communication.
876.bp
877.SH
878APPENDIX. Differences Between Fortran 66 and Fortran 77
879.PP
880The following is a very brief description of the differences
881between the 1966 [2] and the 1977 [1] Standard languages.
882We assume that the reader is familiar with Fortran 66.
883We do not pretend to be complete, precise,
884or unbiased,
885but plan to describe what we feel are the most important aspects of the new language.
886At present the only current information on the 1977 Standard is in publications of the X3J3 Subcommittee
887of the
888American National Standards Institute.
889The following information is from the ``/92'' document.
890This draft Standard is written in English rather than a meta-language, but it is forbidding
891and legalistic.
892No tutorials or textbooks are available yet.
893.NH 0
894Features Deleted from Fortran 66
895.NH 2
896Hollerith
897.IP
898All notions of ``Hollerith''
899(\fIn\|\fBh\fR)
900as data
901have been officially removed, although our compiler, like almost all in the foreseeable future,
902will continue to support this archaism.
903.NH 2
904Extended Range
905.IP
906In Fortran 66, under a set of very restrictive and rarely-understood conditions, it is permissible
907to jump out of the range of a
908.B do
909loop, then jump back into it.
910Extended range has been removed in the Fortran 77 language.
911The restrictions are so special, and the implementation of extended range is so unreliable in many compilers,
912that this change really counts as no loss.
913.NH 1
914Program Form
915.NH 2
916Blank Lines
917.IP
918Completely blank lines are now legal comment lines.
919.NH 2
920Program and Block Data Statements
921.IP
922A main program may now begin with a statement that gives that program an external name:
923.DS
924program work
925.DE
926Block data procedures may also have names.
927.DS
928block data stuff
929.DE
930There is now a rule that only
931.I one
932unnamed
933block data procedure may appear in a program.
934(This rule is not enforced by our system.)
935The Standard does not specify the effect of the program and block data names,
936but they are clearly intended to aid conventional loaders.
937.NH 2
938ENTRY Statement
939.IP
940Multiple entry points are now legal.
941Subroutine and function subprograms may have additional entry points,
942declared by an
943.B entry
944statement with an optional argument list.
945.DS
946entry extra(a, b, c)
947.DE
948Execution begins at the first statement following the
949.B entry
950line.
951All variable declarations must precede all executable statements in the procedure.
952If the procedure begins with a
953.B subroutine
954statement,
955all entry points are subroutine names.
956If it begins with a
957.B function
958statement, each entry is a function entry point,
959with type determined by the type declared for the entry name.
960If any entry is a character-valued function,
961then all entries must be.
962In a function, an entry name of the same type as that where control entered
963must be assigned a value.
964Arguments do not retain their values between calls.
965(The ancient trick of calling one entry point with a large number of arguments
966to cause the procedure to ``remember'' the locations of those arguments,
967then invoking an entry with just a few arguments for later calculation,
968is still illegal.
969Furthermore, the trick doesn't work in our implementation,
970since arguments are not kept in static storage.)
971.NH 2
972DO Loops
973.IP
974.B do
975variables and range parameters may now be of integer, real, or double precision types.
976(The use of floating point
977.B do
978variables is very dangerous
979because of the possibility of unexpected roundoff,
980and we strongly recommend against their use).
981The action of the
982.B do
983statement is now defined for all values of the
984.B do
985parameters.
986The statement
987.DS
988do 10 i = l, u, d
989.DE
990performs
991$ max (0^,^ left floor ( u - l ) / d^ right floor )$
992iterations.
993The
994.B do
995variable has a predictable value when exiting a loop:
996the value at the time a
997.B goto
998or
999.B return
1000terminates the loop;
1001otherwise
1002the value that failed the limit test.
1003.NH 2
1004Alternate Returns
1005.IP
1006In a
1007.B subroutine
1008or subroutine
1009.B entry
1010statement,
1011some of the arguments may be noted by an asterisk, as in
1012.DS
1013subroutine s(a, \(**, b, \(**)
1014.DE
1015The meaning of the ``alternate returns'' is described in section 5.2 of the Appendix.
1016.NH 1
1017Declarations
1018.NH 2
1019CHARACTER Data Type
1020.IP
1021One of the biggest improvements to the language is the addition of a character-string data type.
1022Local and
1023common character variables must have a length denoted by a constant expression:
1024.DS
1025character\(**17 a, b(3,4)
1026character\(**(6+3) c
1027.DE
1028If the length is omitted entirely, it is assumed equal to 1.
1029A character string argument may have a constant length,
1030or the length may be declared to be the same as that of the corresponding actual argument at run time
1031by a statement like
1032.DS
1033character\(**(\(**) a
1034.DE
1035(There is an intrinsic function
1036.B len
1037that returns the actual length of a character string).
1038Character arrays and common blocks containing character variables must be packed:
1039in an array of character variables, the first character of one element must follow the last character of
1040the preceding element, without holes.
1041.NH 2
1042IMPLICIT Statement
1043.IP
1044The traditional implied declaration rules still hold:
1045a variable whose name begins with
1046\fBi, j, k, l, m,\fR or \fBn\fR is of type
1047.B integer,
1048other variables are of type
1049.B real,
1050unless otherwise declared.
1051This general rule may be overridden with an
1052.B implicit
1053statement:
1054.DS
1055implicit real(a-c,g), complex(w-z), character\(**(17) (s)
1056.DE
1057declares that variables whose name begins with an
1058\fBa ,b, c,\fR
1059or
1060\fBg\fR
1061are
1062.B real,
1063those beginning with
1064\fBw, x, y,\fR
1065or
1066\fBz\fR
1067are assumed
1068.B complex,
1069and so on.
1070It is still poor practice to depend on implicit typing, but this statement is an industry standard.
1071.NH 2
1072PARAMETER Statement
1073.IP
1074It is now possible to give a constant a symbolic name, as in
1075.DS
1076parameter (x=17, y=x/3, pi=3.14159d0, s=\(fmhello\(fm)
1077.DE
1078The type of each parameter name is governed by the same implicit and explicit rules as for a variable.
1079The right side of each equal sign must be a constant expression
1080(an expression made up of constants, operators, and already defined parameters).
1081.NH 2
1082Array Declarations
1083.IP
1084Arrays may now have as many as seven dimensions.
1085(Only three were permitted in 1966).
1086The lower bound of each dimension may be declared
1087to be other than 1 by
1088using a colon.
1089Furthermore, an adjustable array bound may be an integer expression involving constants,
1090arguments, and variables in
1091.B common.
1092.DS
1093real a(\(mi5:3, 7, m:n), b(n+1:2\(**n)
1094.DE
1095The upper bound on the last dimension of an array argument may be denoted by an asterisk
1096to indicate that the upper bound is not specified:
1097.DS
1098integer a(5, \(**), b(\(**), c(0:1, \(mi2:\(**)
1099.DE
1100.NH 2
1101SAVE Statement
1102.IP
1103A poorly known rule of Fortran 66 is that local variables in a procedure do not necessarily retain their values between
1104invocations of that procedure.
1105At any instant in the execution of a program,
1106if a common block is declared neither in the currently executing procedure
1107nor in any of the procedures in the chain of callers,
1108all of the variables in that common block also become undefined.
1109(The only exceptions are variables that have been defined in a
1110.B data
1111statement and never changed).
1112These rules permit overlay and stack implementations for the affected variables.
1113Fortran 77 permits one to specify that certain variables and common blocks are to retain their
1114values between invocations.
1115The declaration
1116.DS
1117save a, /b/, c
1118.DE
1119leaves the values of the variables
1120.B a
1121and
1122.B c
1123and all of the contents of common block
1124.B b
1125unaffected by a return.
1126The simple declaration
1127.DS
1128save
1129.DE
1130has this effect on all variables and common blocks in the procedure.
1131A common block must be \fBsave\fRd in every procedure in which it is declared if the desired effect is to occur.
1132.NH 2
1133INTRINSIC Statement
1134.IP
1135All of the functions specified in the Standard are in a single category,
1136``intrinsic functions'', rather than being divided into ``intrinsic'' and ``basic external'' functions.
1137If an intrinsic function is to be passed to another procedure, it must be declared
1138.B intrinsic.
1139Declaring it
1140.B external
1141(as in Fortran 66) causes a function other than the built-in one to be passed.
1142.NH 1
1143Expressions
1144.NH 2
1145Character Constants
1146.IP
1147Character string constants are marked by strings surrounded by apostrophes.
1148If an apostrophe is to be included in a constant, it is repeated:
1149.DS
1150 \(fmabc\(fm
1151 \(fmain\(fm\(fmt\(fm
1152.DE
1153There are no null (zero-length) character strings in Fortran 77.
1154Our compiler has two different quotation marks, `` \(fm ''' and `` " ''.
1155(See Section 2.9 in the main text.)
1156.NH 2
1157Concatenation
1158.IP
1159One new operator has been added, character string concatenation, marked by a double slash
1160(``//'').
1161The result of a concatenation is the string containing the characters of the left operand followed by the characters of
1162the right operand.
1163The strings
1164.DS
1165 \(fmab\(fm // \(fmcd\(fm
1166 \(fmabcd\(fm
1167.DE
1168are equal.
1169The strings being concatenated must be of constant length in all concatenations
1170that are not the right sides of assignments.
1171(The only concatenation expressions in which a
1172character string declared adjustable with a ``\(**(\(**)'' modifier
1173or a substring denotation with nonconstant position values may appear
1174are the right sides of assignments).
1175.NH 2
1176Character String Assignment
1177.IP
1178The left and right sides of a character assignment may not share storage.
1179(The assumed implementation of character assignment is to copy characters from the right to the left side.)
1180If the left side is longer than the right, it is padded with blanks.
1181If the left side is shorter than the right, trailing characters are discarded.
1182.NH 2
1183Substrings
1184.IP
1185It is possible to extract a substring of a character variable or character array element, using the colon notation:
1186.DS
1187a(i,\|j) (m:n)
1188.DE
1189is the string of $(n-m+1)$ characters beginning at the
1190$m sup th$ character of the character array element $a sub ij$.
1191Results are undefined unless $m<=n$.
1192Substrings may be used on the left sides of assignments and as procedure actual arguments.
1193.NH 2
1194Exponentiation
1195.IP
1196It is now permissible to raise real quantities to complex powers,
1197or complex quantities to real or complex powers.
1198(The principal part of the logarithm is used).
1199Also, multiple exponentiation is now defined:
1200.DS
1201a\(**\(**b\(**\(**c = a \(**\(** (b\(**\(**c)
1202.DE
1203.NH 2
1204Relaxation of Restrictions
1205.IP
1206Mixed mode expressions are now permitted.
1207(For instance,
1208it is permissible to combine integer and complex quantities in an expression.)
1209.IP
1210Constant expressions are permitted where a constant is allowed,
1211except in
1212.B data
1213statements.
1214(A constant expression is made up of explicit constants and
1215\fBparameter\fRs
1216and the Fortran operators,
1217except for exponentiation to a floating-point power).
1218An adjustable dimension may now be an integer expression involving constants,
1219arguments, and variables in
1220B common..
1221.IP
1222Subscripts may now be general integer expressions;
1223the old
1224$c v +- c'$
1225rules have been removed.
1226.B do
1227loop bounds may be general integer, real, or double precision expressions.
1228Computed
1229.B goto
1230expressions and I/O unit numbers may be general integer expressions.
1231.NH 1
1232Executable Statements
1233.NH 2
1234IF-THEN-ELSE
1235.IP
1236At last, the
1237if-then-else
1238branching structure has been added to Fortran.
1239It is called a ``Block If''.
1240A Block If begins with a statement of the form
1241.DS
1242if ( . . . ) then
1243.DE
1244and ends with an
1245.DS
1246end if
1247.DE
1248statement.
1249Two other new statements may appear in a Block If.
1250There may be several
1251.DS
1252else if(. . .) then
1253.DE
1254statements,
1255followed by at most one
1256.DS
1257else
1258.DE
1259statement.
1260If the logical expression in the Block If statement is true, the statements following it up to the
1261next
1262.B elseif,
1263.B else,
1264or
1265.B endif
1266are executed.
1267Otherwise, the next
1268.B elseif
1269statement in the group is executed.
1270If none of the
1271.B elseif
1272conditions are true, control passes to the statements following the
1273.B else
1274statement, if any.
1275(The
1276.B else
1277must follow all \fBelseif\fRs in a Block If.
1278Of course, there may be Block Ifs embedded inside of other Block If structures).
1279A
1280case
1281construct may be rendered
1282.DS
1283if (s .eq. \(fmab\(fm) then
1284 . . .
1285else if (s .eq. \(fmcd\(fm) then
1286 . . .
1287else
1288 . . .
1289end if
1290.DE
1291.NH 2
1292Alternate Returns
1293.IP
1294Some of the arguments of a subroutine call may be statement labels preceded by an asterisk, as in
1295.DS
1296call joe(j, \(**10, m, \(**2)
1297.DE
1298A
1299.B return
1300statement may have an integer expression, such as
1301.DS
1302return k
1303.DE
1304If the entry point has
1305$n$
1306alternate return (asterisk) arguments
1307and if $1<=k<=n$, the return is followed by a branch to the corresponding statement label;
1308otherwise the usual return to the statement following the
1309.B call
1310is executed.
1311.NH 1
1312Input/Output
1313.NH 2
1314Format Variables
1315.IP
1316A format may be the value of a character expression (constant or otherwise),
1317or be stored in a character array, as in
1318.DS
1319write(6, \(fm(i5)\(fm) x
1320.DE
1321.NH 2
1322END=, ERR=, and IOSTAT= Clauses
1323.IP
1324A
1325.B read
1326or
1327.B write
1328statement may contain
1329.B end=,
1330.B err=,
1331and
1332.B iostat=
1333clauses, as in
1334.DS
1335write(6, 101, err=20, iostat=a(4))
1336read(5, 101, err=20, end=30, iostat=x)
1337.DE
1338Here 5 and 6 are the
1339.I units
1340on which the I/O is done,
1341101 is the statement number of the associated format,
134220 and 30 are statement numbers,
1343and
1344.B a
1345and
1346.B x
1347are integers.
1348If an error occurs during I/O,
1349control returns to the program at statement 20.
1350If the end of the file is reached,
1351control returns to the program at statement 30.
1352In any case, the variable referred to in
1353the
1354.B iostat=
1355clause is given a value when
1356the I/O statement finishes.
1357(Yes, the value is assigned to the name on the right side of the equal sign.)
1358This value is zero if all went well,
1359negative for end of file,
1360and some positive value for errors.
1361.NH 2
1362Formatted I/O
1363.NH 3
1364Character Constants
1365.IP
1366Character constants in formats are copied literally to the output.
1367Character constants cannot be read into.
1368.DS
1369write(6,\(fm(i2,\(fm\(fm isn\(fm\(fm\(fm\(fmt \(fm\(fm,i1)\(fm) 7, 4
1370.DE
1371produces
1372.DS
1373 7 isn\(fmt 4
1374.DE
1375Here the format is the character constant
1376.DS
1377(i2,\(fm isn\(fm\(fmt \(fm,i1)
1378.DE
1379and the character constant
1380.DS
1381 isn\(fmt
1382.DE
1383is copied into the output.
1384.NH 3
1385Positional Editing Codes
1386.IP
1387.B t,
1388.B tl,
1389.B tr,
1390and
1391.B x
1392codes
1393control where the
1394next character is in the record.
1395\fBtr\fIn\fR
1396or
1397\fIn\fBx\fR
1398specifies that the next character is
1399$n$ to the right of the current position.
1400\fBtl\fIn\fR
1401specifies that the next character is
1402$n$ to the left of the current position,
1403allowing parts of the record to be reconsidered.
1404\fBt\fIn\fR
1405says that the next character is to be character
1406number $n$ in the record.
1407(See section 3.4 in the main text.)
1408.NH 3
1409Colon
1410.IP
1411A colon in the format terminates the I/O operation
1412if there are no more data items in the I/O list,
1413otherwise it has no effect.
1414In the fragment
1415.DS
1416x=\(fm("hello", :, " there", i4)\(fm
1417write(6, x) 12
1418write(6, x)
1419.DE
1420the first
1421.B write
1422statement prints
1423\fBhello there 12\fR,
1424while the second only prints
1425\fBhello\fR.
1426.NH 3
1427Optional Plus Signs
1428.IP
1429According to the Standard,
1430each implementation has the option of putting
1431plus signs in front of non-negative
1432numeric output.
1433The
1434.B sp
1435format code may be used to make the optional plus
1436signs actually appear for all subsequent items
1437while the format is active.
1438The
1439.B ss
1440format code guarantees that the I/O system will not
1441insert the optional plus signs,
1442and the
1443.B s
1444format code restores the default behavior of
1445the I/O system.
1446(Since we never put out optional plus signs,
1447.B ss
1448and
1449.B s
1450codes have the same effect in our implementation.)
1451.NH 3
1452Blanks on Input
1453.IP
1454Blanks in numeric input fields,
1455other than leading blanks
1456will be ignored following a
1457.B bn
1458code in a format
1459statement,
1460and will be treated as zeros following a
1461.B bz
1462code in a format statement.
1463The default for a unit may be changed by using
1464the
1465.B open
1466statement.
1467(Blanks are ignored by default.)
1468.NH 3
1469Unrepresentable Values
1470.IP
1471The Standard requires that if a numeric item
1472cannot be represented in the form required by a format code,
1473the output field must be filled with asterisks.
1474(We think this should have been an option.)
1475.NH 3
1476Iw.m
1477.IP
1478There is a new integer output code,
1479\fBi\fIw.m.\fR
1480It is the same as
1481\fBi\fIw\fR,
1482except that there will be at least $m$
1483digits in the output field,
1484including,
1485if necessary,
1486leading zeros.
1487The case \fBi\fR$w.0$ is special,
1488in that if the value being printed is 0,
1489the output field is
1490entirely blank.
1491\fBi\fIw\fB.1\fR
1492is the same as
1493\fBi\fIw\fR.
1494.NH 3
1495Floating Point
1496.IP
1497On input, exponents may start with the letter
1498\fBE, D, e, \fRor \fBd.\fR
1499All have the same meaning.
1500On output we always use \fBe\fR.
1501The
1502.B e
1503and
1504.B d
1505format codes also have identical meanings.
1506A leading zero before the decimal point in
1507.B e
1508output
1509without a scale factor is optional with the
1510implementation.
1511(We do not print it.)
1512There is a
1513\fBg\fIw.d\fR
1514format code which is the same as
1515\fBe\fIw.d\fR
1516and
1517\fBf\fIw.d\fR
1518on input,
1519but which chooses
1520.B f
1521or
1522.B e
1523formats for output depending.
1524on the size of the number and of $d$.
1525.NH 3
1526``A'' Format Code
1527.IP
1528A codes are used for character values.
1529\fBa\fIw\fR
1530use a field width of $w$,
1531while a plain
1532.B a
1533uses the length of the character item.
1534.NH 2
1535Standard Units
1536.IP
1537There are default formatted input and output units.
1538The statement
1539.DS
1540read 10, a, b
1541.DE
1542reads from the standard unit using format statement 10.
1543The default unit may be explicitly specified by an asterisk, as in
1544.DS
1545read(\(**, 10) a,b
1546.DE
1547Similarly, the standard output units is specified by a
1548.B print
1549statement or an asterisk unit:
1550.DS
1551print 10
1552write(\(**, 10)
1553.DE
1554.NH 2
1555List-Directed Formatting
1556.IP
1557List-directed I/O is a
1558kind of free form input for sequential I/O.
1559It is invoked by using an asterisk as the
1560format identifier, as in
1561.DS
1562read(6, \(**) a,b,c
1563.DE
1564.IP
1565On input,
1566values are separated by strings of blanks
1567and possibly a comma.
1568Values,
1569except for character strings,
1570cannot contain blanks.
1571End of record counts as a blank,
1572except in character strings,
1573where it is ignored.
1574Complex constants are given as two real constants
1575separated by a comma and enclosed in parentheses.
1576A null input field,
1577such as between two consecutive commas,
1578means the corresponding variable in the
1579I/O list is not changed.
1580Values may be preceded by repetition counts,
1581as in
1582.DS
15834\(**(3.,2.) 2\(**, 4\(**\(fmhello\(fm
1584.DE
1585which stands for 4 complex constants, 2 null values,
1586and 4 string constants.
1587.IP
1588For output, suitable formats are chosen for
1589each item.
1590The values of character strings are printed;
1591they are not enclosed in quotes, so they cannot be read back
1592using list-directed input.
1593.NH 2
1594Direct I/O
1595.IP
1596A file connected for direct access consists of
1597a set of equal-sized records each of which is
1598uniquely identified by a positive integer.
1599The records may be written or read in any order,
1600using direct access I/O statements.
1601.IP
1602Direct access
1603.B read
1604and
1605.B write
1606statements
1607have an extra argument,
1608.B rec=,
1609which gives the record number to be read or written.
1610.DS
1611read(2, rec=13, err=20) (a(i), i=1, 203)
1612.DE
1613reads the thirteenth record into the array
1614.B a.
1615.IP
1616The size of the records must be given by an
1617.B open
1618statement
1619(see below).
1620Direct access files may be connected for either formatted
1621or unformatted I/O.
1622.NH 2
1623Internal Files
1624.IP
1625Internal files are character string objects,
1626such as variables or substrings,
1627or arrays of type character.
1628In the former cases there is only a single record
1629in the file,
1630in the latter case each array element is a record.
1631The Standard includes only sequential
1632formatted I/O on internal files.
1633(I/O is not a very precise term to use here,
1634but internal files are dealt with using
1635.B read
1636and
1637.B write).
1638There is no list-directed I/O on internal files.
1639Internal files are used by giving the name of the
1640character object in place of the unit number, as in
1641.DS
1642character\(**80 x
1643read(5,"(a)") x
1644read(x,"(i3,i4)") n1,n2
1645.DE
1646which reads a card image into
1647.B x
1648and then reads
1649two integers from the front of it.
1650A sequential
1651.B read
1652or
1653.B write
1654always starts at the beginning
1655of an internal file.
1656.IP
1657(We also support a compatible extension, direct I/O on internal files.
1658This is like direct I/O on external files,
1659except that the number of records in the file cannot be
1660changed.)
1661.NH 2
1662OPEN, CLOSE, and INQUIRE Statements
1663.IP
1664These statements are used to connect and disconnect
1665units and files,
1666and to gather information about units and files.
1667.NH 3
1668OPEN
1669.IP
1670The
1671.B open
1672statement is used to connect a file with a
1673unit,
1674or to alter some properties of the connection.
1675The following is a minimal example.
1676.DS
1677open(1, file=\(fmfort.junk\(fm)
1678.DE
1679.B open
1680takes a variety of arguments with meanings described below.
1681.EQ
1682delim off
1683.EN
1684.RS
1685. \" macros here
1686.de HP
1687.RT
1688.if !\\(IP .nr IP +1
1689.sp \\n(PDu
1690.ne 3v
1691.in +\\n(PIu
1692.ti -\\n(PIu
1693\fB\\$1\fR\ \c
1694..
1695.de P1
1696.KS
1697.nf
1698.in +.3i
1699.ta .3i .6i .9i 1.2i 1.5i 1.8i
1700.sp
1701..
1702.de P2
1703.fi
1704.in -.3i
1705.sp
1706.KE
1707..
1708.de TH
1709.RT
1710.sp \\n(PDu
1711.ne 3v
1712\fB\\$1\\$2\\$3\\$4\\$5\\$6\fR\ \c
1713..
1714. \" end of macros
1715.HP unit=
1716a small non-negative integer which is the unit to
1717which the file is to be connected.
1718We allow,
1719at the time of this writing,
17200 through 9.
1721If this parameter is the first one in the
1722.B open
1723statement,
1724the
1725.B unit=
1726can be omitted.
1727.HP iostat=
1728is the same as in
1729.B read
1730or
1731.B write.
1732.HP err=
1733is the same as in
1734.B read
1735or
1736.B write.
1737.HP file=
1738a character expression,
1739which when stripped of trailing blanks,
1740is the name of the file to be connected to the unit.
1741The filename should not be given if the
1742.B status=scratch.
1743.HP status=
1744one of
1745.B old,
1746.B new,
1747.B scratch,
1748or
1749.B unknown.
1750If this parameter is not given,
1751.B unknown
1752is assumed.
1753If
1754.B scratch
1755is given,
1756a temporary file will be created.
1757Temporary files are destroyed at the end of execution.
1758If
1759.B new
1760is given,
1761the file will be created if it doesn't exist,
1762or truncated if it does.
1763The meaning of
1764.B unknown
1765is processor dependent;
1766our system treats it as synonymous with
1767.B old.
1768.HP access=
1769.B sequential
1770or
1771.B direct,
1772depending on whether the file is
1773to be opened for sequential or direct I/O.
1774.HP form=
1775.B formatted
1776or
1777.B unformatted.
1778.HP recl=
1779a positive integer specifying the record length of
1780the direct access file being opened.
1781We measure all record lengths in bytes.
1782On
1783.UX
1784systems a record length of 1 has the special meaning explained
1785in section 5.1 of the text.
1786.HP blank=
1787.B null
1788or
1789.B zero.
1790This parameter has meaning only for formatted I/O.
1791The default value is
1792.B null.
1793.B zero
1794means that blanks,
1795other than leading blanks,
1796in numeric input fields are to be treated as zeros.
1797.RE
1798.IP
1799Opening a new file on a unit which is already connected
1800has the effect of first closing the old file.
1801.NH 3
1802CLOSE
1803.IP
1804.B close
1805severs the connection between a unit and a file.
1806The unit number must be given.
1807The optional parameters are
1808.B iostat=
1809and
1810.B err=
1811with
1812their usual meanings,
1813and
1814.B status=
1815either
1816.B keep
1817or
1818.B delete.
1819Scratch files cannot be kept,
1820otherwise
1821.B keep
1822is the default.
1823.B delete
1824means the file will be removed.
1825A simple example is
1826.DS
1827close(3, err=17)
1828.DE
1829.NH 3
1830INQUIRE
1831.IP
1832The
1833.B inquire
1834statement gives information about
1835a unit
1836(``inquire by unit'')
1837or a file (``inquire by file'').
1838Simple examples are:
1839.DS
1840inquire(unit=3, namexx)
1841inquire(file=\(fmjunk\(fm, number=n, exist=l)
1842.DE
1843.RS
1844.HP file=
1845a character variable specifies the file the
1846.B inquire
1847is about.
1848Trailing blanks in the file name are ignored.
1849.HP unit=
1850an integer variable specifies the unit the
1851.B inquire
1852is about.
1853Exactly one of
1854.B file=
1855or
1856.B unit=
1857must be used.
1858.HP "iostat=, err="
1859are as before.
1860.HP exist=
1861a logical variable.
1862The logical variable is set to
1863.B ".true."
1864if the file or unit
1865exists and is set to
1866.B ".false."
1867otherwise.
1868.HP opened=
1869a logical variable.
1870The logical variable is set to
1871.B ".true."
1872if the file
1873is connected to a unit or if the unit is connected
1874to a file,
1875and it is set to
1876.B ".false."
1877otherwise.
1878.HP number=
1879an integer variable to which is assigned the
1880number of the unit connected to the file,
1881if any.
1882.HP named=
1883a logical variable to which is assigned
1884.B ".true."
1885if
1886the file has a name,
1887or
1888.B ".false."
1889otherwise.
1890.HP name=
1891a character variable to which is assigned the name
1892of the file (inquire by file) or the name of the
1893file connected to the unit (inquire by unit).
1894The name will be the full name of the file.
1895.HP access=
1896a character variable to which will be assigned
1897the value
1898.B \(fmsequential\(fm
1899if the connection is for
1900sequential I/O,
1901.B \(fmdirect\(fm
1902if the connection is for direct I/O.
1903The value becomes undefined if there is no connection.
1904.HP sequential=
1905a character variable to which is assigned the
1906value
1907.B \(fmyes\(fm
1908if the file could be connected for
1909sequential I/O,
1910.B \(fmno\(fm
1911if the file could not be connected for sequential I/O,
1912and
1913.B \(fmunknown\(fm
1914if we can't tell.
1915.HP direct=
1916a character variable to which is assigned the value
1917.B \(fmyes\(fm
1918if the file could be connected for direct I/O,
1919.B\(fmno\(fm
1920if the file could not be connected for direct
1921I/O,
1922and
1923.B \(fmunknown\(fm
1924if we can't tell.
1925.HP form=
1926a character variable to which is assigned the value
1927.B \(fmformatted\(fm
1928if the file is connected for formatted I/O,
1929or
1930.B \(fmunformatted\(fm
1931if the file is connected for unformatted
1932I/O.
1933.HP formatted=
1934a character variable to which is assigned the value
1935.B \(fmyes\(fm
1936if the file could be connected for formatted I/O,
1937.B \(fmno\(fm
1938if the file could not be connected for formatted I/O,
1939and
1940.B \(fmunknown\(fm
1941if we can't tell.
1942.HP unformatted=
1943a character variable to which is assigned the value
1944.B \(fmyes\(fm
1945if
1946the file could be connected for unformatted I/O,
1947.B \(fmno\(fm
1948if the file could not be connected for unformatted I/O,
1949and
1950.B \(fmunknown\(fm
1951if we can't tell.
1952.HP recl=
1953an integer variable to which is assigned the record length
1954of the records in the file if the file is connected
1955for direct access.
1956.HP nextrec=
1957an integer variable to which is assigned one more
1958than the number of the the last record read from a file connected
1959for direct access.
1960.HP blank=
1961a character variable to which is assigned the value
1962.B \(fmnull\(fm
1963if null blank control is in effect for the file
1964connected for formatted I/O,
1965.B \(fmzero\(fm
1966if blanks are being converted to zeros and
1967the file is connected for formatted I/O.
1968.RE
1969.PP
1970.I "The gentle reader"
1971will remember that the people who wrote the standard
1972probably weren't thinking of his needs.
1973Here is an example.
1974The declarations are omitted.
1975.DS
1976open(1, file="/dev/console")
1977.DE
1978On a
1979.UX
1980system this statement opens the console for formatted sequential
1981I/O.
1982An
1983.B inquire
1984statement for either unit 1 or file "/dev/console"
1985would reveal that the file exists, is connected to unit 1,
1986has a name, namely "/dev/console",
1987is opened for sequential I/O,
1988could be connected for sequential I/O,
1989could not be connected for direct I/O (can't seek),
1990is connected for formatted I/O,
1991could be connected for formatted I/O,
1992could not be connected for unformatted I/O
1993(can't seek),
1994has neither a record length nor a next record number,
1995and is ignoring blanks in numeric fields.
1996.PP
1997In the
1998.UX
1999system environment,
2000the only way to discover what permissions you have
2001for a file is to open it and try to read and write it.
2002The
2003.B err=
2004parameter will return system error numbers.
2005The
2006.B inquire
2007statement does not give a way of determining permissions.