Movatterモバイル変換


[0]ホーム

URL:


Skip Navigation Links
Exit Print View
Link to oracle.comman pages section 2: System Calls    Oracle Solaris 11 Information Library
Oracle Technology Network
Library
PDF
Print View
Feedback
search filter icon
search icon
 Search Scope:

Document Information

Preface

Introduction

System Calls

access(2)

acct(2)

acl(2)

adjtime(2)

alarm(2)

brk(2)

chdir(2)

chmod(2)

chown(2)

chroot(2)

close(2)

creat(2)

dup(2)

exec(2)

execl(2)

execle(2)

execlp(2)

execv(2)

execve(2)

execvex(2)

execvp(2)

_Exit(2)

_exit(2)

exit(2)

faccessat(2)

facl(2)

fchdir(2)

fchmod(2)

fchmodat(2)

fchown(2)

fchownat(2)

fchroot(2)

fcntl(2)

fexecve(2)

fgetlabel(2)

fork1(2)

fork(2)

forkall(2)

forkallx(2)

forkx(2)

fpathconf(2)

fstat(2)

fstatat(2)

fstatvfs(2)

futimens(2)

futimesat(2)

getacct(2)

getcontext(2)

getdents(2)

getegid(2)

geteuid(2)

getgid(2)

getgroups(2)

getisax(2)

getitimer(2)

getlabel(2)

getmsg(2)

getpflags(2)

getpgid(2)

getpgrp(2)

getpid(2)

getpmsg(2)

getppid(2)

getppriv(2)

getprojid(2)

getrctl(2)

getrlimit(2)

getsid(2)

gettaskid(2)

getuid(2)

getustack(2)

ioctl(2)

issetugid(2)

kill(2)

lchown(2)

link(2)

linkat(2)

llseek(2)

lseek(2)

lstat(2)

_lwp_cond_broadcast(2)

_lwp_cond_reltimedwait(2)

_lwp_cond_signal(2)

_lwp_cond_timedwait(2)

_lwp_cond_wait(2)

_lwp_continue(2)

_lwp_info(2)

_lwp_kill(2)

_lwp_mutex_lock(2)

_lwp_mutex_trylock(2)

_lwp_mutex_unlock(2)

_lwp_self(2)

_lwp_sema_init(2)

_lwp_sema_post(2)

_lwp_sema_trywait(2)

_lwp_sema_wait(2)

_lwp_suspend(2)

memcntl(2)

meminfo(2)

mincore(2)

mkdir(2)

mkdirat(2)

mknod(2)

mknodat(2)

mmap(2)

mmapobj(2)

mount(2)

mprotect(2)

msgctl(2)

msgget(2)

msgids(2)

msgrcv(2)

msgsnap(2)

msgsnd(2)

munmap(2)

nice(2)

ntp_adjtime(2)

ntp_gettime(2)

open(2)

openat(2)

pathconf(2)

pause(2)

pcsample(2)

pipe(2)

poll(2)

p_online(2)

ppoll(2)

pread(2)

priocntl(2)

priocntlset(2)

processor_bind(2)

processor_info(2)

profil(2)

pset_assign(2)

pset_bind(2)

pset_create(2)

pset_destroy(2)

pset_getattr(2)

pset_info(2)

pset_list(2)

pset_setattr(2)

putacct(2)

putmsg(2)

putpmsg(2)

pwrite(2)

read(2)

readlink(2)

readlinkat(2)

readv(2)

rename(2)

renameat(2)

resolvepath(2)

rmdir(2)

sbrk(2)

semctl(2)

semget(2)

semids(2)

semop(2)

semtimedop(2)

setcontext(2)

setegid(2)

seteuid(2)

setgid(2)

setgroups(2)

setitimer(2)

setpflags(2)

setpgid(2)

setpgrp(2)

setppriv(2)

setrctl(2)

setregid(2)

setreuid(2)

setrlimit(2)

setsid(2)

settaskid(2)

setuid(2)

setustack(2)

shmadv(2)

shmat(2)

shmctl(2)

