]>
git.ipfire.org Git - thirdparty/bash.git/blob - examples/loadables/stat.c
1 /* stat - load up an associative array with stat information about a file */
3 /* See Makefile for compilation details. */
6 Copyright (C) 2016 Free Software Foundation, Inc.
8 This file is part of GNU Bash.
9 Bash is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 Bash is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Bash. If not, see <http://www.gnu.org/licenses/>.
25 #if defined (HAVE_UNISTD_H)
31 #include <sys/types.h>
32 #include "posixstat.h"
37 #include "posixtime.h"
43 #include "bashgetopt.h"
63 #define ST_CHASELINK 14
68 static char *arraysubs
[] =
70 "name", "device", "inode", "type", "nlink", "uid", "gid", "rdev",
71 "size", "atime", "mtime", "ctime", "blksize", "blocks", "link", "perms",
76 getstat (fname
, flags
, sp
)
84 if (strncmp (fname
, "/dev/fd/", 8) == 0)
86 if ((legal_number(fname
+ 8, &lfd
) == 0) || (int)lfd
!= lfd
)
109 #if defined (HAVE_READLINK)
110 char linkbuf
[PATH_MAX
];
113 if (fname
&& S_ISLNK (sp
->st_mode
) && (n
= readlink (fname
, linkbuf
, PATH_MAX
)) > 0)
116 return (savestring (linkbuf
));
120 return (savestring (fname
));
160 ret
= (char *)xmalloc (16);
161 snprintf (ret
, 16, "%04o", operms
);
169 char ubits
[4], gbits
[4], obits
[4]; /* u=rwx,g=rwx,o=rwx */
201 ubits
[2] = (m
& S_IXUSR
) ? 's' : 'S';
203 gbits
[2] = (m
& S_IXGRP
) ? 's' : 'S';
205 obits
[2] = (m
& S_IXOTH
) ? 't' : 'T';
207 ret
= (char *)xmalloc (32);
208 snprintf (ret
, 32, "u=%s,g=%s,o=%s", ubits
, gbits
, obits
);
218 modestr
= m
= (char *)xmalloc (8);
263 tlen
= strlen (tbuf
);
264 ret
= savestring (tbuf
);
270 statval (which
, fname
, flags
, sp
)
281 return savestring (fname
);
283 return itos (sp
->st_dev
);
285 return itos (sp
->st_ino
);
287 return (statmode (sp
->st_mode
));
289 return itos (sp
->st_nlink
);
291 return itos (sp
->st_uid
);
293 return itos (sp
->st_gid
);
295 return itos (sp
->st_rdev
);
297 return itos (sp
->st_size
);
299 return ((flags
& 2) ? stattime (sp
->st_atime
) : itos (sp
->st_atime
));
301 return ((flags
& 2) ? stattime (sp
->st_mtime
) : itos (sp
->st_mtime
));
303 return ((flags
& 2) ? stattime (sp
->st_ctime
) : itos (sp
->st_ctime
));
305 return itos (sp
->st_blksize
);
307 return itos (sp
->st_blocks
);
309 return (statlink (fname
, sp
));
311 temp
= sp
->st_mode
& (S_IRWXU
|S_IRWXG
|S_IRWXO
|S_ISUID
|S_ISGID
);
312 return (flags
& 2) ? statperms (temp
) : octalperms (temp
);
314 return savestring ("42");
319 loadstat (vname
, var
, fname
, flags
, sp
)
330 for (i
= 0; arraysubs
[i
]; i
++)
332 key
= savestring (arraysubs
[i
]);
333 value
= statval (i
, fname
, flags
, sp
);
334 v
= bind_assoc_variable (var
, vname
, key
, value
, ASS_FORCE
);
351 reset_internal_getopt ();
352 while ((opt
= internal_getopt (list
, "A:Ll")) != -1)
360 flags
|= 1; /* operate on links rather than resolving them */
379 fname
= list
->word
->word
;
381 if (getstat (fname
, flags
, &st
) < 0)
383 builtin_error ("%s: cannot stat: %s", fname
, strerror (errno
));
384 return (EXECUTION_FAILURE
);
387 unbind_variable (aname
);
388 v
= make_new_assoc_variable (aname
);
391 builtin_error ("%s: cannot create variable", aname
);
392 return (EXECUTION_FAILURE
);
394 if (loadstat (aname
, v
, fname
, flags
, &st
) < 0)
396 builtin_error ("%s: cannot assign file status information", aname
);
397 unbind_variable (aname
);
398 return (EXECUTION_FAILURE
);
401 return (EXECUTION_SUCCESS
);
404 /* An array of strings forming the `long' documentation for a builtin xxx,
405 which is printed by `help xxx'. It must end with a NULL. By convention,
406 the first line is a short description. */
408 "Load an associative array with file status information.",
410 "Take a filename and load the status information returned by a",
411 "stat(2) call on that file into the associative array specified",
412 "by the -A option. The default array name is STAT. If the -L",
413 "option is supplied, stat does not resolve symbolic links and",
414 "reports information about the link itself. The -l option results",
415 "in longer-form listings for some of the fields. The exit status is 0",
416 "unless the stat fails or assigning the array is unsuccessful.",
420 /* The standard structure describing a builtin command. bash keeps an array
421 of these structures. The flags must include BUILTIN_ENABLED so the
422 builtin can be used. */
423 struct builtin stat_struct
= {
424 "stat", /* builtin name */
425 stat_builtin
, /* function implementing the builtin */
426 BUILTIN_ENABLED
, /* initial flags for builtin */
427 stat_doc
, /* array of long documentation strings. */
428 "stat [-lL] [-A aname] file", /* usage synopsis; becomes short_doc */
429 0 /* reserved for internal use */