I came across this post discussing how to print file permissions from the stat
library, but I'm confused on how the definition of the struct works.
When I've defined structs, I've always defined them like struct struct_type *name
. But in the example the struct is defined like struct struct_type name
(without being a pointer). From what I can tell, there should be two different ways to access the information depending on how you define the struct.
Option One Notation
struct stat fileStat; //definition of struct
stat("<filename>", &fileStat); //execute the stat function
fileStat.member_name; //access a member of a struct
Option Two Notation
struct stat *fileStat; //definition of struct
stat("<filename>", fileStat); //execute the stat function
fileStat->member_name; //access a member of a struct
BUT when I switch things from option one notation in the provided example to option two I get VERY different results. What is different about these two options? Isn't it effectively doing the same thing?
Example Code
Option One Notation Program
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(int argc, char **argv)
{
struct stat fileStat;
stat("main", &fileStat);
printf("Information for %s\n", argv[1]);
printf("---------------------------\n");
printf("File Size: \t\t%lld bytes\n", fileStat.st_size);
printf("Number of Links: \t%d\n", fileStat.st_nlink);
printf("File inode: \t\t%llu\n", fileStat.st_ino);
printf("File Permissions: \t");
printf( (S_ISDIR(fileStat.st_mode)) ? "d" : "-");
printf( (fileStat.st_mode & S_IRUSR) ? "r" : "-");
printf( (fileStat.st_mode & S_IWUSR) ? "w" : "-");
printf( (fileStat.st_mode & S_IXUSR) ? "x" : "-");
printf( (fileStat.st_mode & S_IRGRP) ? "r" : "-");
printf( (fileStat.st_mode & S_IWGRP) ? "w" : "-");
printf( (fileStat.st_mode & S_IXGRP) ? "x" : "-");
printf( (fileStat.st_mode & S_IROTH) ? "r" : "-");
printf( (fileStat.st_mode & S_IWOTH) ? "w" : "-");
printf( (fileStat.st_mode & S_IXOTH) ? "x" : "-");
printf("\n\n");
printf("The file %s a symbolic link\n", (S_ISLNK(fileStat.st_mode)) ? "is" : "is not");
return 0;
}
Option One Notation Output
Information for (null)
---------------------------
File Size: 49960 bytes
Number of Links: 1
File inode: 58527531
File Permissions: -rwxr-xr-x
The file is not a symbolic link
Option Two Notation Program
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(int argc, char **argv)
{
struct stat *fileStat;
stat("main", fileStat);
printf("Information for %s\n", argv[1]);
printf("---------------------------\n");
printf("File Size: \t\t%lld bytes\n", fileStat->st_size);
printf("Number of Links: \t%d\n", fileStat->st_nlink);
printf("File inode: \t\t%llu\n", fileStat->st_ino);
printf("File Permissions: \t");
printf( (S_ISDIR(fileStat->st_mode)) ? "d" : "-");
printf( (fileStat->st_mode & S_IRUSR) ? "r" : "-");
printf( (fileStat->st_mode & S_IWUSR) ? "w" : "-");
printf( (fileStat->st_mode & S_IXUSR) ? "x" : "-");
printf( (fileStat->st_mode & S_IRGRP) ? "r" : "-");
printf( (fileStat->st_mode & S_IWGRP) ? "w" : "-");
printf( (fileStat->st_mode & S_IXGRP) ? "x" : "-");
printf( (fileStat->st_mode & S_IROTH) ? "r" : "-");
printf( (fileStat->st_mode & S_IWOTH) ? "w" : "-");
printf( (fileStat->st_mode & S_IXOTH) ? "x" : "-");
printf("\n\n");
printf("The file %s a symbolic link\n", (S_ISLNK(fileStat->st_mode)) ? "is" : "is not");
return 0;
}
Option Two Notation Output
Information for (null)
---------------------------
File Size: 5193343115435036343 bytes
Number of Links: 12487
File inode: 930377443599221576
File Permissions: -r-x--x---
The file is not a symbolic link