| 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 | * get_session_name |
| 30 | * |
| 31 | * Return allocated string with the session name found in the config |
| 32 | * directory. |
| 33 | */ |
| 34 | char *get_session_name(void) |
| 35 | { |
| 36 | char *path, *session_name = NULL; |
| 37 | |
| 38 | /* Get path to config file */ |
| 39 | path = config_get_default_path(); |
| 40 | if (path == NULL) { |
| 41 | goto error; |
| 42 | } |
| 43 | |
| 44 | /* Get session name from config */ |
| 45 | session_name = config_read_session_name(path); |
| 46 | if (session_name == NULL) { |
| 47 | goto error; |
| 48 | } |
| 49 | |
| 50 | DBG2("Config file path found: %s", path); |
| 51 | DBG("Session name found: %s", session_name); |
| 52 | return session_name; |
| 53 | |
| 54 | error: |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | /* |
| 60 | * list_cmd_options |
| 61 | * |
| 62 | * Prints a simple list of the options available to a command. This is intended |
| 63 | * to be easily parsed for bash completion. |
| 64 | */ |
| 65 | void list_cmd_options(FILE *ofp, struct poptOption *options) |
| 66 | { |
| 67 | int i; |
| 68 | struct poptOption *option = NULL; |
| 69 | |
| 70 | for (i = 0; options[i].longName != NULL; i++) { |
| 71 | option = &options[i]; |
| 72 | |
| 73 | fprintf(ofp, "--%s\n", option->longName); |
| 74 | |
| 75 | if (isprint(option->shortName)) { |
| 76 | fprintf(ofp, "-%c\n", option->shortName); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * fls: returns the position of the most significant bit. |
| 83 | * Returns 0 if no bit is set, else returns the position of the most |
| 84 | * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit). |
| 85 | */ |
| 86 | #if defined(__i386) || defined(__x86_64) |
| 87 | static inline |
| 88 | unsigned int fls_u32(uint32_t x) |
| 89 | { |
| 90 | int r; |
| 91 | |
| 92 | asm("bsrl %1,%0\n\t" |
| 93 | "jnz 1f\n\t" |
| 94 | "movl $-1,%0\n\t" |
| 95 | "1:\n\t" |
| 96 | : "=r" (r) : "rm" (x)); |
| 97 | return r + 1; |
| 98 | } |
| 99 | #define HAS_FLS_U32 |
| 100 | #endif |
| 101 | |
| 102 | #if defined(__x86_64) |
| 103 | static inline |
| 104 | unsigned int fls_u64(uint64_t x) |
| 105 | { |
| 106 | long r; |
| 107 | |
| 108 | asm("bsrq %1,%0\n\t" |
| 109 | "jnz 1f\n\t" |
| 110 | "movq $-1,%0\n\t" |
| 111 | "1:\n\t" |
| 112 | : "=r" (r) : "rm" (x)); |
| 113 | return r + 1; |
| 114 | } |
| 115 | #define HAS_FLS_U64 |
| 116 | #endif |
| 117 | |
| 118 | #ifndef HAS_FLS_U64 |
| 119 | static __attribute__((unused)) |
| 120 | unsigned int fls_u64(uint64_t x) |
| 121 | { |
| 122 | unsigned int r = 64; |
| 123 | |
| 124 | if (!x) |
| 125 | return 0; |
| 126 | |
| 127 | if (!(x & 0xFFFFFFFF00000000ULL)) { |
| 128 | x <<= 32; |
| 129 | r -= 32; |
| 130 | } |
| 131 | if (!(x & 0xFFFF000000000000ULL)) { |
| 132 | x <<= 16; |
| 133 | r -= 16; |
| 134 | } |
| 135 | if (!(x & 0xFF00000000000000ULL)) { |
| 136 | x <<= 8; |
| 137 | r -= 8; |
| 138 | } |
| 139 | if (!(x & 0xF000000000000000ULL)) { |
| 140 | x <<= 4; |
| 141 | r -= 4; |
| 142 | } |
| 143 | if (!(x & 0xC000000000000000ULL)) { |
| 144 | x <<= 2; |
| 145 | r -= 2; |
| 146 | } |
| 147 | if (!(x & 0x8000000000000000ULL)) { |
| 148 | x <<= 1; |
| 149 | r -= 1; |
| 150 | } |
| 151 | return r; |
| 152 | } |
| 153 | #endif |
| 154 | |
| 155 | #ifndef HAS_FLS_U32 |
| 156 | static __attribute__((unused)) |
| 157 | unsigned int fls_u32(uint32_t x) |
| 158 | { |
| 159 | unsigned int r = 32; |
| 160 | |
| 161 | if (!x) |
| 162 | return 0; |
| 163 | if (!(x & 0xFFFF0000U)) { |
| 164 | x <<= 16; |
| 165 | r -= 16; |
| 166 | } |
| 167 | if (!(x & 0xFF000000U)) { |
| 168 | x <<= 8; |
| 169 | r -= 8; |
| 170 | } |
| 171 | if (!(x & 0xF0000000U)) { |
| 172 | x <<= 4; |
| 173 | r -= 4; |
| 174 | } |
| 175 | if (!(x & 0xC0000000U)) { |
| 176 | x <<= 2; |
| 177 | r -= 2; |
| 178 | } |
| 179 | if (!(x & 0x80000000U)) { |
| 180 | x <<= 1; |
| 181 | r -= 1; |
| 182 | } |
| 183 | return r; |
| 184 | } |
| 185 | #endif |
| 186 | |
| 187 | static |
| 188 | unsigned int fls_ulong(unsigned long x) |
| 189 | { |
| 190 | #if (CAA_BITS_PER_LONG == 32) |
| 191 | return fls_u32(x); |
| 192 | #else |
| 193 | return fls_u64(x); |
| 194 | #endif |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Return the minimum order for which x <= (1UL << order). |
| 199 | * Return -1 if x is 0. |
| 200 | */ |
| 201 | int get_count_order_u32(uint32_t x) |
| 202 | { |
| 203 | if (!x) |
| 204 | return -1; |
| 205 | |
| 206 | return fls_u32(x - 1); |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * Return the minimum order for which x <= (1UL << order). |
| 211 | * Return -1 if x is 0. |
| 212 | */ |
| 213 | int get_count_order_u64(uint64_t x) |
| 214 | { |
| 215 | if (!x) |
| 216 | return -1; |
| 217 | |
| 218 | return fls_u64(x - 1); |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Return the minimum order for which x <= (1UL << order). |
| 223 | * Return -1 if x is 0. |
| 224 | */ |
| 225 | int get_count_order_ulong(unsigned long x) |
| 226 | { |
| 227 | if (!x) |
| 228 | return -1; |
| 229 | |
| 230 | return fls_ulong(x - 1); |
| 231 | } |