Skip to content

Instantly share code, notes, and snippets.

@mckern
Last active October 9, 2020 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mckern/3349ed18e7ff7e18b96e5a06942fbe11 to your computer and use it in GitHub Desktop.
Save mckern/3349ed18e7ff7e18b96e5a06942fbe11 to your computer and use it in GitHub Desktop.
realpath.c by Adam Liss, public domain with minor modifications by Ryan McKern

realpath.c

Introduction

An extremely simple CLI to POSIX stdlib realpath()

Building & Installation

$ curl -O -L \
  https://gist.githubusercontent.com/mckern/3349ed18e7ff7e18b96e5a06942fbe11/raw/d8a330fe0fb7b6ad56b867d8b91f2c954536ef41/realpath.c
$ make realpath
$ mv realpath /usr/local/bin/

Usage

$ realpath
usage: realpath PATH

License

This program is provided "as-is" to the public domain, without express or implied warranty, for any non-profit use, provided this notice is maintained.

// realpath.c: display the absolute path to a file or directory.
// Adam Liss, August, 2007
// Modified for basic help flags by Ryan McKern, Oct 2020
// This program is provided "as-is" to the public domain, without express or
// implied warranty, for any non-profit use, provided this notice is maintained.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <limits.h>
static char *s_pMyName;
void usage(int return_code);
int main(int argc, char *argv[])
{
char sPath[PATH_MAX];
s_pMyName = strdup(basename(argv[0]));
if (argc != 2)
usage(1);
if (strcmp(argv[1], "-h") == 0 ||
strcmp(argv[1], "--help") == 0)
usage(0);
printf("%s\n", realpath(argv[1], sPath));
return 0;
}
void usage(int return_code)
{
fprintf(stderr, "usage: %s PATH\n", s_pMyName);
exit(return_code);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment