8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libpkg / common / canonize.c
blobff34c40507c014bc54163553bf9ebea80eb6f28b
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #include <string.h>
33 #define isdot(x) ((x[0] == '.') && (!x[1] || (x[1] == '/')))
34 #define isdotdot(x) ((x[0] == '.') && (x[1] == '.') && \
35 (!x[2] || (x[2] == '/')))
37 void
38 canonize(char *file)
40 char *pt, *last;
41 int level;
43 /* remove references such as "./" and "../" and "//" */
44 for (pt = file; *pt; /* void */) {
45 if (isdot(pt))
46 (void) strcpy(pt, pt[1] ? pt+2 : pt+1);
47 else if (isdotdot(pt)) {
48 level = 0;
49 last = pt;
50 do {
51 level++;
52 last += 2;
53 if (*last)
54 last++;
55 } while (isdotdot(last));
56 --pt; /* point to previous '/' */
57 while (level--) {
58 if (pt <= file)
59 return;
60 while ((*--pt != '/') && (pt > file))
63 if (*pt == '/')
64 pt++;
65 (void) strcpy(pt, last);
66 } else {
67 while (*pt && (*pt != '/'))
68 pt++;
69 if (*pt == '/') {
70 while (pt[1] == '/')
71 (void) strcpy(pt, pt+1);
72 pt++;
76 if ((--pt > file) && (*pt == '/'))
77 *pt = '\0';
80 void
81 canonize_slashes(char *file)
83 char *pt;
85 /* remove references such as "//" */
86 for (pt = file; *pt; /* void */) {
87 while (*pt && (*pt != '/'))
88 pt++;
89 if (*pt == '/') {
90 while (pt[1] == '/')
91 (void) strcpy(pt, pt+1);
92 pt++;
95 if ((--pt > file) && (*pt == '/'))
96 *pt = '\0';