2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
27 * Modification History
29 * June 1, 2001 Allan Nathanson <ajn@apple.com>
30 * - public API conversion
32 * January 1, 2001 Allan Nathanson <ajn@apple.com>
40 #include <SystemConfiguration/SystemConfiguration.h>
41 #include <SystemConfiguration/SCPrivate.h>
47 static struct option longopts
[] = {
48 // { "debug", no_argument, 0, 'd' },
49 // { "verbose", no_argument, 0, 'v' },
50 // { "do-not-apply", no_argument, 0, 'n' },
51 { "help", no_argument
, 0, '?' },
57 usage(const char *command
)
59 SCPrint(TRUE
, stderr
, CFSTR("usage: %s [-n] new-set-name\n"), command
);
65 main(int argc
, char **argv
)
67 const char *command
= argv
[0];
70 CFStringRef current
= NULL
;
71 int currentMatched
= 0;
72 CFStringRef newSet
= NULL
; /* set key */
73 CFStringRef newSetUDN
= NULL
; /* user defined name */
75 SCPreferencesRef session
;
78 const void **setKeys
= NULL
;
79 const void **setVals
= NULL
;
82 /* process any arguments */
84 while ((opt
= getopt_long(argc
, argv
, "dvn", longopts
, NULL
)) != -1)
88 _sc_log
= FALSE
; /* enable framework logging */
103 prefix
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("/%@/"), kSCPrefSets
);
106 ? CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingMacRoman
)
109 session
= SCPreferencesCreate(NULL
, CFSTR("Select Set Command"), NULL
);
111 SCPrint(TRUE
, stderr
, CFSTR("SCPreferencesCreate() failed\n"));
115 /* check if a full path to the new "set" was specified */
116 if ((CFStringGetLength(newSet
) > 0) && CFStringHasPrefix(newSet
, prefix
)) {
118 CFMutableStringRef str
;
120 str
= CFStringCreateMutableCopy(NULL
, 0, newSet
);
121 CFStringDelete(str
, CFRangeMake(0, CFStringGetLength(prefix
)));
123 range
= CFStringFind(str
, CFSTR("/"), 0);
124 if (range
.location
!= kCFNotFound
) {
125 SCPrint(TRUE
, stderr
, CFSTR("Set \"%@\" not available\n."), newSet
);
133 sets
= SCPreferencesGetValue(session
, kSCPrefSets
);
135 SCPrint(TRUE
, stderr
, CFSTR("SCPreferencesGetValue(...,%s,...) failed\n"));
139 current
= SCPreferencesGetValue(session
, kSCPrefCurrentSet
);
141 if (CFStringHasPrefix(current
, prefix
)) {
142 CFMutableStringRef tmp
;
144 tmp
= CFStringCreateMutableCopy(NULL
, 0, current
);
145 CFStringDelete(tmp
, CFRangeMake(0, CFStringGetLength(prefix
)));
148 currentMatched
= -1; /* not prefixed */
152 currentMatched
= -2; /* not defined */
155 nSets
= CFDictionaryGetCount(sets
);
157 setKeys
= CFAllocatorAllocate(NULL
, nSets
* sizeof(CFStringRef
), 0);
158 setVals
= CFAllocatorAllocate(NULL
, nSets
* sizeof(CFDictionaryRef
), 0);
159 CFDictionaryGetKeysAndValues(sets
, setKeys
, setVals
);
162 /* check for set with matching name */
163 for (i
= 0; i
< nSets
; i
++) {
164 CFStringRef key
= (CFStringRef
) setKeys
[i
];
165 CFDictionaryRef dict
= (CFDictionaryRef
)setVals
[i
];
167 if ((currentMatched
>= 0) && CFEqual(key
, current
)) {
171 if (CFEqual(newSet
, key
)) {
172 newSetUDN
= CFDictionaryGetValue(dict
, kSCPropUserDefinedName
);
173 if (newSetUDN
) CFRetain(newSetUDN
);
174 current
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%@%@"), prefix
, newSet
);
179 /* check for set with matching user-defined name */
180 for (i
= 0; i
< nSets
; i
++) {
181 CFStringRef key
= (CFStringRef
) setKeys
[i
];
182 CFDictionaryRef dict
= (CFDictionaryRef
)setVals
[i
];
184 newSetUDN
= CFDictionaryGetValue(dict
, kSCPropUserDefinedName
);
185 if ((newSetUDN
!= NULL
) && CFEqual(newSet
, newSetUDN
)) {
187 newSet
= CFRetain(key
);
189 current
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%@%@"), prefix
, newSet
);
195 SCPrint(TRUE
, stderr
, CFSTR("Set \"%@\" not available.\n\n"), newSet
);
198 SCPrint(TRUE
, stderr
,
199 CFSTR("Defined sets include:%s\n"),
200 (currentMatched
> 0) ? " (* == current set)" : "");
202 for (i
= 0; i
< nSets
; i
++) {
203 CFStringRef key
= (CFStringRef
) setKeys
[i
];
204 CFDictionaryRef dict
= (CFDictionaryRef
)setVals
[i
];
205 CFStringRef udn
= CFDictionaryGetValue(dict
, kSCPropUserDefinedName
);
207 SCPrint(TRUE
, stderr
,
208 CFSTR(" %s %@\t(%@)\n"),
209 ((currentMatched
> 0) && CFEqual(key
, current
)) ? "*" : " ",
211 udn
? udn
: CFSTR(""));
214 switch (currentMatched
) {
216 SCPrint(TRUE
, stderr
, CFSTR("\nCurrentSet not defined.\n"));
219 SCPrint(TRUE
, stderr
, CFSTR("\nCurrentSet \"%@\" may not be valid\n"), current
);
222 SCPrint(TRUE
, stderr
, CFSTR("\nCurrentSet \"%@\" not valid\n"), current
);
232 if (!SCPreferencesSetValue(session
, kSCPrefCurrentSet
, current
)) {
233 SCPrint(TRUE
, stderr
,
234 CFSTR("SCPreferencesSetValue(...,%@,%@) failed\n"),
240 if (!SCPreferencesCommitChanges(session
)) {
241 SCPrint(TRUE
, stderr
, CFSTR("SCPreferencesCommitChanges() failed\n"));
246 if (!SCPreferencesApplyChanges(session
)) {
247 SCPrint(TRUE
, stderr
, CFSTR("SCPreferencesApplyChanges() failed\n"));
254 SCPrint(TRUE
, stdout
,
255 CFSTR("%@ updated to %@ (%@)\n"),
258 newSetUDN
? newSetUDN
: CFSTR(""));