linux stat例程解读文件读写权限

/* ************************************************************************
* Filename: stat2.c
* Description:
* Version: 1.0
* Created: 2011年05月11日 16时03分23秒
* Revision: none
* Compiler: gcc
* Author: wenhao (wh), hnrain1004@gmail.com
* Company: sunplusapp
* ***********************************************************************
*/


#include
<stdio.h>
#include
<stdlib.h>
#include
<unistd.h>
#include
<sys/stat.h>
#include
<sys/types.h>
int main(int argc, char *argv[])
{
struct stat buf;
if(argc < 2)
{
printf(
"please input filepath!\n");
exit(
1);
}

if(stat(argv[1],&buf)<0)
{
printf(
"file not exiting.!\n");
exit(
1);
}


if(buf.st_mode&S_IRUSR)
printf(
"user can read!\n");
if(buf.st_mode&S_IWUSR)
printf(
"user can write!\n");
if(buf.st_mode&S_IXUSR)
printf(
"user can execute!\n");

if(buf.st_mode&S_IRGRP)
printf(
"group can read!\n");
if(buf.st_mode&S_IWGRP)
printf(
"group can write!\n");
if(buf.st_mode&S_IXGRP)
printf(
"group can execute!\n");

if(buf.st_mode&S_IROTH)
printf(
"other can read!\n");
if(buf.st_mode&S_IWOTH)
printf(
"other can write!\n");
if(buf.st_mode&S_IXOTH)
printf(
"other can execute!\n");



return 0;
}

posted @ 2011-05-11 16:29  hnrainll  阅读(3031)  评论(0编辑  收藏  举报