Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 27 Jul 2009 21:54:51 -0700 (PDT)
From:      Unga <unga888@yahoo.com>
To:        freebsd-questions@freebsd.org, Mel Flynn <mel.flynn+fbsd.questions@mailing.thruhere.net>
Subject:   Re: How to find what symlink points to?
Message-ID:  <631612.70113.qm@web57002.mail.re3.yahoo.com>

next in thread | raw e-mail | index | archive | help

--- On Tue, 7/28/09, Mel Flynn <mel.flynn+fbsd.questions@mailing.thruhere.n=
et> wrote:

> From: Mel Flynn <mel.flynn+fbsd.questions@mailing.thruhere.net>
> Subject: Re: How to find what symlink points to?
> To: freebsd-questions@freebsd.org
> Date: Tuesday, July 28, 2009, 1:25 AM
> On Monday 27 July 2009 05:45:13 Unga
> wrote:
>=20
> > > > Hi all
> > > >
> > > > I need to remove some unwanted symlinks on
> /dev using
> > >
> > > a C program.
> > >
> > > > The "struct dirent" only shows the symlink
> name, how
> > >
> > > do I find what that
> > >
> > > > symlink points to for verification purpose?
> > >
> > > By using the readlink(2) system call.
> >
> > But readlink(2) fails with errno set to 2. Can
> readlink(2) use with dev
> > nodes?
>=20
> Works for me. errno 2 is ENOENT ("No such file or
> directory"). I would inspect=20
> if your request path points to the right location.
>=20
> % ./rl /dev/stderr
> /dev/stderr =3D> fd/2
>=20
> % cat rl.c
> #include <sys/types.h>
> #include <sys/param.h>
> #include <unistd.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>=20
> #include <err.h>
>=20
> int main(int argc, char **argv)
> {
> =A0 =A0 =A0 =A0 char path[MAXPATHLEN],
> buf[MAXPATHLEN+1];
> =A0 =A0 =A0 =A0 ssize_t res;
>=20
> =A0 =A0 =A0 =A0 if( argc !=3D 2 )
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0
> exit(67);
>=20
> =A0 =A0 =A0 =A0 (void)strlcpy(path, argv[1],
> sizeof(path));
> =A0 =A0 =A0 =A0 res =3D readlink(path, buf,
> sizeof(buf));
> =A0 =A0 =A0 =A0 if( res < 0 )
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0
> err(EXIT_FAILURE, "readlink()");
> =A0 =A0 =A0 =A0 buf[MAXPATHLEN] =3D '\0';
> =A0 =A0 =A0 =A0 printf("%s =3D> %s\n", path,
> buf);
>=20
> =A0 =A0 =A0 =A0 return (0);
> }
>=20

Thanks everybody for valuable replies. In fact, I also used readlink(2) but=
 fed the symlink path directly from dirent, which was partial, readlink(2) =
requires full path.

Unga=0A=0A=0A      



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?631612.70113.qm>