]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxcmd/input.c
2 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "platform_defs.h"
24 #if defined(ENABLE_READLINE)
25 # include <readline/history.h>
26 # include <readline/readline.h>
27 #elif defined(ENABLE_EDITLINE)
28 # include <histedit.h>
31 extern char *progname
;
36 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
39 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
43 #if defined(ENABLE_READLINE)
49 line
= readline(get_prompt());
54 #elif defined(ENABLE_EDITLINE)
55 static char *el_get_prompt(EditLine
*e
) { return get_prompt(); }
66 hist
= history_init();
67 history(hist
, &hevent
, H_SETSIZE
, 100);
68 el
= el_init(progname
, stdin
, stdout
, stderr
);
70 el_set(el
, EL_SIGNAL
, 1);
71 el_set(el
, EL_PROMPT
, el_get_prompt
);
72 el_set(el
, EL_HIST
, history
, (const char *)hist
);
74 line
= strdup(el_gets(el
, &count
));
79 history(hist
, &hevent
, H_ENTER
, line
);
84 # define MAXREADLINESZ 1024
88 char *p
, *line
= malloc(MAXREADLINESZ
);
92 printf("%s", get_prompt());
94 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
98 p
= line
+ strlen(line
);
99 if (p
!= line
&& p
[-1] == '\n')
112 char **rval
= calloc(sizeof(char *), 1);
114 while (rval
&& (p
= strsep(&input
, " ")) != NULL
) {
118 rval
= realloc(rval
, sizeof(*rval
) * (c
+ 1));
147 for (len
= 0, tmp
= val
; tmp
> 0; tmp
= tmp
/ base
)
149 return len
== 0 ? 1 : len
;
152 #define EXABYTES(x) ((long long)(x) << 60)
153 #define PETABYTES(x) ((long long)(x) << 50)
154 #define TERABYTES(x) ((long long)(x) << 40)
155 #define GIGABYTES(x) ((long long)(x) << 30)
156 #define MEGABYTES(x) ((long long)(x) << 20)
157 #define KILOBYTES(x) ((long long)(x) << 10)
169 i
= strtoll(s
, &sp
, 0);
170 if (i
== 0 && sp
== s
)
181 return i
* blocksize
;
183 return i
* sectorsize
;
200 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
201 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
202 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
203 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
204 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
205 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
216 precise
= ((double)value
* 1000 == (double)(int)value
* 1000);
218 if (value
>= EXABYTES(1)) {
219 fmt
= precise
? "%.f EiB" : "%.3f EiB";
220 snprintf(str
, size
, fmt
, TO_EXABYTES(value
));
221 } else if (value
>= PETABYTES(1)) {
222 fmt
= precise
? "%.f PiB" : "%.3f PiB";
223 snprintf(str
, size
, fmt
, TO_PETABYTES(value
));
224 } else if (value
>= TERABYTES(1)) {
225 fmt
= precise
? "%.f TiB" : "%.3f TiB";
226 snprintf(str
, size
, fmt
, TO_TERABYTES(value
));
227 } else if (value
>= GIGABYTES(1)) {
228 fmt
= precise
? "%.f GiB" : "%.3f GiB";
229 snprintf(str
, size
, fmt
, TO_GIGABYTES(value
));
230 } else if (value
>= MEGABYTES(1)) {
231 fmt
= precise
? "%.f MiB" : "%.3f MiB";
232 snprintf(str
, size
, fmt
, TO_MEGABYTES(value
));
233 } else if (value
>= KILOBYTES(1)) {
234 fmt
= precise
? "%.f KiB" : "%.3f KiB";
235 snprintf(str
, size
, fmt
, TO_KILOBYTES(value
));
237 snprintf(str
, size
, "%f bytes", value
);
241 #define MINUTES_TO_SECONDS(m) ((m) * 60)
242 #define HOURS_TO_SECONDS(h) ((h) * MINUTES_TO_SECONDS(60))
243 #define DAYS_TO_SECONDS(d) ((d) * HOURS_TO_SECONDS(24))
244 #define WEEKS_TO_SECONDS(w) ((w) * DAYS_TO_SECONDS(7))
253 i
= strtoul(s
, &sp
, 0);
254 if (i
== 0 && sp
== s
)
258 if ((*sp
== 'm' && sp
[1] == '\0') ||
259 (strcmp(sp
, "minutes") == 0) ||
260 (strcmp(sp
, "minute") == 0))
261 return MINUTES_TO_SECONDS(i
);
262 if ((*sp
== 'h' && sp
[1] == '\0') ||
263 (strcmp(sp
, "hours") == 0) ||
264 (strcmp(sp
, "hour") == 0))
265 return HOURS_TO_SECONDS(i
);
266 if ((*sp
== 'd' && sp
[1] == '\0') ||
267 (strcmp(sp
, "days") == 0) ||
268 (strcmp(sp
, "day") == 0))
269 return DAYS_TO_SECONDS(i
);
270 if ((*sp
== 'w' && sp
[1] == '\0') ||
271 (strcmp(sp
, "weeks") == 0) ||
272 (strcmp(sp
, "week") == 0))
273 return WEEKS_TO_SECONDS(i
);
278 tadd(struct timeval t1
, struct timeval t2
)
280 t1
.tv_usec
+= t2
.tv_usec
;
281 if (t1
.tv_usec
> 1000000) {
282 t1
.tv_usec
-= 1000000;
285 t1
.tv_sec
+= t2
.tv_sec
;
290 tsub(struct timeval t1
, struct timeval t2
)
292 t1
.tv_usec
-= t2
.tv_usec
;
293 if (t1
.tv_usec
< 0) {
294 t1
.tv_usec
+= 1000000;
297 t1
.tv_sec
-= t2
.tv_sec
;
302 tdiv(double value
, struct timeval tv
)
304 return value
/ ((double)tv
.tv_sec
+ ((double)tv
.tv_usec
/ 1000000.0));
307 #define HOURS(sec) ((sec) / (60 * 60))
308 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
309 #define SECONDS(sec) ((sec) % 60)
318 double usec
= (double)tv
->tv_usec
/ 1000000.0;
320 if (format
& TERSE_FIXED_TIME
) {
321 if (!HOURS(tv
->tv_sec
)) {
322 snprintf(ts
, size
, "%u:%02u.%02u",
323 (unsigned int) MINUTES(tv
->tv_sec
),
324 (unsigned int) SECONDS(tv
->tv_sec
),
325 (unsigned int) usec
* 100);
328 format
|= VERBOSE_FIXED_TIME
; /* fallback if hours needed */
331 if ((format
& VERBOSE_FIXED_TIME
) || tv
->tv_sec
) {
332 snprintf(ts
, size
, "%u:%02u:%02u.%02u",
333 (unsigned int) HOURS(tv
->tv_sec
),
334 (unsigned int) MINUTES(tv
->tv_sec
),
335 (unsigned int) SECONDS(tv
->tv_sec
),
336 (unsigned int) usec
* 100);
338 snprintf(ts
, size
, "0.%04u sec", (unsigned int) usec
* 10000);
343 * Convert from a pair of arbitrary user strings into a timespec.
347 timespec_from_string(
350 struct timespec
* ts
)
353 if (!secs
|| !nsecs
|| !ts
)
355 ts
->tv_sec
= strtoull(secs
, &p
, 0);
358 ts
->tv_nsec
= strtoull(nsecs
, &p
, 0);
365 * Convert from arbitrary user strings into a numeric ID.
366 * If it's all numeric, we convert that inplace, else we do
367 * the name lookup, and return the found identifier.
375 unsigned long prid_long
;
379 * Allow either a full numeric or a valid projectname, even
380 * if it starts with a digit.
382 prid_long
= strtoul(project
, &sp
, 10);
383 if (*project
!= '\0' && *sp
== '\0') {
384 if ((prid_long
== ULONG_MAX
&& errno
== ERANGE
)
385 || (prid_long
> (prid_t
)-1))
387 return (prid_t
)prid_long
;
389 prj
= getprnam(project
);
400 unsigned long uid_long
;
403 uid_long
= strtoul(user
, &sp
, 10);
404 if (sp
!= user
&& *sp
== '\0') {
405 if ((uid_long
== ULONG_MAX
&& errno
== ERANGE
)
406 || (uid_long
> (uid_t
)-1))
408 return (uid_t
)uid_long
;
410 pwd
= getpwnam(user
);
421 unsigned long gid_long
;
424 gid_long
= strtoul(group
, &sp
, 10);
425 if (sp
!= group
&& *sp
== '\0') {
426 if ((gid_long
== ULONG_MAX
&& errno
== ERANGE
)
427 || (gid_long
> (gid_t
)-1))
429 return (gid_t
)gid_long
;
431 grp
= getgrnam(group
);
442 for (i
= 0; i
< strlen(str
); i
++) {
443 if (!isdigit(str
[i
]))
449 #define HAVE_FTW_H 1 /* TODO: configure me */
455 int (*fn
)(const char *, const struct stat
*, int, struct FTW
*),
459 fprintf(stderr
, "%s: not implemented, no recursion available\n",