~ubuntu-branches/ubuntu/hoary/xfsprogs/hoary

« back to all changes in this revision

Viewing changes to io/input.c

  • Committer: Bazaar Package Importer
  • Author(s): Nathan Scott
  • Date: 2004-07-28 21:11:38 UTC
  • Revision ID: james.westby@ubuntu.com-20040728211138-0v4pdnunnp7na5lm
Tags: 2.6.20-1
* New upstream release.
* Fix xfs_io segfault on non-XFS files.  (closes: #260470)
* Fix packaging botch, deleted files included.  (closes: #260491)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2003-2004 Silicon Graphics, Inc.  All Rights Reserved.
 
3
 *
 
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.
 
7
 *
 
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.
 
11
 *
 
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.
 
18
 *
 
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.
 
22
 *
 
23
 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
 
24
 * Mountain View, CA  94043, or:
 
25
 *
 
26
 * http://www.sgi.com
 
27
 *
 
28
 * For further information regarding this notice, see:
 
29
 *
 
30
 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
 
31
 */
 
32
 
 
33
#include <xfs/libxfs.h>
 
34
#include "input.h"
 
35
#include "init.h"
 
36
#include "io.h"
 
37
 
 
38
#if defined(ENABLE_READLINE)
 
39
# include <readline/history.h>
 
40
# include <readline/readline.h>
 
41
#elif defined(ENABLE_EDITLINE)
 
42
# include <histedit.h>
 
43
#endif
 
44
 
 
45
static char *
 
46
get_prompt(void)
 
47
{
 
48
        static char     prompt[FILENAME_MAX + 1];
 
49
 
 
50
        if (!prompt[0])
 
51
                snprintf(prompt, sizeof(prompt), "%s> ", progname);
 
52
        return prompt;
 
53
}
 
54
 
 
55
#if defined(ENABLE_READLINE)
 
56
char *
 
57
fetchline(void)
 
58
{
 
59
        char    *line;
 
60
 
 
61
        line = readline(get_prompt());
 
62
        if (line && *line)
 
63
                add_history(line);
 
64
        return line;
 
65
}
 
66
#elif defined(ENABLE_EDITLINE)
 
67
static char *el_get_prompt(EditLine *e) { return get_prompt(); }
 
68
char *
 
69
fetchline(void)
 
70
{
 
71
        static EditLine *el;
 
72
        static History  *hist;
 
73
        HistEvent       hevent;
 
74
        char            *line;
 
75
        int             count;
 
76
 
 
77
        if (!el) {
 
78
                hist = history_init();
 
79
                history(hist, &hevent, H_SETSIZE, 100);
 
80
                el = el_init(progname, stdin, stdout, stderr);
 
81
                el_source(el, NULL);
 
82
                el_set(el, EL_SIGNAL, 1);
 
83
                el_set(el, EL_PROMPT, el_get_prompt);
 
84
                el_set(el, EL_HIST, history, (const char *)hist);
 
85
        }
 
86
        line = strdup(el_gets(el, &count));
 
87
        if (line) {
 
88
                if (count > 0)
 
89
                        line[count-1] = '\0';
 
90
                if (*line)
 
91
                        history(hist, &hevent, H_ENTER, line);
 
92
        }
 
93
        return line;
 
94
}
 
95
#else
 
96
# define MAXREADLINESZ  1024
 
97
char *
 
98
fetchline(void)
 
99
{
 
100
        char    *p, *line = malloc(MAXREADLINESZ);
 
101
 
 
102
        if (!line)
 
103
                return NULL;
 
104
        printf(get_prompt());
 
105
        fflush(stdout);
 
106
        if (!fgets(line, MAXREADLINESZ, stdin)) {
 
107
                free(line);
 
108
                return NULL;
 
109
        }
 
110
        p = line + strlen(line);
 
111
        if (p != line && p[-1] == '\n')
 
112
                p[-1] = '\0';
 
113
        return line;
 
114
}
 
115
#endif
 
116
 
 
117
char **
 
118
breakline(
 
119
        char    *input,
 
120
        int     *count)
 
121
{
 
122
        int     c = 0;
 
123
        char    *p;
 
124
        char    **rval = calloc(sizeof(char *), 1);
 
125
 
 
126
        while ((p = strsep(&input, " ")) != NULL) {
 
127
                if (!*p)
 
128
                        continue;
 
129
                c++;
 
130
                rval = realloc(rval, sizeof(*rval) * (c + 1));
 
131
                rval[c - 1] = p;
 
132
                rval[c] = NULL;
 
133
        }
 
134
        *count = c;
 
135
        return rval;
 
136
}
 
137
 
 
138
void
 
139
doneline(
 
140
        char    *input,
 
141
        char    **vec)
 
142
{
 
143
        free(input);
 
144
        free(vec);
 
145
}
 
146
 
 
147
void
 
148
init_cvtnum(
 
149
        int             *blocksize,
 
150
        int             *sectsize)
 
151
{
 
152
        if (!file || (file->flags & IO_FOREIGN)) {
 
153
                *blocksize = 4096;
 
154
                *sectsize = 512;
 
155
        } else {
 
156
                *blocksize = file->geom.blocksize;
 
157
                *sectsize = file->geom.sectsize;
 
158
        }
 
159
}
 
160
 
 
161
#define EXABYTES(x)     ((long long)(x) << 60)
 
162
#define PETABYTES(x)    ((long long)(x) << 50)
 
163
#define TERABYTES(x)    ((long long)(x) << 40)
 
164
#define GIGABYTES(x)    ((long long)(x) << 30)
 
165
#define MEGABYTES(x)    ((long long)(x) << 20)
 
166
#define KILOBYTES(x)    ((long long)(x) << 10)
 
167
 
 
168
long long
 
169
cvtnum(
 
170
        int             blocksize,
 
171
        int             sectorsize,
 
172
        char            *s)
 
173
{
 
174
        long long       i;
 
175
        char            *sp;
 
176
 
 
177
        i = strtoll(s, &sp, 0);
 
178
        if (i == 0 && sp == s)
 
179
                return -1LL;
 
180
        if (*sp == '\0')
 
181
                return i;
 
182
 
 
183
        if (*sp == 'b' && sp[1] == '\0')
 
184
                return i * blocksize;
 
185
        if (*sp == 's' && sp[1] == '\0')
 
186
                return i * sectorsize;
 
187
        if (*sp == 'k' && sp[1] == '\0')
 
188
                return KILOBYTES(i);
 
189
        if (*sp == 'm' && sp[1] == '\0')
 
190
                return MEGABYTES(i);
 
191
        if (*sp == 'g' && sp[1] == '\0')
 
192
                return GIGABYTES(i);
 
193
        if (*sp == 't' && sp[1] == '\0')
 
194
                return TERABYTES(i);
 
195
        if (*sp == 'p' && sp[1] == '\0')
 
196
                return PETABYTES(i);
 
197
        if (*sp == 'e' && sp[1] == '\0')
 
198
                return  EXABYTES(i);
 
199
        return -1LL;
 
200
}
 
201
 
 
202
#define TO_EXABYTES(x)  ((x) / EXABYTES(1))
 
203
#define TO_PETABYTES(x) ((x) / PETABYTES(1))
 
204
#define TO_TERABYTES(x) ((x) / TERABYTES(1))
 
205
#define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
 
206
#define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
 
207
#define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
 
208
 
 
209
void
 
210
cvtstr(
 
211
        double          value,
 
212
        char            *str,
 
213
        size_t          size)
 
214
{
 
215
        char            *fmt;
 
216
        int             precise;
 
217
 
 
218
        precise = ((double)value * 1000 == (double)(int)value * 1000);
 
219
 
 
220
        if (value >= EXABYTES(1)) {
 
221
                fmt = precise ? "%.f EiB" : "%.3f EiB";
 
222
                snprintf(str, size, fmt, TO_EXABYTES(value));
 
223
        } else if (value >= PETABYTES(1)) {
 
224
                fmt = precise ? "%.f PiB" : "%.3f PiB";
 
225
                snprintf(str, size, fmt, TO_PETABYTES(value));
 
226
        } else if (value >= TERABYTES(1)) {
 
227
                fmt = precise ? "%.f TiB" : "%.3f TiB";
 
228
                snprintf(str, size, fmt, TO_TERABYTES(value));
 
229
        } else if (value >= GIGABYTES(1)) {
 
230
                fmt = precise ? "%.f GiB" : "%.3f GiB";
 
231
                snprintf(str, size, fmt, TO_GIGABYTES(value));
 
232
        } else if (value >= MEGABYTES(1)) {
 
233
                fmt = precise ? "%.f MiB" : "%.3f MiB";
 
234
                snprintf(str, size, fmt, TO_MEGABYTES(value));
 
235
        } else if (value >= KILOBYTES(1)) {
 
236
                fmt = precise ? "%.f KiB" : "%.3f KiB";
 
237
                snprintf(str, size, fmt, TO_KILOBYTES(value));
 
238
        } else {
 
239
                snprintf(str, size, "%f bytes", value);
 
240
        }
 
241
}
 
242
 
 
243
struct timeval
 
244
tsub(struct timeval t1, struct timeval t2)
 
245
{
 
246
        t1.tv_usec -= t2.tv_usec;
 
247
        if (t1.tv_usec < 0) {
 
248
                t1.tv_usec += 1000000;
 
249
                t1.tv_sec--;
 
250
        }
 
251
        t1.tv_sec -= t2.tv_sec;
 
252
        return t1;
 
253
}
 
254
 
 
255
double
 
256
tdiv(double value, struct timeval tv)
 
257
{
 
258
        return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
 
259
}
 
260
 
 
261
#define HOURS(sec)      ((sec) / (60 * 60))
 
262
#define MINUTES(sec)    (((sec) % (60 * 60)) / 60)
 
263
#define SECONDS(sec)    ((sec) % 60)
 
264
 
 
265
void
 
266
timestr(
 
267
        struct timeval  *tv,
 
268
        char            *ts,
 
269
        size_t          size)
 
270
{
 
271
        if (!tv->tv_sec)
 
272
                snprintf(ts, size, "%.4f sec",
 
273
                        ((double) tv->tv_usec / 1000000.0));
 
274
        else
 
275
                snprintf(ts, size, "%02u:%02u:%02u.%-u",
 
276
                        (unsigned int) HOURS(tv->tv_sec),
 
277
                        (unsigned int) MINUTES(tv->tv_sec),
 
278
                        (unsigned int) SECONDS(tv->tv_sec),
 
279
                        (unsigned int) tv->tv_usec);
 
280
}