on Unix C bindings

Well, not exactly Unix. This is one of those sites the permalink's of which i might not care much. If you see this in webarchive, or if it is just cached in your browser or your brain, please note that this is probably still available under some other path.

on signals

with signal function you can assign a handler function (or a constant saying that you want to ignore or restore default behavior) to a signal. That function can do stuff like, sending that signal to another process with kill instead and leaving the receiving process intact.

the kill function returns 0 on success, 1 on failure, and 64 if you asked it to do a group of processes and for some it succeeded but for some it did not.

the function strsignal returns strings such as Interrupt for 2 (SIGINT). I have not found a way to obtain the SIGINT string, but that's probably not hard. Email me if this here is still up and it says i have not found a way and you did, if you want to bother with being helpful.

users

get effective user ID of the user who owns the process of the program with geteuid function

get the struct passwd * for a user ID by using getpwuid function.

from struct passwd * you can get ->pw_name (username), ->pw_dir (home directory path)

you can also get struct passwd * by username by using getpwnam function.

getting current working directory

you can get your current working directory using the function getcwd.

don't use the deprecated getwd, which does not allow to specify the size of your buffer, and can cause error of writing beyond the end of your allocated buffer

there is also a GNU-specific get_current_dir_name function, which will allocate the string for you, note that you will have to free it.

changing the current directory (cd)

one neat trick is to just arithmetically negate the return value of chdir since it can return only 0 or -1 anyway. Note: really, look at what the error was, see the errno

on errno

errno is a global variable that stores the error of last thing that failed that uses it. in <errno.h> you can find not only them by their names, but also functions like strerror which will give you a neat description string for an errno.

fork

the function fork forks your program (process) into two processes. To one it returns zero (the new one, the child), and to the other (the parent, the original one) it returns the new process ID of the child. If there was an error, it returns -1, and you can see the error in errno.

waitpid

the function waitpid will make your program process wait until the process that you called it about will terminate. or, if you ask it to, also if it will stop. and if you ask, also if it gets continued. default is just about termination. look up its manual for info about a bunch of macros that will tell you what does the status value that waitpid wrote for you really means, like WIFEXITED whether it exited normally, WEXITSTATUS with what exit code; WIFSIGNALED whether by unhandled signal, WTERMSIG from what signal.

exec*

the exec* functions replace the current process image with new process image, like from an executable file.

the function execvp and execv take just the filename and the arguments list.

the ones ending with «e» also take a table of KEY=value environment variable entries. but they are inherited besides that.

the ones with «p» differ from the ones without in that they will also, for relative filepaths, look in all the directories specified in the PATH variable.