C programming Help

S

Spartan815

I am writing a program that does a command line search for a specified
file, or to match a pattern (i.e. search for *.h). Once it finds the
file(s) it has to output absolute pathname, size, owner id, group id,
file permissions, time of last read, time of last write, time of last
change. I have a few questions that if anyone could answer I would be
greatly appreciative.

Below is my code, which is barebones right now because I just started.

1) whats the best way to print out the absolute path?
2) for everything else Im using a stat struct... so for permissions Im
not sure Im using the correct thing.
3) if anyone is familiar with time, and how to get it to print in a
friendly way, that would help greatly! I believe I have to use the
ctime function, but Im not sure how (and the man pages confuse the
hell out of me). Right now, it just prints the time out as the number
of seconds since Jan 1st, 1970.


CODE:

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

int main(int argc, char** argv)
{

struct stat file;
stat(argv[1], &file);

char path[PATH_MAX];

int Size = file.st_size;
int OwnerId = file.st_uid;
int GroupId = file.st_gid;
int Permissions = file.st_mode;
int LastRead = file.st_atime;
int LastWrite = file.st_mtime;
int LastChange = file.st_ctime;

if(errno == 2)
printf("ERROR: No such file or directory\n");
else if(errno == 13)
printf("Permission Denied\n");
else if(errno == 20)
printf("Not a directory\n");
else
{
printf("\nAbsolute Pathname: %s", argv[1], path);
printf("\nFile Size (bytes): %d", Size);
printf("\nFile Owner ID: %d", OwnerId);
printf("\nFile Group ID: %d", GroupId);
printf("\nFile Permissions: %d", Permissions);
printf("\nTime of Last Read: %d", LastRead);
printf("\nTime of Last Write: %d", LastWrite);
printf("\nTime of Last Change: %d \n", LastChange);
}

return(0);

}
 
J

j

Spartan815 said:
I am writing a program that does a command line search for a specified
file, or to match a pattern (i.e. search for *.h). Once it finds the
file(s) it has to output absolute pathname, size, owner id, group id,
file permissions, time of last read, time of last write, time of last
change. I have a few questions that if anyone could answer I would be
greatly appreciative.

Below is my code, which is barebones right now because I just started.

1) whats the best way to print out the absolute path?
2) for everything else Im using a stat struct... so for permissions Im
not sure Im using the correct thing.
3) if anyone is familiar with time, and how to get it to print in a
friendly way, that would help greatly! I believe I have to use the
ctime function, but Im not sure how (and the man pages confuse the
hell out of me). Right now, it just prints the time out as the number
of seconds since Jan 1st, 1970.

You are using stat and also needing a system specific
function to get the absolute path of a file.

This is off-topic for this newsgroup.

Try comp.unix.programmer

For future reference, what is on-topic for this newsgroup is
programming in C specified by ISO/IEC/INCITS 9899:1999
and ISO/IEC/ANSI 9899:1990
CODE:

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

int main(int argc, char** argv)
{

struct stat file;
stat(argv[1], &file);

char path[PATH_MAX];

int Size = file.st_size;
int OwnerId = file.st_uid;
int GroupId = file.st_gid;
int Permissions = file.st_mode;
int LastRead = file.st_atime;
int LastWrite = file.st_mtime;
int LastChange = file.st_ctime;

if(errno == 2)
printf("ERROR: No such file or directory\n");
else if(errno == 13)
printf("Permission Denied\n");
else if(errno == 20)
printf("Not a directory\n");
else
{
printf("\nAbsolute Pathname: %s", argv[1], path);
printf("\nFile Size (bytes): %d", Size);
printf("\nFile Owner ID: %d", OwnerId);
printf("\nFile Group ID: %d", GroupId);
printf("\nFile Permissions: %d", Permissions);
printf("\nTime of Last Read: %d", LastRead);
printf("\nTime of Last Write: %d", LastWrite);
printf("\nTime of Last Change: %d \n", LastChange);
}

return(0);

}
 
O

osmium

Spartan815 said:
3) if anyone is familiar with time, and how to get it to print in a
friendly way, that would help greatly! I believe I have to use the
ctime function, but Im not sure how (and the man pages confuse the
hell out of me). Right now, it just prints the time out as the number
of seconds since Jan 1st, 1970.

I think this should help. The address mentions C++ but the content is,
AFAIK, simply C.

http://www.cplusplus.com/ref/ctime/localtime.html
 
R

Richard Heathfield

Spartan815 said:
3) if anyone is familiar with time, and how to get it to print in a
friendly way, that would help greatly! I believe I have to use the
ctime function, but Im not sure how (and the man pages confuse the
hell out of me). Right now, it just prints the time out as the number
of seconds since Jan 1st, 1970.

If you have a time_t (and you do), you can get a struct tm * from it, like
this:

struct tm *tmt = localtime(&my_time_t_object);

if(tmt != NULL)
{
you now have a pointer to a struct tm object. You can hand this pointer to
strftime, together with format specifiers dictating precisely how you'd
like the time to appear. This will populate a string for you, never writing
more bytes to it than you specify.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,137
Messages
2,570,797
Members
47,342
Latest member
eixataze

Latest Threads

Top