Help Please - Printing the path for links

This is a discussion on Help Please - Printing the path for links within the Linux Programming forums, part of the Platform Specific Boards category; Hi I have written this directory listing. The problem I have is that i am trying to print the path ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    9

    Help Please - Printing the path for links

    Hi I have written this directory listing. The problem I have is that i am trying to print the path for the links.

    e.g. like this: "Link to filedata -> /home/user/files/filedata".

    All its printing at the moment is "Link to filedata ->".

    I dont know much about C programming but I know this code works fine as I took it directly from another application where it works fine. Its driving me mad. Please could someone point out what i need to change to make it work.
    Last edited by lewiso; 03-04-2008 at 03:47 PM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    This is Linux, is it not? And it's not C#, it's C.
    Here's a properly indented version:

    Code:
    #include <dirent.h>
    #include <stdio.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <pwd.h>
    #include <grp.h>
    #include <time.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <errno.h>
    #include <unistd.h>
    
    int lsdetails(struct stat *statbuff);
    int outputfiletype(mode_t filetype);
    int recls(char *path);
    int printperms(mode_t mode);
    char monthprint[4];
    int main(int argc, char **argv){
    
    	printf("\n");
    	if (argc<2)
    	{
    		recls(".");
    		return(0);
    	}
    	recls(argv[1]);
    }
    
    int recls(char *path)	{  		/* function expects char as argument 			 */
    	DIR *dir;			/* Declare DIR function, create pointer "*dir"	  	 */			
    	struct dirent *dirslot;		/* Declare struct "dirent", create "*dirslot"		 */
    	struct stat statbuff;		/* Declare struct "stat", create "statbuff"		 */
    	int islnk = 0;
    	char lbuffer[256];
    	char *name;
    
    	chdir(path);
    	dir=opendir(".");		
    	if (dir == NULL) {
    		printf("\nNo such directory\n");
    		return(1);
    	}
    	while (1) {		
    		dirslot=readdir(dir);
    
    
    
    		if (dirslot==NULL) break;//note change	
    
    		lstat(dirslot->d_name, &statbuff); 
    
    		if (strcmp(dirslot->d_name, ".")==0) continue;  
    		if (strcmp(dirslot->d_name, "..")==0) continue;   
    
    		if (S_ISLNK(statbuff.st_mode))  {
    			lsdetails(&statbuff);
    			printf("\033[36m&#37;s\033[m", dirslot->d_name);
    		}
    
    
    		if ((statbuff.st_mode & S_IFMT) == S_IFLNK) {
    			islnk = 1;
    			memset(lbuffer, 0, 256);
    			readlink(name, lbuffer, 256);
    			printf("%s %s \n", (islnk) ? " -> " : "", (islnk) ? lbuffer : " ");
    		}
    
    		if (S_ISREG(statbuff.st_mode))  {
    			lsdetails(&statbuff);
    			printf("\033[0m%s\033[m\n", dirslot->d_name);
    		}
    
    		if (S_ISDIR(statbuff.st_mode))  {
    			lsdetails(&statbuff);
    			printf("\033[34;42m%s\033[m\n", dirslot->d_name);
    		}
    
    		if (S_ISCHR(statbuff.st_mode))  {
    			lsdetails(&statbuff);
    			printf("\033[01;32m%s\033[m\n", dirslot->d_name);
    		}
    
    		/*if (dirslot->d_type == DT_UNKNOWN) {
    		lsdetails(&statbuff);
    		printf("\033[0m%s\033[m\n", dirslot->d_name);	
    		}
    		if (dirslot->d_type == DT_FIFO) {
    		lsdetails(&statbuff);
    		printf("\033[0m%s\033[m\n", dirslot->d_name);	
    		}
    		if (dirslot->d_type == DT_CHR) {
    		lsdetails(&statbuff);
    		printf("\033[01;33m%s\033[m\n", dirslot->d_name);	
    		}
    
    		if (dirslot->d_type == DT_BLK) {
    		lsdetails(&statbuff);
    		printf("\033[0m%s\033[m\n", dirslot->d_name);	
    		}
    
    		if (dirslot->d_type == DT_WHT) {
    		lsdetails(&statbuff);
    		printf("\033[0m%s\033[m\n", dirslot->d_name);	
    		}*/
    
    
    	}
    
    
    	rewinddir(dir);					   /* rewind the directory stream, start from the top */	
    	while (1) {
    		dirslot=readdir(dir);
    		if (dirslot==NULL) return(1);
    		if (strcmp(dirslot->d_name, ".")==0) continue;     /* if d_name equals ".", skip rest of loop */
    		if (strcmp(dirslot->d_name, "..")==0) continue;    /* if d_name equals "..", skip rest of loop */
    
    		lstat(dirslot->d_name, &statbuff);
    
    		if (S_ISDIR(statbuff.st_mode))  {
    			printf("\n./\033[01;34m%s\033[m\n", dirslot->d_name);
    
    
    			if (chdir(dirslot->d_name)==-1) {
    				perror("Error: chdir");
    				return;}
    
    			recls(".");
    			chdir("..");
    
    		}        	
    	}
    }
    
    int printperms(mode_t modebit)	{
    	char perms[10]; 
    	int i;
    	for (i=0;i<9; i++) perms[i]='-';
    	perms[9] = 0;
    
    	if (((modebit) & S_IRUSR)!=0) perms[0] = 'r'; 
    	if (((modebit) & S_IWUSR)!=0) perms[1] = 'w';  //User Perms
    	if (((modebit) & S_IXUSR)!=0) perms[2] = 'x';
    	if (((modebit) & S_IRGRP)!=0) perms[3] = 'r'; 
    	if (((modebit) & S_IWGRP)!=0) perms[4] = 'w';   //Group Perms
    	if (((modebit) & S_IXGRP)!=0) perms[5] = 'x';
    	if (((modebit) & S_IROTH)!=0) perms[6] = 'r';
    	if (((modebit) & S_IWOTH)!=0) perms[7] = 'w';  //Others
    	if (((modebit) & S_IXOTH)!=0) perms[8] = 'x';	
    	//else fprintf(stderr,"FAIL: Cannot determine file permission mask");
    	printf("%s ", perms);
    	return(0);		
    }
    
    int printfiletype(mode_t filetype)	{
    
    	char * mode;
    
    	if(S_ISREG(filetype)) mode = "-";
    	else if(S_ISDIR(filetype)) mode = "d";
    	else if(S_ISCHR(filetype)) mode = "c";
    	else if(S_ISBLK(filetype)) mode = "b";
    	else if(S_ISFIFO(filetype)) mode = "f";
    	else if(S_ISSOCK(filetype)) mode = "s";
    	else if(S_ISLNK(filetype)) mode = "l";
    	else fprintf(stderr,"FAIL: Cannot determine file mode");		
    	printf ("%s", mode);
    	return(0);		
    }
    
    
    
    int lsdetails(struct stat *statbuff) {
    
    	struct passwd *pw;
    	struct group *gp;
    	struct tm *tm_ptr;
    
    
    
    	tm_ptr = gmtime(&statbuff->st_ctime);
    
    	if(tm_ptr-> tm_mon+ 1 == 1) strcpy(monthprint,"Jan");
    	else if(tm_ptr-> tm_mon+ 1 == 2) strcpy(monthprint,"Feb");
    	else if(tm_ptr-> tm_mon+ 1 == 3) strcpy(monthprint,"Mar");
    	else if(tm_ptr-> tm_mon+ 1 == 4) strcpy(monthprint,"Apr");		
    	else if(tm_ptr-> tm_mon+ 1 == 5) strcpy(monthprint,"May");
    	else if(tm_ptr-> tm_mon+ 1 == 6) strcpy(monthprint,"Jun");
    	else if(tm_ptr-> tm_mon+ 1 == 7) strcpy(monthprint,"Jul");
    	else if(tm_ptr-> tm_mon+ 1 == 8) strcpy(monthprint,"Aug");
    	else if(tm_ptr-> tm_mon+ 1 == 9) strcpy(monthprint,"Sep");
    	else if(tm_ptr-> tm_mon+ 1 == 10) strcpy(monthprint,"Oct");		
    	else if(tm_ptr-> tm_mon+ 1 == 11) strcpy(monthprint,"Nov");
    	else if(tm_ptr-> tm_mon+ 1 == 12) strcpy(monthprint,"Dec");
    	else fprintf(stderr, "FAIL: Cannot determine month");
    
    	printfiletype(statbuff->st_mode);
    	printperms(statbuff->st_mode);
    	printf( "%d ", statbuff->st_nlink);
    
    	if ((pw = getpwuid(statbuff->st_uid)) != NULL) printf("%s  ", pw->pw_name);	 
    	else fprintf(stderr,"UID ERROR" );
    
    	if ((gp = getgrgid(statbuff->st_gid)) != NULL) printf("%s  ", gp->gr_name);
    	else fprintf(stderr,"The User is not associated with any group");
    
    	printf( "%-4d ", statbuff->st_size);
    	printf( "Inode: %-4d ", statbuff->st_ino);
    	printf( "%d-%s-%02d %d:%d  ",tm_ptr->tm_year+1900, monthprint, tm_ptr-> tm_mday, tm_ptr-> tm_hour, tm_ptr-> tm_min);
    
    	return 0;
    }
    Thank goodness for Visual Studio.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    9

    Smile

    Yes this is Linux, and yes this is C not C# (typo).

    Thanks for the indented version. Any idea why the link paths are not printing?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    I'm sorry, but I don't know Linux...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    -*- mode: compilation; default-directory: "~/test3/" -*-
    Compilation started at Tue Mar  4 14:57:13
    
    make -k 
    icc -o test3.o -c -g -Wall test3.c 
    test3.c(15): warning #1419: external declaration in primary source file
      int lsdetails(struct stat *statbuff);
          ^
    
    test3.c(16): warning #1419: external declaration in primary source file
      int outputfiletype(mode_t filetype);
          ^
    
    test3.c(17): warning #1419: external declaration in primary source file
      int recls(char *path);
          ^
    
    test3.c(18): warning #1419: external declaration in primary source file
      int printperms(mode_t mode);
          ^
    
    test3.c(29): remark #1011: missing return statement at end of non-void function "main"
      }
      ^
    
    test3.c(50): warning #991: //-style comments are nonstandard
      		if (dirslot==NULL) break;//note change	
      		                         ^
    
    test3.c(52): warning #266: function "lstat" declared implicitly
      		lstat(dirslot->d_name, &statbuff); 
      		^
    
    test3.c(63): error: identifier "S_IFMT" is undefined
      		if ((statbuff.st_mode & S_IFMT) == S_IFLNK) {
      		                        ^
    
    test3.c(63): error: identifier "S_IFLNK" is undefined
      		if ((statbuff.st_mode & S_IFMT) == S_IFLNK) {
      		                                   ^
    
    test3.c(66): warning #266: function "readlink" declared implicitly
      			readlink(name, lbuffer, 256);
      			^
    
    test3.c(119): warning #266: function "lstat" declared implicitly
      		lstat(dirslot->d_name, &statbuff);
      		^
    
    test3.c(127): warning #117: non-void function "recls" should return a value
      				return;}
      				      ^
    
    test3.c(156): warning #1418: external function definition with no prior declaration
      int printfiletype(mode_t filetype)	{
          ^
    
    test3.c(165): warning #266: function "S_ISSOCK" declared implicitly
      	else if(S_ISSOCK(filetype)) mode = "s";
      	        ^
    
    test3.c(200): warning #181: argument is incompatible with corresponding format string conversion
      	printf( "&#37;d ", statbuff->st_nlink);
      	               ^
    
    test3.c(208): warning #181: argument is incompatible with corresponding format string conversion
      	printf( "%-4d ", statbuff->st_size);
      	                 ^
    
    test3.c(209): warning #181: argument is incompatible with corresponding format string conversion
      	printf( "Inode: %-4d ", statbuff->st_ino);
      	                        ^
    
    compilation aborted for test3.c (code 2)
    make: *** [test3.o] Error 2
    make: Target `all' not remade because of errors.
    
    Compilation exited abnormally with code 2 at Tue Mar  4 14:57:13
    I see a lot of warnings - some seems to be very dangerous - you may want to start with fixing them
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    <<moved to Linux and duplicate thread deleted>>
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    9
    I'm not getting any warnings when compiling. Plus the program runs fine, just not printing the links.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    9
    Any ideas what is wrong with it?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I have no idea what is actually wrong, but this is the bit of code that supposedly reads the link:
    Code:
    		if ((statbuff.st_mode & S_IFMT) == S_IFLNK) {
    			islnk = 1;
    			memset(lbuffer, 0, 256);
    			readlink(name, lbuffer, 256);
    			printf("%s %s \n", (islnk) ? " -> " : "", (islnk) ? lbuffer : " ");
    		}
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    9
    got it working in the end.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  3. Path Finding Using Adjacency List.
    By DeepBlackMagic in forum C++ Programming
    Replies: 7
    Last Post: 05-16-2005, 02:34 PM
  4. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21