]>
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));
140 tsub(struct timeval t1
, struct timeval t2
)
142 t1
.tv_usec
-= t2
.tv_usec
;
143 if (t1
.tv_usec
< 0) {
144 t1
.tv_usec
+= 1000000;
147 t1
.tv_sec
-= t2
.tv_sec
;
152 tdiv(double value
, struct timeval tv
)
154 return value
/ ((double)tv
.tv_sec
+ ((double)tv
.tv_usec
/ 1000000.0));
157 #define HOURS(sec) ((sec) / (60 * 60))
158 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
159 #define SECONDS(sec) ((sec) % 60)
168 double usec
= (double)tv
->tv_usec
/ 1000000.0;
170 if (format
& TERSE_FIXED_TIME
) {
171 if (!HOURS(tv
->tv_sec
)) {
172 snprintf(ts
, size
, "%u:%02u.%02u",
173 (unsigned int) MINUTES(tv
->tv_sec
),
174 (unsigned int) SECONDS(tv
->tv_sec
),
175 (unsigned int) usec
* 100);
178 format
|= VERBOSE_FIXED_TIME
; /* fallback if hours needed */
181 if ((format
& VERBOSE_FIXED_TIME
) || tv
->tv_sec
) {
182 snprintf(ts
, size
, "%u:%02u:%02u.%02u",
183 (unsigned int) HOURS(tv
->tv_sec
),
184 (unsigned int) MINUTES(tv
->tv_sec
),
185 (unsigned int) SECONDS(tv
->tv_sec
),
186 (unsigned int) usec
* 100);
188 snprintf(ts
, size
, "0.%04u sec", (unsigned int) usec
* 10000);
193 * Convert from a pair of arbitrary user strings into a timespec.
197 timespec_from_string(
200 struct timespec
* ts
)
203 if (!secs
|| !nsecs
|| !ts
)
205 ts
->tv_sec
= strtoull(secs
, &p
, 0);
208 ts
->tv_nsec
= strtoull(nsecs
, &p
, 0);
219 for (i
= 0; i
< strlen(str
); i
++) {
220 if (!isdigit(str
[i
]))
226 #define HAVE_FTW_H 1 /* TODO: configure me */
232 int (*fn
)(const char *, const struct stat
*, int, struct FTW
*),
236 fprintf(stderr
, "%s: not implemented, no recursion available\n",