TweetFollow Us on Twitter

Sep 01 Programming2

Volume Number: 17 (2001)
Issue Number: 09
Column Tag: Programming Philosophy

Mac vs. Unix traditions

by Marcelo Amarante Ferreira Gomes

How to combine the best of both with MacOS X without gathering the worst

A Big Move

With the advent of Mac OS X, Apple has departed from the traditional Mac OS system software to a new model, that of Unix. This has brought a great impact, both upon users and developers. In this article, I'll concentrate on the impact it has on us, developers.

Mac OS X brings a lot of changes. We now have a single filesystem tree, for instance, instead of a tree for each mounted volume; we have lots of users, instead of the few we had with Classic, even though most are fake, like root or bin; we have a much more complex concept of process ownership, with process groups, effective and real user and group IDs and a controlling tty. These - and lots of other - changes also change the way we code, something that even resembles the move from 68k to PPC in 1994, but is a lot closer to the move from System 6 to System 7 in the late 80's and early 90's.

There were no new things to think of when we moved from 68k to PPC, only lots of programming pitfalls. But when System 7 came up, later to be renamed Mac OS 7, we actually had to think different. Those readers old enough to remember will recall that when System 7 came up, we had to think of High-level events, mostly Apple events, for simple tasks like opening a document, and try to use them whenever possible, instead of the low-level events and Finder lists we were used to. We had to revamp our user interface, so that our applications wouldn't look displaced in that new and way cooler-looking GUI. We had also to do away, once and for all, with our 24-bit stuff and use 32-bit-clean pointers and handles, paying attention to the possibility of the use of virtual memory in the system.

For those of you new to Macintosh, but experienced Unix developers, there were a few similar moves that the Unix community has gone through, too. They have been much smoother, since there was no single company dictating directions to developers, like Apple does in the Mac arena. To name a few, there was the move from the single mainframe with lots of terminals to lots of networked personal workstations; from UUCP and serial-based WAN links to TCP/IP and Ethernet-based LANs; from character-based UI to X-based GUI; from single-computer passwd files to distributed NIS and/or shadow user databases. Some of these were actually additional options, rather than changes. For instance, I hardly recommend the use of NIS, even though I do recommend shadow password files. And even today, I still recommend UUCP links and character-based UI in specific cases. But either the new features or the changes in them required some extra care in the way one writes a new piece of software, and I'm not referring only to programming pitfalls here.

I could go on, naming other changes like these, both in the Mac and Unix fields, but the important thing here is to point out that there are some major moves in development scenarios from time to time. And when such a move brings lots of changes at once, it also produces changes in the programming philosophy. That's what's happening with the introduction of Mac OS X. And those of us who fail to perceive this will probably also fail to survive in a market that is always hungry for state-of-the-art technology.

What's A Programming Philosophy, Anyway?

It's not only about programming caveats. The programming philosophy encompasses the way we design, we use, and even think about the uses for a computer system. Most Macintosh users think of their computers almost as a friend, while Unix users tend to think of them only as a tool to get some job done.

Mac OS has traditionally been very user friendly and highly customizable but very consistent through different applications, both in terms of the interface with the user and the functionality. Unix, while highly customizable in functionality - even more than the Mac in most aspects - is usually very harsh with its fixed user interface. It seems to have been designed to work as a server, while the Mac OS is a great workstation.

Veteran developers always take these facts into account, even if unconsciously, and write code that corresponds to their perception of what the user needs. What Apple is doing now with Mac OS X is bringing the two worlds together, releasing an OS that is great for both a server machine and a workstation. This means that Apple wants users to have the same consistent user interface through all pieces of software they interact with, while having rock-solid system stability and compatibility.

Give Users What They Need

To build a successful Mac application, one must deliver what the user needs. Note that this does not mean that you should code just what the user expects. You can and should surprise the user with new and improved ways to do something. But try to invent features that users would guess when presented with a given screen. Developers with no previous Mac programming experience should observe mainly the Mac human interface guidelines. The typical Mac users expect every application to be similar to all others. They are used to reading the manual only as a last resort. Applications should be intuitive to them.

