Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 10. Interacting with the Environment > Getting Environment Variables

Getting Environment Variables

From: env.m

 3 int main(int argc, char **argv, char **envp)
 4 {
 5   // The C way
 6   while (*envp)
 7   {
 8     printf("%s\n", *envp);
 9     envp++;
10   }
11   printf("Working Directory: %s\n", getenv("PWD")
          );
12
13   // The Objective-C way
14   [NSAutoreleasePool new];
15   NSDictionary *env =
16     [[NSProcessInfo processInfo] environment];
17   for (NSString *key in env)
18   {
19     NSLog(@"%@ = %@", key, [env objectForKey: key
            ]);
20   }
21   NSLog(@"Working Directory: %@\n",
22       [env objectForKey: @"PWD"]);
23   return 0;
24 }

					  

Environment variables can be accessed in three ways on OS X. Two of them will work anywhere, whereas the third will only work on some platforms.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint