LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   A newbie plz help me out(C programming in linux) (https://www.linuxquestions.org/questions/programming-9/a-newbie-plz-help-me-out-c-programming-in-linux-461275/)

tirtha 07-05-2006 02:55 PM

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.

taylor_venable 07-05-2006 03:17 PM

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.

theNbomr 07-05-2006 11:16 PM

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.

sundialsvcs 07-06-2006 09:06 AM

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...) :D

taylor_venable 07-06-2006 09:13 AM

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.

theNbomr 07-06-2006 10:36 AM

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.

introuble 07-06-2006 02:21 PM

Quote:

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


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