Apple has defined the recommended human interface guidelines required to achieve a successful Mac application long ago. These are constantly being improved, as new creative ways to interface with the users are developed, but the basic rules are always the same. What they say can be summarized as:

  • when you add a feature to your software, do it in a way that resembles something you've seen before in another software;
  • if the feature you're adding is radically new, try to imagine what the user would do to access that feature. Better yet, make a prototype of your application and have long-time Mac users play with it for some time. Pay attention to what they try to do, and listen to their comments very carefully.

There are lots of standard Mac OS interface elements that can be used to give users a clue to what they should do to get something done. If your particular feature does not fit into any interface elements, you should probably talk to a veteran Mac user about it. If you both decide there's no interface element that fits your needs, then try to invent your new element as similar as possible to another existing element. And always provide clear visual clues of the way your new element works.

Be Compatible - And International

In Mac OS, as well as in Unix, there are some conventions about where to put settings files, support code libraries, font files and lots of other things. Unlike in Unix, though, these special places should not be referred to by name. Apple has introduced the FindFolder system call a long while ago to give your software the path to these places. You can find the official Apple documentation on FindFolder at [1].

Under the Cocoa programming environment in Mac OS X, Apple thought you wouldn't need FindFolder, so they provided NSUserDefaults instead to store settings. See [2] for more info.

I see some of you asking: "why should I care about making a system call to find out what I already know?" This is a common pitfall. Settings files should go into the System Folder:Preferences folder, right? Not always. Under the Brazilian version of Mac OS, for instance, they should go into the Pasta do Sistema:Preferências folder. Besides, always making sure to call the proper system functions enables Apple to restructure the filesystem layout without breaking any software and without having to support two folder structures at the same time.

With software that don't call FindFolder, international users end up with some settings files in the correct place while some other software create folders with foreign names and store their preferences there. Try to explain that to a user!

A much worse situation comes up with installer software. Say you need to put a certain file in a special folder, like System Folder:Extensions in Mac OS Classic, or to alter one of the rc.* under the /etc in Mac OS X. If you don't make the appropriate system call, your file may end up in the wrong place, and your software simply won't work. Please note that I, as a novice Mac OS X programmer that we all but a few Apple engineers are, haven't been able to find what the call would be to find the proper /etc directory or the rc.* files. Maybe it doesn't even exist, in which case Apple should provide us with such call.

Don't Mess Things Up

The same "don't mess up with the filesystem" can be said to wannabe Unix developers. Developers with no previous Unix experience must take into account various aspects of the Unix culture, especially those that have security implications. Apple itself has made some significant departures from traditional Unix practice.

For instance, most, if not all, Unix variants have a real directory for /etc. With Mac OS X, Apple has decided to make /etc be a symbolic link to /private/etc. There are other systems that put a symbolic link where a real directory would be, most notably Solaris with its /bin being a link to /usr/bin, but generally there's a good reason for this kind of change.

I don't know what was Apple's reason for putting a symbolic link in the place of /etc. The only thing I can think of is to make it easier to change most of the system's configuration by just making the link point elsewhere.

But this kind of change is dangerous, given standard Unix programming practice and system call side effects. It must be done at a very carefully chosen time during the boot process. It could also be done while the system is up, but one should be aware of the implications of this huge change in his or her particular system.

One problem that could appear stems from the fact that when a process opens a file, it gets a file descriptor that refers to whatever has been named by the time the open system call has been made. After that call, it doesn't matter if that file is moved, renamed or even deleted: the file descriptor sticks with the originally opened file. In case of deletion, the disk space is freed only when all of the processes that have opened it have also closed it. With all this, if one ever changes the symbolic link in /etc, there is the risk that one process, for instance, a server started at boot time, has a configuration file open that doesn't correspond to the configuration seen by all other processes started after the link has been changed.

The extent of damage caused by such situation can range from nothing at all to a completely disastrous crash. Or, worse yet, it can pose a security threat, for instance, when a client process thinks that the server will do some sort of security check, while the server thinks that check to be the client's responsibility.

Don't Reinvent the Wheel

Another departure Apple has taken from standard Unix with its Mac OS X was the use of the little known pax archiving utility for its installer process. I would personally have gone with tar, but I wouldn't mind Apple to have chosen cpio instead, since these two are the formats that most (99.9 percent?) die-hard Unix fans prefer to distribute software in.

