LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-26-2016, 07:49 AM   #1
justin0212
LQ Newbie
 
Registered: Feb 2016
Posts: 2

Rep: Reputation: Disabled
Combine code and to get file modify, access and change timestamp result


Hi, I am a newbie in Linux. I hope someone can guide me through. I wanted to do a C program such that it can display the following output:
1) the type of file
2) access permissions
3) size of the file
4) inode number
5) modify, access and change timestamp.

I got some code from the internet and combine a little of my edited code.

Code:
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>

int main( int argc, char *argv[] )
{ 
int i; struct stat buf; char *p;
for (i = 1; i < argc; i++)
{ 
printf("%s: ", argv[i]);
if ( lstat(argv[i], &buf) < 0 ) 
{ 
perror("lstat error"); continue; 
}
if( S_ISREG( buf.st_mode ) ) p = "regular file";
else if ( S_ISDIR( buf.st_mode ) ) p = "directory";
else if ( S_ISCHR( buf.st_mode ) ) p = "character special";
else if ( S_ISBLK( buf.st_mode ) ) p = "block special";
else if ( S_ISFIFO( buf.st_mode ) ) p = "fifo";
else if ( S_ISLNK( buf.st_mode ) ) p = "symbolic link";
else if ( S_ISSOCK( buf.st_mode ) ) p = "socket";
else p = "**unknown file type";

}

int main(int argc, char **argv)
{
    if(argc != 2)    
        return 1;

    struct stat fileStat;
    if(stat(argv[1],&fileStat) < 0)    
        return 1;

    printf("Output Data %s\n",argv[1]);
    printf("---------------------------\n");
    printf("Size of the File: \t\t%d bytes\n",fileStat.st_size);
    printf("No. of Links: \t%d\n",fileStat.st_nlink);
    printf("File inode: \t\t%d\n",fileStat.st_ino);
    printf("%s\n", p); /* print file type */

    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_model & S_IWOTH) ? "w" : "-");
    printf( (fileStat.st_mode & S_IXOTH) ? "x" : "-");
    printf("\n\n");


    return 0;
}


exit(0); 
}
I have done the first four points however, when I tried to compile and run, there are some errors.
Quote:
test.c: In function ‘main’:
test.c:40:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘__off_t’ [-Wformat=]
printf("File Size: \t\t%d bytes\n",fileStat.st_size);
^
test.c:41:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘__nlink_t’ [-Wformat=]
printf("Number of Links: \t%d\n",fileStat.st_nlink);
^
test.c:42:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘__ino_t’ [-Wformat=]
printf("File inode: \t\t%d\n",fileStat.st_ino);
^
test.c:54:22: error: ‘struct stat’ has no member named ‘st_model’
printf( (fileStat.st_model & S_IWOTH) ? "w" : "-");
And as for the last point, I am aware that I need to use "stat" to run but I am unsure how to do so. Please assist me. Thanks

Last edited by justin0212; 02-27-2016 at 02:00 AM. Reason: able to identify one error however, error still persisit
 
Old 02-28-2016, 03:10 PM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,720

Rep: Reputation: 1282Reputation: 1282Reputation: 1282Reputation: 1282Reputation: 1282Reputation: 1282Reputation: 1282Reputation: 1282Reputation: 1282
All of those are integer data types, and what you can do is cast them into int to get rid of the warnings. it
would pay to check the sizes as size_t may be a long long depending on your system.

tricky hint, you can always print "sizeof(off_t)" to see how many bytes it is, if 4, then it is an int, if 8 it is a long long. You can use the same check to see what the sizes are of the others too.

And your st_model is a typographic error.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between Access Modify Change ? hq4ever Linux - General 5 01-17-2010 02:42 AM
Change access.log timestamp,squid proxy gordonb Linux - Software 1 04-20-2006 12:28 AM
access an modify a file from another location superstein Linux - General 1 04-16-2006 08:51 AM
which file to edit to modify source code in red hat 9.0 vinaymudgil007 Linux - Software 1 11-17-2005 06:38 AM
modify file access & modify timestamps i2itstud Linux - General 1 05-20-2003 03:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration