home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / doc / f77 < prev    next >
Encoding:
Text File  |  1979-01-10  |  51.1 KB  |  2,005 lines

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