With the use of pax, Apple has introduced some serious security flaws in their installer process. I have read a great article about this issue, but regrettably, I can't seem to find the URL in my bookmarks. Anyway, the main concern in that article is that pax does not deal very nicely with permissions and ownership data about already existing directories and files. Even worse, if you change a directory for a symbolic link, pax may get confused and delete the link, replacing it with a real directory. If the link had been placed there in order to save disk space and have the tree below it stored in another disk partition or other physical disk, you will run into big trouble.

Pax seems to have been designed to create all of the directories and files that it is installing, and it's not very clever to leave things like they are when it finds something already in place. That way, whenever you install new software, you risk introducing security holes that have long been corrected.

Until Apple fixes this, either by using tar or cpio instead of pax, or by making pax a little smarter, we will have to double-check our systems every time we install something. The easiest way to do so seems to be by looking at the installer package, taking note of all of the files it adds, along with the directories they reside in, and checking for their correct ownership, permissions and file type (regular file, directory, symbolic link, device file, etc.) Obviously, this work would be much easier if you take some time to store all of this information before the installation takes place and make any needed corrections afterwards.

Summing it All Up

Lots of other aspects about good development habits could have been mentioned here, both under Unix and Mac OS, but a magazine article is just too short to even give a good coverage, let alone being complete. The examples given here serve only to give you an idea of what kind of downturns you might run into by not being aware of standard practice.

But they also serve to give you an idea of what a programming philosophy is. It's about the way developers think, and the "right" way of doing things. Everyone will have a different idea of what is the "right" way of doing something, but some basic ideas are generally agreed upon among developers from a given OS. These basic ideas are what is being referred to here as a programming philosophy.

How to Get There

Ok, we have to develop a new programming philosophy, that of Mac OS X, which should be a little different from Classic Mac OS and from Unix, but should not steer too far from either. But how do we get there? That depends on whether you are coming from Mac OS, from some sort of Unix or you are completely new to both. Either way, you've got to take a look at some web page from [3], especially [4].

Mac OS X for Classic Mac OS Developers

Of course Apple wouldn't succeed if they wanted us to rewrite all of our dependable code, that has cost us years of testing and debugging effort. But Apple can't stand still. So, they have provided us with three environments: Classic, Carbon and Cocoa. Ok, you've heard that before, so I'll skip to the important stuff.

Classic takes care of users. They won't have to buy new versions of all of their software title before moving on to X, but it's discontinued now, and it's not worth writing anything new for that environment. Cocoa, on the other hand, is the all-new innovative programming environment, in which all the good stuff is. Unfortunately for us who have invested on C, C++, Pascal or other languages, only Objective-C and Java APIs are available to work with it.

Then, there's Carbon. The environment for those coming from Classic Mac OS is definitely this one. Carbon is something in between Classic and Cocoa. It doesn't offer all the cool stuff that Cocoa offers, but it does offer most of it, including most of the coolest features we've been hearing about Mac OS X. It allows you to move your applications from the old Classic environment with little effort. Better yet, if you write for Carbon, your code may be able to run on Mac OS 9 and, depending on the set of system calls you choose, even on 8.x, by just having CarbonLib installed there. You can learn more about it in [5].

You can still think of files most the same way you did before, call most of your favorite system calls and think of the system mostly as if it really were the familiar Mac OS you've always known - for a while. It won't take long before you start cursing those most words in the previous sentence. You'll want to move on to Cocoa. Carbon is just a bridge to make that transition smoother.

And please try to stick to the rules. Learn the Aqua human interface guidelines (see [6]) and Quartz new API calls that are needed to implement some of Aqua look and feel (see [7] and [8].) Don't just use the same old ways of interfacing with the user. Some of the old methods, like custom MDEFs or WDEFs won't work, anyway.

Mac OS X for Unix Developers

By now, you already know that Cocoa is for you. Or isn't it? That depends on your previous knowledge of either Java or Objective-C or your willingness to learn a new programming language. If you already know any of these two languages, then look no further. You've got to go the Cocoa way, so read [9].

