| 1 | /* |
| 2 | * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License, version 2 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #define _GNU_SOURCE |
| 19 | #include <stdlib.h> |
| 20 | #include <ctype.h> |
| 21 | #include <limits.h> |
| 22 | |
| 23 | #include <common/error.h> |
| 24 | |
| 25 | #include "conf.h" |
| 26 | #include "utils.h" |
| 27 | |
| 28 | /* |
| 29 | * Return the realpath(3) of the path even if the last directory token does not |
| 30 | * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the |
| 31 | * /tmp/test1 does, the real path is returned. In normal time, realpath(3) |
| 32 | * fails if the end point directory does not exist. |
| 33 | */ |
| 34 | char *expand_full_path(const char *path) |
| 35 | { |
| 36 | const char *end_path = path; |
| 37 | char *next, *cut_path, *expanded_path; |
| 38 | |
| 39 | /* Find last token delimited by '/' */ |
| 40 | while ((next = strpbrk(end_path + 1, "/"))) { |
| 41 | end_path = next; |
| 42 | } |
| 43 | |
| 44 | /* Cut last token from original path */ |
| 45 | cut_path = strndup(path, end_path - path); |
| 46 | |
| 47 | expanded_path = malloc(PATH_MAX); |
| 48 | if (expanded_path == NULL) { |
| 49 | goto error; |
| 50 | } |
| 51 | |
| 52 | expanded_path = realpath((char *)cut_path, expanded_path); |
| 53 | if (expanded_path == NULL) { |
| 54 | switch (errno) { |
| 55 | case ENOENT: |
| 56 | ERR("%s: No such file or directory", cut_path); |
| 57 | break; |
| 58 | default: |
| 59 | perror("realpath"); |
| 60 | break; |
| 61 | } |
| 62 | goto error; |
| 63 | } |
| 64 | |
| 65 | /* Add end part to expanded path */ |
| 66 | strcat(expanded_path, end_path); |
| 67 | |
| 68 | free(cut_path); |
| 69 | return expanded_path; |
| 70 | |
| 71 | error: |
| 72 | free(cut_path); |
| 73 | return NULL; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * get_session_name |
| 78 | * |
| 79 | * Return allocated string with the session name found in the config |
| 80 | * directory. |
| 81 | */ |
| 82 | char *get_session_name(void) |
| 83 | { |
| 84 | char *path, *session_name = NULL; |
| 85 | |
| 86 | /* Get path to config file */ |
| 87 | path = config_get_default_path(); |
| 88 | if (path == NULL) { |
| 89 | goto error; |
| 90 | } |
| 91 | |
| 92 | /* Get session name from config */ |
| 93 | session_name = config_read_session_name(path); |
| 94 | if (session_name == NULL) { |
| 95 | goto error; |
| 96 | } |
| 97 | |
| 98 | DBG2("Config file path found: %s", path); |
| 99 | DBG("Session name found: %s", session_name); |
| 100 | return session_name; |
| 101 | |
| 102 | error: |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /* |
| 108 | * list_cmd_options |
| 109 | * |
| 110 | * Prints a simple list of the options available to a command. This is intended |
| 111 | * to be easily parsed for bash completion. |
| 112 | */ |
| 113 | void list_cmd_options(FILE *ofp, struct poptOption *options) |
| 114 | { |
| 115 | int i; |
| 116 | struct poptOption *option = NULL; |
| 117 | |
| 118 | for (i = 0; options[i].longName != NULL; i++) { |
| 119 | option = &options[i]; |
| 120 | |
| 121 | fprintf(ofp, "--%s\n", option->longName); |
| 122 | |
| 123 | if (isprint(option->shortName)) { |
| 124 | fprintf(ofp, "-%c\n", option->shortName); |
| 125 | } |
| 126 | } |
| 127 | } |