Thread: Executing 'alias' Linux shell command from C

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    6

    Executing 'alias' Linux shell command from C

    Hi, I was wondering how to execute Linux shell commands, like 'alias,' from a C program. I understand how to fork and use execl with commands like 'ls' and 'grep', but I cannot find the proper path for alias (thus, not being able to use execl). Any help would be greatly appreciated. Thanks.

    EDIT:
    Figured it out. Thanks everyone.
    Last edited by lan_zer0; 10-03-2007 at 12:56 PM. Reason: Question Answered

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Aliases are internal to the shell. I'm not even sure that an alias is even available to a sub-process through say the environment. Then there's the problem of parsing the alias, which in itself will be no easy task.

    Besides, how do you know what an alias is going to do?
    You might think your 'foo' alias does something like 'ls -l', but if Joe Hacker comes along and redefines foo as 'rm -rf /', then you're going to be looking sheepish.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Some more information on this:
    alias's are usually defined in a shell startup file, such as .bashrc or /etc/bashrc. The are part of the shell, and the startup of the shell will "load" them by reading those files.

    Depending on if the shell is loaded as "interactive" or not, it may not load these startup files.

    To "execute" an alias, you will have to execute the shell, with the right settings to load the startup files, and then give it the alias as an arguement.

    It seems more sensible, in that case, to execute a shell-script, which is approximately equal in measure of "cost", but doesn't rely on some "invisible" startup file (and possibly undefined), instead you explicitly give it a script file that is under your control - and it's a more flexible approach anyways.

    If you don't think my suggestion is the "right" solution, then can you please explain what you are trying to do, and why you think using shell alias's is the right thing to do?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    Thanks for the replies. What I am trying to do is implement a limited 'which' command, one of the options being to display the alias of the input command. So the basic use of my which program would look like this:

    %which -r ls
    alias ='ls'
    /bin/ls

    I have also come across the following solution, but my experience with Linux is limited, so I am a little unsure of what it is doing:

    Code:
          char *buf;
          buf = malloc((strlen(command)+5)*sizeof(char));
          memset(buf, 0, (strlen(command)+5)*sizeof(char));
          strcpy(buf, "-e ");
          strcat(buf, command);
          memset(buf+strlen(command)+3, '=', 1);
          execl("/bin/grep", "grep", buf, STDIN_FILENO, NULL);

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ah, I'm now clear on what you want to do: You want to SEE the alias list of your shell. However, be aware that the only place where the actual CURRENT alias list is available is in the current shell - at least I think so. If you start a new shell from your application, it won't know the alias of the shell that started it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    Would you be able to explain the grep command above? I still don't understand how it is able to grab the alias and print it out. It looks like its doing something like grep -e [command]=, and looking for an occurrence of that pattern somewhere.
    Last edited by lan_zer0; 10-03-2007 at 08:49 AM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't know what it does in detail - it is probably part of a bigger solution, so I'd need to know what the "command" is for example. -e just means that whatever is after -e is what to search for (even if it contains - or some such).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It's also rather weird code - take, for example, this:
    Code:
    memset(buf+strlen(command)+3, '=', 1);
    which could be written as
    Code:
    buf[strlen(command)+3] = '=';
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    Quote Originally Posted by CornedBee View Post
    It's also rather weird code - take, for example, this:
    Code:
    memset(buf+strlen(command)+3, '=', 1);
    which could be written as
    Code:
    buf[strlen(command)+3] = '=';
    Thanks, I'll try that.

    I apologize, I should have explained the code a bit better. Command is the command I want to print the alias out for. So the code is the core of my printAlias function, and I pass in char *command (i.e. ls, grep, make), and it prints out the alias for that command.

    EDIT:
    My confusion comes with the execl statement:

    Code:
    execl("/bin/grep", "grep", buf, STDIN_FILENO, NULL);
    I am unsure what STDIN_FILENO is, and what execl is doing with it.
    Last edited by lan_zer0; 10-03-2007 at 11:35 AM.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    A very good question, because passing it there is actually quite invalid. It's sheer dumb luck that it happens to work, and only because STDIN_FILENO is defined to be 0, which execl searches for.

    Really, the code doesn't do what you want.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    The code seems to work though. I took out STDIN_FILENO, and it seems to work ok. It hangs when i run it directly, but runs fine with a test suite.
    Last edited by lan_zer0; 10-03-2007 at 12:55 PM.

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by lan_zer0 View Post
    Hi, I was wondering how to execute Linux shell commands, like 'alias,' from a C program. I understand how to fork and use execl with commands like 'ls' and 'grep', but I cannot find the proper path for alias (thus, not being able to use execl). Any help would be greatly appreciated. Thanks.
    I don't think what you are trying to do is possible. You are trying to get the alias list from the shell which launched your "which" program. You can only do that by issuing an "alias" command inside that shell. There is no way to do that from another process.

    On my system, the "which" command can read the aliases, but that's because "which" is itself an alias, which pumps the aliases into /usr/bin/which with an option telling it to read the aliases from stdin.

    The reason it is done this way (by making "which" into an alias, itself) is because there's no other way to do it.

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    Aaahhh ok I get it now. The reason the above code works is because I had to set a up an alias for my program that piped the alias output to the program (just like which), then grep for the [command]= pattern.

    Thanks for the help guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII/ANSI in Linux Shell
    By JupiterV2 in forum Linux Programming
    Replies: 5
    Last Post: 10-12-2007, 02:38 PM
  2. command line alias remove for linux
    By iain in forum Tech Board
    Replies: 6
    Last Post: 11-22-2004, 01:39 PM
  3. Linux Shell and mp3
    By afreedboy in forum Linux Programming
    Replies: 2
    Last Post: 06-24-2004, 12:08 PM
  4. 3 linux shell environments
    By SpEkTrE in forum Linux Programming
    Replies: 3
    Last Post: 10-21-2003, 02:26 PM
  5. how to set a function to an alias in C shell
    By xstone in forum Tech Board
    Replies: 1
    Last Post: 09-29-2001, 03:41 PM