[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

15.2 Cross-Unix Portability

In the previous section, we discussed issues related to the C language. Here we will discuss the portability of C programs across different Unix implementations. All modern Unix systems conform to the POSIX.1 (1990 edition) and POSIX.2 (1992 edition) standards. They also all support the sockets interface for networking code. However, there are still significant differences between systems which can affect portability.

We will not discuss portability to older Unix systems which do not conform to the POSIX standards. If you need this sort of portability, you can often find some valuable hints in the set of macros defined by autoconf, and in the ‘configure.in’ files of older programs which use autoconf.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

15.2.1 Cross-Unix Function Calls

Functions not mentioned in POSIX.1 may not be available on all systems. If you want to use one of these functions, you should normally check for its presence by using ‘AC_CHECK_FUNCS’ in your ‘configure.in’ script, and adapt to its absence if possible. Here is a list of some popular functions which are available on many, but not all, modern Unix systems:


There are several portability issues with alloca. See the description of AC_FUNC_ALLOCA in the autoconf manual. Although this function can be very convenient, it is normally best to avoid it in highly portable code.


GNU libtool provides a portable alternate interface to dlopen. See section Dynamic Loading.


In some cases fgets may be used as a fallback. In others, you will need to provide your own version of this function.


On some systems, the page size is available as the macro PAGE_SIZE in the header file ‘sys/param.h’. On others, the page size is available via the sysconf function. If none of those work, you must generally simply guess a value such as 4096.


When this is not available, fall back to a less precise function such as time or ftime (which itself is not available on all systems).


In some cases you can use either mmap or ordinary file I/O. In others, a program which uses mmap will simply not be portable to all Unix systems. Note that mmap is an optional part of the 1996 version of POSIX.1, so it is likely to be added to all Unix systems over time.


Unix systems without ptrace generally provide some other mechanism for debugging subprocesses, such as ‘/proc’. However, there is no widely portable method for controlling subprocesses, as evidenced by the source code to the GNU debugger, gdb.


Different Unix systems handle this differently. On some systems, any program can switch between the effective user ID of the executable and the real user ID. On others, switching to the real user ID is final; some of those systems provide the setreuid function instead to switch the effective and real user ID. The effect when a program run by the superuser calls setuid varies among systems.


If this is not available, then in some cases it will be reasonable to simply use sprintf, and in others you will need to write a little routine to estimate the required length and allocate an appropriate buffer before calling sprintf.


You can normally provide your own version of these simple functions.


When this is not available, just use malloc instead.


When this is not available, just use fork instead.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

15.2.2 Cross-Unix System Interfaces

There are several Unix system interfaces which have associated portability issues. We do not have the space here to discuss all of these in detail across all Unix systems. However, we mention them here to indicate issues where you may need to consider portability.


Many Unix systems provide the ‘curses’ interface for simple graphical terminal access, but the name of the library varies. Typical names are ‘-lcurses’ or ‘-lncurses’. Some Unix systems do not provide ‘curses’, but do provide the ‘-ltermcap’ or ‘-lterminfo’ library. The latter libraries only provide an interface to the ‘termcap’ file or ‘terminfo’ files. These files contain information about specific terminals, the difference being mainly the manner in which they are stored.

proc file system

The ‘/proc’ file system is not available on all Unix systems, and when it is available the actual set of files and their format varies.

pseudo terminals

All Unix systems provide pseudo terminals, but the interface to obtain them varies widely. We recommend examining the configuration of an existing program which uses them, such as GNU emacs or Expect.

shared libraries

Shared libraries differ across Unix systems. The GNU libtool program was written to provide an interface to hide the differences. See section Introducing GNU Libtool.


The ‘termios’ interface to terminals is standard on modern Unix systems. Avoid the older, non-portable, ‘termio’ and ‘tty’ interfaces (these interfaces are defined in ‘termio.h’ and ‘sgtty.h’, respectively).


Many, but not all, Unix systems support multiple threads in a single process, but the interfaces differ. One thread interface, pthreads, was standardized in the 1996 edition of POSIX.1, so Unix systems are likely to converge on that interface over time.


Most Unix systems maintain the ‘utmp’ and ‘wtmp’ files to record information about which users are logged onto the system. However, the format of the information in the files varies across Unix systems, as does the exact location of the files and the functions which some systems provide to access the information. Programs which merely need to obtain login information will be more portable if they invoke a program such as w. Programs which need to update the login information must be prepared to handle a range of portability issues.

X Window System

Version 11 of the X Window System is widely available across Unix systems. The actual release number varies somewhat, as does the set of available programs and window managers. Extensions such as OpenGL are not available on all systems.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated by Ben Elliston on July 10, 2015 using texi2html 1.82.