Solved

stimulate ls -l

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Detect CR LF to each line 12 170
nested if statement in excel help 4 37
Extract specific Urls by tag from a url list using regex & vba in excel 2013 4 20
Fill Null values 5 28
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

829 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