shmdt(2)

shmget(2)

shmids(2)

shmop(2)

sigaction(2)

sigaltstack(2)

sigpending(2)

sigprocmask(2)

sigsend(2)

sigsendset(2)

sigsuspend(2)

sigwait(2)

__sparc_utrap_install(2)

stat(2)

statvfs(2)

stime(2)

swapctl(2)

symlink(2)

symlinkat(2)

sync(2)

sysfs(2)

sysinfo(2)

time(2)

times(2)

uadmin(2)

ulimit(2)

umask(2)

umount(2)

umount2(2)

uname(2)

unlink(2)

unlinkat(2)

ustat(2)

utime(2)

utimensat(2)

utimes(2)

uucopy(2)

vfork(2)

vforkx(2)

vhangup(2)

waitid(2)

wracct(2)

write(2)

writev(2)

yield(2)

stat

, lstat

, fstat

, fstatat

- get file status

Synopsis

#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>intstat(const char *restrictpath,struct stat *restrictbuf);
intlstat(const char *restrictpath,struct stat *restrictbuf);
intfstat(intfildes,struct stat *buf);
intfstatat(intfildes,const char *path,struct stat *buf,intflag);

Description

Thestat() function obtains information about the file pointed to bypath.Read, write, or execute permission of the named file is not required,but all directories listed in the path name leading to the filemust be searchable.

Thelstat() function obtains file attributes similar tostat(), except when thenamed file is a symbolic link; in that caselstat() returns informationabout the link, whilestat() returns information about the file the link references.

Thefstat() function obtains information about an open file known by thefile descriptorfildes, obtained from a successfulopen(2),creat(2),dup(2),fcntl(2), orpipe(2) function. Iffildes references a shared memory object, the system updatesin thestat structure pointed to by thebuf argument only thest_uid,st_gid,st_size, andst_mode fields, and only theS_IRUSR,S_IWUSR,S_IRGRP,S_IWGRP,S_IROTH, andS_IWOTH file permission bits need be valid. The systemcan update other fields and flags. Thefstat() function updates any pending time-relatedfields before writing to thestat structure.

Thefstatat() function obtains file attributes similar to thestat(),lstat(), andfstat() functions. If thepath argument is a relative path, itis resolved relative to thefildes argument rather than the current working directory. Ifpath is absolute, thefildes argument is unused. Ifthefildes argument has the special valueAT_FDCWD, relative paths are resolvedfrom the current working directory. IfAT_SYMLINK_NOFOLLOW is set in theflagargument, the function behaves likelstat() and does not automatically follow symboliclinks. Seefsattr(5). If_AT_TRIGGER is set in theflag argumentand the vnode is a trigger mount point, the mount is performedand the function returns the attributes of the root of the mountedfilesystem.

Thebuf argument is a pointer to astat structure into whichinformation is placed concerning the file. Astat structure includes the followingmembers:

mode_t   st_mode;          /* File mode (see mknod(2)) */ino_t    st_ino;           /* Inode number */dev_t    st_dev;           /* ID of device containing */                           /* a directory entry for this file */dev_t    st_rdev;          /* ID of device */                           /* This entry is defined only for */                           /* char special or block special files */nlink_t  st_nlink;         /* Number of links */uid_t    st_uid;           /* User ID of the file's owner */gid_t    st_gid;           /* Group ID of the file's group */off_t    st_size;          /* File size in bytes */time_t   st_atime;         /* Time of last access */time_t   st_mtime;         /* Time of last data modification */time_t   st_ctime;         /* Time of last file status change */                           /* Times measured in seconds since */                           /* 00:00:00 UTC, Jan. 1, 1970 */long     st_blksize;       /* Preferred I/O block size */blkcnt_t st_blocks;        /* Number of 512 byte blocks allocated*/char     st_fstype[_ST_FSTYPSZ];                           /* Null-terminated type of filesystem */

Descriptions of structure members are as follows:

st_mode

