Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fcntl and fcntl() woes #2203

Closed
p5pRT opened this issue Jul 13, 2000 · 6 comments
Closed

Fcntl and fcntl() woes #2203

p5pRT opened this issue Jul 13, 2000 · 6 comments

Comments

@p5pRT
Copy link

p5pRT commented Jul 13, 2000

Migrated from rt.perl.org#3502 (status was 'resolved')

Searchable as RT3502$

@p5pRT
Copy link
Author

p5pRT commented Jul 13, 2000

From [email protected]

Perhaps I'm misunderstanding the use of fcntl(), but I can't get it to do
what I'd like it to. I've read the fcntl(2) and fcntl(5) man pages, but I
don't know if I'm supposed to unpack() the flags somehow.

[jefpin@​towers ~]$ perl
use Fcntl;
fcntl(STDIN, F_GETFL, $mode = '');
print $mode;
Ý@​

[jefpin@​towers ~]$ perl
use Fcntl;
fcntl(STDIN, F_GETFL, $mode = '');
print $mode & O_RDONLY;
print $mode & O_WRONLY;
print $mode & O_RDWR;
000

[jefpin@​towers ~]$

Perl Info


Site configuration information for perl 5.00503:

Configured by root at Wed Feb  2 15:34:50 EST 2000.

Summary of my perl5 (5.0 patchlevel 5 subversion 3) configuration:
  Platform:
    osname=linux, osvers=2.2.5-22smp, archname=i386-linux
    uname='linux porky.devel.redhat.com 2.2.5-22smp #1 smp wed jun 2 09:11:51 edt 1999 i686 unknown '
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef useperlio=undef d_sfio=undef
  Compiler:
    cc='cc', optimize='-O2 -m486 -fno-strength-reduce', gccversion=egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
    cppflags='-Dbool=char -DHAS_BOOL -I/usr/local/include'
    ccflags ='-Dbool=char -DHAS_BOOL -I/usr/local/include'
    stdchar='char', d_stdstdio=undef, usevfork=false
    intsize=4, longsize=4, ptrsize=4, doublesize=8
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    alignbytes=4, usemymalloc=n, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lnsl -ldl -lm -lc -lposix -lcrypt
    libc=, so=so, useshrplib=false, libperl=libperl.a
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic'
    cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib'

Locally applied patches:
    


@INC for perl 5.00503:
    /usr/lib/perl5/5.00503/i386-linux
    /usr/lib/perl5/5.00503
    /usr/lib/perl5/site_perl/5.005/i386-linux
    /usr/lib/perl5/site_perl/5.005
    .


Environment for perl 5.00503:
    HOME=/home/jefpin
    LANG=en_US
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin
    PERL_BADLANG (unset)
    SHELL=/bin/tcsh

@p5pRT
Copy link
Author

p5pRT commented Jul 13, 2000

From [Unknown Contact. See original ticket]

On Thu, Jul 13, 2000 at 07​:32​:27PM -0400, Jeff Pinyan wrote​:

Perhaps I'm misunderstanding the use of fcntl(), but I can't get it to do
what I'd like it to. I've read the fcntl(2) and fcntl(5) man pages, but I
don't know if I'm supposed to unpack() the flags somehow.

[jefpin@​towers ~]$ perl
use Fcntl;
fcntl(STDIN, F_GETFL, $mode = '');
print $mode;
Ý@​

Unfortunately you are misunderstanding the use of fcntl. This isn't
surprising, considering the requirement for a third argument by perl, and
the differences from ioctl. This could very well be considered a Perl bug,
because fcntl(2) reveals a third argument should be omitted in this case,
and strace reveals perl is indeed omitting said third argument.

  use Fcntl;
  $mode = fcntl(STDIN, F_GETFL, 0) || die("fcntl failed​: $!");
 
  print $mode;

Michael
--
Administrator www.shoebox.net
Programmer, System Administrator www.gallanttech.com

@p5pRT
Copy link
Author

p5pRT commented Jul 13, 2000

