open.c
来自「gcc-fortran,linux使用fortran的编译软件。很好用的。」· C语言 代码 · 共 624 行 · 第 1/2 页
C
624 行
/* Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. Contributed by Andy VaughtThis file is part of the GNU Fortran 95 runtime library (libgfortran).Libgfortran is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.In addition to the permissions in the GNU General Public License, theFree Software Foundation gives you unlimited permission to link thecompiled version of this file into combinations with other programs,and to distribute those combinations without any restriction comingfrom the use of this file. (The General Public License restrictionsdo apply in other respects; for example, they cover modification ofthe file, and distribution when not linked into a combineexecutable.)Libgfortran is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with Libgfortran; see the file COPYING. If not, write tothe Free Software Foundation, 51 Franklin Street, Fifth Floor,Boston, MA 02110-1301, USA. */#include "config.h"#include <unistd.h>#include <stdio.h>#include <string.h>#include "libgfortran.h"#include "io.h"static const st_option access_opt[] = { {"sequential", ACCESS_SEQUENTIAL}, {"direct", ACCESS_DIRECT}, {"append", ACCESS_APPEND}, {NULL, 0}};static const st_option action_opt[] ={ { "read", ACTION_READ}, { "write", ACTION_WRITE}, { "readwrite", ACTION_READWRITE}, { NULL, 0}};static const st_option blank_opt[] ={ { "null", BLANK_NULL}, { "zero", BLANK_ZERO}, { NULL, 0}};static const st_option delim_opt[] ={ { "none", DELIM_NONE}, { "apostrophe", DELIM_APOSTROPHE}, { "quote", DELIM_QUOTE}, { NULL, 0}};static const st_option form_opt[] ={ { "formatted", FORM_FORMATTED}, { "unformatted", FORM_UNFORMATTED}, { NULL, 0}};static const st_option position_opt[] ={ { "asis", POSITION_ASIS}, { "rewind", POSITION_REWIND}, { "append", POSITION_APPEND}, { NULL, 0}};static const st_option status_opt[] ={ { "unknown", STATUS_UNKNOWN}, { "old", STATUS_OLD}, { "new", STATUS_NEW}, { "replace", STATUS_REPLACE}, { "scratch", STATUS_SCRATCH}, { NULL, 0}};static const st_option pad_opt[] ={ { "yes", PAD_YES}, { "no", PAD_NO}, { NULL, 0}};static const st_option convert_opt[] ={ { "native", CONVERT_NATIVE}, { "swap", CONVERT_SWAP}, { "big_endian", CONVERT_BIG}, { "little_endian", CONVERT_LITTLE}, { NULL, 0}};/* Given a unit, test to see if the file is positioned at the terminal point, and if so, change state from NO_ENDFILE flag to AT_ENDFILE. This prevents us from changing the state from AFTER_ENDFILE to AT_ENDFILE. */voidtest_endfile (gfc_unit * u){ if (u->endfile == NO_ENDFILE && file_length (u->s) == file_position (u->s)) u->endfile = AT_ENDFILE;}/* Change the modes of a file, those that are allowed * to be changed. */static voidedit_modes (st_parameter_open *opp, gfc_unit * u, unit_flags * flags){ /* Complain about attempts to change the unchangeable. */ if (flags->status != STATUS_UNSPECIFIED && u->flags.status != flags->status) generate_error (&opp->common, ERROR_BAD_OPTION, "Cannot change STATUS parameter in OPEN statement"); if (flags->access != ACCESS_UNSPECIFIED && u->flags.access != flags->access) generate_error (&opp->common, ERROR_BAD_OPTION, "Cannot change ACCESS parameter in OPEN statement"); if (flags->form != FORM_UNSPECIFIED && u->flags.form != flags->form) generate_error (&opp->common, ERROR_BAD_OPTION, "Cannot change FORM parameter in OPEN statement"); if ((opp->common.flags & IOPARM_OPEN_HAS_RECL_IN) && opp->recl_in != u->recl) generate_error (&opp->common, ERROR_BAD_OPTION, "Cannot change RECL parameter in OPEN statement"); if (flags->action != ACTION_UNSPECIFIED && u->flags.access != flags->access) generate_error (&opp->common, ERROR_BAD_OPTION, "Cannot change ACTION parameter in OPEN statement"); /* Status must be OLD if present. */ if (flags->status != STATUS_UNSPECIFIED && flags->status != STATUS_OLD && flags->status != STATUS_UNKNOWN) generate_error (&opp->common, ERROR_BAD_OPTION, "OPEN statement must have a STATUS of OLD or UNKNOWN"); if (u->flags.form == FORM_UNFORMATTED) { if (flags->delim != DELIM_UNSPECIFIED) generate_error (&opp->common, ERROR_OPTION_CONFLICT, "DELIM parameter conflicts with UNFORMATTED form in " "OPEN statement"); if (flags->blank != BLANK_UNSPECIFIED) generate_error (&opp->common, ERROR_OPTION_CONFLICT, "BLANK parameter conflicts with UNFORMATTED form in " "OPEN statement"); if (flags->pad != PAD_UNSPECIFIED) generate_error (&opp->common, ERROR_OPTION_CONFLICT, "PAD paramter conflicts with UNFORMATTED form in " "OPEN statement"); } if ((opp->common.flags & IOPARM_LIBRETURN_MASK) == IOPARM_LIBRETURN_OK) { /* Change the changeable: */ if (flags->blank != BLANK_UNSPECIFIED) u->flags.blank = flags->blank; if (flags->delim != DELIM_UNSPECIFIED) u->flags.delim = flags->delim; if (flags->pad != PAD_UNSPECIFIED) u->flags.pad = flags->pad; } /* Reposition the file if necessary. */ switch (flags->position) { case POSITION_UNSPECIFIED: case POSITION_ASIS: break; case POSITION_REWIND: if (sseek (u->s, 0) == FAILURE) goto seek_error; u->current_record = 0; u->last_record = 0; test_endfile (u); /* We might be at the end. */ break; case POSITION_APPEND: if (sseek (u->s, file_length (u->s)) == FAILURE) goto seek_error; u->current_record = 0; u->endfile = AT_ENDFILE; /* We are at the end. */ break; seek_error: generate_error (&opp->common, ERROR_OS, NULL); break; } unlock_unit (u);}/* Open an unused unit. */gfc_unit *new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags){ gfc_unit *u2; stream *s; char tmpname[5 /* fort. */ + 10 /* digits of unit number */ + 1 /* 0 */]; /* Change unspecifieds to defaults. Leave (flags->action == ACTION_UNSPECIFIED) alone so open_external() can set it based on what type of open actually works. */ if (flags->access == ACCESS_UNSPECIFIED) flags->access = ACCESS_SEQUENTIAL; if (flags->form == FORM_UNSPECIFIED) flags->form = (flags->access == ACCESS_SEQUENTIAL) ? FORM_FORMATTED : FORM_UNFORMATTED; if (flags->delim == DELIM_UNSPECIFIED) flags->delim = DELIM_NONE; else { if (flags->form == FORM_UNFORMATTED) { generate_error (&opp->common, ERROR_OPTION_CONFLICT, "DELIM parameter conflicts with UNFORMATTED form in " "OPEN statement"); goto fail; } } if (flags->blank == BLANK_UNSPECIFIED) flags->blank = BLANK_NULL; else { if (flags->form == FORM_UNFORMATTED) { generate_error (&opp->common, ERROR_OPTION_CONFLICT, "BLANK parameter conflicts with UNFORMATTED form in " "OPEN statement"); goto fail; } } if (flags->pad == PAD_UNSPECIFIED) flags->pad = PAD_YES; else { if (flags->form == FORM_UNFORMATTED) { generate_error (&opp->common, ERROR_OPTION_CONFLICT, "PAD paramter conflicts with UNFORMATTED form in " "OPEN statement"); goto fail; } } if (flags->position != POSITION_ASIS && flags->access == ACCESS_DIRECT) { generate_error (&opp->common, ERROR_OPTION_CONFLICT, "ACCESS parameter conflicts with SEQUENTIAL access in " "OPEN statement"); goto fail; } else if (flags->position == POSITION_UNSPECIFIED) flags->position = POSITION_ASIS; if (flags->status == STATUS_UNSPECIFIED) flags->status = STATUS_UNKNOWN; /* Checks. */ if (flags->access == ACCESS_DIRECT && (opp->common.flags & IOPARM_OPEN_HAS_RECL_IN) == 0) { generate_error (&opp->common, ERROR_MISSING_OPTION, "Missing RECL parameter in OPEN statement"); goto fail; } if ((opp->common.flags & IOPARM_OPEN_HAS_RECL_IN) && opp->recl_in <= 0) { generate_error (&opp->common, ERROR_BAD_OPTION, "RECL parameter is non-positive in OPEN statement");
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?