Reputation: 247
I am using linux to make a simple web server. I want to know what functions I should use to get a file's right of readablity.
Upvotes: 1
Views: 1672
Reputation: 369
You should use stat function, or fstat if you want to use file descriptor instead of path.
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
int main()
{
char *f = "test.ts";
struct stat *buff = malloc(sizeof(struct stat));
if (stat(f,buff) < 0)
return 1;
printf("Information for %s\n",f);
printf("File Permissions: \t");
printf( (S_ISDIR(buff->st_mode)) ? "d" : "-");
printf( (buff->st_mode & S_IRUSR) ? "r" : "-");
printf( (buff->st_mode & S_IWUSR) ? "w" : "-");
printf( (buff->st_mode & S_IXUSR) ? "x" : "-");
printf( (buff->st_mode & S_IRGRP) ? "r" : "-");
printf( (buff->st_mode & S_IWGRP) ? "w" : "-");
printf( (buff->st_mode & S_IXGRP) ? "x" : "-");
printf( (buff->st_mode & S_IROTH) ? "r" : "-");
printf( (buff->st_mode & S_IWOTH) ? "w" : "-");
printf( (buff->st_mode & S_IXOTH) ? "x\n" : "-\n");
return 0;
}
Upvotes: 1
Reputation:
#include<stdio.h>
#include<sys/stat.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
int i=0;
struct stat buf;
char ptr[]="test.txt";
printf("%s: ",ptr);
if (stat(ptr, &buf) < 0)
{
perror("stat error\n");
return;
}
if(buf.st_mode & S_IROTH)
{
printf("The Others have a read permission for file %s\n",ptr);
}
exit(0);
}
In this above example gives others have a read permission or not for the file test.txt in current directory.
Using the following macros to check permissions for user and others and group.
S_IRWXU - Read, write and execute permissions for owner
S_IRUSR - Owner has read permission
S_IWUSR - Owner has write permission
S_IXUSR - Owner has execute permission
S_IRWXG - Read, write and execute permissions for group
S_IRGRP - Group has read permission
S_IWGRP - Group has write permission
S_IXGRP - Group has execute permission
S_IRWXO - Read, write and execute permissions for others
S_IROTH - Others have read permission
S_IWOTH - Others have write permission
S_IXOTH - Others have execute permission
Using the above macros to modify the code based on what you want in the if condition in the example(if(buf.st_mode & S_IROTH)).
Upvotes: 0
Reputation: 172628
You can try like this:
su username -c 'ls /long/dir/user/yourfilename'
or
su username -s /bin/sh -c 'ls /long/dir/user/yourfilename'
Upvotes: 0