Solved

stimulate ls -l

Posted on 2011-03-04
5
331 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 opening and reading files in the C programming language.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now