Re-writing ls -F command


 
Thread Tools Search this Thread
Top Forums Programming Re-writing ls -F command
# 1  
Old 03-19-2002
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.
# 2  
Old 03-19-2002
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.
# 3  
Old 03-19-2002
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
# 4  
Old 03-20-2002
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run command without over-writing

I have a script called script.sh that has the following in it. cat /home/auto/script.sh #!/bin/bash ARGA=$1 if ; then echo "${ARGA}:Confirmed" else echo "${ARGA}:Unconfirmed" fi I need a way to run this script, and then, after script has confirmed the... (9 Replies)
Discussion started by: SkySmart
9 Replies

2. Shell Programming and Scripting

Preserving newlines when writing loops on the command line in bash

Dear All, I have a question that's been difficult to get an answer to. I often write command line loops, e.g. find files, print name, grep for term, apply sed, etc I use both zsh and bash. When I write a loop e.g. for line in `more myfile.txt` > do > echo $line > done but... (2 Replies)
Discussion started by: JohnK1
2 Replies

3. Shell Programming and Scripting

Writing file name and date from LS command into a file to be imported into mysql

I am looking to do a ls on a folder and have the output of the ls be structured so that is is modificaiton date, file name with the date in a format that is compatible with mysql. I am trying to build a table that stores the last modification date of certain files so I can display it on some web... (4 Replies)
Discussion started by: personalt
4 Replies

4. Red Hat

writing a script

Dear Madam/Sir Who can help me with writing a script doing the following? 1- Read names of files (only files with special name format let say initially they have the same file name start like TT*) 2- Then create an empty files with the same names have been read in step one but with extension... (1 Reply)
Discussion started by: m.nageeb
1 Replies

5. Shell Programming and Scripting

Effect of using eval to execute a command as opposed to writing it on the commandline

cmd='date | wc' or cmd="date | wc" $cmdIf this script is executed, an error is generated. The reason written was that "The execution fails because the pipe is not expanded and is passed to date as an argument". What is meant by expansion of pipe. When we execute date | wc on the command line, it... (2 Replies)
Discussion started by: daudiam
2 Replies

6. OS X (Apple)

Need help writing an Applescript to launch a specific Terminal Command...

I developed a script in Lingon (which is an automated script editor developed for OS X) that is used to automatically restart programs only if they crash. The script itself does just that, but I only want it to load if I'm going to use the specific application that it's designed to protect. In the... (2 Replies)
Discussion started by: JFraser1
2 Replies

7. Shell Programming and Scripting

Need help writing an Applescript to launch a specific Terminal Command...

I developed a script in Lingon (which is an automated script editor developed for OS X) that is used to automatically restart programs only if they crash. The script itself does just that, but I only want it to load if I'm going to use the specific application that it's designed to protect. In... (3 Replies)
Discussion started by: JFraser1
3 Replies

8. Shell Programming and Scripting

Accepting filename as command line param and writing to it

Hi, Is it possible to accept a filename as command line parameter and then write to that file using command redirection? i tried the below script. outputfile=`echo $1` echo "Writing to file" > 'echo $outputfile' exit $returncode but it isnt working. is there any other way to... (9 Replies)
Discussion started by: silas.john
9 Replies

9. Programming

Writing Makefile

Hello All; I am going to write an application in C/C++ which has so many source files and hence created many subdirectories. My area of concern is to write makefile. What i thought that each subdirectory would have own makefile and wanted to have one parent makefile which will call all ... (3 Replies)
Discussion started by: nikhildot
3 Replies

10. Programming

writing your own command in unix/linux

Hi I am very new to Linux programming,otherwise I have exposure to Linux. Was thinking about something like writing my own commands for Linux. Any ideas where to start, any useful links and what I need to know before I start with this. Thanks :) Sidhu (3 Replies)
Discussion started by: Amardeep
3 Replies
Login or Register to Ask a Question