]> git.saurik.com Git - apple/system_cmds.git/blob - arch.tproj/arch.c
system_cmds-550.6.tar.gz
[apple/system_cmds.git] / arch.tproj / arch.c
1 /*
2 * Copyright (c) 1999, 2006, 2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <sys/cdefs.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdbool.h>
28 #include <stdlib.h>
29 #include <stddef.h>
30 #include <unistd.h>
31 #include <spawn.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/param.h>
35 #include <paths.h>
36 #include <err.h>
37 #include <mach/mach.h>
38 #include <mach-o/arch.h>
39 #include <limits.h>
40 #include <sys/fcntl.h>
41 #include <glob.h>
42 #include <CoreFoundation/CoreFoundation.h>
43 #include <NSSystemDirectories.h>
44
45 #ifndef ARCH_PROG
46 #define ARCH_PROG "arch"
47 #endif
48 #ifndef MACHINE_PROG
49 #define MACHINE_PROG "machine"
50 #endif
51
52 #define kKeyExecPath "ExecutablePath"
53 #define kKeyPlistVersion "PropertyListVersion"
54 #define kKeyPrefOrder "PreferredOrder"
55 #define kPlistExtension ".plist"
56 #define kSettingsDir "archSettings"
57
58 static const char envname[] = "ARCHPREFERENCE";
59
60 /* The CPU struct contains the argument buffer to posix_spawnattr_setbinpref_np */
61
62 typedef struct {
63 cpu_type_t *buf;
64 int errs;
65 size_t count;
66 size_t capacity;
67 } CPU;
68
69 typedef struct {
70 const char *arch;
71 cpu_type_t cpu;
72 } CPUTypes;
73
74 static const CPUTypes knownArchs[] = {
75 #if defined(__i386__) || defined(__x86_64__)
76 {"i386", CPU_TYPE_I386},
77 {"x86_64", CPU_TYPE_X86_64},
78 #elif defined(__arm__)
79 {"arm", CPU_TYPE_ARM},
80 #endif
81 #else
82 #error "Unsupported architecture"
83 #endif
84 };
85
86 /* environment SPI */
87 char **_copyenv(char **env);
88 int _setenvp(const char *name, const char *value, int rewrite, char ***envp, void *state);
89 int _unsetenvp(const char *name, char ***envp, void *state);
90
91 /* copy of environment */
92 char **envCopy = NULL;
93 extern char **environ;
94
95 /*
96 * The native 32 and 64-bit architectures (this is relative to the architecture
97 * the arch command is running. NULL means unsupported.
98 */
99 #if defined(__i386__) || defined(__x86_64__)
100 #define NATIVE_32 "i386"
101 #define NATIVE_64 "x86_64"
102 #elif defined(__arm__)
103 #define NATIVE_32 "arm"
104 #define NATIVE_64 NULL
105 #endif
106 #else
107 #error "Unsupported architecture"
108 #endif
109 bool unrecognizednative32seen = false;
110 bool unrecognizednative64seen = false;
111
112 /*
113 * arch - perform the original behavior of the arch and machine commands.
114 * The archcmd flag is non-zero for the arch command, zero for the machine
115 * command. This routine never returns.
116 */
117 static void __dead2
118 arch(int archcmd)
119 {
120 const NXArchInfo *arch = NXGetLocalArchInfo();
121
122 if(!arch)
123 errx(-1, "Unknown architecture.");
124 if(archcmd) {
125 arch = NXGetArchInfoFromCpuType(arch->cputype, CPU_SUBTYPE_MULTIPLE);
126 if(!arch)
127 errx(-1, "Unknown architecture.");
128 }
129 printf("%s%s", arch->name, (isatty(STDIN_FILENO) ? "\n" : ""));
130 exit(0);
131 }
132
133 /*
134 * spawnIt - run the posix_spawn command. cpu is the auto-sizing CPU structure.
135 * pflag is non-zero to call posix_spawnp; zero means to call posix_spawn.
136 * str is the name/path to pass to posix_spawn{,p}, and argv are
137 * the argument arrays to pass. This routine never returns.
138 */
139 static void __dead2
140 spawnIt(CPU *cpu, int pflag, const char *str, char **argv)
141 {
142 posix_spawnattr_t attr;
143 pid_t pid;
144 int ret;
145 size_t copied;
146 size_t count = cpu->count;
147 cpu_type_t *prefs = cpu->buf;
148
149 if(count == 0) {
150 if(unrecognizednative32seen)
151 warnx("Unsupported native 32-bit architecture");
152 if(unrecognizednative64seen)
153 warnx("Unsupported native 64-bit architecture");
154 exit(1);
155 }
156
157 if(unrecognizednative32seen)
158 fprintf(stderr, "warning: unsupported native 32-bit architecture\n");
159 if(unrecognizednative64seen)
160 fprintf(stderr, "warning: unsupported native 64-bit architecture\n");
161
162 if((ret = posix_spawnattr_init(&attr)) != 0)
163 errc(1, ret, "posix_spawnattr_init");
164 /* do the equivalent of exec, rather than creating a separate process */
165 if((ret = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETEXEC)) != 0)
166 errc(1, ret, "posix_spawnattr_setflags");
167 if((ret = posix_spawnattr_setbinpref_np(&attr, count, prefs, &copied)) != 0)
168 errc(1, ret, "posix_spawnattr_setbinpref_np");
169 if(copied != count)
170 errx(1, "posix_spawnattr_setbinpref_np only copied %lu of %lu", copied, count);
171 if(pflag)
172 ret = posix_spawnp(&pid, str, NULL, &attr, argv, envCopy ? envCopy : environ);
173 else
174 ret = posix_spawn(&pid, str, NULL, &attr, argv, envCopy ? envCopy : environ);
175 errc(1, ret, "posix_spawn%s: %s", (pflag ? "p" : ""), str);
176 }
177
178 /*
179 * initCPU - initialize a CPU structure, a dynamically expanding CPU types
180 * array.
181 */
182 static void
183 initCPU(CPU *cpu)
184 {
185 cpu->errs = 0;
186 cpu->count = 0;
187 cpu->capacity = 1;
188 cpu->buf = (cpu_type_t *)malloc(cpu->capacity * sizeof(cpu_type_t));
189 if(!cpu->buf)
190 err(1, "Failed to malloc CPU buffer");
191 }
192
193 /*
194 * addCPU - add a new CPU type value to the CPU structure, expanding
195 * the array as necessary.
196 */
197 static void
198 addCPU(CPU *cpu, cpu_type_t n)
199 {
200 if(cpu->count == cpu->capacity) {
201 cpu_type_t *newcpubuf;
202
203 cpu->capacity *= 2;
204 newcpubuf = (cpu_type_t *)realloc(cpu->buf, cpu->capacity * sizeof(cpu_type_t));
205 if(!newcpubuf)
206 err(1, "Out of memory realloc-ing CPU structure");
207 cpu->buf = newcpubuf;
208 }
209 cpu->buf[cpu->count++] = n;
210 }
211
212 /*
213 * addCPUbyname - add a new CPU type, given by name, to the CPU structure,
214 * expanding the array as necessary. The name is converted to a type value
215 * by the ArchDict dictionary.
216 */
217 static void
218 addCPUbyname(CPU *cpu, const char *name)
219 {
220 int i;
221
222 for (i=0; i < sizeof(knownArchs)/sizeof(knownArchs[0]); i++) {
223 if (0 == strcasecmp(name, knownArchs[i].arch)) {
224 addCPU(cpu, knownArchs[i].cpu);
225 return;
226 }
227 }
228
229 /* Didn't match a string in knownArchs */
230 warnx("Unknown architecture: %s", name);
231 cpu->errs++;
232 }
233
234 /*
235 * useEnv - parse the environment variable for CPU preferences. Use name
236 * to look for program-specific preferences, and append any CPU types to cpu.
237 * Returns the number of CPU types. Returns any specified execute path in
238 * execpath.
239 *
240 * The environment variable ARCHPREFERENCE has the format:
241 * spec[;spec]...
242 * a semicolon separated list of specifiers. Each specifier has the format:
243 * [prog:[execpath:]]type[,type]...
244 * a comma separate list of CPU type names, optionally proceeded by a program
245 * name and an execpath. If program name exist, that types only apply to that
246 * program. If execpath is specified, it is returned. If no program name
247 * exists, then it applies to all programs. So ordering of the specifiers is
248 * important, as the default (no program name) specifier must be last.
249 */
250 static size_t
251 useEnv(CPU *cpu, const char *name, char **execpath)
252 {
253 char *val = getenv(envname);
254 if(!val)
255 return 0;
256
257 /* cp will point to the basename of name */
258 const char *cp = strrchr(name, '/');
259 if(cp) {
260 cp++;
261 if(!*cp)
262 errx(1, "%s: no name after last slash", name);
263 } else
264 cp = name;
265 /* make a copy of the environment variable value, so we can modify it */
266 val = strdup(val);
267 if(!val)
268 err(1, "Can't copy environment %s", envname);
269 char *str = val;
270 char *blk;
271 /* for each specifier */
272 while((blk = strsep(&str, ";")) != NULL) {
273 if(*blk == 0)
274 continue; /* two adjacent semicolons */
275 /* now split on colons */
276 char *n = strsep(&blk, ":");
277 if(blk) {
278 char *p = strsep(&blk, ":");
279 if(!blk) { /* there is only one colon, so no execpath */
280 blk = p;
281 p = NULL;
282 } else if(!*p) /* two consecutive colons, so no execpath */
283 p = NULL;
284 if(!*blk)
285 continue; /* no cpu list, so skip */
286 /* if the name matches, or there is no name, process the cpus */
287 if(!*n || strcmp(n, cp) == 0) {
288 if(cpu->count == 0) { /* only if we haven't processed architectures */
289 char *t;
290 while((t = strsep(&blk, ",")) != NULL)
291 addCPUbyname(cpu, t);
292 }
293 *execpath = (*n ? p : NULL); /* only use the exec path is name is set */
294 break;
295 }
296 } else { /* no colons at all, so process as default */
297 if(cpu->count == 0) { /* only if we haven't processed architectures */
298 blk = n;
299 while((n = strsep(&blk, ",")) != NULL)
300 addCPUbyname(cpu, n);
301 }
302 *execpath = NULL;
303 break;
304 }
305 }
306 if(cpu->errs) /* errors during addCPUbyname are fatal */
307 exit(1);
308 return cpu->count; /* return count of architectures */
309 }
310
311 /*
312 * spawnFromPreference - called when argv[0] is not "arch" or "machine", or
313 * argv[0] was arch, but no commandline architectures were specified.
314 * If the environment variable ARCHPREFERENCE is specified, and there is a
315 * match to argv[0], use the specified cpu preferences. If no exec path
316 * is specified in ARCHPREFERENCE, or no match is found in ARCHPREFERENCE,
317 * get any additional information from a .plist file with the name of argv[0].
318 * This routine never returns.
319 */
320 static void __dead2
321 spawnFromPreferences(CPU *cpu, int needexecpath, char **argv)
322 {
323 char *epath = NULL;
324 char fpath[PATH_MAX];
325 char execpath2[PATH_MAX];
326 CFDictionaryRef plist = NULL;
327 NSSearchPathEnumerationState state;
328 size_t count, i;
329 const char *prog = strrchr(*argv, '/');
330
331 if(prog)
332 prog++;
333 else
334 prog = *argv;
335 if(!*prog)
336 errx(1, "Not program name specified");
337
338 /* check the environment variable first */
339 if((count = useEnv(cpu, prog, &epath)) > 0) {
340 /* if we were called as arch, use posix_spawnp */
341 if(!needexecpath)
342 spawnIt(cpu, 1, (epath ? epath : *argv), argv);
343 /* otherwise, if we have the executable path, call posix_spawn */
344 if(epath)
345 spawnIt(cpu, 0, epath, argv);
346 }
347
348 state = NSStartSearchPathEnumeration(NSLibraryDirectory, NSAllDomainsMask);
349 while ((state = NSGetNextSearchPathEnumeration(state, fpath))) {
350
351 CFURLRef url;
352 CFReadStreamRef stream;
353
354 if (fpath[0] == '~') {
355 glob_t pglob;
356 int gret;
357
358 bzero(&pglob, sizeof(pglob));
359
360 gret = glob(fpath, GLOB_TILDE, NULL, &pglob);
361 if (gret == 0) {
362 int i;
363 for (i=0; i < pglob.gl_pathc; i++) {
364 /* take the first glob expansion */
365 strlcpy(fpath, pglob.gl_pathv[i], sizeof(fpath));
366 break;
367 }
368 }
369 globfree(&pglob);
370 }
371
372 // Handle path
373 strlcat(fpath, "/" kSettingsDir "/", sizeof(fpath));
374 strlcat(fpath, prog, sizeof(fpath));
375 strlcat(fpath, kPlistExtension, sizeof(fpath));
376 // printf("component: %s\n", fpath);
377
378 int fd, ret;
379 size_t length;
380 ssize_t rsize;
381 struct stat sb;
382 void *buffer;
383 fd = open(fpath, O_RDONLY, 0);
384 if (fd >= 0) {
385 ret = fstat(fd, &sb);
386 if (ret == 0) {
387 if (sb.st_size <= SIZE_T_MAX) {
388 length = (size_t)sb.st_size;
389 buffer = malloc(length); /* ownership transferred to CFData */
390 if (buffer) {
391 rsize = read(fd, buffer, length);
392 if (rsize == length) {
393 CFDataRef data = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, buffer, length, kCFAllocatorMalloc);
394 if (data) {
395 buffer = NULL;
396 plist = CFPropertyListCreateWithData(kCFAllocatorDefault, data, kCFPropertyListImmutable, NULL, NULL);
397 CFRelease(data);
398 }
399 }
400 if (buffer) {
401 free(buffer);
402 }
403 }
404 }
405 }
406 close(fd);
407 }
408
409 if (plist) {
410 break;
411 }
412 }
413
414 if (plist) {
415 if (CFGetTypeID(plist) != CFDictionaryGetTypeID())
416 errx(1, "%s: plist not a dictionary", fpath);
417 } else {
418 errx(1, "Can't find any plists for %s", prog);
419 }
420
421
422 int errs = 0; /* scan for all errors and fail later */
423 do { /* begin block */
424 /* check the plist version */
425 CFStringRef vers = CFDictionaryGetValue(plist, CFSTR(kKeyPlistVersion));
426 if(!vers) {
427 warnx("%s: No key %s", fpath, kKeyPlistVersion);
428 errs++;
429 } else if(CFGetTypeID(vers) != CFStringGetTypeID()) {
430 warnx("%s: %s is not a string", fpath, kKeyPlistVersion);
431 errs++;
432 } else if(!CFEqual(vers, CFSTR("1.0"))) {
433 warnx("%s: %s not 1.0", fpath, kKeyPlistVersion);
434 errs++;
435 }
436 /* get the execpath */
437 CFStringRef execpath = CFDictionaryGetValue(plist, CFSTR(kKeyExecPath));
438 if(!execpath) {
439 warnx("%s: No key %s", fpath, kKeyExecPath);
440 errs++;
441 } else if(CFGetTypeID(execpath) != CFStringGetTypeID()) {
442 warnx("%s: %s is not a string", fpath, kKeyExecPath);
443 errs++;
444 }
445 if (!CFStringGetFileSystemRepresentation(execpath, execpath2, sizeof(execpath2))) {
446 warnx("%s: could not get exec path", fpath);
447 errs++;
448 }
449 /* if we already got cpu preferences from ARCHPREFERENCE, we are done */
450 if(count > 0)
451 break;
452 /* otherwise, parse the cpu preferences from the plist */
453 CFArrayRef p = CFDictionaryGetValue(plist, CFSTR(kKeyPrefOrder));
454 if(!p) {
455 warnx("%s: No key %s", fpath, kKeyPrefOrder);
456 errs++;
457 } else if(CFGetTypeID(p) != CFArrayGetTypeID()) {
458 warnx("%s: %s is not an array", fpath, kKeyPrefOrder);
459 errs++;
460 } else if((count = CFArrayGetCount(p)) == 0) {
461 warnx("%s: no entries in %s", fpath, kKeyPrefOrder);
462 errs++;
463 } else {
464 /* finally build the cpu type array */
465 for(i = 0; i < count; i++) {
466 CFStringRef a = CFArrayGetValueAtIndex(p, i);
467 if(CFGetTypeID(a) != CFStringGetTypeID()) {
468 warnx("%s: entry %lu of %s is not a string", fpath, i, kKeyPrefOrder);
469 errs++;
470 } else {
471 char astr[128];
472 if (CFStringGetCString(a, astr, sizeof(astr), kCFStringEncodingASCII)) {
473 addCPUbyname(cpu, astr);
474 }
475 }
476 }
477 }
478 } while(0); /* end block */
479 if(errs) /* exit if there were any reported errors */
480 exit(1);
481
482 CFRelease(plist);
483
484 /* call posix_spawn */
485 spawnIt(cpu, 0, execpath2, argv);
486 }
487
488 static void __dead2
489 usage(int ret)
490 {
491 fprintf(stderr,
492 "Usage: %s\n"
493 " Display the machine's architecture type\n"
494 "Usage: %s {-arch_name | -arch arch_name} ... [-c] [-d envname] ... [-e envname=value] ... [-h] prog [arg ...]\n"
495 " Run prog with any arguments, using the given architecture\n"
496 " order. If no architectures are specified, use the\n"
497 " ARCHPREFERENCE environment variable, or a property list file.\n"
498 " -c will clear out all environment variables before running prog.\n"
499 " -d will delete the given environment variable before running prog.\n"
500 " -e will add the given environment variable/value before running prog.\n"
501 " -h will print usage message and exit.\n",
502 ARCH_PROG, ARCH_PROG);
503 exit(ret);
504 }
505
506 /*
507 * wrapped - check the path to see if it is a link to /usr/bin/arch.
508 */
509 static int
510 wrapped(const char *name)
511 {
512 size_t lp, ln;
513 char *p;
514 char *bp = NULL;
515 char *cur, *path;
516 char buf[MAXPATHLEN], rpbuf[MAXPATHLEN];
517 struct stat sb;
518
519 ln = strlen(name);
520
521 do { /* begin block */
522 /* If it's an absolute or relative path name, it's easy. */
523 if(index(name, '/')) {
524 if(stat(name, &sb) == 0 && S_ISREG(sb.st_mode) && access(name, X_OK) == 0) {
525 bp = (char *)name;
526 break;
527 }
528 errx(1, "%s isn't executable", name);
529 }
530
531 /* search the PATH, looking for name */
532 if((path = getenv("PATH")) == NULL)
533 path = _PATH_DEFPATH;
534
535 cur = alloca(strlen(path) + 1);
536 if(cur == NULL)
537 err(1, "alloca");
538 strcpy(cur, path);
539 while((p = strsep(&cur, ":")) != NULL) {
540 /*
541 * It's a SHELL path -- double, leading and trailing colons
542 * mean the current directory.
543 */
544 if(*p == '\0') {
545 p = ".";
546 lp = 1;
547 } else
548 lp = strlen(p);
549
550 /*
551 * If the path is too long complain. This is a possible
552 * security issue; given a way to make the path too long
553 * the user may execute the wrong program.
554 */
555 if(lp + ln + 2 > sizeof(buf)) {
556 warn("%s: path too long", p);
557 continue;
558 }
559 bcopy(p, buf, lp);
560 buf[lp] = '/';
561 bcopy(name, buf + lp + 1, ln);
562 buf[lp + ln + 1] = '\0';
563 if(stat(buf, &sb) == 0 && S_ISREG(sb.st_mode) && access(buf, X_OK) == 0) {
564 bp = buf;
565 break;
566 }
567 }
568 if(p == NULL)
569 errx(1, "Can't find %s in PATH", name);
570 } while(0); /* end block */
571 if(realpath(bp, rpbuf) == NULL)
572 errx(1, "realpath failed on %s", bp);
573 return (strcmp(rpbuf, "/usr/bin/" ARCH_PROG) == 0);
574 }
575
576 /*
577 * spawnFromArgs - called when arch has arguments specified. The arch command
578 * line arguments are:
579 * % arch [[{-xxx | -arch xxx}]...] prog [arg]...
580 * where xxx is a cpu name, and the command to execute and its arguments follow.
581 * If no commandline cpu names are given, the environment variable
582 * ARCHPREFERENCE is used. This routine never returns.
583 */
584
585 #define MATCHARG(a,m) ({ \
586 const char *arg = *(a); \
587 if(arg[1] == '-') arg++; \
588 strcmp(arg, (m)) == 0; \
589 })
590
591 #define MATCHARGWITHVALUE(a,m,n,e) ({ \
592 const char *ret = NULL; \
593 const char *arg = *(a); \
594 if(arg[1] == '-') arg++; \
595 if(strcmp(arg, (m)) == 0) { \
596 if(*++(a) == NULL) { \
597 warnx(e); \
598 usage(1); \
599 } \
600 ret = *(a); \
601 } else if(strncmp(arg, (m), (n)) == 0 && arg[n] == '=') { \
602 ret = arg + (n) + 1; \
603 } \
604 ret; \
605 })
606
607 #define MAKEENVCOPY(e) \
608 if(!envCopy) { \
609 envCopy = _copyenv(environ); \
610 if(envCopy == NULL) \
611 errx(1, (e)); \
612 }
613
614 static void __dead2
615 spawnFromArgs(CPU *cpu, char **argv)
616 {
617 const char *ap, *ret;
618
619 /* process arguments */
620 for(argv++; *argv && **argv == '-'; argv++) {
621 if((ret = MATCHARGWITHVALUE(argv, "-arch", 5, "-arch without architecture"))) {
622 ap = ret;
623 } else if(MATCHARG(argv, "-32")) {
624 ap = NATIVE_32;
625 if(!ap) {
626 unrecognizednative32seen = true;
627 continue;
628 }
629 } else if(MATCHARG(argv, "-64")) {
630 ap = NATIVE_64;
631 if(!ap) {
632 unrecognizednative64seen = true;
633 continue;
634 }
635 } else if(MATCHARG(argv, "-c")) {
636 free(envCopy);
637 envCopy = _copyenv(NULL); // create empty environment
638 if(!envCopy)
639 errx(1, "Out of memory processing -c");
640 continue;
641 } else if((ret = MATCHARGWITHVALUE(argv, "-d", 2, "-d without envname"))) {
642 MAKEENVCOPY("Out of memory processing -d");
643 _unsetenvp(ret, &envCopy, NULL);
644 continue;
645 } else if((ret = MATCHARGWITHVALUE(argv, "-e", 2, "-e without envname=value"))) {
646 MAKEENVCOPY("Out of memory processing -e");
647 const char *cp = strchr(ret, '=');
648 if(!cp) {
649 warnx("-e %s: no equal sign", ret);
650 usage(1);
651 }
652 cp++; // skip to value
653 /*
654 * _setenvp() only uses the name before any equal sign found in
655 * the first argument.
656 */
657 _setenvp(ret, cp, 1, &envCopy, NULL);
658 continue;
659 } else if(MATCHARG(argv, "-h")) {
660 usage(0);
661 } else {
662 ap = *argv + 1;
663 if(*ap == '-') ap++;
664 }
665 addCPUbyname(cpu, ap);
666 }
667 if(cpu->errs)
668 exit(1);
669 if(!*argv || !**argv) {
670 warnx("No command to execute");
671 usage(1);
672 }
673 /* if the program is already a link to arch, then force execpath */
674 int needexecpath = wrapped(*argv);
675
676 /*
677 * If we don't have any architecutures, try ARCHPREFERENCE and plist
678 * files.
679 */
680 if((cpu->count == 0) || needexecpath)
681 spawnFromPreferences(cpu, needexecpath, argv); /* doesn't return */
682
683 /*
684 * Call posix_spawnp on the program name.
685 */
686 spawnIt(cpu, 1, *argv, argv);
687 }
688
689
690 /* the main() routine */
691 int
692 main(int argc, char **argv)
693 {
694 const char *prog = getprogname();
695 int my_name_is_arch;
696 CPU cpu;
697
698 if(strcmp(prog, MACHINE_PROG) == 0) {
699 if(argc > 1)
700 errx(-1, "no arguments accepted");
701 arch(0); /* the "machine" command was called */
702 } else if((my_name_is_arch = (strcmp(prog, ARCH_PROG) == 0))) {
703 if(argc == 1)
704 arch(1); /* the "arch" command with no arguments was called */
705 }
706
707 initCPU(&cpu);
708
709 if(my_name_is_arch)
710 spawnFromArgs(&cpu, argv);
711 else
712 spawnFromPreferences(&cpu, 1, argv);
713
714 /* should never get here */
715 errx(1, "returned from spawn");
716 }