Re: How to find what symlink points to?
On Monday 27 July 2009 20:54:51 Unga wrote: > 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. Nope it doesn't. It's the classical "opendir does not chdir" problem. readlink(2) requires a path that resolves from the current working directory of the program. If you opendir(3) /dev and are in /, then dirent->d_name is not valid for your cwd. Either you have to chdir or prepend the path given to opendir to dirent->d_name. % ./rl /dev/stdout stdout => fd/1 See the diff on previous rl.c below. -- Mel --- rl.c.orig 2009-07-27 09:19:58.0 -0800 +++ rl.c2009-07-27 21:25:48.0 -0800 @@ -9,13 +9,31 @@ int main(int argc, char **argv) { - char path[MAXPATHLEN], buf[MAXPATHLEN+1]; + char path[MAXPATHLEN], buf[MAXPATHLEN+1], *ptr; ssize_t res; if( argc != 2 ) exit(67); (void)strlcpy(path, argv[1], sizeof(path)); + ptr = strrchr(path, '/'); + if( ptr != NULL ) + { + char *tmp = ptr + 1; + + if( ptr == path ) + { + if( strlen(path) == 1 ) + errx(67, "/ can never be a symlink"); + chdir("/"); + } + else + { + *ptr = '\0'; + (void)chdir(path); + } + (void)strlcpy(path, tmp, sizeof(path)); + } res = readlink(path, buf, sizeof(buf)); if( res < 0 ) err(EXIT_FAILURE, "readlink()"); ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
Re: How to find what symlink points to?
--- On Tue, 7/28/09, Mel Flynn wrote: > From: Mel Flynn > 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: > > > > > 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? > > Works for me. errno 2 is ENOENT ("No such file or > directory"). I would inspect > if your request path points to the right location. > > % ./rl /dev/stderr > /dev/stderr => fd/2 > > % cat rl.c > #include > #include > #include > #include > #include > #include > > #include > > int main(int argc, char **argv) > { > char path[MAXPATHLEN], > buf[MAXPATHLEN+1]; > ssize_t res; > > if( argc != 2 ) > > exit(67); > > (void)strlcpy(path, argv[1], > sizeof(path)); > res = readlink(path, buf, > sizeof(buf)); > if( res < 0 ) > > err(EXIT_FAILURE, "readlink()"); > buf[MAXPATHLEN] = '\0'; > printf("%s => %s\n", path, > buf); > > return (0); > } > 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 ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
Re: How to find what symlink points to?
On Monday 27 July 2009 05:45:13 Unga wrote: > > > 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? Works for me. errno 2 is ENOENT ("No such file or directory"). I would inspect if your request path points to the right location. % ./rl /dev/stderr /dev/stderr => fd/2 % cat rl.c #include #include #include #include #include #include #include int main(int argc, char **argv) { char path[MAXPATHLEN], buf[MAXPATHLEN+1]; ssize_t res; if( argc != 2 ) exit(67); (void)strlcpy(path, argv[1], sizeof(path)); res = readlink(path, buf, sizeof(buf)); if( res < 0 ) err(EXIT_FAILURE, "readlink()"); buf[MAXPATHLEN] = '\0'; printf("%s => %s\n", path, buf); return (0); } -- Mel ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
Re: How to find what symlink points to?
On Mon, Jul 27, 2009 at 8:45 AM, Unga wrote: > > --- On Mon, 7/27/09, Erik Trulsson wrote: > >> From: Erik Trulsson >> Subject: Re: How to find what symlink points to? >> To: "Unga" >> Cc: freebsd-questions@freebsd.org >> Date: Monday, July 27, 2009, 9:36 PM >> On Mon, Jul 27, 2009 at 05:44:59AM >> -0700, Unga wrote: >> > >> > 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? > > Unga > Doesn't 'ls -alh' provide that information? Andrew ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
Re: How to find what symlink points to?
--- On Mon, 7/27/09, Erik Trulsson wrote: > From: Erik Trulsson > Subject: Re: How to find what symlink points to? > To: "Unga" > Cc: freebsd-questions@freebsd.org > Date: Monday, July 27, 2009, 9:36 PM > On Mon, Jul 27, 2009 at 05:44:59AM > -0700, Unga wrote: > > > > 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? Unga ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
Re: How to find what symlink points to?
On Mon, Jul 27, 2009 at 05:44:59AM -0700, Unga wrote: > > 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. -- Erik Trulsson ertr1...@student.uu.se ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"