The mode of the file as described for themknod() function. In addition to the modes described on themknod(2) manual page, the mode of a file can also beS_IFSOCK if the file is a socket,S_IFDOOR if the file is a door,S_IFPORT if the file is an event port, orS_IFLNK if the file is a symbolic link.S_IFLNK can be returned either bylstat() or byfstat() when theAT_SYMLINK_NOFOLLOW flag is set.

st_ino

This field uniquely identifies the file in a given file system. The pairst_ino andst_dev uniquely identifies regular files.

st_dev

This field uniquely identifies the file system that contains the file. Its value may be used as input to theustat() function to determine more information about this file system. No other meaning is associated with this value.

st_rdev

This field should be used only by administrative commands. It is valid only for block special or character special files and only has meaning on the system where the file was configured.

st_nlink

This field should be used only by administrative commands.

st_uid

The user ID of the file's owner.

st_gid

The group ID of the file's group.

st_size

For regular files, this is the address of the end of the file. For block special or character special, this is not defined. See alsopipe(2).

st_atime

Time when file data was last accessed. Some of the functions that change this member are:creat(),mknod(),pipe(),utime(2), andread(2).

st_mtime

Time when data was last modified. Some of the functions that change this member are:creat(),mknod(),pipe(),utime(), andwrite(2).

st_ctime

Time when file status was last changed. Some of the functions that change this member are:chmod(2),chown(2),creat(2),link(2),mknod(2),pipe(2),rename(2),unlink(2),utime(2), andwrite(2).

st_blksize

A hint as to the “best” unit size for I/O operations. This field is not defined for block special or character special files.

st_blocks

The total number of physical blocks of size 512 bytes actually allocated on disk. This field is not defined for block special or character special files.

st_fstype

A null-teminated string that uniquely identifies the type of the filesystem that contains the file.

Return Values

Upon successful completion,0 is returned. Otherwise,-1 is returned anderrnois set to indicate the error.

Errors

Thestat(),fstat(),lstat(), andfstatat() functions will fail if:

EIO

An error occurred while reading from the file system.

EOVERFLOW

The file size in bytes or the number of blocks allocated to the file or the file serial number cannot be represented correctly in the structure pointed to bybuf.

Thestat(),lstat(), andfstatat() functions will fail if:

EACCES

Search permission is denied for a component of the path prefix.

EFAULT

Thebuf orpath argument points to an illegal address.

EINTR

A signal was caught during the execution of thestat() orlstat() function.

ELOOP

A loop exists in symbolic links encountered during the resolution of thepath argument.

ENAMETOOLONG

The length of thepath argument exceeds {PATH_MAX}, or the length of apath component exceeds {NAME_MAX} while_POSIX_NO_TRUNC is in effect.

ENOENT

A component ofpath does not name an existing file orpath is an empty string.

ENOLINK

Thepath argument points to a remote machine and the link to that machine is no longer active.

ENOTDIR

A component of the path prefix is not a directory, or thefildes argument does not refer to a valid directory when given a non-null relative path.

Thefstat() andfstatat() functions will fail if:

EBADF

Thefildes argument is not a valid open file descriptor. Thefildes argument tofstatat() can also have the valid value ofAT_FDCWD.

EFAULT

Thebuf argument points to an illegal address.

EINTR

A signal was caught during the execution of thefstat() function.

ENOLINK

Thefildes argument points to a remote machine and the link to that machine is no longer active.

Thestat(),fstat(), andlstat() functions may fail if:

EOVERFLOW

One of the members is too large to store in thestat structure pointed to bybuf.

Thestat() andlstat() functions may fail if:

ELOOP

More than {SYMLOOP_MAX} symbolic links were encountered during the resolution of thepath argument.

ENAMETOOLONG

As a result of encountering a symbolic link in resolution of thepath argument, the length of the substituted pathname strings exceeds {PATH_MAX}.

Thestat() andfstatat() functions may fail if:

ENXIO

Thepath argument names a character or block device special file and the corresponding I/O device has been retired by the fault management framework.

Examples

Example 1 Usestat() to obtain file status information.

The following example shows how to obtain file status information for afile named/home/cnd/mod1. The structure variable buffer is defined for thestatstructure.

