2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
24 * Modification History
26 * June 1, 2001 Allan Nathanson <ajn@apple.com>
27 * - public API conversion
29 * January 1, 2001 Allan Nathanson <ajn@apple.com>
35 #include <SystemConfiguration/SystemConfiguration.h>
36 #include <SystemConfiguration/SCPrivate.h>
43 usage(const char *command
)
45 SCPrint(TRUE
, stderr
, CFSTR("usage: %s [-n] new-set-name\n"), command
);
51 main(int argc
, char **argv
)
53 const char *command
= argv
[0];
56 CFStringRef current
= NULL
;
57 int currentMatched
= 0;
58 CFStringRef newSet
= NULL
; /* set key */
59 CFStringRef newSetUDN
= NULL
; /* user defined name */
61 SCPreferencesRef session
;
64 const void **setKeys
= NULL
;
65 const void **setVals
= NULL
;
68 /* process any arguments */
70 while ((opt
= getopt(argc
, argv
, "dvn")) != -1)
74 _sc_log
= FALSE
; /* enable framework logging */
89 prefix
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("/%@/"), kSCPrefSets
);
92 ? CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingMacRoman
)
95 session
= SCPreferencesCreate(NULL
, CFSTR("Select Set Command"), NULL
);
97 SCPrint(TRUE
, stderr
, CFSTR("SCPreferencesCreate() failed\n"));
101 /* check if a full path to the new "set" was specified */
102 if ((CFStringGetLength(newSet
) > 0) && CFStringHasPrefix(newSet
, prefix
)) {
104 CFMutableStringRef str
;
106 str
= CFStringCreateMutableCopy(NULL
, 0, newSet
);
107 CFStringDelete(str
, CFRangeMake(0, CFStringGetLength(prefix
)));
109 range
= CFStringFind(str
, CFSTR("/"), 0);
110 if (range
.location
!= kCFNotFound
) {
111 SCPrint(TRUE
, stderr
, CFSTR("Set \"%@\" not available\n."), newSet
);
119 sets
= SCPreferencesGetValue(session
, kSCPrefSets
);
121 SCPrint(TRUE
, stderr
, CFSTR("SCPreferencesGetValue(...,%s,...) failed\n"));
125 current
= SCPreferencesGetValue(session
, kSCPrefCurrentSet
);
127 if (CFStringHasPrefix(current
, prefix
)) {
128 CFMutableStringRef tmp
;
130 tmp
= CFStringCreateMutableCopy(NULL
, 0, current
);
131 CFStringDelete(tmp
, CFRangeMake(0, CFStringGetLength(prefix
)));
134 currentMatched
= -1; /* not prefixed */
138 currentMatched
= -2; /* not defined */
141 nSets
= CFDictionaryGetCount(sets
);
143 setKeys
= CFAllocatorAllocate(NULL
, nSets
* sizeof(CFStringRef
), 0);
144 setVals
= CFAllocatorAllocate(NULL
, nSets
* sizeof(CFDictionaryRef
), 0);
145 CFDictionaryGetKeysAndValues(sets
, setKeys
, setVals
);
148 /* check for set with matching name */
149 for (i
=0; i
<nSets
; i
++) {
150 CFStringRef key
= (CFStringRef
) setKeys
[i
];
151 CFDictionaryRef dict
= (CFDictionaryRef
)setVals
[i
];
153 if ((currentMatched
>= 0) && CFEqual(key
, current
)) {
157 if (CFEqual(newSet
, key
)) {
158 newSetUDN
= CFDictionaryGetValue(dict
, kSCPropUserDefinedName
);
159 if (newSetUDN
) CFRetain(newSetUDN
);
160 current
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%@%@"), prefix
, newSet
);
165 /* check for set with matching user-defined name */
166 for (i
=0; i
<nSets
; i
++) {
167 CFStringRef key
= (CFStringRef
) setKeys
[i
];
168 CFDictionaryRef dict
= (CFDictionaryRef
)setVals
[i
];
170 newSetUDN
= CFDictionaryGetValue(dict
, kSCPropUserDefinedName
);
171 if ((newSetUDN
!= NULL
) && CFEqual(newSet
, newSetUDN
)) {
173 newSet
= CFRetain(key
);
175 current
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%@%@"), prefix
, newSet
);
181 SCPrint(TRUE
, stderr
, CFSTR("Set \"%@\" not available.\n"), newSet
);
186 SCPrint(TRUE
, stderr
, CFSTR("\n"));
187 SCPrint(TRUE
, stderr
,
188 CFSTR("Defined sets include:%s\n"),
189 (currentMatched
> 0) ? " (* == current set)" : "");
191 for (i
=0; i
<nSets
; i
++) {
192 CFStringRef key
= (CFStringRef
) setKeys
[i
];
193 CFDictionaryRef dict
= (CFDictionaryRef
)setVals
[i
];
194 CFStringRef udn
= CFDictionaryGetValue(dict
, kSCPropUserDefinedName
);
196 SCPrint(TRUE
, stderr
,
197 CFSTR(" %s %@\t(%@)\n"),
198 ((currentMatched
> 0) && CFEqual(key
, current
)) ? "*" : " ",
200 udn
? udn
: CFSTR(""));
203 switch (currentMatched
) {
205 SCPrint(TRUE
, stderr
, CFSTR("\nCurrentSet not defined.\n"));
208 SCPrint(TRUE
, stderr
, CFSTR("\nCurrentSet \"%@\" may not be valid\n"), current
);
211 SCPrint(TRUE
, stderr
, CFSTR("\nCurrentSet \"%@\" not valid\n"), current
);
221 if (!SCPreferencesSetValue(session
, kSCPrefCurrentSet
, current
)) {
222 SCPrint(TRUE
, stderr
,
223 CFSTR("SCPreferencesSetValue(...,%@,%@) failed\n"),
229 if (!SCPreferencesCommitChanges(session
)) {
230 SCPrint(TRUE
, stderr
, CFSTR("SCPreferencesCommitChanges() failed\n"));
235 if (!SCPreferencesApplyChanges(session
)) {
236 SCPrint(TRUE
, stderr
, CFSTR("SCPreferencesApplyChanges() failed\n"));
243 SCPrint(TRUE
, stdout
,
244 CFSTR("%@ updated to %@ (%@)\n"),
247 newSetUDN
? newSetUDN
: CFSTR(""));