]>
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>
22 #if defined(ENABLE_READLINE)
23 # include <readline/history.h>
24 # include <readline/readline.h>
25 #elif defined(ENABLE_EDITLINE)
26 # include <histedit.h>
29 extern char *progname
;
34 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
37 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
41 #if defined(ENABLE_READLINE)
47 line
= readline(get_prompt());
52 #elif defined(ENABLE_EDITLINE)
53 static char *el_get_prompt(EditLine
*e
) { return get_prompt(); }
64 hist
= history_init();
65 history(hist
, &hevent
, H_SETSIZE
, 100);
66 el
= el_init(progname
, stdin
, stdout
, stderr
);
68 el_set(el
, EL_SIGNAL
, 1);
69 el_set(el
, EL_PROMPT
, el_get_prompt
);
70 el_set(el
, EL_HIST
, history
, (const char *)hist
);
72 line
= strdup(el_gets(el
, &count
));
77 history(hist
, &hevent
, H_ENTER
, line
);
82 # define MAXREADLINESZ 1024
86 char *p
, *line
= malloc(MAXREADLINESZ
);
92 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
96 p
= line
+ strlen(line
);
97 if (p
!= line
&& p
[-1] == '\n')
110 char **rval
= calloc(sizeof(char *), 1);
112 while (rval
&& (p
= strsep(&input
, " ")) != NULL
) {
116 rval
= realloc(rval
, sizeof(*rval
) * (c
+ 1));
137 #define EXABYTES(x) ((long long)(x) << 60)
138 #define PETABYTES(x) ((long long)(x) << 50)
139 #define TERABYTES(x) ((long long)(x) << 40)
140 #define GIGABYTES(x) ((long long)(x) << 30)
141 #define MEGABYTES(x) ((long long)(x) << 20)
142 #define KILOBYTES(x) ((long long)(x) << 10)
153 i
= strtoll(s
, &sp
, 0);
154 if (i
== 0 && sp
== s
)
159 if (*sp
== 'b' && sp
[1] == '\0')
160 return i
* blocksize
;
161 if (*sp
== 's' && sp
[1] == '\0')
162 return i
* sectorsize
;
163 if (*sp
== 'k' && sp
[1] == '\0')
165 if (*sp
== 'm' && sp
[1] == '\0')
167 if (*sp
== 'g' && sp
[1] == '\0')
169 if (*sp
== 't' && sp
[1] == '\0')
171 if (*sp
== 'p' && sp
[1] == '\0')
173 if (*sp
== 'e' && sp
[1] == '\0')
178 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
179 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
180 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
181 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
182 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
183 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
194 precise
= ((double)value
* 1000 == (double)(int)value
* 1000);
196 if (value
>= EXABYTES(1)) {
197 fmt
= precise
? "%.f EiB" : "%.3f EiB";
198 snprintf(str
, size
, fmt
, TO_EXABYTES(value
));
199 } else if (value
>= PETABYTES(1)) {
200 fmt
= precise
? "%.f PiB" : "%.3f PiB";
201 snprintf(str
, size
, fmt
, TO_PETABYTES(value
));
202 } else if (value
>= TERABYTES(1)) {
203 fmt
= precise
? "%.f TiB" : "%.3f TiB";
204 snprintf(str
, size
, fmt
, TO_TERABYTES(value
));
205 } else if (value
>= GIGABYTES(1)) {
206 fmt
= precise
? "%.f GiB" : "%.3f GiB";
207 snprintf(str
, size
, fmt
, TO_GIGABYTES(value
));
208 } else if (value
>= MEGABYTES(1)) {
209 fmt
= precise
? "%.f MiB" : "%.3f MiB";
210 snprintf(str
, size
, fmt
, TO_MEGABYTES(value
));
211 } else if (value
>= KILOBYTES(1)) {
212 fmt
= precise
? "%.f KiB" : "%.3f KiB";
213 snprintf(str
, size
, fmt
, TO_KILOBYTES(value
));
215 snprintf(str
, size
, "%f bytes", value
);
219 #define MINUTES_TO_SECONDS(m) ((m) * 60)
220 #define HOURS_TO_SECONDS(h) ((h) * MINUTES_TO_SECONDS(60))
221 #define DAYS_TO_SECONDS(d) ((d) * HOURS_TO_SECONDS(24))
222 #define WEEKS_TO_SECONDS(w) ((w) * DAYS_TO_SECONDS(7))
231 i
= strtoul(s
, &sp
, 0);
232 if (i
== 0 && sp
== s
)
236 if ((*sp
== 'm' && sp
[1] == '\0') ||
237 (strcmp(sp
, "minutes") == 0) ||
238 (strcmp(sp
, "minute") == 0))
239 return MINUTES_TO_SECONDS(i
);
240 if ((*sp
== 'h' && sp
[1] == '\0') ||
241 (strcmp(sp
, "hours") == 0) ||
242 (strcmp(sp
, "hour") == 0))
243 return HOURS_TO_SECONDS(i
);
244 if ((*sp
== 'd' && sp
[1] == '\0') ||
245 (strcmp(sp
, "days") == 0) ||
246 (strcmp(sp
, "day") == 0))
247 return DAYS_TO_SECONDS(i
);
248 if ((*sp
== 'w' && sp
[1] == '\0') ||
249 (strcmp(sp
, "weeks") == 0) ||
250 (strcmp(sp
, "week") == 0))
251 return WEEKS_TO_SECONDS(i
);
256 tadd(struct timeval t1
, struct timeval t2
)
258 t1
.tv_usec
+= t2
.tv_usec
;
259 if (t1
.tv_usec
> 1000000) {
260 t1
.tv_usec
-= 1000000;
263 t1
.tv_sec
+= t2
.tv_sec
;
268 tsub(struct timeval t1
, struct timeval t2
)
270 t1
.tv_usec
-= t2
.tv_usec
;
271 if (t1
.tv_usec
< 0) {
272 t1
.tv_usec
+= 1000000;
275 t1
.tv_sec
-= t2
.tv_sec
;
280 tdiv(double value
, struct timeval tv
)
282 return value
/ ((double)tv
.tv_sec
+ ((double)tv
.tv_usec
/ 1000000.0));
285 #define HOURS(sec) ((sec) / (60 * 60))
286 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
287 #define SECONDS(sec) ((sec) % 60)
296 double usec
= (double)tv
->tv_usec
/ 1000000.0;
298 if (format
& TERSE_FIXED_TIME
) {
299 if (!HOURS(tv
->tv_sec
)) {
300 snprintf(ts
, size
, "%u:%02u.%02u",
301 (unsigned int) MINUTES(tv
->tv_sec
),
302 (unsigned int) SECONDS(tv
->tv_sec
),
303 (unsigned int) usec
* 100);
306 format
|= VERBOSE_FIXED_TIME
; /* fallback if hours needed */
309 if ((format
& VERBOSE_FIXED_TIME
) || tv
->tv_sec
) {
310 snprintf(ts
, size
, "%u:%02u:%02u.%02u",
311 (unsigned int) HOURS(tv
->tv_sec
),
312 (unsigned int) MINUTES(tv
->tv_sec
),
313 (unsigned int) SECONDS(tv
->tv_sec
),
314 (unsigned int) usec
* 100);
316 snprintf(ts
, size
, "0.%04u sec", (unsigned int) usec
* 10000);
321 * Convert from arbitrary user strings into a numeric ID.
322 * If its all numeric, we convert that inplace, else we do
323 * the name lookup, and return the found identifier.
334 prid
= (prid_t
)strtoul(project
, &sp
, 10);
337 prj
= getprnam(project
);
351 uid
= (uid_t
)strtoul(user
, &sp
, 10);
354 pwd
= getpwnam(user
);
368 gid
= (gid_t
)strtoul(group
, &sp
, 10);
371 grp
= getgrnam(group
);
377 #define HAVE_FTW_H 1 /* TODO: configure me */
383 int (*fn
)(const char *, const struct stat
*, int, struct FTW
*),
387 fprintf(stderr
, "%s: not implemented, no recursion available\n",