These libraries all provide facilities to create processes in a simple way, to open, read and write files. These must all be at the lowest common denominator of all the platforms.
On the other hand, to get the best performance or to use all of the features supplied by an Operating System, you need to use the specific facilities provided by that O/S. These will almost certainly be O/S specific, so that to use them means that your programs will be less portable than using the general libraries.
Using system specific facilities means that you are using systems programming services. This may be for
/usr/man
. It is accessible using the command
man [-s section] commandThe command
apropos keywordcan be used to search for commands.
Section 2 of the manual is the O/S basic API. For example,
man 2 opengives the entry for opening a file.
Section 3 of the manual is additional C level calls, some at the O/S level, some from others. For example
man 3 getlogin
void
functions). Systems calls usually use this value
to signal errors
unsigned int sleep(unsigned int seconds)
int uname(struct utsname *name)attempts to read system values. If it succeeds, it returns a non-negative value. If it fails, it returns -1
NULL
pointer to signal an error. For example, the Unix function
char *getlogin()attempts to find the login name of the user. If it succeeds, it returns a pointer to the string. If it fails it returns
NULL
.
int creat(char *path, mode_t mode)attempts to create a file. If it succeeds, it returns a non-negative file descriptor. If it fails, it returns -1.
/usr/include
and
used in C programs by the
#include <...>mechanism.
Values about sizes of primitive data types is stored in
/usr/include/values.h
/usr/include/types.h
(or includes that they point to).
(Warning: reading these can cause brain-strain!).
Read the online doco instead.
getlogin()
is defined by
char *getlogin(void)It returns a string holding the login name, or
NULL
on
failure. You should not modify the returned string.
#include <stdio.h> int main(int argc, char **argv) { char *login; if ((login = getlogin()) != NULL) { printf("Login name %s\n", login); } else { fprintf(stderr, "Can't find login name\n"); exit(1); } exit(0); }
#include <stdlib.h> char *getenv(char *name)takes a string as argument, and returns a string as value. The argument is the variable whose value you want. The return is the value or
NULL
of the variable is not set. You should not
modify the return value.
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { char *path; if ((path = getenv("PATH")) != NULL) { printf("Path is: %s\n", path); } else { fprintf(stderr, "Can't find the value of PATH\n"); exit(1); } exit(0); }
/etc/passwd
. The function getpwnam()
returns
such information given a user's name as a string.
#include <pwd.h> struct passwd *getpwnam(char *name)The returned structure is
struct passwd { char *pw_name; /* Username. */ char *pw_passwd; /* Password. */ uid_t pw_uid; /* User ID. */ gid_t pw_gid; /* Group ID. */ char *pw_dir; /* Home directory. */ char *pw_shell; /* Shell program. */ };This may be used as
#include <stdio.h> #include <pwd.h> int main(int argc, char *argv[]) { struct passwd *pass; char *name = "jan"; if ((pass = getpwnam(name)) != NULL) { printf("Home dir of %s is %s \n", name, pass->pw_dir); } else { fprintf(stderr, "Can't find password info for %s\n", name); exit(1); } exit(0); }
#include <sys/utsname.h> int uname(struct *utsname name)
#define _SYS_NMLN 257 struct utsname { char sysname[_SYS_NMLN]; char nodename[_SYS_NMLN]; char release[_SYS_NMLN]; char version[_SYS_NMLN]; char machine[_SYS_NMLN-65]; }This may be used as
#include <stdio.h> #include <sys/utsname.h> int main(int argc, char *argv[]) { struct utsname name; if (uname(&name) != -1) { printf("System name: %s\n", name.sysname); } else { fprintf(stderr, "Can't find system name\n"); exit(1); } exit(0); }