am 2f9c5fd4: Merge "Add standard options to readlink" into klp-dev

* commit '2f9c5fd4b9676c59839c45bf4877551188dbc22a':
  Add standard options to readlink
This commit is contained in:
Michael Wright 2013-08-26 15:03:07 -07:00 committed by Android Git Automerger
commit 2c8fa5eecf
1 changed files with 44 additions and 8 deletions

View File

@ -34,26 +34,62 @@
#include <stdlib.h>
#include <unistd.h>
static int skip_newline, quiet_errors, canonicalize;
static void usage(char* name) {
fprintf(stderr, "Usage: %s FILE\n", name);
fprintf(stderr, "Usage: %s [OPTION]... FILE\n", name);
}
int readlink_main(int argc, char* argv[]) {
if (argc != 2) {
int c;
while ((c = getopt(argc, argv, "nfqs")) != -1) {
switch (c) {
case 'n':
skip_newline = 1;
break;
case 'f':
canonicalize = 1;
break;
case 'q':
case 's':
quiet_errors = 1;
break;
case '?':
default:
usage(argv[0]);
return EXIT_FAILURE;
}
}
int index = optind;
if (argc - index != 1) {
usage(argv[0]);
return EXIT_FAILURE;
}
char name[PATH_MAX+1];
ssize_t len = readlink(argv[1], name, PATH_MAX);
if (canonicalize) {
if(!realpath(argv[optind], name)) {
if (!quiet_errors) {
perror("readlink");
}
return EXIT_FAILURE;
}
} else {
ssize_t len = readlink(argv[1], name, PATH_MAX);
if (len < 0) {
perror("readlink");
return EXIT_FAILURE;
if (len < 0) {
if (!quiet_errors) {
perror("readlink");
}
return EXIT_FAILURE;
}
name[len] = '\0';
}
name[len] = '\0';
printf("%s\n", name);
fputs(name, stdout);
if (!skip_newline) {
fputs("\n", stdout);
}
return EXIT_SUCCESS;
}