]>
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>
23 #if defined(ENABLE_READLINE)
24 # include <readline/history.h>
25 # include <readline/readline.h>
26 #elif defined(ENABLE_EDITLINE)
27 # include <histedit.h>
30 extern char *progname
;
35 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
38 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
42 #if defined(ENABLE_READLINE)
48 line
= readline(get_prompt());
53 #elif defined(ENABLE_EDITLINE)
54 static char *el_get_prompt(EditLine
*e
) { return get_prompt(); }
65 hist
= history_init();
66 history(hist
, &hevent
, H_SETSIZE
, 100);
67 el
= el_init(progname
, stdin
, stdout
, stderr
);
69 el_set(el
, EL_SIGNAL
, 1);
70 el_set(el
, EL_PROMPT
, el_get_prompt
);
71 el_set(el
, EL_HIST
, history
, (const char *)hist
);
73 line
= strdup(el_gets(el
, &count
));
78 history(hist
, &hevent
, H_ENTER
, line
);
83 # define MAXREADLINESZ 1024
87 char *p
, *line
= malloc(MAXREADLINESZ
);
93 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
97 p
= line
+ strlen(line
);
98 if (p
!= line
&& p
[-1] == '\n')
111 char **rval
= calloc(sizeof(char *), 1);
113 while (rval
&& (p
= strsep(&input
, " ")) != NULL
) {
117 rval
= realloc(rval
, sizeof(*rval
) * (c
+ 1));
138 #define EXABYTES(x) ((long long)(x) << 60)
139 #define PETABYTES(x) ((long long)(x) << 50)
140 #define TERABYTES(x) ((long long)(x) << 40)
141 #define GIGABYTES(x) ((long long)(x) << 30)
142 #define MEGABYTES(x) ((long long)(x) << 20)
143 #define KILOBYTES(x) ((long long)(x) << 10)
155 i
= strtoll(s
, &sp
, 0);
156 if (i
== 0 && sp
== s
)
167 return i
* blocksize
;
169 return i
* sectorsize
;
186 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
187 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
188 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
189 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
190 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
191 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
202 precise
= ((double)value
* 1000 == (double)(int)value
* 1000);
204 if (value
>= EXABYTES(1)) {
205 fmt
= precise
? "%.f EiB" : "%.3f EiB";
206 snprintf(str
, size
, fmt
, TO_EXABYTES(value
));
207 } else if (value
>= PETABYTES(1)) {
208 fmt
= precise
? "%.f PiB" : "%.3f PiB";
209 snprintf(str
, size
, fmt
, TO_PETABYTES(value
));
210 } else if (value
>= TERABYTES(1)) {
211 fmt
= precise
? "%.f TiB" : "%.3f TiB";
212 snprintf(str
, size
, fmt
, TO_TERABYTES(value
));
213 } else if (value
>= GIGABYTES(1)) {
214 fmt
= precise
? "%.f GiB" : "%.3f GiB";
215 snprintf(str
, size
, fmt
, TO_GIGABYTES(value
));
216 } else if (value
>= MEGABYTES(1)) {
217 fmt
= precise
? "%.f MiB" : "%.3f MiB";
218 snprintf(str
, size
, fmt
, TO_MEGABYTES(value
));
219 } else if (value
>= KILOBYTES(1)) {
220 fmt
= precise
? "%.f KiB" : "%.3f KiB";
221 snprintf(str
, size
, fmt
, TO_KILOBYTES(value
));
223 snprintf(str
, size
, "%f bytes", value
);
227 #define MINUTES_TO_SECONDS(m) ((m) * 60)
228 #define HOURS_TO_SECONDS(h) ((h) * MINUTES_TO_SECONDS(60))
229 #define DAYS_TO_SECONDS(d) ((d) * HOURS_TO_SECONDS(24))
230 #define WEEKS_TO_SECONDS(w) ((w) * DAYS_TO_SECONDS(7))
239 i
= strtoul(s
, &sp
, 0);
240 if (i
== 0 && sp
== s
)
244 if ((*sp
== 'm' && sp
[1] == '\0') ||
245 (strcmp(sp
, "minutes") == 0) ||
246 (strcmp(sp
, "minute") == 0))
247 return MINUTES_TO_SECONDS(i
);
248 if ((*sp
== 'h' && sp
[1] == '\0') ||
249 (strcmp(sp
, "hours") == 0) ||
250 (strcmp(sp
, "hour") == 0))
251 return HOURS_TO_SECONDS(i
);
252 if ((*sp
== 'd' && sp
[1] == '\0') ||
253 (strcmp(sp
, "days") == 0) ||
254 (strcmp(sp
, "day") == 0))
255 return DAYS_TO_SECONDS(i
);
256 if ((*sp
== 'w' && sp
[1] == '\0') ||
257 (strcmp(sp
, "weeks") == 0) ||
258 (strcmp(sp
, "week") == 0))
259 return WEEKS_TO_SECONDS(i
);
264 tadd(struct timeval t1
, struct timeval t2
)
266 t1
.tv_usec
+= t2
.tv_usec
;
267 if (t1
.tv_usec
> 1000000) {
268 t1
.tv_usec
-= 1000000;
271 t1
.tv_sec
+= t2
.tv_sec
;
276 tsub(struct timeval t1
, struct timeval t2
)
278 t1
.tv_usec
-= t2
.tv_usec
;
279 if (t1
.tv_usec
< 0) {
280 t1
.tv_usec
+= 1000000;
283 t1
.tv_sec
-= t2
.tv_sec
;
288 tdiv(double value
, struct timeval tv
)
290 return value
/ ((double)tv
.tv_sec
+ ((double)tv
.tv_usec
/ 1000000.0));
293 #define HOURS(sec) ((sec) / (60 * 60))
294 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
295 #define SECONDS(sec) ((sec) % 60)
304 double usec
= (double)tv
->tv_usec
/ 1000000.0;
306 if (format
& TERSE_FIXED_TIME
) {
307 if (!HOURS(tv
->tv_sec
)) {
308 snprintf(ts
, size
, "%u:%02u.%02u",
309 (unsigned int) MINUTES(tv
->tv_sec
),
310 (unsigned int) SECONDS(tv
->tv_sec
),
311 (unsigned int) usec
* 100);
314 format
|= VERBOSE_FIXED_TIME
; /* fallback if hours needed */
317 if ((format
& VERBOSE_FIXED_TIME
) || tv
->tv_sec
) {
318 snprintf(ts
, size
, "%u:%02u:%02u.%02u",
319 (unsigned int) HOURS(tv
->tv_sec
),
320 (unsigned int) MINUTES(tv
->tv_sec
),
321 (unsigned int) SECONDS(tv
->tv_sec
),
322 (unsigned int) usec
* 100);
324 snprintf(ts
, size
, "0.%04u sec", (unsigned int) usec
* 10000);
329 * Convert from arbitrary user strings into a numeric ID.
330 * If its all numeric, we convert that inplace, else we do
331 * the name lookup, and return the found identifier.
343 * Allow either a full numeric or a valid projectname, even
344 * if it starts with a digit.
346 prid
= (prid_t
)strtoul(project
, &sp
, 10);
347 if (*project
!= '\0' && *sp
== '\0')
349 prj
= getprnam(project
);
363 uid
= (uid_t
)strtoul(user
, &sp
, 10);
366 pwd
= getpwnam(user
);
380 gid
= (gid_t
)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",