aboutsummaryrefslogtreecommitdiff
path: root/bin/kiss-readlink.c
blob: 279e1b912e5ddd14ccc9d37dbec29275dc8a4c1c (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
// kiss-readlink --- a utility replacement for readlink
// See LICENSE for copyright information

// This is basically a 'readlink -f' command.
#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>
#include <string.h>

int main(int argc, char *argv[]) {

  char buf[512];
  char bname[512];
  strcpy(bname, "/");
  strcat(bname, (basename(argv[1])));

  if (argc != 2 || strcmp(argv[1], "--help") == 0) {
    printf("usage: %s <file>\n", argv[0]);
    return(1);
  }

  if (!realpath(argv[1], buf)) {

    if (!realpath(dirname(argv[1]), buf)) {
    perror("realpath");
    return(1);
    }
    strcat(buf, bname);
  }

  printf("%s\n", buf);
  return(0);
}