realpath.3

classic Classic list List threaded Threaded
32 messages Options
12
Reply | Threaded
Open this post in threaded view
|

realpath.3

Kjell-5
I discovered this behavior when chasing down an edge case in another
piece of code. I believe it should be documented.

Comments?

Index: realpath.3
===================================================================
RCS file: /usr/local/cvsweb/openbsd/src/lib/libc/stdlib/realpath.3,v
retrieving revision 1.13
diff -u -r1.13 realpath.3
--- realpath.3 3 Apr 2005 18:59:15 -0000 1.13
+++ realpath.3 3 May 2006 18:42:36 -0000
@@ -72,6 +72,9 @@
 must exist when
 .Fn realpath
 is called.
+If
+.Fa pathname
+contains an empty string, the current working directory is returned.
 .Sh RETURN VALUES
 The
 .Fn realpath

Reply | Threaded
Open this post in threaded view
|

Re: realpath.3

Jared Yanovich
On Wed, 03 May 2006 12:45:11 -0600
[hidden email] wrote:

> Index: realpath.3
> ===================================================================
> RCS file: /usr/local/cvsweb/openbsd/src/lib/libc/stdlib/realpath.3,v
> retrieving revision 1.13
> diff -u -r1.13 realpath.3
> --- realpath.3 3 Apr 2005 18:59:15 -0000 1.13
> +++ realpath.3 3 May 2006 18:42:36 -0000
> @@ -72,6 +72,9 @@
>  must exist when
>  .Fn realpath
>  is called.
> +If
> +.Fa pathname
> +contains an empty string, the current working directory is returned.

POSIX says

        char *realpath(const char *restrict file_name,
            char *restrict resolved_name);

        ERRORS
       

            The realpath() function shall fail if:

       
            [ENOENT]
                A component of file_name does not name an existing
                file or file_name points to an empty string.

So if this is to be documented your way, I think it should be
mentioned that such behavior should not be relied upon.

Reply | Threaded
Open this post in threaded view
|

Re: realpath.3

Kjell-5
Ah, excellent. Thanks Jared.

> POSIX says
>
> char *realpath(const char *restrict file_name,
>    char *restrict resolved_name);
>
> ERRORS
>
>
>    The realpath() function shall fail if:
>
>
>    [ENOENT]
>        A component of file_name does not name an existing
> file or file_name points to an empty string.
>
> So if this is to be documented your way, I think it should be
> mentioned that such behavior should not be relied upon.

I would think that if it differs from POSIX,
it should either be corrected, or documented, yes.

But that's "a component of file_name does not name an existing file"
except for the last component, isn't it? Or does BSD differ here, too?

-kj

Reply | Threaded
Open this post in threaded view
|

Re: realpath.3

Ray Lai
In reply to this post by Jared Yanovich
On Thu, May 04, 2006 at 02:43:27PM -0400, Jared Yanovich wrote:

> On Wed, 03 May 2006 12:45:11 -0600
> [hidden email] wrote:
>
> > Index: realpath.3
> > ===================================================================
> > RCS file: /usr/local/cvsweb/openbsd/src/lib/libc/stdlib/realpath.3,v
> > retrieving revision 1.13
> > diff -u -r1.13 realpath.3
> > --- realpath.3 3 Apr 2005 18:59:15 -0000 1.13
> > +++ realpath.3 3 May 2006 18:42:36 -0000
> > @@ -72,6 +72,9 @@
> >  must exist when
> >  .Fn realpath
> >  is called.
> > +If
> > +.Fa pathname
> > +contains an empty string, the current working directory is returned.
>
> POSIX says
>
> char *realpath(const char *restrict file_name,
>    char *restrict resolved_name);
>
> ERRORS
>
>
>    The realpath() function shall fail if:
>
>
>    [ENOENT]
>        A component of file_name does not name an existing
> file or file_name points to an empty string.
>
> So if this is to be documented your way, I think it should be
> mentioned that such behavior should not be relied upon.

Is there a reason why realpath() should not be changed to conform
to POSIX?

-Ray-

Reply | Threaded
Open this post in threaded view
|

Re: realpath.3

Ted Unangst-2
On 5/4/06, Ray Lai <[hidden email]> wrote:
> >           [ENOENT]
> >               A component of file_name does not name an existing
> >               file or file_name points to an empty string.
> >
> > So if this is to be documented your way, I think it should be
> > mentioned that such behavior should not be relied upon.
>
> Is there a reason why realpath() should not be changed to conform
> to POSIX?

