Solved

stimulate ls -l

Posted on 2011-03-04
5
335 Views
Last Modified: 2012-06-21
Hi. If anyone can help me with ls –l  I appreciate that. I've written program in c to stimulate ls -l.
 it's works well except the link. I need to print out the line like this
 lrwxrwxrwx 1 admin admin 11 Feb 27 19:08 figC.18 -> ipc/fifo1.c.  
 but it’s just print   lrwxrwxrwx 1 admin admin 11 Feb 27 19:08 figC.18.  I need to make my program print the link also.
Thanks for your help
0
Comment
Question by:eng_h
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 35042002
You saw http://www.experts-exchange.com/Programming/Languages/C/Q_20416430.html , right?

Also, look at http://www.ibm.com/developerworks/aix/library/au-unix-readdir.html :

•S_ISBLK(mode) -- Is this a block special file? (usually a block-based device of some sort)
•S_ISCHR(mode) -- Is this a character special file? (again, usually a character-based device of some sort)
•S_ISDIR(mode) -- Is this a directory?
•S_ISFIFO(mode) -- Is this a pipe or FIFO special file?
•S_ISLNK(mode) -- Is this a symbolic link?
•S_ISREG(mode) -- Is this a regular file?
0
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
ID: 35042007
Also, in  http://www.ibm.com/developerworks/aix/library/au-unix-readdir.html is verifcation code that link is valid:

Listing 5. Process the entry
(void)strncpy( pathName, theDir, PATH_MAX );
        (void)strncat( pathName, "/", PATH_MAX );
        (void)strncat( pathName, entry.d_name, PATH_MAX );
        
        if( lstat( pathName, &entryInfo ) == 0 ) {
            /* stat() succeeded, let's party */
            count++;
            
            if( S_ISDIR( entryInfo.st_mode ) ) {            
/* directory */
                printf( "processing %s/\n", pathName );
                count += process_directory( pathName );
            } else if( S_ISREG( entryInfo.st_mode ) ) { 
/* regular file */
                printf( "\t%s has %lld bytes\n",
                    pathName, (long long)entryInfo.st_size );
            } else if( S_ISLNK( entryInfo.st_mode ) ) { 
/* symbolic link */
                char targetName[PATH_MAX + 1];
                if( readlink( pathName, targetName, PATH_MAX ) != -1 ) {
                    printf( "\t%s -> %s\n", pathName, targetName );
                } else {
                    printf( "\t%s -> (invalid symbolic link!)\n",
 pathName );
                }
            }
        } else {
            printf( "Error statting %s: %s\n", pathName, strerror( 
errno ) );
        }

Open in new window

0
 

Author Closing Comment

by:eng_h
ID: 35044522
The solution was so helpful.
0
 

Author Comment

by:eng_h
ID: 35044544
I used this part  in my program
                  char targetName[PATH_MAX + 1];
                if( readlink( pathName, targetName, PATH_MAX ) != -1 )
               {
                    printf( "\t%s -> %s\n", pathName, targetName );
                }
                 else
                      printf( "\t%s -> (invalid symbolic link!)\n", pathName );

and it worked with me.
Thanks so much for your time phoffric !!
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35044988
Glad you found it helpful.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question