But if you are a seasoned C, C++, Pascal, Fortran, Basic, or something else programmer, you might want to give Carbon a try, at least for a while. Be warned, though, that you'll have a much harder time than your fellow Classic Mac OS programmers trying to go this way. While they already know most of the philosophy involved in writing good Mac OS software, you'll have to learn that from the beginning.

The Aqua API will be something new, and don't even think about writing an X-windows application. Most Mac OS X systems won't even have X-windows installed. But don't let that discourage you. The concepts involved are very similar. You still have a main() function calling an event loop and acting on events, just like on X-windows. Only the API will be new. Well, not only the API - you'll have to play by the Aqua rules. Read [6]; reread [6], then read it once again. Tired of reading? Read it backwards once more, just in case. It's never too much to stress how important the GUI rules are to keep Mac OS consistent.

"Damn, I could forget about all this GUI stuff and write a character-only app," I hear you saying. Ok, go ahead. But don't forget what your intended audience is. The average Mac OS user doesn't even know what a character-based UI is, let alone how to get to a command prompt. But if your audience is just a bunch of colleagues of yours and you do write a character-based application, please play by the traditional Unix rules, and try not to mess up with the System Folder or some other Mac OSisms brought over from Classic to Mac OS X.

Mac OS X for Newbies

If you've read the previous sections, you stand a good chance of already knowing which way to go. You don't know any programming language at all? Learn Java and/or Objective-C and go the Cocoa way. You already know some C and only want to know what this fruit-branded OS is about? Ok, try Carbon.

No matter what environment you choose to start working with, please, please, please! Follow the rules. All of them.

The Bottom Line

So, no matter what your background in some other OS is, if you don't have a good knowledge on both Unix and Mac OS standard practices, please reserve some time to learn about the culture on these systems. And don't try to save time on this! It will take lots of reading until you can say you're ready to write a successful application for Mac OS X. If at all possible, talking to veteran developers and users should take much of your time, too. But you will spend much more time, money and effort by going ahead coding, writing manuals, license agreements and other documentation, investing on marketing and distribution, only to find out that the users didn't like your software.

Worse than that, when many developers start coding without regard to standard practice, eventually some of them will come up with exceptionally good software that users will like and use although it doesn't follow standard practice. It may even happen with your title. But don't be happy with that.

When a significant number of software titles disregarding minimal guidelines start to gain acceptance, there will be nothing to call standard practice anymore. And Mac OS won't have the consistency it has today. Users will have a hard time figuring out how to use software; various titles will start being incompatible with one another. Then everything Apple and the Unix community across the globe have fought for and accomplished during many years will have been thrown away.

Apple fails to comply with traditional Unix practice in a few spots with dangerous consequences, but those trouble spots are simple to fix. They seem to be doing an overall good job and moving in the right direction. Let's hope that all - or at least most - of us will also get this right, and let's make Mac OS X a success by combining the best of Classic Mac OS with the best that Unix has to offer. And let's not pour in the worst by accident.

Acknowledgements

I'd like to thank Jacques do Prado Brandão, my friendly neighbor who, even not knowing a thing about computers or operating systems, was always there to help me revise some grammatical glitches that stupid word processors can't, as well as keeping a straight line of thought throughout the whole article.

Someone else to mention is Sergio de Souza Prallon, a fellow Brazilian consultant who has read - and understood - this article in its entirety. Being a Unix-only guy, he came up with the idea of writing the entire How to Get There section. Then he lied to me and told me it was good. :-)

And I could never forget about Neil Ticktin, MacTech's publisher. He has told me that the ideas I have presented here could make up a good article, thus giving the spark that ignited this whole thing. Has it blown anything up?

References


Marcelo Amarante Ferreira Gomes is an independent Macintosh, Unix and Internet consultant. He works most of the time with his Brazilian partners, and plays most of the time with his kids and/or his Lombard Powerbook that has the single most important OS in the world, besides most other junk in this industry. You can reach him at suporte@mac.com... if you're lucky. :-)

 
AAPL
$524.94
Apple Inc.
+5.93
MSFT
$40.01
Microsoft Corpora
-0.39
GOOG
$536.10
Google Inc.
-20.44