i don't think so.  currently behavior is rather surprising.  posix
makes sense here.

Reply | Threaded
Open this post in threaded view
|

Re: realpath.3

Ray Lai
On Thu, May 04, 2006 at 12:48:22PM -0700, Ted Unangst wrote:

> On 5/4/06, Ray Lai <[hidden email]> wrote:
> >>           [ENOENT]
> >>               A component of file_name does not name an existing
> >>               file or file_name points to an empty string.
> >>
> >> So if this is to be documented your way, I think it should be
> >> mentioned that such behavior should not be relied upon.
> >
> >Is there a reason why realpath() should not be changed to conform
> >to POSIX?
>
> i don't think so.  currently behavior is rather surprising.  posix
> makes sense here.

Okay?

-Ray-

Index: regress/lib/libc/Makefile
===================================================================
RCS file: /cvs/src/regress/lib/libc/Makefile,v
retrieving revision 1.23
diff -u -p -r1.23 Makefile
--- regress/lib/libc/Makefile 25 Mar 2006 20:28:19 -0000 1.23
+++ regress/lib/libc/Makefile 4 May 2006 20:36:52 -0000
@@ -2,7 +2,7 @@
 
 SUBDIR+= _setjmp alloca atexit db getaddrinfo getcap getopt_long hsearch longjmp
 SUBDIR+= locale malloc
-SUBDIR+= netdb popen regex setjmp setjmp-signal sigreturn sigsetjmp
+SUBDIR+= netdb popen realpath regex setjmp setjmp-signal sigreturn sigsetjmp
 SUBDIR+= sprintf strerror strtonum telldir time vis
 
 .if (${MACHINE_ARCH} != "vax")
