LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

stat(2)

Last updated at Posted at 2015-03-16

int stat(const char *pathname, struct stat *buf);

stat 構造体
/usr/include/bits/stat.h


#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>

struct stat info;

if( 0 < stat("file_name", &info) ) {
  perror("stat");
  return 1;
}

mode_t mod = info.st_mode;
if( (mod & S_IFMT) == S_IFREG) puts("regular file");
if( (mod & S_IFMT) == S_IFDIR) puts("directory");
if( (mod & S_IFMT) == S_IFCHR) puts("char device");
if( (mod & S_IFMT) == S_IFBLK) puts("block device");
if( (mod & S_IFMT) == S_IFLNK) puts("simbolic link");
if( (mod & S_IFMT) == S_IFSOCK) puts("socket");


fputs("owner access mode: ", stdout);
(mod & S_IRUSR & S_IRUSR) ? putchar('r') : putchar('-');
(mod & S_IWUSR & S_IWUSR) ? putchar('w') : putchar('-');
(mod & S_IXUSR & S_IXUSR) ? putchar('x') : putchar('-');
putchar('\n');

JM Project
http://linuxjm.sourceforge.jp/html/LDP_man-pages/man2/stat.2.html

stat - システムコールの説明 - Linux コマンド集 一覧表
http://kazmax.zpp.jp/cmd/s/stat.2.html

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up