1

I am writing a C program, part of which requires me to get the User's operating system. For example, to get the home directory I can do the following:

const char *homedir;
homedir = getpwuid(getuid())->pw_dir;
printf("Home dir: %s\n",homedir);

I can not locate anything related to the OS in pwd.h is there a different file I should be looking at ?

Mutating Algorithm
  • 2,604
  • 2
  • 29
  • 66
  • "How do I get the User's operating system in UNIX?" – um, `return "Unix";`? – The Paramagnetic Croissant Feb 16 '15 at 17:09
  • @TheParamagneticCroissant Sorry, that was a bad question, I meant, get the user's operating system in general through a C program. My apologies. – Mutating Algorithm Feb 16 '15 at 17:12
  • I get that it's not pretty, but you can just check for the existence of some characteristic filesystem paths. `/usr` will usually mean a UNIX of some kind, `/Applications`, `/Library`, and `/Users` all indicates OSX, `C:/` anything or `System32` indicates Windows, and so on. From there you can look at things like `uname` or some Win32 API call to get more specific version information. – Linuxios Feb 16 '15 at 17:24
  • 1
    The C language does not define a method of querying the OS name, or indeed stipulate that there is an OS. Why would your program need to know that? – n. m. could be an AI Feb 16 '15 at 17:30
  • @Linuxios A single executable program normally cannot contain both uname and a WinAPI call, and call either depending on some run-time condition. – n. m. could be an AI Feb 16 '15 at 17:35
  • There seems to be some confusion as to what you want. The 2 answers so far return information *about* the OS, whereas your question seems to imply you want access to the directory of the OS. If you tell us what you want to *do* (rather than what think you *want*) we may better be able to help you. – Peter M Feb 16 '15 at 17:46

3 Answers3

2

Most of the times (it's not guaranteed), a C compiler defines macros that identify the current operating system (the macro-family, at the very least).

Eg. to identify Windows, Linux and Mac you could do:

#import <stdio.h>

typedef enum {

  UNKNOWN,
  WINDOWS,
  LINUX,
  OSX

} OS;

OS current_os();

int main() {

  printf("%d",current_os());
  return 0;

}

OS current_os() {
  #ifdef _WIN32
    return WINDOWS;
  #elif (defined(__APPLE__) && defined(__MACH__))
    return OSX;
  #elif defined(__gnu_linux__)
    return LINUX;
  #else
    return UNKNOWN;
  #endif 
}

On my Mac this prints 3, like it's supposed to do.

You can find a comprehensive list of those macros here.

Matteo Pacini
  • 21,796
  • 7
  • 67
  • 74
2

On SVr4 and POSIX.1-2001 and later compliant UNIXes, use uname() -- for more info, man 2 uname to info about which UNIX, kernel version, and hardware identifier you are on.

Not sure for non-UNIX.

http://man7.org/linux/man-pages/man2/uname.2.html

JohnH
  • 2,713
  • 12
  • 21
1

On Windows, you'll use Win API functions to get exact version (such as GetVersionEx API call).

On Linux - you can use uname(struct utsname *buf) call (man 2 uname).

Other platforms may provide different APIs.

If you want to be portable across operating systems, you should provide a layer of abstraction that hides OS-specific calls and compile-in proper platform-specific implementation that shares a common interface.

Abstractions - this is The Way in cross-platform programming.

Alternatively, you may use pre-processor, which provides some basic platform information at compile-time. Preprocessor is used quite often to select platform-specific code to compile.

ezaquarii
  • 1,914
  • 13
  • 15