On Fri, Jun 5, 2009 at 10:50 AM, L.Tomei<[email protected]> wrote:
> Is there an easy way in J to recognize Unix symbolic links?

Just use the dynamic call interface (15!:0).  As the function you want
to call, lstat, is in libc, there'd probably be all kinds of
complications from calling it directly, so the easiest way is to just
write a wrapper.  Here's an example of what works on my system.

[am]king ~/a/tmp$ cat wrap_lstat.c
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
int
wrap_lstat_type(const char *fname) {
        struct stat b;
        int r;
        r = lstat(fname, &b);
        return r ? -1 : b.st_mode;
}
[am]king ~/a/tmp$ gcc -Wall -O -fPIC -shared -o wrap_lstat.so wrap_lstat.c
[am]king ~/a/tmp$ jconsole
   ,.l=: '/usr/' ,&.:>"_ 0 {."1] 1!:0 <'/usr/*'
+------------+
|/usr/bin    |
+------------+
|/usr/lib    |
+------------+
|/usr/src    |
+------------+
|/usr/sbin   |
+------------+
|/usr/X11R6  |
+------------+
|/usr/games  |
+------------+
|/usr/lib32  |
+------------+
|/usr/lib64  |
+------------+
|/usr/local  |
+------------+
|/usr/share  |
+------------+
|/usr/include|
+------------+
   lstat_type=: './wrap_lstat.so wrap_lstat_type > i *c' (15!:0) ,@:<@:>
   islink =: 8b120000 = 8b170000 (17 b.) lstat_type
   (<@:islink,])"0 l
+-+------------+
|0|/usr/bin    |
+-+------------+
|0|/usr/lib    |
+-+------------+
|0|/usr/src    |
+-+------------+
|0|/usr/sbin   |
+-+------------+
|0|/usr/X11R6  |
+-+------------+
|0|/usr/games  |
+-+------------+
|1|/usr/lib32  |
+-+------------+
|1|/usr/lib64  |
+-+------------+
|0|/usr/local  |
+-+------------+
|0|/usr/share  |
+-+------------+
|0|/usr/include|
+-+------------+
   [am]king ~/a/tmp$ l /usr
./   X11R6/  games/    lib/    lib64@  sbin/   src/
../  bin/    include/  lib32@  local/  share/
[am]king ~/a/tmp$

The output of ls shows that lib32 and lib64 are symlinks on my system,
and the islink function in J gives the same result.  (I was lazy and
just hardcoded the usual values of the constants S_IFMT and S_IFLNK,
but you could export these from the C wrapper as well if you wanted to
go for sure.)

(This is on an amd64 linux system.)

Ambrus
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to