Tidy indent and change user id with -u flag

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@3843 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 1999-03-03 06:49:00 +00:00
parent b6e905d98d
commit f297891c86

View file

@ -25,6 +25,7 @@
#include <Foundation/NSUserDefaults.h>
#include <Foundation/NSDebug.h>
#include <Foundation/NSAutoreleasePool.h>
#include <Foundation/NSPathUtilities.h>
int
@ -44,7 +45,8 @@ main(int argc, char** argv)
int i;
proc = [NSProcessInfo processInfo];
if (proc == nil) {
if (proc == nil)
{
NSLog(@"defaults: unable to get process information!\n");
[pool release];
exit(0);
@ -52,9 +54,11 @@ main(int argc, char** argv)
args = [proc arguments];
for (i = 1; i < [args count]; i++) {
for (i = 1; i < [args count]; i++)
{
if ([[args objectAtIndex: i] isEqual: @"--help"] ||
[[args objectAtIndex: i] isEqual: @"help"]) {
[[args objectAtIndex: i] isEqual: @"help"])
{
printf(
"The 'defaults' command lets you to read and modify a user's defaults.\n\n"
"This program replaces the old NeXTstep style dread, dwrite, and dremove\n"
@ -106,7 +110,8 @@ main(int argc, char** argv)
[pool release];
exit(0);
}
else if ([[args objectAtIndex: i] isEqual: @"plist"]) {
else if ([[args objectAtIndex: i] isEqual: @"plist"])
{
printf(
"A property list is a method of providing structured information consisting\n"
"of strings, arrays, dictionaries, and binary data.\n\n"
@ -161,28 +166,36 @@ property list which is set as the value of a default.\n\n");
}
i = 1;
if ([args count] <= i) {
if ([args count] <= i)
{
NSLog(@"defaults: too few arguments supplied!\n");
[pool release];
exit(0);
}
if ([[args objectAtIndex: i] isEqual: @"-u"]) {
if ([args count] > ++i) {
if ([[args objectAtIndex: i] isEqual: @"-u"])
{
if ([args count] > ++i)
{
user = [args objectAtIndex: i++];
}
else {
else
{
NSLog(@"defaults: no name supplied for -u option!\n");
[pool release];
exit(0);
}
}
if (user) {
if (user)
{
GSSetUserName(user);
defs = [[NSUserDefaults alloc] initWithUser: user];
}
else {
else
{
defs = [NSUserDefaults standardUserDefaults];
}
if (defs == nil) {
if (defs == nil)
{
NSLog(@"defaults: unable to access defaults database!\n");
[pool release];
exit(0);
@ -190,28 +203,36 @@ property list which is set as the value of a default.\n\n");
/* We don't want this tool in the defaults database - so remove it. */
[defs removePersistentDomainForName: [proc processName]];
if ([args count] <= i) {
if ([args count] <= i)
{
NSLog(@"defaults: too few arguments supplied!\n");
[pool release];
exit(0);
}
if ([[args objectAtIndex: i] isEqual: @"read"] ||
[[args objectAtIndex: i] isEqual: @"readkey"]) {
if ([[args objectAtIndex: i] isEqual: @"read"]) {
if ([args count] == ++i) {
[[args objectAtIndex: i] isEqual: @"readkey"])
{
if ([[args objectAtIndex: i] isEqual: @"read"])
{
if ([args count] == ++i)
{
name = nil;
owner = nil;
}
else {
else
{
owner = [args objectAtIndex: i++];
if ([args count] > i) {
if ([args count] > i)
{
name = [args objectAtIndex: i];
}
}
}
else {
if ([args count] == ++i) {
else
{
if ([args count] == ++i)
{
NSLog(@"defaults: too few arguments supplied!\n");
[pool release];
exit(0);
@ -221,28 +242,35 @@ property list which is set as the value of a default.\n\n");
}
domains = [defs persistentDomainNames];
for (i = 0; i < [domains count]; i++) {
for (i = 0; i < [domains count]; i++)
{
NSString *domainName = [domains objectAtIndex: i];
if (owner == nil || [owner isEqual: domainName]) {
if (owner == nil || [owner isEqual: domainName])
{
NSDictionary *dom;
dom = [defs persistentDomainForName: domainName];
if (dom) {
if (name == nil) {
if (dom)
{
if (name == nil)
{
NSEnumerator *enumerator;
NSString *key;
enumerator = [dom keyEnumerator];
while ((key = [enumerator nextObject]) != nil) {
while ((key = [enumerator nextObject]) != nil)
{
id obj = [dom objectForKey: key];
const char *ptr;
printf("%s %s '",
[domainName cString], [key cString]);
ptr = [[obj description] cString];
while (*ptr) {
if (*ptr == '\'') {
while (*ptr)
{
if (*ptr == '\'')
{
putchar('\'');
}
putchar(*ptr);
@ -251,17 +279,21 @@ property list which is set as the value of a default.\n\n");
printf("'\n");
}
}
else {
else
{
id obj = [dom objectForKey: name];
if (obj) {
if (obj)
{
const char *ptr;
printf("%s %s '",
[domainName cString], [name cString]);
ptr = [[obj description] cString];
while (*ptr) {
if (*ptr == '\'') {
while (*ptr)
{
if (*ptr == '\'')
{
putchar('\'');
}
putchar(*ptr);
@ -275,14 +307,17 @@ property list which is set as the value of a default.\n\n");
}
}
if (found == NO && name != nil) {
if (found == NO && name != nil)
{
printf("defaults read: couldn't read default\n");
}
}
else if ([[args objectAtIndex: i] isEqual: @"write"]) {
else if ([[args objectAtIndex: i] isEqual: @"write"])
{
id obj;
if ([args count] == ++i) {
if ([args count] == ++i)
{
int size = BUFSIZ;
char *buf = objc_malloc(size);
@ -290,7 +325,8 @@ property list which is set as the value of a default.\n\n");
* Read from stdin - grow buffer as necessary since defaults
* values are quoted property lists which may be huge.
*/
while (fgets(buf, BUFSIZ, stdin) != 0) {
while (fgets(buf, BUFSIZ, stdin) != 0)
{
char *ptr;
char *start;
char *str;
@ -301,22 +337,28 @@ property list which is set as the value of a default.\n\n");
* Expect domain name as a space delimited string.
*/
ptr = start;
while (*ptr && !isspace(*ptr)) {
while (*ptr && !isspace(*ptr))
{
ptr++;
}
if (*ptr) {
if (*ptr)
{
*ptr++ = '\0';
}
while (isspace(*ptr)) {
while (isspace(*ptr))
{
ptr++;
}
if (*start == '\0') {
if (*start == '\0')
{
printf("defaults write: invalid input - nul domain name\n");
[pool release];
exit(0);
}
for (str = start; *str; str++) {
if (isspace(*str)) {
for (str = start; *str; str++)
{
if (isspace(*str))
{
printf("defaults write: invalid input - "
"space in domain name.\n");
[pool release];
@ -330,23 +372,29 @@ property list which is set as the value of a default.\n\n");
* Expect defaults key as a space delimited string.
*/
ptr = start;
while (*ptr && !isspace(*ptr)) {
while (*ptr && !isspace(*ptr))
{
ptr++;
}
if (*ptr) {
if (*ptr)
{
*ptr++ = '\0';
}
while (isspace(*ptr)) {
while (isspace(*ptr))
{
ptr++;
}
if (*start == '\0') {
if (*start == '\0')
{
printf("defaults write: invalid input - "
"nul default name.\n");
[pool release];
exit(0);
}
for (str = start; *str; str++) {
if (isspace(*str)) {
for (str = start; *str; str++)
{
if (isspace(*str))
{
printf("defaults write: invalid input - "
"space in default name.\n");
[pool release];
@ -360,56 +408,70 @@ property list which is set as the value of a default.\n\n");
* may cover multiple lines.
*/
start = ptr;
if (*start == '\'') {
for (ptr = ++start; ; ptr++) {
if (*ptr == '\0') {
if (*start == '\'')
{
for (ptr = ++start; ; ptr++)
{
if (*ptr == '\0')
{
int pos = ptr - buf;
if (size - pos < BUFSIZ) {
if (size - pos < BUFSIZ)
{
char *tmp;
int spos = start - buf;
tmp = objc_realloc(buf, size + BUFSIZ);
if (tmp) {
if (tmp)
{
size += BUFSIZ;
buf = tmp;
ptr = &buf[pos];
start = &buf[spos];
}
else {
else
{
printf("defaults write: fatal error - "
"out of memory.\n");
[pool release];
exit(0);
}
}
if (fgets(ptr, BUFSIZ, stdin) == 0) {
if (fgets(ptr, BUFSIZ, stdin) == 0)
{
printf("defaults write: invalid input - "
"no final quote.\n");
[pool release];
exit(0);
}
}
if (*ptr == '\'') {
if (ptr[1] == '\'') {
if (*ptr == '\'')
{
if (ptr[1] == '\'')
{
strcpy(ptr, &ptr[1]);
}
else {
else
{
break;
}
}
}
}
else {
else
{
ptr = start;
while (*ptr && !isspace(*ptr)) {
while (*ptr && !isspace(*ptr))
{
ptr++;
}
}
if (*ptr) {
if (*ptr)
{
*ptr++ = '\0';
}
if (*start == '\0') {
if (*start == '\0')
{
printf("defaults write: invalid input - "
"empty property list\n");
[pool release];
@ -421,67 +483,80 @@ property list which is set as the value of a default.\n\n");
* an NSString or a structured property list.
*/
obj = [NSString stringWithCString: start];
if (*start == '(' || *start == '{' || *start == '<') {
if (*start == '(' || *start == '{' || *start == '<')
{
id tmp = [obj propertyList];
if (tmp == nil) {
if (tmp == nil)
{
printf("defaults write: invalid input - "
"bad property list\n");
[pool release];
exit(0);
}
else {
else
{
obj = tmp;
}
}
domain = [[defs persistentDomainForName: owner] mutableCopy];
if (domain == nil) {
if (domain == nil)
{
domain = [NSMutableDictionary dictionaryWithCapacity:1];
}
[domain setObject: obj forKey: name];
[defs setPersistentDomain: domain forName: owner];
}
}
else {
else
{
owner = [args objectAtIndex: i++];
if ([args count] <= i) {
if ([args count] <= i)
{
NSLog(@"defaults: no dictionary or key for write!\n");
[pool release];
exit(0);
}
name = [args objectAtIndex: i++];
if ([args count] > i) {
if ([args count] > i)
{
const char *ptr;
value = [args objectAtIndex: i];
ptr = [value cString];
if (*ptr == '(' || *ptr == '{' || *ptr == '<') {
if (*ptr == '(' || *ptr == '{' || *ptr == '<')
{
obj = [value propertyList];
if (obj == nil) {
if (obj == nil)
{
printf("defaults write: invalid input - "
"bad property list\n");
[pool release];
exit(0);
}
}
else {
else
{
obj = value;
}
domain = [[defs persistentDomainForName: owner] mutableCopy];
if (domain == nil) {
if (domain == nil)
{
domain = [NSMutableDictionary dictionaryWithCapacity:1];
}
[domain setObject: obj forKey: name];
[defs setPersistentDomain: domain forName: owner];
}
else {
else
{
domain = [name propertyList];
if (domain == nil ||
[domain isKindOfClass: [NSDictionary class]] == NO) {
[domain isKindOfClass: [NSDictionary class]] == NO)
{
NSLog(@"defaults write: domain is not a dictionary!\n");
[pool release];
exit(0);
@ -489,31 +564,39 @@ property list which is set as the value of a default.\n\n");
}
}
if ([defs synchronize] == NO) {
if ([defs synchronize] == NO)
{
NSLog(@"defaults: unable to write to defaults database - %s\n",
strerror(errno));
}
}
else if ([[args objectAtIndex: i] isEqual: @"delete"]) {
if ([args count] == ++i) {
else if ([[args objectAtIndex: i] isEqual: @"delete"])
{
if ([args count] == ++i)
{
char buf[BUFSIZ];
while (fgets(buf, sizeof(buf), stdin) != 0) {
while (fgets(buf, sizeof(buf), stdin) != 0)
{
char *ptr;
char *start;
start = buf;
ptr = start;
while (*ptr && !isspace(*ptr)) {
while (*ptr && !isspace(*ptr))
{
ptr++;
}
if (*ptr) {
if (*ptr)
{
*ptr++ = '\0';
}
while (isspace(*ptr)) {
while (isspace(*ptr))
{
ptr++;
}
if (*start == '\0') {
if (*start == '\0')
{
printf("defaults delete: invalid input\n");
[pool release];
exit(0);
@ -521,70 +604,88 @@ property list which is set as the value of a default.\n\n");
owner = [NSString stringWithCString: start];
start = ptr;
ptr = start;
while (*ptr && !isspace(*ptr)) {
while (*ptr && !isspace(*ptr))
{
ptr++;
}
if (*ptr) {
if (*ptr)
{
*ptr++ = '\0';
}
while (isspace(*ptr)) {
while (isspace(*ptr))
{
ptr++;
}
if (*start == '\0') {
if (*start == '\0')
{
printf("defaults delete: invalid input\n");
[pool release];
exit(0);
}
name = [NSString stringWithCString: start];
domain = [[defs persistentDomainForName: owner] mutableCopy];
if (domain == nil || [domain objectForKey: name] == nil) {
if (domain == nil || [domain objectForKey: name] == nil)
{
printf("defaults delete: couldn't remove %s owned by %s\n",
[name cString], [owner cString]);
}
else {
else
{
[domain removeObjectForKey: name];
[defs setPersistentDomain: domain forName: owner];
}
}
}
else {
else
{
owner = [args objectAtIndex: i++];
if ([args count] > i) {
if ([args count] > i)
{
name = [args objectAtIndex: i];
}
else {
else
{
name = nil;
}
if (name) {
if (name)
{
domain = [[defs persistentDomainForName: owner] mutableCopy];
if (domain == nil || [domain objectForKey: name] == nil) {
if (domain == nil || [domain objectForKey: name] == nil)
{
printf("dremove: couldn't remove %s owned by %s\n",
[name cString], [owner cString]);
}
else {
else
{
[domain removeObjectForKey: name];
[defs setPersistentDomain: domain forName: owner];
}
}
else {
else
{
[defs removePersistentDomainForName: owner];
}
}
if ([defs synchronize] == NO) {
if ([defs synchronize] == NO)
{
NSLog(@"defaults: unable to write to defaults database - %s\n",
strerror(errno));
}
}
else if ([[args objectAtIndex: i] isEqual: @"domains"]) {
else if ([[args objectAtIndex: i] isEqual: @"domains"])
{
domains = [defs persistentDomainNames];
for (i = 0; i < [domains count]; i++) {
for (i = 0; i < [domains count]; i++)
{
NSString *domainName = [domains objectAtIndex: i];
printf("%s\n", [domainName cString]);
}
}
else if ([[args objectAtIndex: i] isEqual: @"find"]) {
if ([args count] == ++i) {
else if ([[args objectAtIndex: i] isEqual: @"find"])
{
if ([args count] == ++i)
{
NSLog(@"defaults: no arguments for find!\n");
[pool release];
exit(0);
@ -592,30 +693,37 @@ property list which is set as the value of a default.\n\n");
name = [args objectAtIndex: i];
domains = [defs persistentDomainNames];
for (i = 0; i < [domains count]; i++) {
for (i = 0; i < [domains count]; i++)
{
NSString *domainName = [domains objectAtIndex: i];
NSDictionary *dom;
if ([domainName isEqual: name]) {
if ([domainName isEqual: name])
{
printf("%s\n", [domainName cString]);
found = YES;
}
dom = [defs persistentDomainForName: domainName];
if (dom) {
if (dom)
{
NSEnumerator *enumerator;
NSString *key;
enumerator = [dom keyEnumerator];
while ((key = [enumerator nextObject]) != nil) {
while ((key = [enumerator nextObject]) != nil)
{
id obj = [dom objectForKey: key];
if ([key isEqual: name]) {
if ([key isEqual: name])
{
printf("%s %s\n", [domainName cString], [key cString]);
found = YES;
}
if ([obj isKindOfClass: [NSString class]]) {
if ([obj isEqual: name]) {
if ([obj isKindOfClass: [NSString class]])
{
if ([obj isEqual: name])
{
printf("%s %s %s\n",
[domainName cString],
[key cString],
@ -627,11 +735,13 @@ property list which is set as the value of a default.\n\n");
}
}
if (found == NO) {
if (found == NO)
{
printf("defaults find: couldn't find value\n");
}
}
else {
else
{
NSLog(@"defaults: unknown option supplied!\n");
}
@ -639,4 +749,3 @@ property list which is set as the value of a default.\n\n");
exit(0);
}