UNIX & GNU/Linux - System calls - Using open()

The open() system call function is used to open a file. We have to pass the pathname of the file we want to open, then a flag in the second parameter. This second one has to be a flag among the following ones: O_RDONLY, O_WRONLY or O_RDWR. An important thing is that this function will always return a non negative integer on success. This return value will be used by others functions, such as read() or write() for example, as a file descriptor. As all system calls, if the function returns -1, it will mean that an error has occurred. Errors are POSIX standard. The value 0, 1 and 2 are already reserved by the system, so if we open a new file that exists, its integer value will be 3 or more. Let’s see why. Write it successively on your favourite terminal: ls -la /dev/ stdin ls -la /dev/stdout ls -la /dev/stderr ...

February 3, 2011

UNIX & GNU/Linux - Tips and tricks - Change the screen resolution

The command to change the screen resolution is xrandr, To see all size available for your screen: $ xrandr To choose the another one resolution just use the -s option with the resolution, for example: $ xrandr -s 800x600

February 3, 2011

UNIX & GNU/Linux - gcc - Commands -v

With the -v command of the gcc, you can see all programs invoked by the compiler. Let’s try with an example of the -v command: gcc -v Result (certainly different on your system): Using built-in specs. Target: i686-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch=i686 --build=i686-redhat-linux Thread model: posix gcc version 4.4.4 20100503 (Red Hat 4.4.4-2) (GCC)

February 2, 2011

UNIX & GNU/Linux - tar - The xvf command

The xvf command of the tar tool allows to extract files from a file with the .tgz extension, in the current folder. Let’s see an example of the xvf command: $ tar xvf /home/login/Downloads/myArchive.tgz It will extract all files present in the archive into the current directory in a new directory with the name of the first directory of the archive file.

February 2, 2011

UNIX & GNU/Linux - Tips and tricks - Change keyboard language

An easy way to change the keyboard language on Linux is to write this command on your favourite terminal: setxkbmap lang Where lang is the language you want. As an example to change the keyboard in french: setxbmap fr Or for an american keyboard: setxbmap us

January 30, 2011

UNIX & GNU/Linux - System calls - Using tgetflag()

The tgetflag() function is a Linux system call function. It is designed to be used with the getenv() and tgetent() functions. The tgetflag() function returns a boolean value: 1 on success or 0 on fail. With the tgetflag() function, we have to put an only one string parameter. You have to use the two letters of the appropriate termcaps. You can find all of them in the Boolean Capabilities of the termcap manual. ...

January 20, 2011

UNIX & GNU/Linux - System calls - Using getenv()

The getenv() function is a Linux system call function. It is designed to know what is the name of an environment variable. The main use is for the “TERM” variable. Let’s see an example of the getenv() function: /* ** Made by BadproG.com */ #include int main() { const char *name; name = "TERM"; printf("My terminal is %s.\n", getenv(name)); return (0); }

January 19, 2011

UNIX & GNU/Linux - System calls - Using tgetent()

The tgetent() function is a Linux system call function. It is designed to be used with other termcap functions such as: tgetflag tgetnum tgetstr tgoto tputs With the tgetent() function, we stock in the first parameter (a buffer pointer), the capabilities of the second parameter (the environment variable retrieved by the getenv() function). This function returns 1 on success, 0 if there is not a such description and -1 if the terminfo database is not found. ...

January 19, 2011

UNIX & GNU/Linux - System calls - Using tgetnum()

The tgetnum() function is a Linux system call function. It is designed to be used with the getenv() and tgetent() functions. If the description of the terminal is not found, most probably because there is no result in the tgetent() function, the tgetnum() one will return -1. With the tgetnum() function, we have to put an only one string parameter. You have to use the two letters of the appropriate termcaps. You can find all of them in the Numeric capabilities of the termcap manual. ...

January 19, 2011

UNIX & GNU/Linux - System calls - opendir()

The opendir() system call function is used to open a directory and to return a pointer on this directory. Let’s take an example of the using of the opendir system call function. First, we have to create two files: a directory named hello and file name world. mkdir hello touch world And now let’s create the main.c file: /* ** Made by BadproG.com */ #include #include #include #include int main(int c, char *v[]) { DIR *myDirectory; myDirectory = opendir(v[1]); if (c == 2) { if (myDirectory) { puts("OK the folder is opened."); /* ** closedir */ if (closedir(myDirectory) == 0) puts("The directory is now closed."); else puts("The directory can not be closed."); } else if (errno == ENOENT) puts("This directory does not exist."); else if (errno == ENOTDIR) puts("This file is not a directory."); else if (errno == EACCES) puts("You do not have the right to open this folder."); else puts("That's a new error, check the manual."); } else puts("Sorry we need exactly 2 arguments."); return (0); } Let’s try: ...

January 11, 2011