]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxcmd/input.c
2 * Copyright (c) 2003-2005 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
33 #include <xfs/libxfs.h>
34 #include <xfs/input.h>
36 #if defined(ENABLE_READLINE)
37 # include <readline/history.h>
38 # include <readline/readline.h>
39 #elif defined(ENABLE_EDITLINE)
40 # include <histedit.h>
46 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
49 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
53 #if defined(ENABLE_READLINE)
59 line
= readline(get_prompt());
64 #elif defined(ENABLE_EDITLINE)
65 static char *el_get_prompt(EditLine
*e
) { return get_prompt(); }
76 hist
= history_init();
77 history(hist
, &hevent
, H_SETSIZE
, 100);
78 el
= el_init(progname
, stdin
, stdout
, stderr
);
80 el_set(el
, EL_SIGNAL
, 1);
81 el_set(el
, EL_PROMPT
, el_get_prompt
);
82 el_set(el
, EL_HIST
, history
, (const char *)hist
);
84 line
= strdup(el_gets(el
, &count
));
89 history(hist
, &hevent
, H_ENTER
, line
);
94 # define MAXREADLINESZ 1024
98 char *p
, *line
= malloc(MAXREADLINESZ
);
102 printf(get_prompt());
104 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
108 p
= line
+ strlen(line
);
109 if (p
!= line
&& p
[-1] == '\n')
122 char **rval
= calloc(sizeof(char *), 1);
124 while (rval
&& (p
= strsep(&input
, " ")) != NULL
) {
128 rval
= realloc(rval
, sizeof(*rval
) * (c
+ 1));
149 #define EXABYTES(x) ((long long)(x) << 60)
150 #define PETABYTES(x) ((long long)(x) << 50)
151 #define TERABYTES(x) ((long long)(x) << 40)
152 #define GIGABYTES(x) ((long long)(x) << 30)
153 #define MEGABYTES(x) ((long long)(x) << 20)
154 #define KILOBYTES(x) ((long long)(x) << 10)
165 i
= strtoll(s
, &sp
, 0);
166 if (i
== 0 && sp
== s
)
171 if (*sp
== 'b' && sp
[1] == '\0')
172 return i
* blocksize
;
173 if (*sp
== 's' && sp
[1] == '\0')
174 return i
* sectorsize
;
175 if (*sp
== 'k' && sp
[1] == '\0')
177 if (*sp
== 'm' && sp
[1] == '\0')
179 if (*sp
== 'g' && sp
[1] == '\0')
181 if (*sp
== 't' && sp
[1] == '\0')
183 if (*sp
== 'p' && sp
[1] == '\0')
185 if (*sp
== 'e' && sp
[1] == '\0')
190 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
191 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
192 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
193 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
194 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
195 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
206 precise
= ((double)value
* 1000 == (double)(int)value
* 1000);
208 if (value
>= EXABYTES(1)) {
209 fmt
= precise
? "%.f EiB" : "%.3f EiB";
210 snprintf(str
, size
, fmt
, TO_EXABYTES(value
));
211 } else if (value
>= PETABYTES(1)) {
212 fmt
= precise
? "%.f PiB" : "%.3f PiB";
213 snprintf(str
, size
, fmt
, TO_PETABYTES(value
));
214 } else if (value
>= TERABYTES(1)) {
215 fmt
= precise
? "%.f TiB" : "%.3f TiB";
216 snprintf(str
, size
, fmt
, TO_TERABYTES(value
));
217 } else if (value
>= GIGABYTES(1)) {
218 fmt
= precise
? "%.f GiB" : "%.3f GiB";
219 snprintf(str
, size
, fmt
, TO_GIGABYTES(value
));
220 } else if (value
>= MEGABYTES(1)) {
221 fmt
= precise
? "%.f MiB" : "%.3f MiB";
222 snprintf(str
, size
, fmt
, TO_MEGABYTES(value
));
223 } else if (value
>= KILOBYTES(1)) {
224 fmt
= precise
? "%.f KiB" : "%.3f KiB";
225 snprintf(str
, size
, fmt
, TO_KILOBYTES(value
));
227 snprintf(str
, size
, "%f bytes", value
);
231 #define MINUTES_TO_SECONDS(m) ((m) * 60)
232 #define HOURS_TO_SECONDS(h) ((h) * MINUTES_TO_SECONDS(60))
233 #define DAYS_TO_SECONDS(d) ((d) * HOURS_TO_SECONDS(24))
234 #define WEEKS_TO_SECONDS(w) ((w) * DAYS_TO_SECONDS(7))
243 i
= strtoul(s
, &sp
, 0);
244 if (i
== 0 && sp
== s
)
248 if ((*sp
== 'm' && sp
[1] == '\0') ||
249 (strcmp(sp
, "minutes") == 0) ||
250 (strcmp(sp
, "minute") == 0))
251 return MINUTES_TO_SECONDS(i
);
252 if ((*sp
== 'h' && sp
[1] == '\0') ||
253 (strcmp(sp
, "hours") == 0) ||
254 (strcmp(sp
, "hour") == 0))
255 return HOURS_TO_SECONDS(i
);
256 if ((*sp
== 'd' && sp
[1] == '\0') ||
257 (strcmp(sp
, "days") == 0) ||
258 (strcmp(sp
, "day") == 0))
259 return DAYS_TO_SECONDS(i
);
260 if ((*sp
== 'w' && sp
[1] == '\0') ||
261 (strcmp(sp
, "weeks") == 0) ||
262 (strcmp(sp
, "week") == 0))
263 return WEEKS_TO_SECONDS(i
);
268 tadd(struct timeval t1
, struct timeval t2
)
270 t1
.tv_usec
+= t2
.tv_usec
;
271 if (t1
.tv_usec
> 1000000) {
272 t1
.tv_usec
-= 1000000;
275 t1
.tv_sec
+= t2
.tv_sec
;
280 tsub(struct timeval t1
, struct timeval t2
)
282 t1
.tv_usec
-= t2
.tv_usec
;
283 if (t1
.tv_usec
< 0) {
284 t1
.tv_usec
+= 1000000;
287 t1
.tv_sec
-= t2
.tv_sec
;
292 tdiv(double value
, struct timeval tv
)
294 return value
/ ((double)tv
.tv_sec
+ ((double)tv
.tv_usec
/ 1000000.0));
297 #define HOURS(sec) ((sec) / (60 * 60))
298 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
299 #define SECONDS(sec) ((sec) % 60)
308 double usec
= (double)tv
->tv_usec
/ 1000000.0;
310 if (format
& TERSE_FIXED_TIME
) {
311 if (!HOURS(tv
->tv_sec
)) {
312 snprintf(ts
, size
, "%u:%02u.%02u",
313 (unsigned int) MINUTES(tv
->tv_sec
),
314 (unsigned int) SECONDS(tv
->tv_sec
),
315 (unsigned int) usec
* 100);
318 format
|= VERBOSE_FIXED_TIME
; /* fallback if hours needed */
321 if ((format
& VERBOSE_FIXED_TIME
) || tv
->tv_sec
) {
322 snprintf(ts
, size
, "%u:%02u:%02u.%02u",
323 (unsigned int) HOURS(tv
->tv_sec
),
324 (unsigned int) MINUTES(tv
->tv_sec
),
325 (unsigned int) SECONDS(tv
->tv_sec
),
326 (unsigned int) usec
* 100);
328 snprintf(ts
, size
, "0.%04u sec", (unsigned int) usec
* 10000);
333 * Convert from arbitrary user strings into a numeric ID.
334 * If its all numeric, we convert that inplace, else we do
335 * the name lookup, and return the found identifier.
346 prid
= strtoul(project
, &sp
, 10);
349 prj
= getprnam(project
);
363 uid
= strtoul(user
, &sp
, 10);
366 pwd
= getpwnam(user
);
380 gid
= strtoul(group
, &sp
, 10);
383 grp
= getgrnam(group
);
389 #define HAVE_FTW_H 1 /* TODO: configure me */
395 int (*fn
)(const char *, const struct stat
*, int, struct FTW
*),
399 fprintf(stderr
, "%s: not implemented, no recursion available\n",