Page 1 of 2

PHP core files

Posted: Sun Apr 08, 2007 11:24 am
by Oren
Does anybody know where I can view the core code of a specific PHP function?

The PHP manual on is_file() says: "is_file — Tells whether the filename is a regular file". What do they mean by "regular file"?
I would like to see the implementation of the is_file() function (it is written in C if memory serves...).

Posted: Sun Apr 08, 2007 12:39 pm
by Buddha443556
The PHP download page: http://www.php.net/downloads.php

Under "Complete Source Code".

Posted: Sun Apr 08, 2007 12:40 pm
by Benjamin
Regular file probably means that it it's not a directory or symbolic link.

Posted: Sun Apr 08, 2007 12:59 pm
by Oren
Thanks guys, I'll take a look and tell you if I find anything that is worth to mention.

Posted: Sun Apr 08, 2007 1:30 pm
by Oren
Ok, I downloaded it... Any ideas where I can find the is_file() function?

Posted: Sun Apr 08, 2007 1:34 pm
by Buddha443556
Oren wrote:Thanks guys, I'll take a look and tell you if I find anything that is worth to mention.
I think it uses fstat(), there's a do_fstat() in php-5.2.1>ext>main>streams>plain_wrapper.c

Posted: Sun Apr 08, 2007 1:42 pm
by Buddha443556
Oh ... There's reference to is_file() in php-5.2.1 > ext > standard > filestat.c

Looks like it uses the streams statbuf though ... which is how I wound up in plain_wrapper.c

Posted: Sun Apr 08, 2007 6:00 pm
by dreamscape
I believe all they mean by "regular file" is that it is in fact a file and not a directory. I will also return true if the file is a symbolic link that points to a file.

I think they say "regular file" because the file_exists() function works on both files and directories.

Posted: Sun Apr 08, 2007 9:44 pm
by Jenk
They used the words regular to thwart the question of "what kind of file?" Think of it as "Regular, every day, generic file."

Posted: Sun Apr 08, 2007 9:55 pm
by Benjamin
I wonder why it resolves symlinks. A symbolic link is not a file.

Posted: Sun Apr 08, 2007 10:03 pm
by Jenk
Yes, it is a file. It's data contains the path to that of the linked file - the OS recognises this and thus it appears as a shortcut. Much like the Windows ".lnk" files appear as shortcuts, this is what a symbolic link is.

a Hard linked file is what does not appear as it's own file, as that is actually a shared inode, not a 'redirect' as symlinks are.

Posted: Mon Apr 09, 2007 9:22 am
by Oren
Thanks Buddha443556, dreamscape, Jenk and astions, thanks guys.

I did not find it, but I don't think I need it anymore, your help was enough so I don't need to see the core code.
With that said, if someone has already found it, I won't mind see the code.

Thanks again guys :wink:

Posted: Mon Apr 09, 2007 9:54 am
by Weirdan
Oren wrote: With that said, if someone has already found it, I won't mind see the code.
It's in ext/standard/filestat.c file, is_file is expanded using FileFunction macro which calls the php_stat function which deep inside of it calls the php_stream_stat_path_ex and then uses the following macro to check if the file is regular:

Code: Select all

#ifndef S_ISREG
#define S_ISREG(mode)   (((mode)&S_IFMT) == S_IFREG)
#endif
where mode is ssb.sb.st_mode obtained from php_stream_stat_path_ex

P.S.: dig deeper if you want :D

Posted: Mon Apr 09, 2007 10:18 am
by Oren
Oh thanks Weirdan.

I know some C, but very basic. Can you explain in words/pseudo code what these lines mean?

Code: Select all

#ifndef S_ISREG
#define S_ISREG(mode)   (((mode)&S_IFMT) == S_IFREG)
#endif 

Posted: Mon Apr 09, 2007 10:54 am
by feyd
It's called a macro. What the macro does is perform a bitwise comparison of mode and the constant S_IFMT then compares that result with S_IFREG.