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

The system call stat function is designed to retrieve statistics of a file or a directory. Simply add the path of your file or directory in the first parameter, then the variable of type struct stat in the second parameter. The function stat returns 0 on success and -1 on error. Let’s try an example of the system call stat function: #include #include #include #include #include #include int main(int c, char *v[]) { struct stat *buf; buf = malloc(sizeof(*buf)); if (stat(v[1], buf) == 0) { printf("buf->st_dev = %d\n", buf->st_dev); printf("buf->st_ino = %d\n", buf->st_ino); printf("buf->st_mode = %d\n", buf->st_mode); printf("buf->st_nlink = %d\n", buf->st_nlink); printf("buf->st_uid = %d\n", buf->st_uid); printf("buf->st_gid = %d\n", buf->st_gid); printf("buf->st_rdev = %d\n", buf->st_rdev); printf("buf->st_size = %d\n", buf->st_size); printf("buf->st_blksize = %d\n", buf->st_blksize); printf("buf->st_blocks = %d\n", buf->st_blocks); printf("buf->st_atime = %d\n", buf->st_atime); printf("buf->st_mtime = %d\n", buf->st_mtime); printf("buf->st_ctime = %d\n", buf->st_ctime); } return (0); } You are now ready to create a file, named hello.bp (the extension is free of course). ...

April 7, 2011

UNIX & GNU/Linux - User commands - Using man

The man of Linux can help us to find some helpful information of functions. This manual is splitted into eight parts. General commands System calls C or other language library functions Special files (usually devices, those found in /dev) and drivers File formats and conventions Games and screensavers Miscellaneous System administration commands and daemons For example, to see the man of the open() function, you can use the: $ man 2 open or ...

April 7, 2011

UNIX & GNU/Linux - User commands - Using wc

The user command wc is a GNU/Linux tool written by Paul Rubin and David MacKenzie. It helps us to display the number of lines, words and bytes counted in one or several files. Let’s see an example of the user command wc by executing this command in our favourite terminal: $ wc -l * It will display the number of lines in all files present in the current directory. You can combine all flags at the same time and the result will be displayed in different colums: ...

March 27, 2011

UNIX & GNU/Linux - User commands - Using grep

Maybe you sometimes asked yourself how to find a part of a code inside all your files. But how to find this part of code? We can use the grep user command to display this part of a code. We can moreover display this code in color. Let’s see an example of the grep user command. The line below has to be typed directly in your favourite shell: $ grep --color main * The result is the code “main” in red and the file where the code is found (in purple). ...

March 22, 2011

UNIX & GNU/Linux - User commands - Using fs

To set ACL correctly, you can use the fs user command. Let’s take an example to set the ACL: $ fs setacl path/until/directory name command If we want to see ACL list, let’s execute this with the la (for l ist a cl): $ fs la path/until/directory To remove all the ACL rights for the user system:anyuser we have to write it (sa for s eta cl): $ fs sa directory system:anyuser none

March 18, 2011

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

The execve() system call function is used to execute a binary executable or a script. The function returns nothing on success and -1 on error. The first parameter must be the path of a binary executable or a script. The second must be an array of pointers on a character (char *myArray[]), and the last pointer must be set to NULL. The third parameter must be an environment. Using the execve() syscall First example /** * Badprog.com * main.c */ #include #include #include #include #include int main() { int e; char *envp[] = { NULL }; char *argv[] = { "/bin/ls", "-l", NULL }; e = execve("/bin/ls", argv, envp); if (e == -1) fprintf(stderr, "Error: %s\n", strerror(errno)); return 0; } Compiling gcc main.c -o execve-me ; ./execve-me Ouput The output is a classic ls -la: ...

March 6, 2011

UNIX & GNU/Linux - Emacs - Phases of the Moon

Yes it really exits! You can see, with Emacs, the phase of the Moon for the next 12 weeks. Make an ALT + X and write this: lunar-phases Result: Tuesday, January 4, 2011: New Moon 10:05am (CET) Wednesday, January 12, 2011: First Quarter Moon 12:33pm (CET) Wednesday, January 19, 2011: Full Moon 10:19pm (CET) Wednesday, January 26, 2011: Last Quarter Moon 2:03pm (CET) Thursday, February 3, 2011: New Moon 3:33am (CET) Friday, February 11, 2011: First Quarter Moon 8:20am (CET) Friday, February 18, 2011: Full Moon 9:33am (CET) Friday, February 25, 2011: Last Quarter Moon 12:32am (CET) Friday, March 4, 2011: New Moon 9:48pm (CET) Sunday, March 13, 2011: First Quarter Moon 12:46am (CET) Saturday, March 19, 2011: Full Moon 7:07pm (CET) Saturday, March 26, 2011: Last Quarter Moon 1:12pm (CET) Enjoy!

February 23, 2011

UNIX & GNU/Linux - Emacs - Display number lines

If you want to see number of lines in Emacs, you have to activate it. Let’s start this tiny tutorial: Open Emacs, click ALT + X and write linum-mode then type RETURN. Well done, it works! To deactivate it, just retype this command.

February 15, 2011

UNIX & GNU/Linux - gdb - With Emacs

To start gdb, we have to add the -g flag at the compilation time. So it is easy to add it in a Makefile. We are also using the Emacs IDE for running gdb. Note that the code below does not work because we want to see the segmentation fault with gdb. Let’s create a main.c file in our gdb mini tutorial: #include #include int main(int c, char *v[]) { char *string; int i; i = 0; while (i < strlen(v[1])) { printf("%c", string[i]); i++; } return (0); } Let see an example of the gdb using: ...

February 14, 2011

UNIX & GNU/Linux - tcsh - Setting prompt with country flags

If you have always dreamed to change the classic prompt of your favorite Shell, it is time to see how to do it. Open your tcsh’s config that you can find in your home, it is called .tcshrc and inside, modify the line: set prompt='(%n@%m %h)' by set prompt='<%T %/%B%{\033[1;31m%}>%{\033[0m%}%b' Open a new terminal or juste write “. .bashrc” on your terminal, and miracle, you have now the time, your current path and a > in red and bold! ...

February 14, 2011