+#define FSN 8
+#define MAXINO (1<<22)
+#define INOTYP unsigned long
+#define regbit_qry(x,y) ( x[( (y) / sizeof(INOTYP) )] & 1<<( (y) % sizeof(INOTYP) ) )
+#define regbit_set(x,y) ( x[( (y) / sizeof(INOTYP) )] |= 1<<( (y) % sizeof(INOTYP) ) )
+
+int hlink_check(struct stat*info)
+{
+ /*
+ * for speed this subroutine should only be called
+ * - if the file has more than one hardlink
+ * - if the file is a resolved softlink
+ */
+ /* the persistent variables */
+ static INOTYP *list[FSN];
+ static dev_t name[FSN];
+ /* some temporary variables */
+ int fsn, is_registered=0;
+
+ /* assertions - in case parameters are lowered for less memory usage */
+ assert(fsn<FSN);
+ assert((info->st_ino)/sizeof(INOTYP)<MAXINO);
+
+ /* search which internal registration number is used for this filesystem */
+ for( fsn=0; (name[fsn]!=(info->st_dev)) && (name[fsn]!=0) ; fsn++);
+
+ /* if file system is not registered yet, do it and leave */
+ if( name[fsn] == 0 )
+ {
+ name[fsn] = (info->st_dev);
+ /* provide space for the bitmap that maps the inodes of this file system */
+ list[fsn] = (INOTYP*)calloc(MAXINO,sizeof(INOTYP));
+ /* no comparison is needed in empty lists ... return */
+ if(debug) fprintf(stderr, "Debug >> Linked >> Init List %04x @mem %04lx\n", (int)name[fsn], (long)&list[fsn] );
+ } else
+ {
+ /* this looks more complicated than it really is */
+ /* the idea is very simple:
+ * provide a bitmap that maps all inodes of a file system
+ * to mark all files that have already been visited.
+ * If it is already visited, do not add it to the playlist
+ */
+ /*
+ * The difficulty is as follows:
+ * struct inode_bitmap { char registered:1; } bitmap[1<<MAXINO];
+ * would be byte-aligned and would allocate at least eight times the needed space.
+ * Feel free to change the definitions that are involved here, if you know better.
+ */
+ if( regbit_qry( list[fsn], (info->st_ino) ) ) is_registered=1;
+ else regbit_set( list[fsn], (info->st_ino) );
+ /*
+ * the debug expression is more complicated then the working stuff
+ */
+ if(debug)
+ fprintf(stderr, "Debug >> Linked >> DEV %04x INO %06x => "
+ "list[%02x][%04x] = %04x & %04x --> %s registered\n",
+ (int)info->st_dev, (int)info->st_ino, fsn, (int)((info->st_ino)/sizeof(INOTYP)),
+ (int)list[fsn][(info->st_ino)/sizeof(INOTYP)],
+ 1<<((info->st_ino)%sizeof(INOTYP)), is_registered?"Already":"Not" );
+ }
+ return is_registered;
+}
+