The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Accepting filename as command line param and writing to it silas.john Shell Programming and Scripting 9 02-06-2008 06:26 AM
writing your own command in unix/linux Amardeep High Level Programming 3 03-15-2007 06:49 PM
Writing to a floppy jlrusso Filesystems, Disks and Memory 2 09-23-2003 11:04 PM
writing to /tmp vtran4270 UNIX for Advanced & Expert Users 3 04-04-2003 01:22 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-19-2002
Registered User
 

Join Date: Feb 2002
Location: US
Posts: 14
Re-writing ls -F command

As the subject states, I am re-writing the ls -F command in C++, but I am having trouble adding some of the Formatting characters to the files names. Here is my code:

Code:
 if (strcmp(buff, "-F")==0)
  {
     struct stat s;
     stat(direntp->d_name,&s);
     cout<<direntp->d_name;
     if(s.st_mode & S_IFDIR)
          cout<<"/"<<endl;
   else
     if(s.st_mode & S_IFIFO)
         cout<<"|"<<endl;
   else
     if(s.st_mode & S_IFLNK)
         cout<<" "<<endl;
   else
     if(s.st_mode & S_IFDOOR)
         cout<<">"<<endl;
   else
     if(s.st_mode & S_IFSOCK)
         cout<<"="<<endl;
   else
     if((s.st_mode & S_IXUSR) && (s.st_mode & S_IFLNK))
           cout<<"*"<<endl;
     else
       cout<<"?"<<endl;
  }//end if
It is adding the correct formatting character ("/") for directories, but not for my executable files such as "a.out" and "paige_sh" which should have an asterick (*). Here is a sample run:

Code:
mars:$ a.out
-F
./
../
shell1.cpp
env.c
core

a.out
modshell2.1.cpp
read_command
shell.sh
shell
test.sh
ansipr
shell2.c.save
paige_sh
modshell1.2.cpp
modshell1.3.cpp
gid.c
modshell2.3.cpp
1814shell.cpp
cpplist
uid.c
listfile.txt
listDir.cpp
modshell2.cpp
modshell2.2.cpp
rewritels.cpp
writels2.cpp
justafile.txt
justafile2.txt
justadir/
justadir2/
can anyone help?? Thanks.
Reply With Quote
Forum Sponsor
  #2  
Old 03-19-2002
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,658
It looks like you are trying to append an asterisk if both S_IXUSR and S_IFLNK are set. But earlier in your massive "if" statement you have already tested for S_IFLNK being set all by itself. So no condition can allow to reach the point where you append an asterisk.

Even if you square this away, you have a couple of other problems. S_IFLNK is a symlink, so instead of appending a blank (and what would be the point of that anyway), you want to append an at-sign.

Also, I think that for executables, you want to ensure first that S_IFREG is set and second that one or more of S_IXUSR, S_IXGRP, or S_IXOTH is set.
Reply With Quote
  #3  
Old 03-19-2002
Registered User
 

Join Date: Feb 2002
Location: US
Posts: 14
well I only added the && part for the executables after it didnt work in the first place. I ran ls -la regularly and saw that the executables had symbolic links so added to && part to see if it worked since the executables proved true for both conditions.

I HAD the "@" for the links, but took it off so that I could actually SEE what I was lookin at when I ran it...that's the purpose of the white space instead of the symbol. I was going to replace it after I was completely through with the program.

However, I am going to take a look @ the other things you suggested and get back at you. Thanks
Reply With Quote
  #4  
Old 03-20-2002
Registered User
 

Join Date: Feb 2002
Location: US
Posts: 14
problem solved

Quote:
Originally posted by Perderabo
It looks like you are trying to append an asterisk if both S_IXUSR and S_IFLNK are set. But earlier in your massive "if" statement you have already tested for S_IFLNK being set all by itself. So no condition can allow to reach the point where you append an asterisk.

Thanks. It turned out that the executables needed to be placed before the links because it was negating it.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:30 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0