MacTech Search:
Community Search:

Software Updates via MacUpdate

VMware Fusion 6.0.3 - Run Windows apps a...
VMware Fusion allows you to create a Virtual Machine on your Mac and run Windows (including Windows 8.1) and Windows software on your Mac. Run your favorite Windows applications alongside Mac... Read more
Tweetbot 1.5.1 - Popular iOS twitter cli...
Tweetbot is a full-featured OS X Twitter client with a lot of personality. Whether it's the meticulously-crafted interface, sounds and animation, or features like multiple timelines and column views... Read more
Mac DVDRipper Pro 4.1.7 - Copy, backup,...
Mac DVDRipper Pro is the DVD backup solution that lets you protect your DVDs from scratches, save your batteries by reading your movies from your hard disk, manage your collection with just a few... Read more
PDFpenPro 6.2 - Advanced PDF toolkit for...
PDFpenPro allows users to edit PDF's easily. Add text, images and signatures. Fill out PDF forms. Merge or split PDF documents. Reorder and delete pages. Even correct text and edit graphics! Create... Read more
PDFpen 6.2 - Edit and annotate PDFs with...
PDFpen allows users to easily edit PDF's. Add text, images and signatures. Fill out PDF forms. Merge or split PDF documents. Reorder and delete pages. Even correct text and edit graphics! Features... Read more
Monolingual 1.5.9 - Remove unwanted OS X...
Monolingual is a program for removing unnecesary language resources from OS X, in order to reclaim several hundred megabytes of disk space. It requires a 64-bit capable Intel-based Mac and at least... Read more
Maya 2015 - Professional 3D modeling and...
Maya is an award-winning software and powerful, integrated 3D modeling, animation, visual effects, and rendering solution. Because Maya is based on an open architecture, all your work can be scripted... Read more
Starcraft II: Wings of Liberty 1.1.1.180...
Download the patch by launching the Starcraft II game and downloading it through the Battle.net connection within the app. Starcraft II: Wings of Liberty is a strategy game played in real-time. You... Read more
Sibelius 7.5.0 - Music notation solution...
Sibelius is the world's best-selling music notation software for Mac. It is as intuitive to use as a pen, yet so powerful that it does most things in less than the blink of an eye. The demo includes... Read more
Typinator 5.9 - Speedy and reliable text...
Typinator turbo-charges your typing productivity. Type a little. Typinator does the rest. We've all faced projects that require repetitive typing tasks. With Typinator, you can store commonly used... Read more

Latest Forum Discussions

See All

Have a Special Dead Trigger 2 Easter Bas...
Have a Special Dead Trigger 2 Easter Basket Full of Goodies, Courtesy of Madfinger Games Posted by Rob Rich on April 18th, 2014 [ permalink ] Dead Trigger 2 | Read more »
Almost All of Playdek’s Library is on Sa...
Almost All of Playdek’s Library is on Sale Right Now, and You Should Check it Out Posted by Rob Rich on April 18th, 2014 [ permalink ] Playdek has released quite a few great iOS ports of board and card games over the years, and now most of them... | Read more »
Zynga Launches Brand New Farmville Exper...
Zynga Launches Brand New Farmville Experience with Farmville 2: Country Escape Posted by Tre Lawrence on April 18th, 2014 [ permalink ] | Read more »
David. Review
David. Review By Cata Modorcea on April 18th, 2014 Our Rating: :: MINIMALISTIC IN A DIFFERENT WAYUniversal App - Designed for iPhone and iPad David is a minimalistic game wrapped inside of a soothing atmosphere in which the hero... | Read more »
Eyefi Unveils New Eyefi Cloud Service Th...
Eyefi Unveils New Eyefi Cloud Service That Allows Users to Share Media Across Personal Devices Posted by Tre Lawrence on April 18th, 2014 [ permalink ] | Read more »
Tales from the Dragon Mountain: The Lair...
Tales from the Dragon Mountain: The Lair Review By Jennifer Allen on April 18th, 2014 Our Rating: :: STEADY ADVENTURINGiPad Only App - Designed for the iPad Treading a safe path, Tales from the Dragon Mountain: The Lair is a... | Read more »
Yahoo Updates Flickr App with Advanced E...
Yahoo Updates Flickr App with Advanced Editing Features and More Posted by Tre Lawrence on April 18th, 2014 [ permalink ] | Read more »
My Incredible Body - A Kid's App to...
My Incredible Body - A Kid's App to Learn about the Human Body 1.1.00 Device: iOS Universal Category: Education Price: $2.99, Version: 1.1.00 (iTunes) Description: Wouldn’t it be cool to look inside yourself and see what was going on... | Read more »
Trials Frontier Review
Trials Frontier Review By Carter Dotson on April 18th, 2014 Our Rating: :: A ROUGH LANDINGUniversal App - Designed for iPhone and iPad Trials Frontier finally brings the famed stunt racing franchise to mobile, but how much does its... | Read more »
Evernote Business Notebook by Moleskin I...
Evernote Business Notebook by Moleskin Introduced – Support Available in Evernote for iOS Posted by Tre Lawrence on April 18th, 2014 [ permalink ] | Read more »

