coreutils

General Software Utilities
git clone http://git.omkov.net/coreutils
Log | Tree | Refs | README | LICENCE | Download

coreutils/src/realpath.c (66 lines, 1.5 KiB) -rw-r--r-- blame download

01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
// realpath.c, version 1.0.1
// OMKOV coreutils realpath
// Copyright (C) 2020, Jakob Wakeling
// MIT Licence

#include "util/error.h"
#include "util/optget.h"

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#define VERSION "1.0.1"

static struct lop lops[] = {
	{ "help",    ARG_NUL, 256 },
	{ "version", ARG_NUL, 257 },
	{ NULL, 0, 0 }
};

static inline int rpath(const char *path);

static void hlp(void);
static void ver(void);

int main(int ac, char *av[]) { A0 = av[0];
	struct opt opt = OPTGET_INIT; opt.str = ""; opt.lops = lops;
	for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) {
	case 256: { hlp(); return 0; }
	case 257: { ver(); return 0; }
	default: { return 1; }
	}
	
	bool warned = false;
	
	if (opt.ind == ac) { rpath("."); }
	else for (char **p = &av[opt.ind]; *p; ++p) if (rpath(*p)) {
		warn("%s: %s\n", *p, serr()); warned = true;
	}
	
	return warned;
}

static inline int rpath(const char *file) {
	char *path;
	
	if (!(path = realpath(file, NULL))) { return 1; }
	fputs(path, stdout); fputc('\n', stdout);
	
	free(path); return 0;
}

static void hlp(void) {
	puts("realpath - resolve an absolute pathname\n");
	puts("usage: realpath [file...]\n");
	puts("options:");
	puts("  --help     Display help information");
	puts("  --version  Display version information");
}

static void ver() {
	puts("OMKOV coreutils realpath, version " VERSION);
	puts("Copyright (C) 2020, Jakob Wakeling");
	puts("MIT Licence (https://opensource.org/licenses/MIT)");
}