blob: 5d39d835f7198e7ba70252fbaac468aac8dbbdb5 [file] [log] [blame]
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "linker_utils.h"
18#include "linker_debug.h"
19
20bool normalize_path(const char* path, std::string* normalized_path) {
21 // Input should be an absolute path
22 if (path[0] != '/') {
23 PRINT("canonize_path - invalid input: '%s', the input path should be absolute", path);
24 return false;
25 }
26
27 const size_t len = strlen(path) + 1;
28 char buf[len];
29
30 const char* in_ptr = path;
31 char* out_ptr = buf;
32
33 while (*in_ptr != 0) {
34 if (*in_ptr == '/') {
35 char c1 = in_ptr[1];
36 if (c1 == '.') {
37 char c2 = in_ptr[2];
38 if (c2 == '/') {
39 in_ptr += 2;
40 continue;
41 } else if (c2 == '.' && (in_ptr[3] == '/' || in_ptr[3] == 0)) {
42 in_ptr += 3;
43 while (out_ptr > buf && *--out_ptr != '/') {
44 }
45 if (in_ptr[0] == 0) {
46 // retain '/'
47 out_ptr++;
48 }
49 continue;
50 }
51 } else if (c1 == '/') {
52 ++in_ptr;
53 continue;
54 }
55 }
56 *out_ptr++ = *in_ptr++;
57 }
58
59 *out_ptr = 0;
60 *normalized_path = buf;
61 return true;
62}
63