···11+/*
22+This file is part of Darling.
33+44+Copyright (C) 2017 Lubos Dolezel
55+66+Darling is free software: you can redistribute it and/or modify
77+it under the terms of the GNU General Public License as published by
88+the Free Software Foundation, either version 3 of the License, or
99+(at your option) any later version.
1010+1111+Darling is distributed in the hope that it will be useful,
1212+but WITHOUT ANY WARRANTY; without even the implied warranty of
1313+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1414+GNU General Public License for more details.
1515+1616+You should have received a copy of the GNU General Public License
1717+along with Darling. If not, see <http://www.gnu.org/licenses/>.
1818+*/
1919+2020+#include <stdio.h>
2121+#include <getopt.h>
2222+#include <stdlib.h>
2323+#include <string.h>
2424+#include <unistd.h>
2525+#include <errno.h>
2626+#include "xcselect.h"
2727+2828+void printUsage(void);
2929+void doReset(void);
3030+void doSwitch(const char* path);
3131+void doPrintManPaths(void);
3232+3333+int main(int argc, const char** argv)
3434+{
3535+ if (argc == 1)
3636+ {
3737+ fprintf(stderr, "xcode-select: error: no command option given\n");
3838+ printUsage();
3939+ }
4040+ else if (argc == 2 || argc == 3)
4141+ {
4242+ if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)
4343+ {
4444+ printUsage();
4545+ }
4646+ else if (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-version") == 0)
4747+ {
4848+ printf("xcode-select for Darling, version 1.0\n");
4949+ }
5050+ else if (strcmp(argv[1], "-p") == 0 || strcmp(argv[1], "--print-path") == 0 || strcmp(argv[1], "-print-path") == 0)
5151+ {
5252+ char path[1024];
5353+ bool is_cmd_line;
5454+5555+ if (xcselect_get_developer_dir_path(path, sizeof(path), &is_cmd_line))
5656+ {
5757+ printf("%s\n", path);
5858+ }
5959+ else
6060+ {
6161+ fprintf(stderr, "xcode-select: error: unable to get active developer directory\n");
6262+ return 1;
6363+ }
6464+ }
6565+ else if (strcmp(argv[1], "--show-manpaths") == 0)
6666+ {
6767+ doPrintManPaths();
6868+ }
6969+ else if (strcmp(argv[1], "--install") == 0)
7070+ {
7171+ if (access("/Library/Developer/CommandLineTools/usr/lib/libxcrun.dylib", F_OK) == 0)
7272+ {
7373+ fprintf(stderr, "xcode-select: error: command line tools are already installed\n");
7474+ return 1;
7575+ }
7676+ else
7777+ {
7878+ printf("Please visit https://developer.apple.com/download/more/ and download\n"
7979+ "a package named \"Command Line Tools\".\n\n"
8080+ "After that, install them by running `installer -pkg file.pkg -target /`.\n");
8181+ }
8282+ }
8383+ else if (strcmp(argv[1], "-r") == 0 || strcmp(argv[1], "--reset") == 0)
8484+ {
8585+ doReset();
8686+ }
8787+ else if (strcmp(argv[1], "-s") == 0 || strcmp(argv[1], "--switch") == 0 || strcmp(argv[1], "-switch") == 0)
8888+ {
8989+ if (argc != 3)
9090+ {
9191+ fprintf(stderr, "xcode-select: error: missing argument to '%s'\n", argv[1]);
9292+ return 1;
9393+ }
9494+9595+ doSwitch(argv[2]);
9696+ }
9797+ else
9898+ {
9999+ fprintf(stderr, "xcode-select: error: unknown option: %s\n", argv[1]);
100100+ printUsage();
101101+ return 1;
102102+ }
103103+104104+ return 0;
105105+ }
106106+ else
107107+ {
108108+ fprintf(stderr, "xcode-select: error: bad argument count\n");
109109+ printUsage();
110110+ }
111111+ return 1;
112112+}
113113+114114+void printUsage(void)
115115+{
116116+ fprintf(stderr, "Usage: xcode-select [options]\n\n");
117117+ fprintf(stderr, "xcode-select is used to set up path to the active developer directory.\n"
118118+ "This affects both toolchain commands (such as clang or make) and Xcode-specific\n"
119119+ "tools (such as xcodebuild).\n\n");
120120+ fprintf(stderr, "Options:\n"
121121+ " -h, --help print this help message\n"
122122+ " -p, --print-path print the path of the active developer directory\n"
123123+ " -s <path>, --switch <path> change the path of the active developer directory\n"
124124+ " --install trigger the installation of command line developer tools\n"
125125+ " -v, --version print the version of this tool\n"
126126+ " -r, --reset reset to the default developer directory\n");
127127+}
128128+129129+static void killLink(const char* path)
130130+{
131131+ if (unlink(path) != 0)
132132+ {
133133+ if (errno != ENOENT)
134134+ {
135135+ fprintf(stderr, "xcode-select: error: cannot remove existing link at '%s': %s\n",
136136+ path, strerror(errno));
137137+ exit(1);
138138+ }
139139+ }
140140+}
141141+142142+void doReset(void)
143143+{
144144+ killLink("/var/db/xcode_select_link");
145145+ killLink("/usr/share/xcode-select/xcode_dir_link");
146146+ killLink("/usr/share/xcode-select/xcode_dir_path");
147147+}
148148+149149+void doSwitch(const char* path)
150150+{
151151+ char buffer[1024];
152152+ bool unused;
153153+154154+ if (!xcselect_find_developer_contents_from_path(path, buffer, unused, sizeof(buffer)))
155155+ {
156156+ fprintf(stderr, "xcode-select: error: invalid developer directory '%s'\n", path);
157157+ exit(1);
158158+ }
159159+160160+ doReset();
161161+162162+ umask(022);
163163+ if (symlink(buffer, "/var/db/xcode_select_link") != 0)
164164+ {
165165+ fprintf(stderr, "xcode-select: error: unable to create symlink: %s\n",
166166+ strerror(errno));
167167+ exit(1);
168168+ }
169169+}
170170+171171+void doPrintManPaths(void)
172172+{
173173+ xcselect_manpaths *xcp;
174174+ const char *path;
175175+ unsigned i, count;
176176+177177+ xcp = xcselect_get_manpaths(NULL);
178178+ if (xcp != NULL) {
179179+ count = xcselect_manpaths_get_num_paths(xcp);
180180+ for (i = 0; i < count; i++) {
181181+ path = xcselect_manpaths_get_path(xcp, i);
182182+ if (path != NULL) {
183183+ puts(path);
184184+ }
185185+ }
186186+ xcselect_manpaths_free(xcp);
187187+ }
188188+}
189189+
+1-1
src/xcselect/xcrun-shim.c
···1616You should have received a copy of the GNU General Public License
1717along with Darling. If not, see <http://www.gnu.org/licenses/>.
1818*/
1919-#include "libxcselect.h"
1919+#include "xcselect.h"
2020#include <stdlib.h>
2121#include <string.h>
2222
+1-1
src/xcselect/xcrun.c
···1616You should have received a copy of the GNU General Public License
1717along with Darling. If not, see <http://www.gnu.org/licenses/>.
1818*/
1919-#include "libxcselect.h"
1919+#include "xcselect.h"
2020#include <stdlib.h>
2121#include <string.h>
2222