Slashdot Mirror


ANSI C89 and POSIX portability?

LordNite asks: "Here is the situation. I am maintaining a piece of source code which is written in K&R C. One of the original goals of this code was to be as portable as possible to as many platforms as possible. The code runs on UNIX and its clones as well as OS/2. The code avoids POSIX functions such as mmap(2) since at the time it was initially written (early 1990s) POSIX was not very wide spread. The code is well written, but in need of some serious fixing. As I go around fixing parts of the code I would also like to modernize it a bit. Since it is now 2004, can I rely on ANSI C89 and POSIX routines without sacrificing the portability of this code? (Yes, I do realize that the purpose of POSIX is code portability...) I am not really interested in the OS/2 port at this time. I am just interested in keeping portability with UNIX clones. To put my question another way: Are there any UNIX-like OSes in common use, which are currently developed and supported by some entity either OSS or proprietary, that do not support POSIX and ANSI C89?"

9 of 85 comments (clear)

  1. Re:Purpose? by rjh · · Score: 4, Informative

    I've known the submitter for about ten years now, a little less, so maybe I can answer your question for you.

    The reason is simple. He's a hacker. He sees something that works but is inelegant; and he'd like to be able to make it a little more elegant. It's that simple.

    So the answer to "why risk introducing a bunch of bugs just to make the software 'more modern'" is not a facetious "job security".

    The answer is elegance.

    If you can't appreciate that answer, that's a strong sign you're not qualified to have an opinion.

    Please note, by the by, that I almost agreed with you. I recommended that he be very careful in what he fixes and how. That's worlds apart from saying "leave it alone, you don't know what you're doing". Any answer of "leave it alone" is fundamentally anti-hacker.

  2. POSIX and C89 by molo · · Score: 4, Informative

    Ok, POSIX is all about the system calls and C library functions. C89 is about compiler support. They are seperate and don't go hand-in-hand.

    About POSIX and Unix compatibility. There are a handful of Unixes that remain important and widely deployed. They are:

    Solaris
    HP-UX
    AIX
    Linux
    *BSD
    MacOSX

    They pretty much all have modern APIs in recent versions. The older unixes have recently added a bunch of Linux-like modern APIs to make portability easier. This was the reason behind HPUX 11i (the 'i' denotes "internet ready", but what they really mean is glibc/*BSD apis). This is also the reason behind the AIX 5L name (AKA 5.1, 5.2) (L = linux affinity, same deal, new GNU/*BSD apis). You know that MacOSX uses a BSD-based userland, so you're fine there. Then there's Solaris, of which recent versions (>=2.6) are in good shape. Recent versions of the proprietary unixes even have /dev/[u]random and /proc filesystems. There's a lot of common interfaces, as long as you target relatively recent releases.

    Ok, once you figure out what platforms you are targeting, you need to figure out what compilers you will support. All of the proprietary Unixes have their own C compiler (sometimes only available for a fee). Many are not fully ANSI compliant. They are definitely not all C99 compliant. That is the bad news.

    The good news is that gcc is available for all of the major platforms. This is what gcc excels at, it is highly portable. You can use this to your advantage to get things working on these platforms. If your users then want to get them working with other compilers, that is worth a shot too (non-gcc compilers often produce better optimizations, etc.).. but it will be hit or miss.

    Testing. I highly recommend the HP Testdrive program. They make available a bunch of machines with various HP hardware running various operating systems from Linux on Alpha to HPUX on IA64, including Tru64 (aka OSF/1). http://www.testdrive.compaq.com/

    Sourceforge also has a build farm which includes Solaris on sparc and x86 and MacOS X. http://sourceforge.net/docman/display_doc.php?doci d=762&group_id=1#platforms You have to be a developer of a sourceforge project to get access, but its a good deal.

    Good luck. Hope this helps.
    -molo

    HPUX Note: Many people think that 11i is for the Itanium platform. That is not the case. 11i is version 11.11 and higher. 11.11 is for HPPA. 11.20 and higher are for IA64. Both are called 11i.

    --
    Using your sig line to advertise for friends is lame.
    1. Re:POSIX and C89 by Anonymous Coward · · Score: 1, Informative

      It is also worth mentioning that all currently maintained unix-like operating systems support the Single Unix Specification. The specification can be downloaded for free from www.opengroup.org. The latest version of the specification is SUSv3, but as a coder you're better off using SUSv2 (released 1998) since SUSv2 wasn't widely implemented until about 2001 and it is quite likely that some of your customers will want to run your software on older OS versions.

  3. Obligatory book reference by DarkDust · · Score: 2, Informative

    AFAIK really all UNIX and UNIX-like systems support at least POSIX-1988, and most even support SUS1 and higher. Heck, even Windows NT/2000 are (partly) POSIX compatible, and to my knowledge Windows XP can be made POSIX compatible with some extra package from MicroSoft, but I don't know which).

    I really recommend Advanced UNIX Programming, it's an excellent book which not only discusses and explains POSIX and SUS APIs but also where you can expect those APIs to be avaible and how to test for them to be sure. It was also reviewed here at Slashdot.

  4. What part of POSIX? by hubertf · · Score: 2, Informative

    POSIX is not a single specification, but consists of quite a number of parts. While some of the basic interfaces can be considered of widespread use, I wouldn't recommend using others like realtime handling or maybe even threads.

    - Hubert

  5. QNX? by calidoscope · · Score: 2, Informative

    QNX is still being used - the latest version now hs gcc support - versus that Watcom compiler for earlier versions (4.25 and earlier).

    --
    A Shadeless room is a brighter room.
  6. Use Samba as a reference by lkaos · · Score: 4, Informative

    We go through great lengths in Samba to be as portable as possible. Our build farm runs the most popular unices on all sorts of architectures (you'd be amazed how different Linux on x86 can be than Linux on say a s390). We support a ton of platforms including some as obscure as the Amiga.

    What I'd do if I were you is to just grep the Samba source code before you use a function. You'll likely find a list of platforms that it doesn't work on, or that simply doesn't have that particular function. You may also find workarounds for bugs in particular implementations.

    --
    int func(int a);
    func((b += 3, b));
  7. Don't use autoconf.... by V.+Mole · · Score: 2, Informative

    ...unless you really want/need to support ancient and obscure platforms. But since the question specifically *said* modern and maintained platforms, then autoconf will just uglify and complicate your code.

    And in answer to the original question: yes, if you're talking modern unix or unix-like systems (i.e. AIX 4.3 or 5, Solaris 7 or later, Linux/glibc or BSD from the last several years, HPUX 11), then yes, you can assume POSIX and C89 (although for AIX, Solaris, and HPUX you'll need to buy a compiler or install GCC).

  8. Re:Not necessarily by squiggleslash · · Score: 2, Informative
    It's actually not a bad compiler, it's just about 15 years out of date - older if you consider the fact that function prototypes and other nicities were pretty much becoming standard in the late eighties without the help of ANSI.

    It's usable for just about anything except compiling code intended for other systems. I didn't really think, for some reason, about the option of compiling GCC locally (eg into our user account), I may try that and see how far we can get. That would at least also allow us to get around the Perl et al issues too.

    --
    You are not alone. This is not normal. None of this is normal.