From [Unknown Contact. See original ticket]

On Jul 13, Michael Fowler said​:

On Thu, Jul 13, 2000 at 07​:32​:27PM -0400, Jeff Pinyan wrote​:

use Fcntl;
fcntl(STDIN, F_GETFL, $mode = '');

Unfortunately you are misunderstanding the use of fcntl. This isn't
surprising, considering the requirement for a third argument by perl, and
the differences from ioctl. This could very well be considered a Perl bug,
because fcntl(2) reveals a third argument should be omitted in this case,
and strace reveals perl is indeed omitting said third argument.

use Fcntl;
$mode = fcntl(STDIN, F_GETFL, 0) || die("fcntl failed​: $!");
print $mode;

Then the Perl documentation is wrong​:

  use Fcntl;
  fcntl($filehandle, F_GETFL, $packed_return_buffer)
  or die "can't fcntl F_GETFL​: $!";

And as for the return value... what flags can I test against it?

jeffp@​hut [12​:05am] ~ #102> perl
use Fcntl;
print fcntl($_, F_GETFL, 0) . "\n" for *STDIN, *STDOUT, *STDERR;
__END__
2
2
2

I'd provide a documentation patch if I had any idea what was truth.

@p5pRT
Copy link
Author

p5pRT commented Jul 13, 2000

From [Unknown Contact. See original ticket]

On Fri, Jul 14, 2000 at 12​:07​:17AM -0400, Jeff Pinyan wrote​:

And as for the return value... what flags can I test against it?

jeffp@​hut [12​:05am] ~ #102> perl
use Fcntl;
print fcntl($_, F_GETFL, 0) . "\n" for *STDIN, *STDOUT, *STDERR;
__END__
2
2
2

I'd provide a documentation patch if I had any idea what was truth.

It appears testing against STDIN, STDOUT, and STDERR is giving you results
you don't expect (I don't expect them to be open O_RDWR either..).

  use Fcntl;

  sysopen(IN, "foo.txt", O_RDONLY) || die( "open in​: $!");
  sysopen(OUT, "foo.txt", O_WRONLY) || die( "open out​: $!");
  sysopen(BOTH, "foo.txt", O_RDWR) || die("open both​: $!");

  foreach my $fh (*STDIN, *STDOUT, *STDERR, *IN, *OUT, *BOTH) {
  printf(
  "handle​: %-6s mode​: %d\n",
  *$fh{NAME}, fcntl($fh, F_GETFL, 0) || die("fcntl​: $!")
  );
  }

  __END__
  handle​: STDIN mode​: 2 # O_RDWR
  handle​: STDOUT mode​: 2 # O_RDWR
  handle​: STDERR mode​: 2 # O_RDWR
  handle​: IN mode​: 0 # O_RDONLY
  handle​: OUT mode​: 1 # O_WRONLY
  handle​: BOTH mode​: 2 # O_RDWR

So it produces what's expected, it was only the test case chosen causing
confusion.

Michael
--
Administrator www.shoebox.net
Programmer, System Administrator www.gallanttech.com

@p5pRT
Copy link
Author

p5pRT commented Jul 14, 2000

From [Unknown Contact. See original ticket]

On Jul 13, Michael Fowler said​:

use Fcntl;
sysopen(IN, "foo.txt", O_RDONLY) || die( "open in​: $!");
sysopen(OUT, "foo.txt", O_WRONLY) || die( "open out​: $!");
sysopen(BOTH, "foo.txt", O_RDWR) || die("open both​: $!");
foreach my $fh (*STDIN, *STDOUT, *STDERR, *IN, *OUT, *BOTH) {
printf(
"handle​: %-6s mode​: %d\n",
*$fh{NAME}, fcntl($fh, F_GETFL, 0) || die("fcntl​: $!")
);
}

Thank you for the clarification. :)

@p5pRT
Copy link
Author

p5pRT commented Dec 23, 2006

@smpeters - Status changed from 'open' to 'resolved'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant