]>
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
20 #include "xfs/input.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));
139 #define EXABYTES(x) ((long long)(x) << 60)
140 #define PETABYTES(x) ((long long)(x) << 50)
141 #define TERABYTES(x) ((long long)(x) << 40)
142 #define GIGABYTES(x) ((long long)(x) << 30)
143 #define MEGABYTES(x) ((long long)(x) << 20)
144 #define KILOBYTES(x) ((long long)(x) << 10)
156 i
= strtoll(s
, &sp
, 0);
157 if (i
== 0 && sp
== s
)
168 return i
* blocksize
;
170 return i
* sectorsize
;
187 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
188 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
189 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
190 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
191 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
192 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
203 precise
= ((double)value
* 1000 == (double)(int)value
* 1000);
205 if (value
>= EXABYTES(1)) {
206 fmt
= precise
? "%.f EiB" : "%.3f EiB";
207 snprintf(str
, size
, fmt
, TO_EXABYTES(value
));
208 } else if (value
>= PETABYTES(1)) {
209 fmt
= precise
? "%.f PiB" : "%.3f PiB";
210 snprintf(str
, size
, fmt
, TO_PETABYTES(value
));
211 } else if (value
>= TERABYTES(1)) {
212 fmt
= precise
? "%.f TiB" : "%.3f TiB";
213 snprintf(str
, size
, fmt
, TO_TERABYTES(value
));
214 } else if (value
>= GIGABYTES(1)) {
215 fmt
= precise
? "%.f GiB" : "%.3f GiB";
216 snprintf(str
, size
, fmt
, TO_GIGABYTES(value
));
217 } else if (value
>= MEGABYTES(1)) {
218 fmt
= precise
? "%.f MiB" : "%.3f MiB";
219 snprintf(str
, size
, fmt
, TO_MEGABYTES(value
));
220 } else if (value
>= KILOBYTES(1)) {
221 fmt
= precise
? "%.f KiB" : "%.3f KiB";
222 snprintf(str
, size
, fmt
, TO_KILOBYTES(value
));
224 snprintf(str
, size
, "%f bytes", value
);
228 #define MINUTES_TO_SECONDS(m) ((m) * 60)
229 #define HOURS_TO_SECONDS(h) ((h) * MINUTES_TO_SECONDS(60))
230 #define DAYS_TO_SECONDS(d) ((d) * HOURS_TO_SECONDS(24))
231 #define WEEKS_TO_SECONDS(w) ((w) * DAYS_TO_SECONDS(7))
240 i
= strtoul(s
, &sp
, 0);
241 if (i
== 0 && sp
== s
)
245 if ((*sp
== 'm' && sp
[1] == '\0') ||
246 (strcmp(sp
, "minutes") == 0) ||
247 (strcmp(sp
, "minute") == 0))
248 return MINUTES_TO_SECONDS(i
);
249 if ((*sp
== 'h' && sp
[1] == '\0') ||
250 (strcmp(sp
, "hours") == 0) ||
251 (strcmp(sp
, "hour") == 0))
252 return HOURS_TO_SECONDS(i
);
253 if ((*sp
== 'd' && sp
[1] == '\0') ||
254 (strcmp(sp
, "days") == 0) ||
255 (strcmp(sp
, "day") == 0))
256 return DAYS_TO_SECONDS(i
);
257 if ((*sp
== 'w' && sp
[1] == '\0') ||
258 (strcmp(sp
, "weeks") == 0) ||
259 (strcmp(sp
, "week") == 0))
260 return WEEKS_TO_SECONDS(i
);
265 tadd(struct timeval t1
, struct timeval t2
)
267 t1
.tv_usec
+= t2
.tv_usec
;
268 if (t1
.tv_usec
> 1000000) {
269 t1
.tv_usec
-= 1000000;
272 t1
.tv_sec
+= t2
.tv_sec
;
277 tsub(struct timeval t1
, struct timeval t2
)
279 t1
.tv_usec
-= t2
.tv_usec
;
280 if (t1
.tv_usec
< 0) {
281 t1
.tv_usec
+= 1000000;
284 t1
.tv_sec
-= t2
.tv_sec
;
289 tdiv(double value
, struct timeval tv
)
291 return value
/ ((double)tv
.tv_sec
+ ((double)tv
.tv_usec
/ 1000000.0));
294 #define HOURS(sec) ((sec) / (60 * 60))
295 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
296 #define SECONDS(sec) ((sec) % 60)
305 double usec
= (double)tv
->tv_usec
/ 1000000.0;
307 if (format
& TERSE_FIXED_TIME
) {
308 if (!HOURS(tv
->tv_sec
)) {
309 snprintf(ts
, size
, "%u:%02u.%02u",
310 (unsigned int) MINUTES(tv
->tv_sec
),
311 (unsigned int) SECONDS(tv
->tv_sec
),
312 (unsigned int) usec
* 100);
315 format
|= VERBOSE_FIXED_TIME
; /* fallback if hours needed */
318 if ((format
& VERBOSE_FIXED_TIME
) || tv
->tv_sec
) {
319 snprintf(ts
, size
, "%u:%02u:%02u.%02u",
320 (unsigned int) HOURS(tv
->tv_sec
),
321 (unsigned int) MINUTES(tv
->tv_sec
),
322 (unsigned int) SECONDS(tv
->tv_sec
),
323 (unsigned int) usec
* 100);
325 snprintf(ts
, size
, "0.%04u sec", (unsigned int) usec
* 10000);
330 * Convert from arbitrary user strings into a numeric ID.
331 * If it's all numeric, we convert that inplace, else we do
332 * the name lookup, and return the found identifier.
340 unsigned long prid_long
;
344 * Allow either a full numeric or a valid projectname, even
345 * if it starts with a digit.
347 prid_long
= strtoul(project
, &sp
, 10);
348 if (*project
!= '\0' && *sp
== '\0') {
349 if ((prid_long
== ULONG_MAX
&& errno
== ERANGE
)
350 || (prid_long
> (prid_t
)-1))
352 return (prid_t
)prid_long
;
354 prj
= getprnam(project
);
365 unsigned long uid_long
;
368 uid_long
= strtoul(user
, &sp
, 10);
370 if ((uid_long
== ULONG_MAX
&& errno
== ERANGE
)
371 || (uid_long
> (uid_t
)-1))
373 return (uid_t
)uid_long
;
375 pwd
= getpwnam(user
);
386 unsigned long gid_long
;
389 gid_long
= strtoul(group
, &sp
, 10);
391 if ((gid_long
== ULONG_MAX
&& errno
== ERANGE
)
392 || (gid_long
> (gid_t
)-1))
394 return (gid_t
)gid_long
;
396 grp
= getgrnam(group
);
407 for (i
= 0; i
< strlen(str
); i
++) {
408 if (!isdigit(str
[i
]))
414 #define HAVE_FTW_H 1 /* TODO: configure me */
420 int (*fn
)(const char *, const struct stat
*, int, struct FTW
*),
424 fprintf(stderr
, "%s: not implemented, no recursion available\n",