1*55381082Smuffin /* 2*55381082Smuffin * CDDL HEADER START 3*55381082Smuffin * 4*55381082Smuffin * The contents of this file are subject to the terms of the 5*55381082Smuffin * Common Development and Distribution License, Version 1.0 only 6*55381082Smuffin * (the "License"). You may not use this file except in compliance 7*55381082Smuffin * with the License. 8*55381082Smuffin * 9*55381082Smuffin * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*55381082Smuffin * or http://www.opensolaris.org/os/licensing. 11*55381082Smuffin * See the License for the specific language governing permissions 12*55381082Smuffin * and limitations under the License. 13*55381082Smuffin * 14*55381082Smuffin * When distributing Covered Code, include this CDDL HEADER in each 15*55381082Smuffin * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*55381082Smuffin * If applicable, add the following below this CDDL HEADER, with the 17*55381082Smuffin * fields enclosed by brackets "[]" replaced with your own identifying 18*55381082Smuffin * information: Portions Copyright [yyyy] [name of copyright owner] 19*55381082Smuffin * 20*55381082Smuffin * CDDL HEADER END 21*55381082Smuffin */ 22*55381082Smuffin /* 23*55381082Smuffin * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*55381082Smuffin * Use is subject to license terms. 25*55381082Smuffin */ 26*55381082Smuffin 27*55381082Smuffin #pragma ident "%Z%%M% %I% %E% SMI" 28*55381082Smuffin 29*55381082Smuffin #include <locale.h> 30*55381082Smuffin #include <stdio.h> 31*55381082Smuffin #include <stdlib.h> 32*55381082Smuffin #include <string.h> 33*55381082Smuffin #include <unistd.h> 34*55381082Smuffin #include <libintl.h> 35*55381082Smuffin 36*55381082Smuffin int 37*55381082Smuffin main(int argc, char **argv) 38*55381082Smuffin { 39*55381082Smuffin char *p; 40*55381082Smuffin char *string; 41*55381082Smuffin 42*55381082Smuffin /* 43*55381082Smuffin * For better performance, defer the setlocale()/textdomain() 44*55381082Smuffin * calls until they get really required. 45*55381082Smuffin */ 46*55381082Smuffin #if !defined(TEXT_DOMAIN) 47*55381082Smuffin #define TEXT_DOMAIN "SYS_TEST" 48*55381082Smuffin #endif 49*55381082Smuffin if (argc == 1) { 50*55381082Smuffin (void) puts("."); 51*55381082Smuffin return (0); 52*55381082Smuffin } 53*55381082Smuffin if (strcmp(argv[1], "--") == 0) { 54*55381082Smuffin argv++; 55*55381082Smuffin argc--; 56*55381082Smuffin if (argc == 1) { 57*55381082Smuffin (void) puts("."); 58*55381082Smuffin return (0); 59*55381082Smuffin } 60*55381082Smuffin } 61*55381082Smuffin if (argc > 2) { 62*55381082Smuffin (void) setlocale(LC_ALL, ""); 63*55381082Smuffin (void) textdomain(TEXT_DOMAIN); 64*55381082Smuffin (void) fprintf(stderr, gettext("Usage: dirname [ path ]\n")); 65*55381082Smuffin return (1); 66*55381082Smuffin } 67*55381082Smuffin 68*55381082Smuffin string = argv[1]; 69*55381082Smuffin 70*55381082Smuffin if (*string == '\0') { 71*55381082Smuffin (void) puts("."); 72*55381082Smuffin return (0); 73*55381082Smuffin } 74*55381082Smuffin 75*55381082Smuffin /* remove trailing slashes */ 76*55381082Smuffin p = string + strlen(string) - 1; 77*55381082Smuffin while (p >= string && *p == '/') 78*55381082Smuffin *p-- = '\0'; 79*55381082Smuffin 80*55381082Smuffin if (*string == '\0') { 81*55381082Smuffin /* string contained only slashes */ 82*55381082Smuffin (void) puts("/"); 83*55381082Smuffin return (0); 84*55381082Smuffin } 85*55381082Smuffin 86*55381082Smuffin /* remove non-slashes */ 87*55381082Smuffin while (p >= string && *p != '/') 88*55381082Smuffin *p-- = '\0'; 89*55381082Smuffin 90*55381082Smuffin if (*string == '\0') { 91*55381082Smuffin /* string did not begin with a slash */ 92*55381082Smuffin (void) puts("."); 93*55381082Smuffin return (0); 94*55381082Smuffin } 95*55381082Smuffin 96*55381082Smuffin /* remove slashes delimiting dirname and basename */ 97*55381082Smuffin while (p >= string && *p == '/') 98*55381082Smuffin *p-- = '\0'; 99*55381082Smuffin 100*55381082Smuffin if (*string == '\0') { 101*55381082Smuffin /* no dirname part found */ 102*55381082Smuffin (void) puts("/"); 103*55381082Smuffin return (0); 104*55381082Smuffin } 105*55381082Smuffin /* now string points to dirname part */ 106*55381082Smuffin (void) puts(string); 107*55381082Smuffin return (0); 108*55381082Smuffin } 109