#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>struct stat buffer;int         status;...status = stat("/home/cnd/mod1", &buffer);

Example 2 Usestat() to get directory information.

The following example fragment gets status information for each entry in adirectory. The call to thestat() function stores file information in thestat structure pointed to bystatbuf. The lines that follow thestat()call format the fields in thestat structure for presentation to the userof the program.

#include <sys/types.h>#include <sys/stat.h>#include <dirent.h>#include <pwd.h>#include <grp.h>#include <time.h>#include <locale.h>#include <langinfo.h>#include <stdio.h>#include <stdint.h>struct dirent *dp;struct stat   statbuf;struct passwd *pwd;struct group  *grp;struct tm     *tm;char          datestring[256];.../* Loop through directory entries */while ((dp = readdir(dir)) != NULL) {    /* Get entry's information. */    if (stat(dp->d_name, &statbuf) == -1)    continue;      /* Print out type, permissions, and number of links. */     printf("%10.10s", sperm (statbuf.st_mode));     printf("%4d", statbuf.st_nlink);      /* Print out owners name if it is found using getpwuid(). */     if ((pwd = getpwuid(statbuf.st_uid)) != NULL)        printf(" %-8.8s", pwd->pw_name);     else        printf(" %-8d", statbuf.st_uid);      /* Print out group name if it's found using getgrgid(). */     if ((grp = getgrgid(statbuf.st_gid)) != NULL)        printf(" %-8.8s", grp->gr_name);     else        printf(" %-8d", statbuf.st_gid);     /* Print size of file. */     printf(" %9jd", (intmax_t)statbuf.st_size);     tm = localtime(&statbuf.st_mtime);      /* Get localized date string. */     strftime(datestring, sizeof(datestring), nl_langinfo(D_T_FMT), tm);      printf(" %s %s\n", datestring, dp->d_name); }

Example 3 Usefstat() to obtain file status information.

The following example shows how to obtain file status information for afile named/home/cnd/mod1. The structure variable buffer is defined for thestatstructure. The/home/cnd/mod1 file is opened with read/write privileges and is passed to theopen file descriptorfildes.

#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>struct stat buffer;int         status;...fildes = open("/home/cnd/mod1", O_RDWR);status = fstat(fildes, &buffer);

Example 4 Uselstat() to obtain symbolic link status information.

The following example shows how to obtain status information for a symboliclink named/modules/pass1. The structure variable buffer is defined for thestatstructure. If thepath argument specified the filename for the file pointed toby the symbolic link (/home/cnd/mod1), the results of calling the function wouldbe the same as those returned by a call to thestat()function.

#include <sys/stat.h>struct stat buffer;int         status;...status = lstat("/modules/pass1", &buffer);

Usage

Ifchmod() orfchmod() is used to change the file group ownerpermissions on a file with non-trivial ACL entries, only the ACL maskis set to the new permissions and the group owner permission bitsin the file's mode field (defined inmknod(2)) are unchanged. Anon-trivial ACL entry is one whose meaning cannot be represented in thefile's mode field alone. The new ACL mask permissions might change theeffective permissions for additional users and groups that have ACL entries onthe file.

Thestat(),fstat(), andlstat() functions have transitional interfaces for 64-bit fileoffsets. Seelf64(5).

Attributes

Seeattributes(5) for descriptions of the following attributes:

ATTRIBUTE TYPE
ATTRIBUTE VALUE
Interface Stability
Committed
MT-Level
Async-Signal-Safe
Standard
Seebelow.

Forstat(),fstat(), andlstat(), seestandards(5).

See Also

access(2),chmod(2),chown(2),creat(2),link(2),mknod(2),pipe(2),read(2),time(2),unlink(2),utime(2),write(2),fattach(3C),stat.h(3HEAD),attributes(5),fsattr(5),lf64(5),standards(5)

Copyright © 2011, Oracle and/or its affiliates. All rights reserved.Legal Notices
PreviousNext

[8]ページ先頭

©2009-2025 Movatter.jp