dirname.c revision 1591
1238384Sjkim/*-
2238384Sjkim * Copyright (c) 1991, 1993, 1994
3238384Sjkim *	The Regents of the University of California.  All rights reserved.
4238384Sjkim *
5238384Sjkim * Redistribution and use in source and binary forms, with or without
6238384Sjkim * modification, are permitted provided that the following conditions
7238384Sjkim * are met:
8238384Sjkim * 1. Redistributions of source code must retain the above copyright
9238384Sjkim *    notice, this list of conditions and the following disclaimer.
10238384Sjkim * 2. Redistributions in binary form must reproduce the above copyright
11238384Sjkim *    notice, this list of conditions and the following disclaimer in the
12238384Sjkim *    documentation and/or other materials provided with the distribution.
13238384Sjkim * 3. All advertising materials mentioning features or use of this software
14238384Sjkim *    must display the following acknowledgement:
15238384Sjkim *	This product includes software developed by the University of
16238384Sjkim *	California, Berkeley and its contributors.
17238384Sjkim * 4. Neither the name of the University nor the names of its contributors
18238384Sjkim *    may be used to endorse or promote products derived from this software
19238384Sjkim *    without specific prior written permission.
20238384Sjkim *
21238384Sjkim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22238384Sjkim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23238384Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24238384Sjkim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25238384Sjkim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26238384Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27238384Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28238384Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29238384Sjkim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30238384Sjkim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31238384Sjkim * SUCH DAMAGE.
32238384Sjkim */
33238384Sjkim
34238384Sjkim#ifndef lint
35238384Sjkimstatic char copyright[] =
36238384Sjkim"@(#) Copyright (c) 1991, 1993, 1994\n\
37238384Sjkim	The Regents of the University of California.  All rights reserved.\n";
38238384Sjkim#endif /* not lint */
39238384Sjkim
40238384Sjkim#ifndef lint
41238384Sjkimstatic char sccsid[] = "@(#)dirname.c	8.3 (Berkeley) 4/2/94";
42238384Sjkim#endif /* not lint */
43238384Sjkim
44238384Sjkim#include <stdio.h>
45238384Sjkim#include <stdlib.h>
46238384Sjkim
47238384Sjkimvoid usage __P((void));
48238384Sjkim
49238384Sjkimint
50238384Sjkimmain(argc, argv)
51238384Sjkim	int argc;
52238384Sjkim	char **argv;
53238384Sjkim{
54238384Sjkim	char *p;
55238384Sjkim	int ch;
56238384Sjkim
57238384Sjkim	while ((ch = getopt(argc, argv, "")) != EOF)
58238384Sjkim		switch(ch) {
59238384Sjkim		case '?':
60238384Sjkim		default:
61238384Sjkim			usage();
62238384Sjkim		}
63238384Sjkim	argc -= optind;
64238384Sjkim	argv += optind;
65238384Sjkim
66238384Sjkim	if (argc != 1)
67238384Sjkim		usage();
68238384Sjkim
69238384Sjkim	/*
70238384Sjkim	 * (1) If string is //, skip steps (2) through (5).
71238384Sjkim	 * (2) If string consists entirely of slash characters, string
72238384Sjkim	 *     shall be set to a single slash character.  In this case,
73238384Sjkim	 *     skip steps (3) through (8).
74238384Sjkim	 */
75238384Sjkim	for (p = *argv;; ++p) {
76238384Sjkim		if (!*p) {
77238384Sjkim			if (p > *argv)
78238384Sjkim				(void)printf("/\n");
79238384Sjkim			else
80238384Sjkim				(void)printf(".\n");
81238384Sjkim			exit(0);
82238384Sjkim		}
83238384Sjkim		if (*p != '/')
84238384Sjkim			break;
85238384Sjkim	}
86238384Sjkim
87238384Sjkim	/*
88238384Sjkim	 * (3) If there are any trailing slash characters in string, they
89238384Sjkim	 *     shall be removed.
90238384Sjkim	 */
91238384Sjkim	for (; *p; ++p);
92238384Sjkim	while (*--p == '/')
93238384Sjkim		continue;
94238384Sjkim	*++p = '\0';
95238384Sjkim
96238384Sjkim	/*
97238384Sjkim	 * (4) If there are no slash characters remaining in string,
98238384Sjkim	 *     string shall be set to a single period character.  In this
99238384Sjkim	 *     case skip steps (5) through (8).
100238384Sjkim	 *
101238384Sjkim	 * (5) If there are any trailing nonslash characters in string,
102238384Sjkim	 *     they shall be removed.
103238384Sjkim	 */
104238384Sjkim	while (--p >= *argv)
105238384Sjkim		if (*p == '/')
106238384Sjkim			break;
107238384Sjkim	++p;
108238384Sjkim	if (p == *argv) {
109238384Sjkim		(void)printf(".\n");
110238384Sjkim		exit(0);
111238384Sjkim	}
112238384Sjkim
113238384Sjkim	/*
114238384Sjkim	 * (6) If the remaining string is //, it is implementation defined
115238384Sjkim	 *     whether steps (7) and (8) are skipped or processed.
116238384Sjkim	 *
117238384Sjkim	 * This case has already been handled, as part of steps (1) and (2).
118238384Sjkim	 */
119238384Sjkim
120238384Sjkim	/*
121238384Sjkim	 * (7) If there are any trailing slash characters in string, they
122238384Sjkim	 *     shall be removed.
123238384Sjkim	 */
124238384Sjkim	while (--p >= *argv)
125238384Sjkim		if (*p != '/')
126238384Sjkim			break;
127238384Sjkim	++p;
128238384Sjkim
129238384Sjkim	/*
130238384Sjkim	 * (8) If the remaining string is empty, string shall be set to
131238384Sjkim	 *     a single slash character.
132238384Sjkim	 */
133238384Sjkim	*p = '\0';
134238384Sjkim	(void)printf("%s\n", p == *argv ? "/" : *argv);
135238384Sjkim	exit(0);
136238384Sjkim}
137238384Sjkim
138238384Sjkimvoid
139238384Sjkimusage()
140238384Sjkim{
141238384Sjkim
142238384Sjkim	(void)fprintf(stderr, "usage: dirname path\n");
143238384Sjkim	exit(1);
144238384Sjkim}
145238384Sjkim