Price Scanner via MacPrices.net

Free HopTo 2.2 Helps Enhance Your Productivit...
The HopTo app helps you do more on your iPad by providing more and easier adaccess to files and documents. Version 2.2 adds Egnyte and HopTo’s Mac OSX File Connector. If you already have the hopTo... Read more
National Distracted Driving Awareness Month:...
As the country recognizes National Distracted Driving Awareness Month, Sprint is reminding wireless consumers to focus on driving while behind the wheel, to not text or email while driving, and to... Read more
13-inch 2.4GHz Retina MacBook Pro available f...
Abt has the 13″ 2.4GHz 128GB Retina MacBook Pro available for $1229 including free shipping. Their price is $70 off MSRP. Read more
iMacs on sale for up to $160 off MSRP this we...
Best Buy has iMacs on sale for up to $160 off MSRP for a limited time. Choose free home shipping or free instant local store pickup (if available). Prices are valid for online orders only, in-store... Read more
iPad Airs on sale this weekend for up to $100...
Best Buy has WiFi iPad Airs on sale for $50 off MSRP and WiFi + Cellular iPad Airs on sale for $100 off MSRP on their online store for a limited time, with prices now starting at $449. Choose free... Read more
Apple restocks refurbished Mac minis starting...
The Apple Store has restocked Apple Certified Refurbished Mac minis for up to $150 off the cost of new models. Apple’s one-year warranty is included with each mini, and shipping is free: - 2.5GHz Mac... Read more
Hyundai Brings Apple CarPlay To The 2015 Sona...
Hyundai Motor America has announced it will bring Apple CarPlay functionality to the 2015 Sonata. CarPlay is pitched as a smarter, safer and easier way to use iPhone in the car and gives iPhone users... Read more
Updated iPads Coming Sooner Than We Had Thoug...
MacRumors, cites KGI securities analyst Ming Chi Kuo, well-respected as an Apple product prognisticator, saying that Apple will introduce an upgraded iPad Air and iPad mini in 2014/Q3, meaning the... Read more
Toshiba Unveils New High And Low End Laptop M...
Toshiba has announced new laptop models covering both the high-end and low-end of the notebook computer spectrum. Toshiba 4K Ultra HD Laptop Toshiba’s new Satellite P55t features one of the world’s... Read more
Save up to $270 with Apple refurbished 13-inc...
The Apple Store has Apple Certified Refurbished October 2013 13″ Retina MacBook Pros available starting at $1099, with models up to $270 off MSRP. Apple’s one-year warranty is standard, and shipping... Read more

Jobs Board

*Apple* Automotive Parts Department position...
Apple Automotive is one of the fastest growing dealer…and it shows. Consider making the switch to the Apple Automotive Group today! At Apple Automotive, we Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
*Apple* Retail - Manager - Holyoke - Apple I...
Job Summary Keeping an Apple Store thriving requires a diverse set of leadership skills, and as a Manager, you’re a master of them all. In the store’s fast-paced, Read more
*Apple* Retail - Manager - Apple (United Sta...
Job SummaryKeeping an Apple Store thriving requires a diverse set of leadership skills, and as a Manager, you're a master of them all. In the store's fast-paced, dynamic Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.