2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
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.
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.
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.
23 #include <common/error.h>
24 #include <common/utils.h>
33 * Return allocated string with the session name found in the config
36 char *get_session_name(void)
38 char *path
, *session_name
= NULL
;
40 /* Get path to config file */
41 path
= utils_get_home_dir();
46 /* Get session name from config */
47 session_name
= config_read_session_name(path
);
48 if (session_name
== NULL
) {
52 DBG2("Config file path found: %s", path
);
53 DBG("Session name found: %s", session_name
);
63 * List commands line by line. This is mostly for bash auto completion and to
64 * avoid difficult parsing.
66 void list_commands(struct cmd_struct
*commands
, FILE *ofp
)
69 struct cmd_struct
*cmd
= NULL
;
72 while (cmd
->name
!= NULL
) {
73 fprintf(ofp
, "%s\n", cmd
->name
);
82 * Prints a simple list of the options available to a command. This is intended
83 * to be easily parsed for bash completion.
85 void list_cmd_options(FILE *ofp
, struct poptOption
*options
)
88 struct poptOption
*option
= NULL
;
90 for (i
= 0; options
[i
].longName
!= NULL
; i
++) {
93 fprintf(ofp
, "--%s\n", option
->longName
);
95 if (isprint(option
->shortName
)) {
96 fprintf(ofp
, "-%c\n", option
->shortName
);
102 * fls: returns the position of the most significant bit.
103 * Returns 0 if no bit is set, else returns the position of the most
104 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
106 #if defined(__i386) || defined(__x86_64)
108 unsigned int fls_u32(uint32_t x
)
116 : "=r" (r
) : "rm" (x
));
122 #if defined(__x86_64)
124 unsigned int fls_u64(uint64_t x
)
132 : "=r" (r
) : "rm" (x
));
139 static __attribute__((unused
))
140 unsigned int fls_u64(uint64_t x
)
147 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
151 if (!(x
& 0xFFFF000000000000ULL
)) {
155 if (!(x
& 0xFF00000000000000ULL
)) {
159 if (!(x
& 0xF000000000000000ULL
)) {
163 if (!(x
& 0xC000000000000000ULL
)) {
167 if (!(x
& 0x8000000000000000ULL
)) {
176 static __attribute__((unused
))
177 unsigned int fls_u32(uint32_t x
)
183 if (!(x
& 0xFFFF0000U
)) {
187 if (!(x
& 0xFF000000U
)) {
191 if (!(x
& 0xF0000000U
)) {
195 if (!(x
& 0xC0000000U
)) {
199 if (!(x
& 0x80000000U
)) {
208 unsigned int fls_ulong(unsigned long x
)
210 #if (CAA_BITS_PER_LONG == 32)
218 * Return the minimum order for which x <= (1UL << order).
219 * Return -1 if x is 0.
221 int get_count_order_u32(uint32_t x
)
226 return fls_u32(x
- 1);
230 * Return the minimum order for which x <= (1UL << order).
231 * Return -1 if x is 0.
233 int get_count_order_u64(uint64_t x
)
238 return fls_u64(x
- 1);
242 * Return the minimum order for which x <= (1UL << order).
243 * Return -1 if x is 0.
245 int get_count_order_ulong(unsigned long x
)
250 return fls_ulong(x
- 1);
This page took 0.036476 seconds and 4 git commands to generate.