I do not understand how execlp() works in Linux

Arman Iqbal

I have spent the last 2 days trying to understand the execlp() system call, but yet here I am. Let me get straight to the issue.

The man page of execlp declares the system call as int execlp(const char *file, const char *arg, ...); with the description: The const char arg and subsequent ellipses in the execl(), execlp(), and execle() functions can be thought of as arg0, arg1, ..., argn.

Yet I see the system call being called like this in our text book: execlp(“/bin/sh”, ..., “ls -l /bin/??”, ...); (the "..." are for us to figure out as students). However this system call doesn´t even resemble anything like the declaration on the man page of the system call.

I am super confused. Any help is appreciated.

nos

this prototype:

  int execlp(const char *file, const char *arg, ...);

Says that execlp ìs a variable argument function. It takes 2 const char *. The rest of the arguments, if any, are the additional arguments to hand over to program we want to run - also char * - all these are C strings (and the last argument must be a NULL pointer)

So, the file argument is the path name of an executable file to be executed. arg is the string we want to appear as argv[0] in the executable. By convention, argv[0] is just the file name of the executable, normally it's set to the same as file.

The ... are now the additional arguments to give to the executable.

Say you run this from a commandline/shell:

$ ls

That'd be execlp("ls", "ls", (char *)NULL); Or if you run

$ ls -l /

That'd be execlp("ls", "ls", "-l", "/", (char *)NULL);

So on to execlp("/bin/sh", ..., "ls -l /bin/??", ...);

Here you are going to the shell, /bin/sh , and you're giving the shell a command to execute. That command is "ls -l /bin/??". You can run that manually from a commandline/shell:

 $ ls -l /bin/??

Now, how do you run a shell and tell it to execute a command ? You open up the documentation/man page for your shell and read it.

What you want to run is:

$ /bin/sh -c "ls -l /bin/??"

This becomes

  execlp("/bin/sh","/bin/sh", "-c", "ls -l /bin/??", (char *)NULL);

Side note: The /bin/?? is doing pattern matching, this pattern matching is done by the shell, and it expands to all files under /bin/ with 2 characters. If you simply did

  execlp("ls","ls", "-l", "/bin/??", (char *)NULL);

Probably nothing would happen (unless there's a file actually named /bin/??) as there's no shell that interprets and expands /bin/??

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

我不明白execlp()在Linux中的工作方式

来自分类Dev

Do I understand this Function.prototype.call() code correctly?

来自分类Dev

How do I prevent Apache service from auto-starting on Linux?

来自分类Dev

If I do not understand the difference in stability between Debian stable and Debian testing, am I better off with Debian testing?

来自分类Dev

使用 execlp 在 c 中使用多个参数作为字符串运行 linux 命令

来自分类Dev

Trying to understand why this Python code works

来自分类Dev

Trying to understand a simple Linux code

来自分类常见问题

How do I resolve ClassNotFoundException?

来自分类Dev

系统调用Execlp()

来自分类Dev

execlp 函数的错误使用

来自分类Dev

Execlp 不工作

来自分类Dev

Documentation to understand mm part of Linux kernel?

来自分类Dev

How RedoLog works in Oracle?

来自分类Dev

How do i sort my listview alphabetically?

来自分类Dev

How do I parse a RestSharp response into a class?

来自分类Dev

How do I override a default keybinding in LightTable?

来自分类Dev

How do I modularize polyfills in Angular?

来自分类Dev

How do I declare a driver as global?

来自分类Dev

How do I combine lenses and functors?

来自分类Dev

How do I parse JSON in Racket?

来自分类Dev

How do I link to a pdf in AngularJS?

来自分类Dev

How do I include jquery in javafx webview?

来自分类Dev

How do I retrieve the text of a cell comment

来自分类Dev

How do I write a custom module for AngularJS?

来自分类Dev

How do I iterate over a file?

来自分类Dev

How do I define the size of a CollectionView on rotate

来自分类Dev

How do I count selected checkboxes in Angular?

来自分类Dev

How do I combine three observables such that

来自分类Dev

Basic Ocaml: How do I compile this?

Related 相关文章

热门标签

归档