Exec 함수의 0번째 매개변수

Exec 함수의 0번째 매개변수

다음은 표준 exec*기능입니다.

int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg,  ..., char * const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[], char *const envp[]);

Unix 규칙은 실행될 프로그램의 이름을 인수 배열의 첫 번째 멤버로 전달하는 것입니다.

어떤 현실적인 맥락에서 의미 있는 일을 하기보다 관습에서 벗어나는 것이 합리적입니까?경로/파일그리고매개변수/매개변수 v[0]?

답변1

다양한 이유로 이 차이는 에서 프로세스가 표시되는 방식을 변경하는 데 사용됩니다 ps. 이들 중 일부는 운영 체제 변경을 통해 제거되었습니다.

이를 염두에 두고 발견된 몇 가지 예에 대한 링크는 다음과 같습니다.

  • execlp()의 처음 두 매개변수에 동일한 값을 전달하는 이유는 무엇입니까?
    특히,@SammyKumonen심볼릭 링크를 실제 파일처럼 "보이게" 만드는 방법에 대한 설명입니다.
  • hide.c /*---------------------------------------------------------------------------+ | Copyright (c) 1992 Oracle Corporation Belmont, California, USA | | All rights reserved | +---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------+ | FILENAME | | hide.c | | DESCRIPTION | | Hides arguments for programs on UNIX systems. | | Can be used as a program prefix: hide program arguments | | or as a symbolic link. If this program is not invoked as hide, it | | will hide its arguments and invoke the program name.hide | | The best way to use this is to rename your critical programs to | | program.hide, and create a symbolic link program to hide. | | mv sqlplus sqlplus.hide; ln -s hide sqlplus | | Thus when sqlplus is invoked, its arguments will be hidden | | NOTES | | This program works by padding 3000 '/' chars in argv[0]. This fools | | all known ps's. This will reduce the argument capacity of your | | program by 3000 chars. A good enhancement would be to reduce the | | padding if needed so that no arguments are lost - would require a | | method of determining the max argument size on the system. Some | | system's provide the E2BIG error on exec. | | There is some performace penalty for using this program, but it is | | minimal because this program is so small - the biggest cost is the | | extra exec required to get this program started. | | HISTORY | | 09/15/92 R Brodersen Created, based on D Beusee's hideargs() | | 09/17/92 D Beusee Fixed to compile on any system | +---------------------------------------------------------------------------*/

답변2

프로그램의 동작은 호출되는 이름에 따라 달라질 수 있습니다.

예를 bash들어 다음과 같습니다.POSIX 모드다음과 같이 호출되는 경우 sh:

$ bash -c 'set() { echo 1; }; set'
1

하지만:

$ ARGV0=sh bash -c 'set() { echo 1; }; set'
sh: `set': is a special builtin

( zsh사용ARGV0전달할 변수 argv[0])

관련 정보