Skip to content

Commit ce2bd58

Browse files
committed
add optional support to replace argv0 (fixes microsoft#472)
1 parent 2f0f67d commit ce2bd58

File tree

6 files changed

+44
-10
lines changed

6 files changed

+44
-10
lines changed

src/interfaces.ts

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ interface IBasePtyForkOptions {
115115
export interface IPtyForkOptions extends IBasePtyForkOptions {
116116
uid?: number;
117117
gid?: number;
118+
argv0?: string;
118119
}
119120

120121
export interface IWindowsPtyForkOptions extends IBasePtyForkOptions {

src/native.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface IWinptyNative {
1919
}
2020

2121
interface IUnixNative {
22-
fork(file: string, args: string[], parsedEnv: string[], cwd: string, cols: number, rows: number, uid: number, gid: number, useUtf8: boolean, helperPath: string, onExitCallback: (code: number, signal: number) => void): IUnixProcess;
22+
fork(file: string, args: string[], parsedEnv: string[], cwd: string, cols: number, rows: number, uid: number, gid: number, useUtf8: boolean, helperPath: string, onExitCallback: (code: number, signal: number) => void, argv0: string): IUnixProcess;
2323
open(cols: number, rows: number): IUnixOpenProcess;
2424
process(fd: number, pty?: string): string;
2525
resize(fd: number, cols: number, rows: number): void;

src/unix/pty.cc

+13-7
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ pty_posix_spawn(char** argv, char** env,
158158
NAN_METHOD(PtyFork) {
159159
Nan::HandleScope scope;
160160

161-
if (info.Length() != 11 ||
161+
if (info.Length() != 12 ||
162162
!info[0]->IsString() ||
163163
!info[1]->IsArray() ||
164164
!info[2]->IsArray() ||
@@ -169,9 +169,10 @@ NAN_METHOD(PtyFork) {
169169
!info[7]->IsNumber() ||
170170
!info[8]->IsBoolean() ||
171171
!info[9]->IsString() ||
172-
!info[10]->IsFunction()) {
172+
!info[10]->IsFunction() ||
173+
!info[11]->IsString()) {
173174
return Nan::ThrowError(
174-
"Usage: pty.fork(file, args, env, cwd, cols, rows, uid, gid, utf8, helperPath, onexit)");
175+
"Usage: pty.fork(file, args, env, cwd, cols, rows, uid, gid, utf8, helperPath, onexit, argv0)");
175176
}
176177

177178
// file
@@ -247,19 +248,23 @@ NAN_METHOD(PtyFork) {
247248
// helperPath
248249
Nan::Utf8String helper_path(info[9]);
249250

251+
// argv0
252+
Nan::Utf8String argv0(info[11]);
253+
250254
pid_t pid;
251255
int master;
252256
#if defined(__APPLE__)
253257
int argc = argv_->Length();
254-
int argl = argc + 4;
258+
int argl = argc + 5;
255259
char **argv = new char*[argl];
256260
argv[0] = strdup(*helper_path);
257261
argv[1] = strdup(*cwd_);
258262
argv[2] = strdup(*file);
263+
argv[3] = strdup(*argv0);
259264
argv[argl - 1] = NULL;
260265
for (int i = 0; i < argc; i++) {
261266
Nan::Utf8String arg(Nan::Get(argv_, i).ToLocalChecked());
262-
argv[i + 3] = strdup(*arg);
267+
argv[i + 4] = strdup(*arg);
263268
}
264269

265270
int err = -1;
@@ -276,7 +281,7 @@ NAN_METHOD(PtyFork) {
276281
int argc = argv_->Length();
277282
int argl = argc + 2;
278283
char **argv = new char*[argl];
279-
argv[0] = strdup(*file);
284+
argv[0] = strdup(*argv0);
280285
argv[argl - 1] = NULL;
281286
for (int i = 0; i < argc; i++) {
282287
Nan::Utf8String arg(Nan::Get(argv_, i).ToLocalChecked());
@@ -342,7 +347,8 @@ NAN_METHOD(PtyFork) {
342347
{
343348
char **old = environ;
344349
environ = env;
345-
execvp(argv[0], argv);
350+
char* file_ = strdup(*file);
351+
execvp(file_, argv);
346352
environ = old;
347353
perror("execvp(3) failed.");
348354
_exit(1);

src/unix/spawn-helper.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ int main (int argc, char** argv) {
1212

1313
char *cwd = argv[1];
1414
char *file = argv[2];
15-
argv = &argv[2];
15+
argv = &argv[3];
1616

1717
if (strlen(cwd) && chdir(cwd) == -1) {
1818
_exit(1);

src/unixTerminal.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,31 @@ if (process.platform !== 'win32') {
365365
}
366366
});
367367
});
368+
it('should default argv0 to file', (done) => {
369+
const term = new UnixTerminal('/bin/sh',
370+
[ '-c', 'echo $0' ]);
371+
let argv0 = '';
372+
term.on('data', (data) => {
373+
argv0 = argv0.concat(data);
374+
});
375+
term.on('exit', () => {
376+
assert.strictEqual(argv0.trim(), '/bin/sh');
377+
done();
378+
});
379+
});
380+
it('should allow an alternate argv0', (done) => {
381+
const term = new UnixTerminal('/bin/sh',
382+
[ '-c', 'echo $0' ],
383+
{ argv0: 'alternate' });
384+
let argv0 = '';
385+
term.on('data', (data) => {
386+
argv0 = argv0.concat(data);
387+
});
388+
term.on('exit', () => {
389+
assert.strictEqual(argv0.trim(), 'alternate');
390+
done();
391+
});
392+
});
368393
});
369394
});
370395
}

src/unixTerminal.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@ export class UnixTerminal extends Terminal {
110110
this.emit('exit', code, signal);
111111
};
112112

113+
const argv0 = opt.argv0 ?? file;
114+
113115
// fork
114-
const term = pty.fork(file, args, parsedEnv, cwd, this._cols, this._rows, uid, gid, (encoding === 'utf8'), helperPath, onexit);
116+
const term = pty.fork(file, args, parsedEnv, cwd, this._cols, this._rows, uid, gid, (encoding === 'utf8'), helperPath, onexit, argv0);
115117

116118
this._socket = new PipeSocket(term.fd);
117119
if (encoding !== null) {

0 commit comments

Comments
 (0)