Differences between current version and predecessor to the previous major change of fstat(2).
Other diffs: Previous Revision, Previous Author, or view the Annotated Edit History
Newer page: | version 9 | Last edited on Wednesday, November 6, 2002 11:55:28 pm | by PerryLorier | |
Older page: | version 5 | Last edited on Wednesday, November 6, 2002 9:16:22 pm | by PerryLorier | Revert |
@@ -91,9 +91,8 @@
;[ELOOP]: Too many symbolic links encountered while traversing the path.
;[EFAULT]: Bad address.
;[EACCES]: Permission denied.
;[ENOMEM]: Out of memory (i.e. kernel memory).
-;[ENAMETOOLONG]: File name too long.
!!CONFORMING TO
The fstat(2) calls conform to SVr4, SVID, POSIX, X/OPEN, BSD 4.3. SVr4 documents additional fstat(2) error conditions [EINTR], [ENOLINK], and [EOVERFLOW].
@@ -134,6 +133,116 @@
|0800|S_CDF| |004000|directory is a context dependent file (HP-UX)
A sticky command appeared in Version 32V AT
+
+!!EXAMPLE
+
+ /* fstat(2)'s a file, and displays the results
+ *
+ */
+
+ #include <sys/stat.h> /* for struct stat, fstat(2), S_* constants */
+ #include <sys/types.h>/* for mode_t, uid_t, etc... */
+ #include <errno.h> /* for errno */
+ #include <fcntl.h> /* for open(2), O_* */
+ #include <pwd.h> /* for getpwuid(3) */
+ #include <string.h> /* for strerror(3) */
+ #include <stdio.h> /* for printf(3) etc */
+ #include <time.h> /* for asctime(3) */
+ #include <unistd.h> /* for close(2) */
+
+ int do_stat(char *filename)
+ {
+ int fd;
+ struct stat statbuf;
+
+ fd = open(filename,O_RDONLY);
+
+ if (fd == -1) {
+ printf("fstat: open(\"%s\",O_RDONLY): %s (%i)",
+ filename,
+ strerror(errno),
+ errno);
+ return 1;
+ }
+
+ if (fstat(fd,&statbuf) == -1) {
+ printf("fstat: fstat: %s (%i)",
+ strerror(errno),
+ errno);
+ return 1;
+ }
+
+ if (S_ISREG(statbuf.st_mode)) printf("Regular file");
+ else if (S_ISDIR(statbuf.st_mode)) printf("Directory");
+ else if (S_ISCHR(statbuf.st_mode)) printf("Character Device");
+ else if (S_ISBLK(statbuf.st_mode)) printf("Block Device");
+ else if (S_ISFIFO(statbuf.st_mode)) printf("Fifo");
+ else if (S_ISLNK(statbuf.st_mode)) printf("Symbolic Link");
+ else if (S_ISSOCK(statbuf.st_mode)) printf("Socket");
+ printf(": \"%s\"\n",filename);
+ printf("Size: %-20i Blocks: %-10i Block Size: %-10i\n",
+ (int)statbuf.st_size,
+ (int)statbuf.st_blocks,
+ (int)statbuf.st_blksize);
+ printf("Device: 0x%06llx (%7llu) Inode: %-10li Links: %i\n",
+ statbuf.st_dev,
+ statbuf.st_dev,
+ statbuf.st_ino,
+ statbuf.st_nlink);
+ printf("Access: %c%c%c%c%c%c%c%c%c (0%-5o) User ID: %i(%s) Group ID: %i(%s)\n",
+ statbuf.st_mode & S_IRUSR ? 'r' : '-',
+ statbuf.st_mode & S_IWUSR ? 'w' : '-',
+ statbuf.st_mode & S_ISUID ? (
+ statbuf.st_mode & S_IXUSR ? 's' : 'S'
+ ) : (
+ statbuf.st_mode & S_IXUSR ? 'x' : '-'
+ ),
+ statbuf.st_mode & S_IRGRP ? 'r' : '-',
+ statbuf.st_mode & S_IWGRP ? 'w' : '-',
+ statbuf.st_mode & S_ISGID ? (
+ statbuf.st_mode & S_IXGRP ? 's' : 'S'
+ ) : (
+ statbuf.st_mode & S_IXGRP ? 'x' : '-'
+ ),
+ statbuf.st_mode & S_IROTH ? 'r' : '-',
+ statbuf.st_mode & S_IWOTH ? 'w' : '-',
+ statbuf.st_mode & S_ISVTX ? (
+ statbuf.st_mode & S_IXGRP ? 't' : 'T'
+ ) : (
+ statbuf.st_mode & S_IXGRP ? 'x' : '-'
+ ),
+ statbuf.st_mode,
+ statbuf.st_uid,getpwuid(statbuf.st_uid)->pw_name,
+ statbuf.st_gid,getpwuid(statbuf.st_gid)->pw_name);
+ printf("Access Time: %s", ctime(&statbuf.st_atime));
+ printf("Modify Time: %s", ctime(&statbuf.st_mtime));
+ printf("Change Time: %s", ctime(&statbuf.st_ctime));
+
+ if (close(fd) == -1) {
+ printf("fstat: close(fd): %s (%i)",
+ strerror(errno),
+ errno);
+ return 1;
+ }
+
+ return 0;
+ }
+
+ int main(int argc, char **argv)
+ {
+ int i;
+
+ if (argc<2) {
+ printf("%s: too few arguments\n",argv[[0]);
+ printf("usage: %s filename...\n",argv[[0]);
+ return 1;
+ }
+ for (i=1;i<argc;i++) {
+ do_stat(argv[[i]);
+ }
+ return 0;
+ }
+
!!SEE ALSO
-chmod(2), chown(2), readlink(2), utime(2)
+chmod(2), chown(2), readlink(2), utime(2), fstatfs(2), stat
(2)