#include "common.h"
#include <getopt.h>
#include <error.h>
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
static void __attribute__ ((noreturn)) die_bad_usage()
{
fprintf(stderr, "Try `%s --help' for more information.\n", program_invocation_name);
exit(EXIT_FAILURE);
}
static void print_help()
{
printf("Usage: %s [OPTION]\n", program_invocation_name);
puts("Print the full filename of the current working directory.");
putchar('\n');
puts(" -h, --help display this help and exit");
}
int main(int argc, char **argv)
{
static const struct option longopts[] = {
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
};
int c;
char buf[PATH_MAX];
atexit(close_stdout);
while ((c = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
switch (c) {
case 'h':
print_help();
return EXIT_SUCCESS;
case '?':
die_bad_usage();
}
if (optind < argc)
error(0, 0, "ignoring non-option arguments");
if (!getcwd(buf, PATH_MAX))
error(EXIT_FAILURE, errno, "getcwd");
puts(buf);
return EXIT_SUCCESS;
}