LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-05-2006, 02:55 PM   #1
tirtha
LQ Newbie
 
Registered: Mar 2006
Posts: 10

Rep: Reputation: 0
A newbie plz help me out(C programming in linux)


I am stuck in understanding a code given in one of the leading books on linux programming, the program is to print the content of a dir. the place I dont understand , I marked with `?`. I use FC4. Here goes the code :

#include<unistd.h>
#include<stdio.h>
#include<dirent.h>
#include<string.h>
#include<sys/stat.h>
#include<stdlib.h>

void printdir(char *dir, int depth)//What depth refer?
{
DIR *dp;
struct dirent *entry;
struct stat statbuff;
if((dp = opendir(dir)) == NULL)
{
fprintf(stderr,"cannot open dir %s\n",dir);
return;
}
chdir(dir);
while((entry=readdir(dp)) != NULL)
/* what about initialisation of dp*/
{
lstat(entry->d_name,&statbuff);
/* what about initialisation of entry ?*/
if(S_ISDR(statbuf.st_mode))
{
if(strcpm(".",entry->d_name)==0 || strcmp("..",entry->d_name)==0 )
printf("%*s%s/\n",depth,"",entry->d_name);
printdir(entry->d_name,depth+4);
}
else
printf("%*s%s\n",depth,"",entry->d_name);
}
chdir("..");
closedir(dp);
}

int main()
{
printdir("/home",0);
exit(0);
}



plz help me....
can anyone provide me a link from where I can start linux programming.

Last edited by tirtha; 07-05-2006 at 03:43 PM.
 
Old 07-05-2006, 03:17 PM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
The initializations you're referring to are assignments which occur inside the conditional "if" and "while" statements. In C, the binary assignment operator ("=") returns the value of the right-hand side, in addition to performing the assignment to the left-hand side as a side-effect. Thus, you commonly see things like "while ((c = fgetc(fp)) != EOF)" where the result of fgetc(fp) is assigned to c and then compared against EOF.

Last edited by taylor_venable; 07-05-2006 at 03:18 PM.
 
Old 07-05-2006, 11:16 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Code:
DIR *dp;
struct dirent *entry;
struct stat statbuff;
if((dp = opendir(dir)) == NULL)
{
fprintf(stderr,"cannot open dir %s\n",dir);
return;
}
chdir(dir);
while((entry=readdir(dp)) != NULL)   /*  <========= entry init'd here   */
/* what about initialisation of dp*/
{
lstat(entry->d_name,&statbuff);
/* what about initialisation of entry ?*/
if(S_ISDR(statbuf.st_mode))
{
'entry' is assigned the return value from 'readdir(dp)'.

It is helpful to the readers of your questions if you place [ CODE] [ /CODE] wrappers around your source code.

--- rod.
 
Old 07-06-2006, 09:06 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,672
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
Something else to think about...

Even though C/C++ provide these "helpful" constructions, they are confusing (as you saw demonstrated), and you might well elect not to use them. In any modern compiler, they won't make any difference in the final code.

What matters .. what matters most above all .. is that your code is clear, expressive of what you want to do, and maintainable (long after you've been smooshed by a bread-truck .. or, if you prefer, long after you've cashed in your stock-options and retired to Pango-Pango where you are now idly using solid gold coins as poker-chips). So, if you find constructions like these confusing, don't use them in your code (but do be prepared to see them in somebody else's). When you write your code, put meaningful comments in it. Try to write as little of your code as possible at 1:30 AM.

(Wait until at least 3:30 AM...)
 
Old 07-06-2006, 09:13 AM   #5
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Quote:
Originally Posted by theNbomr
Code:
while((entry=readdir(dp)) != NULL)   /*  <========= entry init'd here   */
This is somewhat nitpicky, but with all due respect to theNbomr, this technically isn't initialization. entry is considered an uninitialized variable because it isn't assigned when it is created and thus initially contains garbage.

Last edited by taylor_venable; 07-06-2006 at 09:13 AM. Reason: Was missing end "code" tag.
 
Old 07-06-2006, 10:36 AM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
t_v, you pick a mean nit :-) and many would feel insulted by your exquisite pedantry. However, you are quite correct. I inadvertantly used the term 'initialize', because it was the word used by the original poster.

--- rod.
 
Old 07-06-2006, 02:21 PM   #7
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Rep: Reputation: 31
Quote:
I am stuck in understanding a code given in one of the leading books on linux programming,
And what book might that be?
 
  


Reply



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
Linux Programming tools - Plz help monty_neo Programming 1 07-17-2004 06:05 AM
Plz help a linux newbie! I have no sound!!!! AtomiCTheGr8 Linux - Newbie 11 07-31-2002 08:04 AM
Newbie to LINUX LAN/WAN Networking, plz help! lax2sman Linux - Networking 7 02-11-2002 01:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:16 PM.

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
Open Source Consulting | Domain Registration