| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #include "common.h" |
| |
| #include <ctype.h> |
| #include <stdlib.h> |
| |
| #include "find_home.h" |
| |
| enum mode { current = 42, by_user, tilde }; |
| |
| |
| |
| |
| |
| |
| int main(int argc, char **argv) |
| { |
| const char *h; |
| char *tofree = NULL; |
| int read_env = 0; |
| enum mode mode = current; |
| |
| if (argc >= 2) { |
| if (!isdigit((unsigned char)argv[1][0])) { |
| if (argv[1][0] == '~') { |
| mode = tilde; |
| } else { |
| mode = by_user; |
| } |
| } else { |
| read_env = atoi(argv[1]); |
| } |
| } |
| |
| switch (mode) { |
| case by_user: |
| h = find_home_user(argv[1]); |
| break; |
| case tilde: |
| h = tofree = tildeexpand(argv[1]); |
| break; |
| case current: |
| h = find_home(read_env); |
| break; |
| default: |
| abort(); |
| } |
| |
| if (h != NULL) { |
| (void)puts(h); |
| } else { |
| perror(argv[0]); |
| } |
| if (tofree) |
| free(tofree); |
| |
| exit(h ? EXIT_SUCCESS : EX_ERROR); |
| } |