aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/xcode/realpath/realpath.c
blob: 06addf38c7e5e379ba5e56484d4b6e87f8788849 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2015 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//
//  realpath.c
//
//  A simple implementation of realpath for Mac OS X.
//  This implementation follows gnu/linux conventions and allows the last
//  component to not exist:
//  http://www.gnu.org/software/coreutils/manual/html_node/realpath-invocation.html
//  Debian requires all components to exist.
//

#include <errno.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

// Print a simple error message and exit.
static void PrintError(const char *argv[]) {
  fprintf(stderr, "%s: %s\n", argv[1], strerror(errno));
  exit(1);
}

// Concatenate two paths together adding a '/' if appropriate.
// Returned pointer is owned by client and should be freed.
static char *JoinPaths(const char *path1, const char* path2) {
  size_t len1 = strlen(path1);
  size_t len2 = strlen(path2);
  // +1 for '/' and +1 for '\0'
  size_t totalSize = len1 + 1 + len2 + 1;
  char *outPath = malloc(totalSize);
  if (outPath == NULL) {
    return NULL;
  }
  strlcpy(outPath, path1, totalSize);
  if (len1 > 0 && len2 > 0) {
    if (path1[len1 - 1] != '/' && path2[0] != '/') {
      strlcat(outPath, "/", totalSize);
    }
  }
  strlcat(outPath, path2, totalSize);
  return outPath;
}

// Since this is a simple utility that quits immediately, we are not worrying
// about making the code more complex by freeing up any memory allocations.
int main(int argc, const char *argv[]) {
  if (argc != 2) {
    fprintf(stderr, "realpath <path>\n");
    return 1;
  }
  const char *path = argv[1];
  char *goodPath = realpath(path, NULL);
  if (goodPath == NULL) {
    if ((errno != ENOENT) || (strlen(path) == 0)) {
      PrintError(argv);
    }

    // If only the last element is missing, then call realpath on the parent
    // dir and append the basename back onto it.

    // Technically the strdup is not required on Mac OS X, but this
    // keeps things compatible with other basename/dirname implementations
    // that do require a string they can modify.
    char *dirCopy = strdup(path);
    char *baseCopy = strdup(path);
    if (dirCopy == NULL || baseCopy == NULL) {
      PrintError(argv);
    }
    char *dir = dirname(dirCopy);
    if (dir == NULL) {
      PrintError(argv);
    }
    char *base = basename(baseCopy);
    if (base == NULL) {
      PrintError(argv);
    }
    char *realdir = realpath(dir, NULL);
    if (realdir == NULL) {
      PrintError(argv);
    }
    goodPath = JoinPaths(realdir, base);
    if (goodPath == NULL) {
      PrintError(argv);
    }
  }
  fprintf(stdout, "%s\n", goodPath);
  return 0;
}