Index: regress/lib/libc/realpath/Makefile
===================================================================
RCS file: regress/lib/libc/realpath/Makefile
diff -N regress/lib/libc/realpath/Makefile
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ regress/lib/libc/realpath/Makefile 4 May 2006 20:36:52 -0000
@@ -0,0 +1,5 @@
+# $OpenBSD$
+
+PROG= realpathtest
+
+.include <bsd.regress.mk>
Index: regress/lib/libc/realpath/realpathtest.c
===================================================================
RCS file: regress/lib/libc/realpath/realpathtest.c
diff -N regress/lib/libc/realpath/realpathtest.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ regress/lib/libc/realpath/realpathtest.c 4 May 2006 20:36:52 -0000
@@ -0,0 +1,24 @@
+/*      $OpenBSD$ */
+
+/*
+ * Written by Raymond Lai <[hidden email]>.
+ * Public domain.
+ */
+
+#include <sys/param.h>
+
+#include <err.h>
+#include <stdlib.h>
+
+int
+main(int argc, char *argv[])
+{
+ char path[PATH_MAX];
+
+ if (realpath("", path) == NULL)
+ warn(NULL);
+ else
+ return (1);
+
+ return (0);
+}
Index: lib/libc/stdlib/realpath.c
===================================================================
RCS file: /cvs/src/lib/libc/stdlib/realpath.c,v
retrieving revision 1.13
diff -u -p -r1.13 realpath.c
--- lib/libc/stdlib/realpath.c 8 Aug 2005 08:05:37 -0000 1.13
+++ lib/libc/stdlib/realpath.c 4 May 2006 20:36:52 -0000
@@ -54,6 +54,10 @@ realpath(const char *path, char resolved
 
  serrno = errno;
  symlinks = 0;
+ if (strcmp(path, "") == 0) {
+ errno = ENOENT;
+ return (NULL);
+ }
  if (path[0] == '/') {
  resolved[0] = '/';
  resolved[1] = '\0';

Reply | Threaded
Open this post in threaded view
|

Re: realpath.3

Kjell-5
> > >Is there a reason why realpath() should not be changed to conform
> > >to POSIX?
> >
> > i don't think so.  currently behavior is rather surprising.  posix
> > makes sense here.
>
> Okay?

It would be worth it to first scour the tree to ensure nothing else
(apart from mg, which is where I noticed this little curio) relies on
this unexpected behavior.

-kj

Reply | Threaded
Open this post in threaded view
|

Re: realpath.3

Thorsten Glaser-3
In reply to this post by Ray Lai
Ray Lai dixit:

>--- lib/libc/stdlib/realpath.c 8 Aug 2005 08:05:37 -0000 1.13
>+++ lib/libc/stdlib/realpath.c 4 May 2006 20:36:52 -0000
>@@ -54,6 +54,10 @@ realpath(const char *path, char resolved
>
> serrno = errno;
> symlinks = 0;
>+ if (strcmp(path, "") == 0) {

*cough*

+ if (path && !*path) {

//mirabile
--
> emacs als auch vi zum Kotzen finde (joe rules) und pine f|r den einzig
> bedienbaren textmode-mailclient halte (und ich hab sie alle ausprobiert). ;)
Hallooooo, ich bin der Holger ("Hallo Holger!"), und ich bin ebenfalls
... pine-User, und das auch noch gewohnheitsmd_ig ("Oooooooohhh").  [aus dasr]

Reply | Threaded
Open this post in threaded view
|

Re: realpath.3

Marc Espie-2
On Fri, May 05, 2006 at 12:13:31PM +0000, Thorsten Glaser wrote:

> Ray Lai dixit:
>
> >--- lib/libc/stdlib/realpath.c 8 Aug 2005 08:05:37 -0000 1.13
> >+++ lib/libc/stdlib/realpath.c 4 May 2006 20:36:52 -0000
> >@@ -54,6 +54,10 @@ realpath(const char *path, char resolved
> >
> > serrno = errno;
> > symlinks = 0;
> >+ if (strcmp(path, "") == 0) {
>
> *cough*
>
> + if (path && !*path) {
>
> //mirabile

Show me a link that says that realpath must do something extra with null
pointers... dumping core is perfectly acceptable to me.

Repeat after me: empty strings and nul pointers are distinct things.

Reply | Threaded
Open this post in threaded view
|

Re: realpath.3

Ray Lai
On Fri, May 05, 2006 at 03:35:32PM +0200, Marc Espie wrote:

> On Fri, May 05, 2006 at 12:13:31PM +0000, Thorsten Glaser wrote:
> > Ray Lai dixit:
> >
> > >--- lib/libc/stdlib/realpath.c 8 Aug 2005 08:05:37 -0000 1.13
> > >+++ lib/libc/stdlib/realpath.c 4 May 2006 20:36:52 -0000
> > >@@ -54,6 +54,10 @@ realpath(const char *path, char resolved
> > >
> > > serrno = errno;
> > > symlinks = 0;
> > >+ if (strcmp(path, "") == 0) {
> >
> > *cough*
> >
> > + if (path && !*path) {
> >
> > //mirabile
>
> Show me a link that says that realpath must do something extra with null
> pointers... dumping core is perfectly acceptable to me.

http://www.opengroup.org/onlinepubs/007908799/xsh/realpath.html

-Ray-

Reply | Threaded
Open this post in threaded view
|

Re: realpath.3

Ray Lai
In reply to this post by Thorsten Glaser-3
On Fri, May 05, 2006 at 12:13:31PM +0000, Thorsten Glaser wrote:

> Ray Lai dixit:
>
> >--- lib/libc/stdlib/realpath.c 8 Aug 2005 08:05:37 -0000 1.13
> >+++ lib/libc/stdlib/realpath.c 4 May 2006 20:36:52 -0000
> >@@ -54,6 +54,10 @@ realpath(const char *path, char resolved
> >
> > serrno = errno;
> > symlinks = 0;
> >+ if (strcmp(path, "") == 0) {
>
> *cough*
>
> + if (path && !*path) {

Is this optimization preferred?  Well, I guess I'll stick it in.

This also brought to my attention that if path or resolved are NULL
realpath should return NULL, setting errno to EINVAL.

-Ray-

Index: lib/libc/stdlib/realpath.c
===================================================================
RCS file: /cvs/src/lib/libc/stdlib/realpath.c,v
retrieving revision 1.13
diff -u -p -r1.13 realpath.c
--- lib/libc/stdlib/realpath.c 8 Aug 2005 08:05:37 -0000 1.13
+++ lib/libc/stdlib/realpath.c 5 May 2006 13:43:22 -0000
@@ -54,6 +54,14 @@ realpath(const char *path, char resolved
 
  serrno = errno;
  symlinks = 0;
+ if (path == NULL || resolved == NULL) {
+ errno = EINVAL;
+ return (NULL);
+ }
+ if (path[0] == '\0') {
+ errno = ENOENT;
+ return (NULL);
+ }
  if (path[0] == '/') {
  resolved[0] = '/';
  resolved[1] = '\0';
Index: regress/lib/libc/Makefile
===================================================================
RCS file: /cvs/src/regress/lib/libc/Makefile,v
retrieving revision 1.23
diff -u -p -r1.23 Makefile
--- regress/lib/libc/Makefile 25 Mar 2006 20:28:19 -0000 1.23
+++ regress/lib/libc/Makefile 5 May 2006 13:43:22 -0000
@@ -2,7 +2,7 @@
 
 SUBDIR+= _setjmp alloca atexit db getaddrinfo getcap getopt_long hsearch longjmp
 SUBDIR+= locale malloc
-SUBDIR+= netdb popen regex setjmp setjmp-signal sigreturn sigsetjmp
+SUBDIR+= netdb popen realpath regex setjmp setjmp-signal sigreturn sigsetjmp
 SUBDIR+= sprintf strerror strtonum telldir time vis
 
 .if (${MACHINE_ARCH} != "vax")
Index: regress/lib/libc/realpath/Makefile
===================================================================
RCS file: regress/lib/libc/realpath/Makefile
diff -N regress/lib/libc/realpath/Makefile
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ regress/lib/libc/realpath/Makefile 5 May 2006 13:43:22 -0000
@@ -0,0 +1,5 @@
+# $OpenBSD$
+
+PROG= realpathtest
+
+.include <bsd.regress.mk>
Index: regress/lib/libc/realpath/realpathtest.c
===================================================================
RCS file: regress/lib/libc/realpath/realpathtest.c
diff -N regress/lib/libc/realpath/realpathtest.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ regress/lib/libc/realpath/realpathtest.c 5 May 2006 13:43:22 -0000
@@ -0,0 +1,34 @@
+/*      $OpenBSD$ */
+
+/*
+ * Written by Raymond Lai <[hidden email]>.
+ * Public domain.
+ */
+
+#include <sys/param.h>
+
+#include <errno.h>
+#include <stdlib.h>
+
+int
+main(int argc, char *argv[])
+{
+ char path[PATH_MAX];
+
+ if (realpath("", path) != NULL)
+ return (1);
+ if (errno != ENOENT)
+ return (1);
+
+ if (realpath(NULL, path) != NULL)
+ return (1);
+ if (errno != EINVAL)
+ return (1);
+
+ if (realpath("/", NULL) != NULL)
+ return (1);
+ if (errno != EINVAL)
+ return (1);
+
+ return (0);
+}

Reply | Threaded
Open this post in threaded view
|

Re: realpath.3

Jared Yanovich
On Fri, 5 May 2006 09:44:41 -0401
Ray Lai <[hidden email]> wrote:

> > >--- lib/libc/stdlib/realpath.c 8 Aug 2005 08:05:37 -0000 1.13
> > >+++ lib/libc/stdlib/realpath.c 4 May 2006 20:36:52 -0000
> > >@@ -54,6 +54,10 @@ realpath(const char *path, char resolved
> > >
> > > serrno = errno;
> > > symlinks = 0;
> > >+ if (strcmp(path, "") == 0) {
> >
> > *cough*
> >
> > + if (path && !*path) {
>
> Is this optimization preferred?  Well, I guess I'll stick it in.

No, it should not matter.

> This also brought to my attention that if path or resolved are NULL
> realpath should return NULL, setting errno to EINVAL.

Only 'path'.  POSIX says:

             char *realpath(const char *restrict file_name,
                   char *restrict resolved_name);

        DESCRIPTION

            If resolved_name is a null pointer, the behavior of realpath()
            is implementation-defined.

Reply | Threaded
Open this post in threaded view
|

Re: realpath.3

Ray Lai
On Fri, May 05, 2006 at 10:22:04AM -0400, Jared Yanovich wrote:

> On Fri, 5 May 2006 09:44:41 -0401
> Ray Lai <[hidden email]> wrote:
>
> > > >--- lib/libc/stdlib/realpath.c 8 Aug 2005 08:05:37 -0000 1.13
> > > >+++ lib/libc/stdlib/realpath.c 4 May 2006 20:36:52 -0000
> > > >@@ -54,6 +54,10 @@ realpath(const char *path, char resolved
> > > >
> > > > serrno = errno;
> > > > symlinks = 0;
> > > >+ if (strcmp(path, "") == 0) {
> > >
> > > *cough*
> > >
> > > + if (path && !*path) {
> >
> > Is this optimization preferred?  Well, I guess I'll stick it in.
>
> No, it should not matter.
>
> > This also brought to my attention that if path or resolved are NULL
> > realpath should return NULL, setting errno to EINVAL.
>
> Only 'path'.  POSIX says:
>
>     char *realpath(const char *restrict file_name,
>           char *restrict resolved_name);
>
> DESCRIPTION
>
>    If resolved_name is a null pointer, the behavior of realpath()
>    is implementation-defined.

So now if `resolved' is NULL it will core dump, as it already does.
Keep this in mind, however:

        FUTURE DIRECTIONS

        In the future, passing a null pointer to realpath() for the
        resolved_name argument may be defined to have realpath()
        allocate space for the generated pathname.

(http://www.opengroup.org/onlinepubs/000095399/functions/realpath.html)

-Ray-

Index: lib/libc/stdlib/realpath.c
===================================================================
RCS file: /cvs/src/lib/libc/stdlib/realpath.c,v
retrieving revision 1.13
diff -u -p -r1.13 realpath.c
--- lib/libc/stdlib/realpath.c 8 Aug 2005 08:05:37 -0000 1.13
+++ lib/libc/stdlib/realpath.c 5 May 2006 15:23:32 -0000
@@ -54,6 +54,14 @@ realpath(const char *path, char resolved
 
  serrno = errno;
  symlinks = 0;
+ if (path == NULL) {
+ errno = EINVAL;
+ return (NULL);
+ }
+ if (path[0] == '\0') {
+ errno = ENOENT;
+ return (NULL);
+ }
  if (path[0] == '/') {
  resolved[0] = '/';
  resolved[1] = '\0';
Index: regress/lib/libc/Makefile
===================================================================
RCS file: /cvs/src/regress/lib/libc/Makefile,v
retrieving revision 1.23
diff -u -p -r1.23 Makefile
--- regress/lib/libc/Makefile 25 Mar 2006 20:28:19 -0000 1.23
+++ regress/lib/libc/Makefile 5 May 2006 15:23:32 -0000
@@ -2,7 +2,7 @@
 
 SUBDIR+= _setjmp alloca atexit db getaddrinfo getcap getopt_long hsearch longjmp
 SUBDIR+= locale malloc
-SUBDIR+= netdb popen regex setjmp setjmp-signal sigreturn sigsetjmp
+SUBDIR+= netdb popen realpath regex setjmp setjmp-signal sigreturn sigsetjmp
 SUBDIR+= sprintf strerror strtonum telldir time vis
 
 .if (${MACHINE_ARCH} != "vax")
Index: regress/lib/libc/realpath/Makefile
===================================================================
RCS file: regress/lib/libc/realpath/Makefile
diff -N regress/lib/libc/realpath/Makefile
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ regress/lib/libc/realpath/Makefile 5 May 2006 15:23:32 -0000
@@ -0,0 +1,5 @@
+# $OpenBSD$
+
+PROG= realpathtest
+
+.include <bsd.regress.mk>
Index: regress/lib/libc/realpath/realpathtest.c
===================================================================
RCS file: regress/lib/libc/realpath/realpathtest.c
diff -N regress/lib/libc/realpath/realpathtest.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ regress/lib/libc/realpath/realpathtest.c 5 May 2006 15:23:32 -0000
@@ -0,0 +1,40 @@
+/*      $OpenBSD$ */
+
+/*
+ * Written by Raymond Lai <[hidden email]>.
+ * Public domain.
+ */
+
+#include <sys/param.h>
+
+#include <errno.h>
+#include <signal.h>
+#include <stdlib.h>
+
+/* ARGSUSED */
+void
+sighdlr(int sig)
+{
+ exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ char path[PATH_MAX];
+
+ if (realpath("", path) != NULL)
+ return (1);
+ if (errno != ENOENT)
+ return (1);
+
+ if (realpath(NULL, path) != NULL)
+ return (1);
+ if (errno != EINVAL)
+ return (1);
+
+ signal(SIGSEGV, sighdlr);
+ realpath("/", NULL);
+
+ return (1);
+}

Reply | Threaded
Open this post in threaded view
|

Re: realpath.3

Jared Yanovich
On Fri, 5 May 2006 11:24:28 -0400
Ray Lai <[hidden email]> wrote:

> > > > > serrno = errno;
> > > > > symlinks = 0;
> > > > >+ if (strcmp(path, "") == 0) {
> > > >
> > > > *cough*
> > > >
> > > > + if (path && !*path) {
> > >
> > > Is this optimization preferred?  Well, I guess I'll stick it in.
> >
> > No, it should not matter.
> >
> > > This also brought to my attention that if path or resolved are NULL
> > > realpath should return NULL, setting errno to EINVAL.
> >
> > Only 'path'.  POSIX says:
> >
> >     char *realpath(const char *restrict file_name,
> >           char *restrict resolved_name);
> >
> > DESCRIPTION
> >
> >    If resolved_name is a null pointer, the behavior of realpath()
> >    is implementation-defined.
>
> So now if `resolved' is NULL it will core dump, as it already does.

I never said that wasn't a good idea, I just meant to point out
that behavior is not forced one way or another.  I do think it
is a good idea ;)

Reply | Threaded
Open this post in threaded view
|

Re: realpath.3

Thorsten Glaser-3
In reply to this post by Jared Yanovich
Jared Yanovich dixit:

>> > >+ if (strcmp(path, "") == 0) {

>> > + if (path && !*path) {
>>
>> Is this optimization preferred?  Well, I guess I'll stick it in.
>
>No, it should not matter.

After all, it saves an expensive function call. (The check can
also be expressed as
+ if (!path || !*path) {
if you want to catch path==NULL and return EINVAL for it.)

Just my 0.05 EUR
//mirabile
--
> emacs als auch vi zum Kotzen finde (joe rules) und pine f|r den einzig
> bedienbaren textmode-mailclient halte (und ich hab sie alle ausprobiert). ;)
Hallooooo, ich bin der Holger ("Hallo Holger!"), und ich bin ebenfalls
... pine-User, und das auch noch gewohnheitsmd_ig ("Oooooooohhh").  [aus dasr]

Reply | Threaded
Open this post in threaded view
|

Re: realpath.3

Ted Unangst-2
On 5/5/06, Thorsten Glaser <[hidden email]> wrote:

> Jared Yanovich dixit:
>
> >> > >+ if (strcmp(path, "") == 0) {
>
> >> > +  if (path && !*path) {
> >>
> >> Is this optimization preferred?  Well, I guess I'll stick it in.
> >
> >No, it should not matter.
>
> After all, it saves an expensive function call.

why are you compiling without optimization turned on?

> (The check can
> also be expressed as
> +       if (!path || !*path) {
> if you want to catch path==NULL and return EINVAL for it.)

except they're supposed to set different error codes.

Reply | Threaded
Open this post in threaded view
|

Re: realpath.3

Thorsten Glaser-3
Ted Unangst dixit:

> why are you compiling without optimization turned on?

To quote Theo de Raadt:

   I would love a new C compiler, but mostly because I feel that the gcc
   model is unmaintainable in the long term. What the gcc people have
   created is a compiler that is heavy on optimization. The result of
   their imperfect efforts thus is a compiler that generates incorrect
   code from time to time, and this affects us all. That also makes it
   very slow. I would love to see a new C compiler that was fully
   compliant, did minimal optimization, [...]

//mirabile
--
> emacs als auch vi zum Kotzen finde (joe rules) und pine f|r den einzig
> bedienbaren textmode-mailclient halte (und ich hab sie alle ausprobiert). ;)
Hallooooo, ich bin der Holger ("Hallo Holger!"), und ich bin ebenfalls
... pine-User, und das auch noch gewohnheitsmd_ig ("Oooooooohhh").  [aus dasr]

Reply | Threaded
Open this post in threaded view
|

Re: realpath.3

Constantine A. Murenin
In reply to this post by Thorsten Glaser-3
> +       if (!path || !*path) {

Do path and *path hold boolean values? Does anyone read style(9)?

Reply | Threaded
Open this post in threaded view
|

Re: realpath.3

Ray Lai
On Fri, May 05, 2006 at 09:51:52PM +0100, Constantine A. Murenin wrote:
> >+       if (!path || !*path) {
>
> Do path and *path hold boolean values? Does anyone read style(9)?

There's nothing wrong with testing whether variables are set to
NULL, '\0', or 0 with `!'.

-Ray-

Reply | Threaded
Open this post in threaded view
|

Re: realpath.3

Ray Lai
In reply to this post by Ted Unangst-2
On Fri, May 05, 2006 at 12:57:28PM -0700, Ted Unangst wrote:

> On 5/5/06, Thorsten Glaser <[hidden email]> wrote:
> >Jared Yanovich dixit:
> >
> >>> > >+ if (strcmp(path, "") == 0) {
> >
> >>> > +  if (path && !*path) {
> >>>
> >>> Is this optimization preferred?  Well, I guess I'll stick it in.
> >>
> >>No, it should not matter.
> >
> >After all, it saves an expensive function call.
>
> why are you compiling without optimization turned on?
>
> >(The check can
> >also be expressed as
> >+       if (!path || !*path) {
> >if you want to catch path==NULL and return EINVAL for it.)
>
> except they're supposed to set different error codes.

I interpreted it as:

        if (!path || !*path) {
                if (!path)
                        errno = EINVAL;
                else
                        errno = ENOENT;
                return (NULL);
        }

which I find clearer.

-Ray-

Index: lib/libc/stdlib/realpath.c
===================================================================
RCS file: /cvs/src/lib/libc/stdlib/realpath.c,v
retrieving revision 1.13
diff -u -p -r1.13 realpath.c
--- lib/libc/stdlib/realpath.c 8 Aug 2005 08:05:37 -0000 1.13
+++ lib/libc/stdlib/realpath.c 5 May 2006 21:52:09 -0000
@@ -54,6 +54,13 @@ realpath(const char *path, char resolved
 
  serrno = errno;
  symlinks = 0;
+ if (path == NULL || path[0] == '\0') {
+ if (path == NULL)
+ errno = EINVAL;
+ else
+ errno = ENOENT;
+ return (NULL);
+ }
  if (path[0] == '/') {
  resolved[0] = '/';
  resolved[1] = '\0';
Index: regress/lib/libc/Makefile
===================================================================
RCS file: /cvs/src/regress/lib/libc/Makefile,v
retrieving revision 1.23
diff -u -p -r1.23 Makefile
--- regress/lib/libc/Makefile 25 Mar 2006 20:28:19 -0000 1.23
+++ regress/lib/libc/Makefile 5 May 2006 21:52:09 -0000
@@ -2,7 +2,7 @@
 
 SUBDIR+= _setjmp alloca atexit db getaddrinfo getcap getopt_long hsearch longjmp
 SUBDIR+= locale malloc
-SUBDIR+= netdb popen regex setjmp setjmp-signal sigreturn sigsetjmp
+SUBDIR+= netdb popen realpath regex setjmp setjmp-signal sigreturn sigsetjmp
 SUBDIR+= sprintf strerror strtonum telldir time vis
 
 .if (${MACHINE_ARCH} != "vax")
Index: regress/lib/libc/realpath/Makefile
===================================================================
RCS file: regress/lib/libc/realpath/Makefile
diff -N regress/lib/libc/realpath/Makefile
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ regress/lib/libc/realpath/Makefile 5 May 2006 21:52:09 -0000
@@ -0,0 +1,5 @@
+# $OpenBSD$
+
+PROG= realpathtest
+
+.include <bsd.regress.mk>
Index: regress/lib/libc/realpath/realpathtest.c
===================================================================
RCS file: regress/lib/libc/realpath/realpathtest.c
diff -N regress/lib/libc/realpath/realpathtest.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ regress/lib/libc/realpath/realpathtest.c 5 May 2006 21:52:09 -0000
@@ -0,0 +1,40 @@
+/*      $OpenBSD$ */
+
+/*
+ * Written by Raymond Lai <[hidden email]>.
+ * Public domain.
+ */
+
+#include <sys/param.h>
+
+#include <errno.h>
+#include <signal.h>
+#include <stdlib.h>
+
+/* ARGSUSED */
+void
+sighdlr(int sig)
+{
+ exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ char path[PATH_MAX];
+
+ if (realpath("", path) != NULL)
+ return (1);
+ if (errno != ENOENT)
+ return (1);
+
+ if (realpath(NULL, path) != NULL)
+ return (1);
+ if (errno != EINVAL)
+ return (1);
+
+ signal(SIGSEGV, sighdlr);
+ realpath("/", NULL);
+
+ return (1);
+}

12