473,659 Members | 2,681 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C programming Help

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("Permiss ion Denied\n");
else if(errno == 20)
printf("Not a directory\n");
else
{
printf("\nAbsol ute 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);

}
Nov 14 '05 #1
3 5320
j

"Spartan815 " <ab************ @hotmail.com> wrote in message
news:d8******** *************** ***@posting.goo gle.com...
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.progr ammer

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("Permiss ion Denied\n");
else if(errno == 20)
printf("Not a directory\n");
else
{
printf("\nAbsol ute 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);

}

Nov 14 '05 #2
Spartan815 writes:
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
Nov 14 '05 #3
Spartan815 wrote:
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_t ime_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.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2423
by: Mark | last post by:
Hello. I am new to programming and Python and was wondering if someone could help get me started. I picked Python to start learning to prgram because of some things I have read about it (easy to learn, object oriented, clear syntax, etc...). Can anyone assist in getting me started with learning to program and Python? Recommended reading material? Online tutorials? Recommended development tools (wxpython, pythonwin, etc...)? I am a...
2
1836
by: Mike Maxwell | last post by:
(apologies if this is not the right place to ask--suggestions on a more appropriate venue welcome!) I would like to do some literate programming-type writing, probably using the DocBook format. The paper by Norman Walsh entitled "Literate Programming in XML" seems to be a good way to do that, using a couple small namespace extensions (src:fragment and src:fragref). I could write it in raw XML if need be, but I'd rather use s.t. like...
12
8467
by: G. | last post by:
Hi all, During my degree, BEng (Hons) Electronics and Communications Engineering, we did C programming every year, but I never kept it up, as I had no interest and didn't see the point. But now I really want to get back into it as I see a point with GNU/Linux. I want to get my old skills back and write something or help on some projects etc. I need some good books. I used to have one called "A Book On C", but sold it,
3
2473
by: user | last post by:
Hi all, At the outset, I regret having to post this slightly OT post here. However, I strongly feel that people in this group would be the best to advise me on my predicament. I am working as a QA in an MNC in India, since I graduated in September 2003. I am working in a QA role which requires me to do some Winrunner automation, and modifying/creating a framework in Perl for Automated Regression Testing of some command Line utilities...
26
2240
by: Kanthi Kiran Narisetti | last post by:
Hi ALL, I am new to C and programming . I need suggestions for the IDE to be used as a begginer and should I start on Windows or Linux ?? I am also looking for resources for C programming on Internet with examples on each concept. I appreciate if any one can help me in this regard.
42
2883
by: Kevin Spencer | last post by:
Is it just me, or am I really observing a trend away from analysis and probem-solving amongst programmers? Let me be more specific: It seems that every day, in greater numbers, people are coming to these programming newsgroups and asking for ready-made solutions to fairly simple programming problems. They either want someone to write code for them, or point them to a ready-made chunk of software that does what they need to do. Now, I'm...
25
2574
by: Dave | last post by:
I want to spend two minutes on my soapbox. I love C#. I am so productive in it it's ridiculous. But it's so easy to write code that uses poor design principles. Example: just making variables public instead of using properties. Another example: passing references to forms to other classes so they can modify things (tight coupling). So in essence, there are so many ways to do things that many of them are just flat-out poor...
20
2196
by: Mike | last post by:
Hi all, I am cramming for an technical interview about C++ and programming... I believe there will be problems on data-structures and algorithms... could anybody recommend some online resources/references/websites so that I can find summary sheet about the confusing points of C++ and typical storage/time complexity of common data structures and common algorithms? I do have those big textbooks but I found they don't provide a fast...
13
2034
by: 1jasong | last post by:
Hello I am new to programming and want to know what is a good free compiler is. I am asking this because there are so many of them and I just want to start programming also a good website for programming to help me, I want to major in the computer programming field and want to know if there are any really good tutorial sites. Thanks your input is much appriciated.
139
5912
by: Joe Mayo | last post by:
I think I become more and more alone... Everybody tells me that C++ is better, because once a project becomes very large, I should be happy that it has been written in C++ and not C. I'm the only guy thinking that C is a great programming language and that there is no need to program things object oriented. Many people says also that they save more time by programming projects object oriented, but I think its faster to program them in a...
1
8535
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8629
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4338
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2757
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.