Skip to content

Commit 8d013c4

Browse files
committed
Multiple bug fixes, better management of environment variables for
sub-process
1 parent 7e28ab9 commit 8d013c4

File tree

46 files changed

+361
-169
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+361
-169
lines changed

awk-23.30.1/src/b.c

+3
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ void penter(Node *p) /* set up parent pointers and leaf indices */
231231

232232
void freetr(Node *p) /* free parse tree */
233233
{
234+
if (type(p) == 0)
234235
switch (type(p)) {
235236
ELEAF
236237
LEAF
@@ -249,6 +250,8 @@ void freetr(Node *p) /* free parse tree */
249250
// fprintf(stderr, "Freeing tree %x\n", p); fflush(stderr);
250251
xfree(p);
251252
break;
253+
case 0:
254+
break; /* iOS specific addition, since we always clear the tree */
252255
default: /* can't happen */
253256
FATAL("can't happen: unknown type %d in freetr", type(p));
254257
break;

awk-23.30.1/src/run.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ void freeTree(Node *u, int eraseSelf) /* scan the entire tree, and frees the
154154

155155
if (u == NULL) return;
156156
if ((int)u == 0x1) return; // safeguard
157-
157+
// iOS addition to avoid a crash:
158+
if (u == STRING) return; // yyparse can create nodes with fake addresses
159+
if (u == REGEXPR) return; // yyparse can create nodes with fake addresses
160+
158161
// If it's a tree, freetr will do the job:
159162
if ((u->nobj == CCL) || (u->nobj == NCCL) || (u->nobj == CHAR) || (u->nobj == DOT) || (u->nobj == FINAL)
160163
|| (u->nobj == ALL) || (u->nobj == EMPTYRE) || (u->nobj == STAR) || (u->nobj == PLUS) || (u->nobj == QUEST)
@@ -165,6 +168,11 @@ void freeTree(Node *u, int eraseSelf) /* scan the entire tree, and frees the
165168
for (a = u; a; a = anext) {
166169
if ((a->ntype == NSTAT) || (a->ntype == NEXPR)) {
167170
for (int i = 0; i < a->nnarg; i++) {
171+
// Nodes created with itonp, cannot free with freeTree:
172+
if ((a->nobj == BLTIN) && (i == 0)) continue;
173+
if ((a->nobj == ARG) && (i == 0)) continue;
174+
if ((a->nobj == VARNF) && (i == 0)) continue;
175+
if ((a->nobj == GETLINE) && (i == 1)) continue;
168176
freeTree(a->narg[i], 0); // never free narg, it was allocated as part of the node
169177
a->narg[i] = NULL;
170178
}

bsd_diff/diffreg.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@ static void
10711071
change(char *file1, FILE *f1, char *file2, FILE *f2, int a, int b, int c, int d,
10721072
int *pflags)
10731073
{
1074-
static size_t max_context = 64;
1074+
static __thread size_t max_context = 64;
10751075
long curpos;
10761076
int i, nc, f;
10771077
const char *walk;

bsd_find/find.c

+11
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ find_execute(PLAN *plan, char *paths[])
187187

188188
exitstatus = 0;
189189
while (errno = 0, (entry = fts_read(tree)) != NULL) {
190+
// debugging
191+
char dirBefore[MAXPATHLEN];
192+
getwd(dirBefore);
193+
//
190194
if (maxdepth != -1 && entry->fts_level >= maxdepth) {
191195
if (fts_set(tree, entry, FTS_SKIP))
192196
err(1, "%s", entry->fts_path);
@@ -235,6 +239,13 @@ find_execute(PLAN *plan, char *paths[])
235239
* the work specified by the user on the command line.
236240
*/
237241
for (p = plan; p && (p->execute)(p, entry); p = p->next);
242+
// debugging
243+
char dirAfter[MAXPATHLEN];
244+
getwd(dirAfter);
245+
if (strcmp(dirBefore, dirAfter) != 0) {
246+
fprintf(stderr, "Active directory changed: %s %s\n", ios_getBookmarkedVersion(dirBefore), ios_getBookmarkedVersion(dirAfter));
247+
}
248+
//
238249
}
239250
e = errno;
240251
finish_execplus();

bsd_find/function.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ doexec: if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
672672
default: { // We need to go through both branches:
673673
// case 0: {
674674
/* change dir back from where we started */
675-
if (!(plan->flags & F_EXECDIR) &&
675+
if (!(plan->flags & F_EXECDIR) &&
676676
!(ftsoptions & FTS_NOCHDIR) && ios_fchdir_nolock(dotfd)) {
677677
warn("chdir");
678678
_exit(1);

bsd_find/main.c

+3
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ find_main(int argc, char *argv[])
170170
ftsoptions |= FTS_NOCHDIR;
171171
}
172172

173+
// iOS: force FTS_NOCHDIR for exec commands. Slower, but no bugs.
174+
ftsoptions |= FTS_NOCHDIR;
175+
173176
exit(find_execute(find_formplan(argv), start));
174177
}
175178

libarchive/config.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
#define HAVE_FCNTL_H 1
154154

155155
/* Define to 1 if you have the `fork' function. */
156-
#define HAVE_FORK 1
156+
// #define HAVE_FORK 1
157157

158158
/* Define to 1 if fseeko (and presumably ftello) exists and is declared. */
159159
#define HAVE_FSEEKO 1
@@ -559,7 +559,7 @@
559559
#define HAVE_UTIME_H 1
560560

561561
/* Define to 1 if you have the `vfork' function. */
562-
#define HAVE_VFORK 1
562+
// #define HAVE_VFORK 1
563563

564564
/* Define to 1 if you have the `vprintf' function. */
565565
#define HAVE_VPRINTF 1

libarchive/libarchive/libarchive/archive_read_support_compression_program.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ archive_read_support_compression_program(struct archive *a, const char *cmd)
7272
*/
7373
int
7474
archive_read_support_compression_program_signature(struct archive *_a,
75-
const char *cmd, void *signature, size_t signature_len)
75+
const char *cmd, const void *signature, size_t signature_len)
7676
{
7777
(void)_a; /* UNUSED */
7878
(void)cmd; /* UNUSED */

libc_replacement.c

+101-41
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#undef fputs
2222
#undef fputc
2323
#undef putw
24+
#undef putp
2425
#undef fflush
2526
#undef getenv
2627
#undef setenv
@@ -215,7 +216,7 @@ static inline const pid_t ios_nextAvailablePid() {
215216
inline void ios_storeThreadId(pthread_t thread) {
216217
// To avoid issues when a command starts a command without forking,
217218
// we only store thread IDs for the first thread of the "process".
218-
// fprintf(stderr, "Unlocking pid %x, storing thread %x current value: %x\n", current_pid, thread, thread_ids[current_pid]);
219+
// fprintf(stderr, "Unlocking pid %d, storing thread %x current value: %x\n", current_pid, thread, thread_ids[current_pid]);
219220
if (thread_ids[current_pid] == -1) {
220221
thread_ids[current_pid] = thread;
221222
}
@@ -254,8 +255,8 @@ char* libc_getenv(const char* variableName) {
254255
}
255256

256257
extern void set_session_errno(int n);
257-
int ios_setenv(const char* variableName, const char* value, int overwrite) {
258-
if (environment[current_pid] != NULL) {
258+
int ios_setenv_pid(const char* variableName, const char* value, int overwrite, int pid) {
259+
if (environment[pid] != NULL) {
259260
if (variableName == NULL) {
260261
set_session_errno(EINVAL);
261262
return -1;
@@ -269,9 +270,9 @@ int ios_setenv(const char* variableName, const char* value, int overwrite) {
269270
set_session_errno(EINVAL);
270271
return -1;
271272
}
272-
char** envp = environment[current_pid];
273+
char** envp = environment[pid];
273274
unsigned long varNameLen = strlen(variableName);
274-
for (int i = 0; i < numVariablesSet[current_pid]; i++) {
275+
for (int i = 0; i < numVariablesSet[pid]; i++) {
275276
if (envp[i] == NULL) { continue; }
276277
if (strncmp(variableName, envp[i], varNameLen) == 0) {
277278
if (strlen(envp[i]) > varNameLen) {
@@ -286,18 +287,26 @@ int ios_setenv(const char* variableName, const char* value, int overwrite) {
286287
}
287288
}
288289
// Not found so far, add it to the list:
289-
int pos = numVariablesSet[current_pid];
290-
environment[current_pid] = realloc(envp, (numVariablesSet[current_pid] + 2) * sizeof(char*));
291-
environment[current_pid][pos] = malloc(strlen(variableName) + strlen(value) + 2);
292-
environment[current_pid][pos + 1] = NULL;
293-
sprintf(environment[current_pid][pos], "%s=%s", variableName, value);
294-
numVariablesSet[current_pid] += 1;
290+
int pos = numVariablesSet[pid];
291+
environment[pid] = realloc(envp, (numVariablesSet[pid] + 2) * sizeof(char*));
292+
environment[pid][pos] = malloc(strlen(variableName) + strlen(value) + 2);
293+
environment[pid][pos + 1] = NULL;
294+
sprintf(environment[pid][pos], "%s=%s", variableName, value);
295+
numVariablesSet[pid] += 1;
295296
return 0;
296297
} else {
297298
return setenv(variableName, value, overwrite);
298299
}
299300
}
300301

302+
int ios_setenv_parent(const char* variableName, const char* value, int overwrite) {
303+
return ios_setenv_pid(variableName, value, overwrite, previousPid[current_pid]);
304+
}
305+
306+
int ios_setenv(const char* variableName, const char* value, int overwrite) {
307+
return ios_setenv_pid(variableName, value, overwrite, current_pid);
308+
}
309+
301310
int ios_putenv(char* string) {
302311
if (environment[current_pid] != NULL) {
303312
unsigned length;
@@ -336,10 +345,10 @@ int ios_putenv(char* string) {
336345
}
337346
}
338347

339-
int ios_unsetenv(const char* variableName) {
348+
int ios_unsetenv_pid(const char* variableName, int pid) {
340349
// Someone calls unsetenv once the process has been terminated.
341350
// Best thing to do is erase the environment and return
342-
if (environment[current_pid] != NULL) {
351+
if (environment[pid] != NULL) {
343352
if (variableName == NULL) {
344353
set_session_errno(EINVAL);
345354
return -1;
@@ -353,42 +362,50 @@ int ios_unsetenv(const char* variableName) {
353362
set_session_errno(EINVAL);
354363
return -1;
355364
}
356-
char** envp = environment[current_pid];
365+
char** envp = environment[pid];
357366
unsigned long varNameLen = strlen(variableName);
358-
for (int i = 0; i < numVariablesSet[current_pid]; i++) {
367+
for (int i = 0; i < numVariablesSet[pid]; i++) {
359368
if (envp[i] == NULL) { continue; }
360369
if (strncmp(variableName, envp[i], varNameLen) == 0) {
361370
if (strlen(envp[i]) > varNameLen) {
362371
if (envp[i][varNameLen] == '=') {
363372
// This variable is defined in the current environment:
364373
free(envp[i]);
365374
envp[i] = NULL;
366-
if (i < numVariablesSet[current_pid] - 1) {
367-
for (int j = i; j < numVariablesSet[current_pid] - 1; j++) {
375+
if (i < numVariablesSet[pid] - 1) {
376+
for (int j = i; j < numVariablesSet[pid] - 1; j++) {
368377
envp[j] = envp[j+1];
369378
}
370-
envp[numVariablesSet[current_pid] - 1] = NULL;
379+
envp[numVariablesSet[pid] - 1] = NULL;
371380
}
372-
numVariablesSet[current_pid] -= 1;
373-
environment[current_pid] = realloc(envp, (numVariablesSet[current_pid] + 1) * sizeof(char*));
381+
numVariablesSet[pid] -= 1;
382+
environment[pid] = realloc(envp, (numVariablesSet[pid] + 1) * sizeof(char*));
374383
return 0;
375384
}
376385
}
377386
}
378387
}
379388
/*
380-
for (int i = 0; i < numVariablesSet[current_pid]; i++) {
381-
char* position = strstr(envp[i],"=");
382-
if (strncmp(variableName, envp[i], position - envp[i]) == 0) {
383-
}
384-
} */
389+
for (int i = 0; i < numVariablesSet[pid]; i++) {
390+
char* position = strstr(envp[i],"=");
391+
if (strncmp(variableName, envp[i], position - envp[i]) == 0) {
392+
}
393+
} */
385394
// Not found:
386395
return 0;
387396
} else {
388397
return unsetenv(variableName);
389398
}
390399
}
391400

401+
int ios_unsetenv_parent(const char* variableName) {
402+
return ios_unsetenv_pid(variableName, previousPid[current_pid]);
403+
}
404+
405+
int ios_unsetenv(const char* variableName) {
406+
return ios_unsetenv_pid(variableName, current_pid);
407+
}
408+
392409

393410
// store environment variables (called from execve)
394411
// Copy the entire environment:
@@ -488,6 +505,8 @@ pid_t ios_currentPid() {
488505
return current_pid;
489506
}
490507

508+
// Note to self: do not redefine getpid() unless you have a way to make it consistent even when a "process" starts a new thread.
509+
// 0MQ and asyncio rely on this.
491510
pid_t fork(void) { return ios_nextAvailablePid(); } // increases current_pid by 1.
492511
pid_t ios_fork(void) { return ios_nextAvailablePid(); } // increases current_pid by 1.
493512
pid_t vfork(void) { return ios_nextAvailablePid(); }
@@ -570,31 +589,72 @@ void vwarnx(const char *fmt, va_list args)
570589
}
571590
// void err(int eval, const char *fmt, ...);
572591
void err(int eval, const char *fmt, ...) {
573-
va_list argptr;
574-
va_start(argptr, fmt);
575-
vwarn(fmt, argptr);
576-
va_end(argptr);
592+
if (fmt != NULL) {
593+
va_list argptr;
594+
va_start(argptr, fmt);
595+
vwarn(fmt, argptr);
596+
va_end(argptr);
597+
}
598+
ios_exit(eval);
599+
}
600+
// void errc(int eval, int errorcode, const char *fmt, ...);
601+
void errc(int eval, int errorcode, const char *fmt, ...) {
602+
if (thread_stderr == NULL) thread_stderr = stderr;
603+
if (fmt != NULL) {
604+
va_list argptr;
605+
va_start(argptr, fmt);
606+
fputs(ios_progname(), thread_stderr);
607+
fputs(": ", thread_stderr);
608+
vfprintf(thread_stderr, fmt, argptr);
609+
fputs(": ", thread_stderr);
610+
fputs(strerror(errorcode), thread_stderr);
611+
putc('\n', thread_stderr);
612+
va_end(argptr);
613+
}
577614
ios_exit(eval);
578615
}
579616
// void errx(int eval, const char *fmt, ...);
580617
void errx(int eval, const char *fmt, ...) {
581-
va_list argptr;
582-
va_start(argptr, fmt);
583-
vwarnx(fmt, argptr);
584-
va_end(argptr);
618+
if (fmt != NULL) {
619+
va_list argptr;
620+
va_start(argptr, fmt);
621+
vwarnx(fmt, argptr);
622+
va_end(argptr);
623+
}
585624
ios_exit(eval);
586625
}
587626
// void warn(const char *fmt, ...);
588627
void warn(const char *fmt, ...) {
589-
va_list argptr;
590-
va_start(argptr, fmt);
591-
vwarn(fmt, argptr);
592-
va_end(argptr);
628+
if (fmt != NULL) {
629+
va_list argptr;
630+
va_start(argptr, fmt);
631+
vwarn(fmt, argptr);
632+
va_end(argptr);
633+
}
593634
}
594635
// void warnx(const char *fmt, ...);
595636
void warnx(const char *fmt, ...) {
596-
va_list argptr;
597-
va_start(argptr, fmt);
598-
vwarnx(fmt, argptr);
599-
va_end(argptr);
637+
if (fmt != NULL) {
638+
va_list argptr;
639+
va_start(argptr, fmt);
640+
vwarnx(fmt, argptr);
641+
va_end(argptr);
642+
}
643+
}
644+
// void warnc(int code, const char *fmt, ...);
645+
void warnc(int code, const char *fmt, ...) {
646+
if (thread_stderr == NULL) thread_stderr = stderr;
647+
fputs(ios_progname(), thread_stderr);
648+
if (fmt != NULL)
649+
{
650+
va_list argptr;
651+
va_start(argptr, fmt);
652+
fputs(": ", thread_stderr);
653+
vfprintf(thread_stderr, fmt, argptr);
654+
vwarn(fmt, argptr);
655+
va_end(argptr);
656+
}
657+
fputs(": ", thread_stderr);
658+
fputs(strerror(code), thread_stderr);
659+
putc('\n', thread_stderr);
600660
}

shell_cmds/basename/basename.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ static char sccsid[] = "@(#)basename.c 8.4 (Berkeley) 5/4/95";
4343
#endif /* not lint */
4444
#endif
4545

46-
#include <sys/cdefs.h>
47-
__RCSID("$FreeBSD: src/usr.bin/basename/basename.c,v 1.14 2002/09/04 23:28:52 dwmalone Exp $");
46+
// #include <sys/cdefs.h>
47+
// __RCSID("$FreeBSD: src/usr.bin/basename/basename.c,v 1.14 2002/09/04 23:28:52 dwmalone Exp $");
4848

4949
#include <err.h>
5050
#include <libgen.h>

shell_cmds/dirname/dirname.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ static const char copyright[] =
4040
#ifndef lint
4141
static const char sccsid[] = "@(#)dirname.c 8.4 (Berkeley) 5/4/95";
4242
#endif /* not lint */
43-
#include <sys/cdefs.h>
44-
__RCSID("$FreeBSD: src/usr.bin/dirname/dirname.c,v 1.11 2002/07/28 15:43:56 dwmalone Exp $");
43+
// #include <sys/cdefs.h>
44+
// __RCSID("$FreeBSD: src/usr.bin/dirname/dirname.c,v 1.11 2002/07/28 15:43:56 dwmalone Exp $");
4545

4646
#include <err.h>
4747
#include <libgen.h>

0 commit comments

Comments
 (0)