Gas vs irregular files

Ian Lance Taylor ian@wasabisystems.com
Wed Dec 17 09:39:00 GMT 2003


Nick Clifton <nickc@redhat.com> writes:

> > The ChangeLog entry states it's for detecting directories -- if so,
> > it should do that explicitly with S_ISDIR().
> 
> Well yes and no.  The point is that it is not sensible to attempt to
> assemble anything other than a regular text file.  Hence the code uses
> S_ISREG() to check for files for which there is no point in even
> attempting to call open() or read().

But why bother?  Why not simply trust the user?

I think a program like gas should just read the specified file.  If
that fails, it fails.  There is no reason to check in advance whether
or not it will fail.  What purpose does that serve, except to slow
down the assembler in 99.999% of all runs?

I don't even like the call to stat().  To me that serves no purpose.
If you just want to fix the error message to say "No such file", don't
waste time by checking for the highly unusual case beforehand:
instead, check after a failure.  Instead of this:

      struct stat statbuf;

      if (stat (filename, &statbuf) < 0)
	{
	  as_bad (_("%s: No such file"), filename);
	  return;
	}
      else if (! S_ISREG (statbuf.st_mode))
	{
	  as_bad (_("'%s' is not an ordinary file"), filename);
	  return;
	}

      f_in = fopen (filename, FOPEN_RT);

  ...

  if (f_in == (FILE *) 0)
    {
      as_bad (_("can't open %s for reading"), file_name);
      as_perror ("%s", file_name);
      return;
    }

do this:

      f_in = fopen (filename, FOPEN_RT);

  ...

  if (f_in == NULL)
    {
      if (errno == ENOENT)
        as_bad (_("%s: No such file"), filename);
      else
        {
          as_bad (_("can't open %s for reading"), file_name);
          as_perror ("%s", file_name);
        }
      return;
    }

Ian



More information about the Binutils mailing list