Need some help with this...

sh and bash do not produce any outputs whereas the others produce. The alias command works if I type it outside the program but "which alias" says it cannot find the command.

choose one that works and do popen("ksh -c alias","r") or which ever you choose, noting of course the format of the data it produces.

Which shell do you normally use? Try "ps -p $$".

Its bash... Ok I need to construct that string in this manner:

"csh -c "alias ls""

Hehe... if it was php I would've gotten away by typing "csh -c \"alias ls\""...

And Bingo... it works even here :slight_smile:

I was trying to refine my program. So I was just changing the order. I mean when something like ./uwhich -r ls is passed, it has to display the alias as well as the path to ls. It was displaying in the reverse order i.e. first path and then alias so i just interchanged the if logic by putting if(readalias == 1) first and if(allpaths == 1) as second one. But now, the second if loop is not displaying anything. I mean it is entering the loop but nothing is happening. Could I be doing something wrong here?

And as a matter of fact, if I want to find the particular command not just using the $PATH environment variable but in the whole system, is there any other variable for that? I mean, I should be able to get the variable wherever it is.

No, but you could recurse yourself using opendir/readdir/closedir or by popen("find / -print","r")

I thought so. Hehe... I thought there could be another environment variable for this purpose. Blame it on my ignorance... And any idea about the first question too?

No, otherwise the variable would have to change everytime somebody created or removed a directory.

Not really as we can't see your code from here, sounds like an uninitialised variable or mistaken assumption.

For some reason, the array paths is getting destroyed after alias does its job. Atleast thats what I observed.. My alias code is something like this:

if(aliasoption == 1)  {
            strcpy(buff, "csh -c \"alias ");
            strcat(buff, command);
            strcat(buff, wordclose);
            /*printf("%s",final);*/
            fp = popen(buff,"r");
            if(fp == NULL)
            {
                  printf("Handle Error");
            }
            /*else printf("Opened\n");
            printf("You asked for an alias\n");*/
            printf("alias %s= ",command);
            while(fgets(buff,sizeof(buff),fp) != NULL)
                  printf("%s",buff);
            pclose(fp);
            printf("\nNew Line");
        }

Edit: I tried defining paths[] as a Global Variable but no luck...

Anyways, I got it working by pasting the same code somewhere in the alias code. Well, though I'm not sure why it happened, it does work now...

The trick now is to go through what you have written and see what is redundent or duplicated.

I haven't done anything actually. I just copied the text under the if conditional loop that actually performs the check into the alias loop and initiated the print command before the alias command was put to work...

I am seeing a type of display I mean if the path is long, unix is substituting a '~'. How would I do that here? For example, if I find a command in a path, how would I substitute a tilde in the output?

Over egging the pudding I think.

~ is normally short-hand for $HOME.

You would have to look at what you were going to print, decide if it's too long, if so decide where to truncate, then print your truncated string and truncation indicator.

Yup. Got it :slight_smile: Thanks

I am still wondering if I will be able to get the alias to work without changing the shells.... I don't understand why it isn't working...

Have you found a program called "alias"? If not then "alias" is an internal command. The "popen" program uses the default shell, if this does not support "alias", it will fail.

Oh.. No I didn't find it... I really should learn more before I ask all these doubts I guess... Hehe... Sorry for the trouble though... and thank you...

No problem, it is important to realise there are internal and external commands.

In theory you should only need about three internal commands, "cd", "exit" and "exec". :slight_smile:

It gets confusing with things like "echo" where it's typically internal and external and behaves differently depending on the shell or your path.

Yup.. I'm seeing that slowly... OSes have evolved so much my god! :slight_smile: