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);
}
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);
}