/* %Z%%M%	%I% %E% Copyright 2004-2006 J. Schilling */
/*
 *	Definitions for directory tree walking
 *
 *	Copyright (c) 2004-2006 J. Schilling
 */
/*
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * See the file CDDL.Schily.txt in this distribution for details.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file CDDL.Schily.txt from this distribution.
 */

#ifndef	_WALK_H
#define	_WALK_H
/*
 * Flags to control treewalk() via 'walkflags'.
 *
 *	WALK_CHDIR	is not implemented, treewalk() always does chdir()
 *
 *	WALK_PHYS	Clearing WALK_PHYS has highest precedence and equals
 *			'find -follow'. If WALK_PHYS is clear, always use stat.
 *			By default, WALK_PHYS should be set.
 *
 *	WALK_ARGFOLLOW	If WALK_ARGFOLLOW is set, symlinks that are used as
 *			the first argument for trewalk() are followed even
 *			if WALK_PHYS is set. Setting WALK_ARGFOLLOW equals
 *			'find -H'.
 *
 *	WALK_ALLFOLLOW	If WALK_ALLFOLLOW is set, all symlinks are followed
 *			even if WALK_PHYS is set. Setting WALK_ALLFOLLOW
 *			equals 'find -L'.
 */
#define	WALK_PHYS	1	/* Use lstat() instead of stat()	*/
#define	WALK_MOUNT	2	/* Do not cross mount points		*/
#define	WALK_DEPTH	4	/* Call content before calling the dir	*/
#define	WALK_CHDIR	8	/* Use chdir() to each directory	*/
#define	WALK_ARGFOLLOW	0x10	/* Use stat() for top level args only	*/
#define	WALK_ALLFOLLOW	0x20	/* Use stat() for all files		*/
#define	WALK_NOSTAT	0x40	/* Avoid to call stat() if st_nlink =>2 */
#define	WALK_NOEXIT	0x100	/* Do not exit() in case of hard errors	*/

/*
 * The 'type' argument to walkfun.
 */
#define	WALK_F		1	/* File	*/
#define	WALK_SL		2	/* Symbolic Link */
#define	WALK_D		3	/* Directory */
#define	WALK_DP		4	/* Directory previously visited */
#define	WALK_DNR	5	/* Directory with no read permission */
#define	WALK_NS		6	/* Unknown file type stat failed */
#define	WALK_SLN	7	/* Symbolic Link that points to nonexistent file */


struct WALK {
	int	flags;		/* Flags for communication with (*walkfun)() */
	int	base;		/* Filename offset in path for  (*walkfun)() */
	int	level;		/* The nesting level set up for (*walkfun)() */
	int	walkflags;	/* treewalk() control flags		    */
	int	maxdepth;	/* (*walkfun)() private, unused by treewalk  */
	int	mindepth;	/* (*walkfun)() private, unused by treewalk  */
	char	*lname;		/* (*walkfun)() private, unused by treewalk  */
	void	*tree;		/* (*walkfun)() private, unused by treewalk  */
	void	*patstate;	/* (*walkfun)() private, unused by treewalk  */
	int	err;		/* (*walkfun)() private, unused by treewalk  */
	int	pflags;		/* (*walkfun)() private, unused by treewalk  */
	int	auxi;		/* (*walkfun)() private, unused by treewalk  */
};

/*
 * Flags in struct WALK used to communicate with (*walkfun)()
 */
#define	WALK_WF_PRUNE	1	/* (*walkfun)() -> walk(): abort waking tree	*/
#define	WALK_WF_NOCHDIR	2	/* walk() -> (*walkfun)(): WALK_DNR is cannot chdir() */
#define	WALK_WF_NOCWD	4	/* walk() -> caller: cannot get working dir	*/
#define	WALK_WF_NOHOME	8	/* walk() -> caller: cannot chdir("..")		*/

typedef	int	(*walkfun)	__PR((char *_nm, struct stat *_fs, int _type, struct WALK *_state));

extern	int	treewalk	__PR((char *_nm, walkfun _fn, struct WALK *_state));
extern	int	walkhome	__PR((struct WALK *_state));
extern	int	walkclose	__PR((struct WALK *_state));

#endif	/* _WALK_H */
