/*     
 * $RCSfile: dirname.c,v $
 *
 * x-kernel v3.3
 *
 * Copyright (c) 1993,1991,1990,1996  Arizona Board of Regents
 *
 * $Log: dirname.c,v $
 * Revision 1.2  1996/01/27 00:04:58  slm
 * Updated copyright and version.
 *
 * Revision 1.1  1995/07/29  03:06:35  slm
 * Initial revision
 *
 * Revision 1.4.1.1  1994/11/14  15:42:17  hkaram
 * New branch
 *
 * Revision 1.4  1994/04/20  23:03:40  gordon
 * replaced rindex with strrchr for Solaris
 *
 * Revision 1.3  1993/12/13  18:50:44  menze
 * Modifications from UMass:
 *
 *   [ 93/06/09          nahum ]
 *   Added fixes to get utils to build under IRIX
 */


#include <stdio.h>

#ifdef X_SOLARIS
#include <string.h>
#endif /* X_SOLARIS */

extern	char *	rindex(
#ifdef __STDC__
		       char *, char
#endif		       
		       );

/* 
 * Not all platforms support the 'dirname' utility, so we provide this one. 
 */


void
main( argc, argv )
    int		argc;
    char	**argv;
{
    char	*str;
    char	*lastSlash;

    str = (argc > 1) ? argv[1] : "";
    
#ifdef X_SOLARIS
    lastSlash = strrchr(str, '/');
#else
    lastSlash = rindex(str, '/');
#endif /* X_SOLARIS */

    if ( lastSlash == 0 ) {
	/* 
	 * No '/' in the pathname.
	 */
	str = ".";
    } else if ( lastSlash == str ) {
	/* 
	 * File in root directory '/'
	 */
	str = "/";
    } else {
	/* 
	 * Print everything up to the last slash
	 */
	*lastSlash = 0;
    }
    printf("%s\n", str);
}
  
