1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
|
/* stat - load up an associative array with stat information about a file */
/* See Makefile for compilation details. */
/*
Copyright (C) 2016 Free Software Foundation, Inc.
This file is part of GNU Bash.
Bash is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Bash is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Bash. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include <stdio.h>
#include <sys/types.h>
#include "posixstat.h"
#include <stdio.h>
#include <pwd.h>
#include <grp.h>
#include <errno.h>
#include "posixtime.h"
#include "bashansi.h"
#include "shell.h"
#include "builtins.h"
#include "common.h"
#include "bashgetopt.h"
#ifndef errno
extern int errno;
#endif
#define ST_NAME 0
#define ST_DEV 1
#define ST_INO 2
#define ST_MODE 3
#define ST_NLINK 4
#define ST_UID 5
#define ST_GID 6
#define ST_RDEV 7
#define ST_SIZE 8
#define ST_ATIME 9
#define ST_MTIME 10
#define ST_CTIME 11
#define ST_BLKSIZE 12
#define ST_BLOCKS 13
#define ST_CHASELINK 14
#define ST_PERMS 15
#define ST_END 16
static char *arraysubs[] =
{
"name", "device", "inode", "type", "nlink", "uid", "gid", "rdev",
"size", "atime", "mtime", "ctime", "blksize", "blocks", "link", "perms",
0
};
static int
getstat (fname, flags, sp)
const char *fname;
int flags;
struct stat *sp;
{
intmax_t lfd;
int fd, r;
if (strncmp (fname, "/dev/fd/", 8) == 0)
{
if ((legal_number(fname + 8, &lfd) == 0) || (int)lfd != lfd)
{
errno = EINVAL;
return -1;
}
fd = lfd;
r = fstat(fd, sp);
}
#ifdef HAVE_LSTAT
else if (flags & 1)
r = lstat(fname, sp);
#endif
else
r = stat(fname, sp);
return r;
}
static char *
statlink (fname, sp)
char *fname;
struct stat *sp;
{
#if defined (HAVE_READLINK)
char linkbuf[PATH_MAX];
int n;
if (fname && S_ISLNK (sp->st_mode) && (n = readlink (fname, linkbuf, PATH_MAX)) > 0)
{
linkbuf[n] = '\0';
return (savestring (linkbuf));
}
else
#endif
return (savestring (fname));
}
static char *
octalperms (m)
int m;
{
int operms;
char *ret;
operms = 0;
if (m & S_IRUSR)
operms |= 0400;
if (m & S_IWUSR)
operms |= 0200;
if (m & S_IXUSR)
operms |= 0100;
if (m & S_IRGRP)
operms |= 0040;
if (m & S_IWGRP)
operms |= 0020;
if (m & S_IXGRP)
operms |= 0010;
if (m & S_IROTH)
operms |= 0004;
if (m & S_IWOTH)
operms |= 0002;
if (m & S_IXOTH)
operms |= 0001;
if (m & S_ISUID)
operms |= 04000;
if (m & S_ISGID)
operms |= 02000;
if (m & S_ISVTX)
operms |= 01000;
ret = (char *)xmalloc (16);
snprintf (ret, 16, "%04o", operms);
return ret;
}
static char *
statperms (m)
int m;
{
char ubits[4], gbits[4], obits[4]; /* u=rwx,g=rwx,o=rwx */
int i;
char *ret;
i = 0;
if (m & S_IRUSR)
ubits[i++] = 'r';
if (m & S_IWUSR)
ubits[i++] = 'w';
if (m & S_IXUSR)
ubits[i++] = 'x';
ubits[i] = '\0';
i = 0;
if (m & S_IRGRP)
gbits[i++] = 'r';
if (m & S_IWGRP)
gbits[i++] = 'w';
if (m & S_IXGRP)
gbits[i++] = 'x';
gbits[i] = '\0';
i = 0;
if (m & S_IROTH)
obits[i++] = 'r';
if (m & S_IWOTH)
obits[i++] = 'w';
if (m & S_IXOTH)
obits[i++] = 'x';
obits[i] = '\0';
if (m & S_ISUID)
ubits[2] = (m & S_IXUSR) ? 's' : 'S';
if (m & S_ISGID)
gbits[2] = (m & S_IXGRP) ? 's' : 'S';
if (m & S_ISVTX)
obits[2] = (m & S_IXOTH) ? 't' : 'T';
ret = (char *)xmalloc (32);
snprintf (ret, 32, "u=%s,g=%s,o=%s", ubits, gbits, obits);
return ret;
}
static char *
statmode(mode)
int mode;
{
char *modestr, *m;
modestr = m = (char *)xmalloc (8);
if (S_ISBLK (mode))
*m++ = 'b';
if (S_ISCHR (mode))
*m++ = 'c';
if (S_ISDIR (mode))
*m++ = 'd';
if (S_ISREG(mode))
*m++ = '-';
if (S_ISFIFO(mode))
*m++ = 'p';
if (S_ISLNK(mode))
*m++ = 'l';
if (S_ISSOCK(mode))
*m++ = 's';
#ifdef S_ISDOOR
if (S_ISDOOR (mode))
*m++ = 'D';
#endif
#ifdef S_ISWHT
if (S_ISWHT(mode))
*m++ = 'W';
#endif
#ifdef S_ISNWK
if (S_ISNWK(mode))
*m++ = 'n';
#endif
#ifdef S_ISMPC
if (S_ISMPC (mode))
*m++ = 'm';
#endif
*m = '\0';
return (modestr);
}
static char *
stattime (t)
time_t t;
{
char *tbuf, *ret;
size_t tlen;
tbuf = ctime (&t);
tlen = strlen (tbuf);
ret = savestring (tbuf);
ret[tlen-1] = '\0';
return ret;
}
static char *
statval (which, fname, flags, sp)
int which;
char *fname;
int flags;
struct stat *sp;
{
int temp;
switch (which)
{
case ST_NAME:
return savestring (fname);
case ST_DEV:
return itos (sp->st_dev);
case ST_INO:
return itos (sp->st_ino);
case ST_MODE:
return (statmode (sp->st_mode));
case ST_NLINK:
return itos (sp->st_nlink);
case ST_UID:
return itos (sp->st_uid);
case ST_GID:
return itos (sp->st_gid);
case ST_RDEV:
return itos (sp->st_rdev);
case ST_SIZE:
return itos (sp->st_size);
case ST_ATIME:
return ((flags & 2) ? stattime (sp->st_atime) : itos (sp->st_atime));
case ST_MTIME:
return ((flags & 2) ? stattime (sp->st_mtime) : itos (sp->st_mtime));
case ST_CTIME:
return ((flags & 2) ? stattime (sp->st_ctime) : itos (sp->st_ctime));
case ST_BLKSIZE:
return itos (sp->st_blksize);
case ST_BLOCKS:
return itos (sp->st_blocks);
case ST_CHASELINK:
return (statlink (fname, sp));
case ST_PERMS:
temp = sp->st_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID);
return (flags & 2) ? statperms (temp) : octalperms (temp);
default:
return savestring ("42");
}
}
static int
loadstat (vname, var, fname, flags, sp)
char *vname;
SHELL_VAR *var;
char *fname;
int flags;
struct stat *sp;
{
int i;
char *key, *value;
SHELL_VAR *v;
for (i = 0; arraysubs[i]; i++)
{
key = savestring (arraysubs[i]);
value = statval (i, fname, flags, sp);
v = bind_assoc_variable (var, vname, key, value, ASS_FORCE);
}
return 0;
}
int
stat_builtin (list)
WORD_LIST *list;
{
int opt, flags;
char *aname, *fname;
struct stat st;
SHELL_VAR *v;
aname = "STAT";
flags = 0;
reset_internal_getopt ();
while ((opt = internal_getopt (list, "A:Ll")) != -1)
{
switch (opt)
{
case 'A':
aname = list_optarg;
break;
case 'L':
flags |= 1; /* operate on links rather than resolving them */
break;
case 'l':
flags |= 2;
break;
CASE_HELPOPT;
default:
builtin_usage ();
return (EX_USAGE);
}
}
list = loptend;
if (list == 0)
{
builtin_usage ();
return (EX_USAGE);
}
fname = list->word->word;
if (getstat (fname, flags, &st) < 0)
{
builtin_error ("%s: cannot stat: %s", fname, strerror (errno));
return (EXECUTION_FAILURE);
}
unbind_variable (aname);
v = make_new_assoc_variable (aname);
if (v == 0)
{
builtin_error ("%s: cannot create variable", aname);
return (EXECUTION_FAILURE);
}
if (loadstat (aname, v, fname, flags, &st) < 0)
{
builtin_error ("%s: cannot assign file status information", aname);
unbind_variable (aname);
return (EXECUTION_FAILURE);
}
return (EXECUTION_SUCCESS);
}
/* An array of strings forming the `long' documentation for a builtin xxx,
which is printed by `help xxx'. It must end with a NULL. By convention,
the first line is a short description. */
char *stat_doc[] = {
"Load an associative array with file status information.",
"",
"Take a filename and load the status information returned by a",
"stat(2) call on that file into the associative array specified",
"by the -A option. The default array name is STAT. If the -L",
"option is supplied, stat does not resolve symbolic links and",
"reports information about the link itself. The -l option results",
"in longer-form listings for some of the fields. The exit status is 0",
"unless the stat fails or assigning the array is unsuccessful.",
(char *)NULL
};
/* The standard structure describing a builtin command. bash keeps an array
of these structures. The flags must include BUILTIN_ENABLED so the
builtin can be used. */
struct builtin stat_struct = {
"stat", /* builtin name */
stat_builtin, /* function implementing the builtin */
BUILTIN_ENABLED, /* initial flags for builtin */
stat_doc, /* array of long documentation strings. */
"stat [-lL] [-A aname] file", /* usage synopsis; becomes short_doc */
0 /* reserved for internal use */
};
|