Translate

Sunday, September 29, 2013

Touchpad Tap Enable with XFCE

This is just a quick record regarding how to enable touch pad tapping with XFCE.
I'm using Debian Wheezy, XFCE4 and a Positivo MODO Black Netbook.

Method I - Init script.
Create a script with the code below and tell your XFCE to run it when it starts up.
Synclient TapButton1=1 LBCornerButton=2 RBCornerButton=3 MaxTapTime=150  SingleTapTimeout=150 MaxDoubleTapTime=150 

Method II - Add your settings to X configs.
Edit the file /usr/share/X11/xorg.conf.d/50-synaptics.conf and make the Touch Pad section look like this:
Section "InputClass"                                                
    Identifier "touchpad catchall"
    Driver "synaptics"
    MatchIsTouchpad "on"
    MatchDevicePath "/dev/input/event*"
    Option "TapButton1" "1"
    Option "RBCornerButton" "3"
EndSection 
 
That's it ;-)

Wednesday, September 11, 2013

Adjusting Gedit's embedded terminal plugin

Hello again!

This is another very quick tip! At this time, let's focus on Gedit's embedded terminal.

Well, when you install Gedit and load its terminal plugin you get really frustrated with its default colors as they suck a lot! They *really suck!*

Let's see how we can make this useful plugin look *a lot better* :-)

First things first!
Install the dconf-tools package and (if you didn't it already) the gedit-plugins.

apt-get install dconf-tools gedit-plugins

Now, open dconf-editor and go to:

org->gnome->gedit->plugins->terminal

And make sure all the settings look like this:


 If you did everything right, your embedded terminal should look like this one (except for the bash color theme ;-) )


That's it!

By the way, this is Debian (of course it's Debian!) Wheezy with XFCE.

Have fun! =)

Wednesday, August 28, 2013

Wifi connection via interfaces file on Debian

Hi there!
This is a very short guide on how to setup your WiFi connection using the /etc/network/interfaces file on Debian. This is a good trick as you'll have WiFi network access even before your graphical interface comes up! ;-)

I don't use any login managers nor network managers. This is enough for me. I hope it helps! :-)

Once you have wireless interface driver loaded and working, check what's your interface with the command "iwconfig".

# This is the wireless interface
auto wlan0
iface wlan0 inet dhcp 
    # This is your network SSID
    wpa-ssid linksys
    # This is your AP password
    wpa-psk 12345678

Now all you have to do is protect that file as it has your network's secret key ;-)
 
chmod 0600 /etc/network/interfaces
ls /etc/network/interfaces
-rw------- 1 root root 324 Jul 15 19:14 /etc/network/interfaces 
 
I told you it would be very short! Have fun! ;-)

Wednesday, June 26, 2013

Installing/Upgrading E17 on Debian 7.0 (Stable) - Updated 07/18/2013

Hi everyone.

I just installed E17 on my Wheezy box at work, and it works very well.
Box specs we are interested in are:
* Debian version: Wheezy (amd64)
* Custom kernel: Linux optimus 3.2.0-4-amd64 #1 SMP Debian 3.2.41-2+deb7u2 x86_64 GNU/Linux
* CPU: AMD Phenom(tm) II X2 B55 Processor
* RAM: 8 GB
* Video: ATI RV730 PRO [Radeon HD 4650]

All you have to do is to read the documentation on E17's official page. It also includes commands for installing E17 on Ubuntu 12.04. Well, to make things easier, I just adapted the procedures and made a very simple script, following the instructions.

This is the upgraded and improved version of the script.
I used it to install E17 on my home computer and to upgrade E17 on my laptop computer.
Box specs we are interested in are:
* Debian version: Wheezy (amd64)
* Custom kernel: Linux blackstar 3.8.6-blackstar-x6 #2 SMP PREEMPT x86_64 GNU/Linux
* CPU: AMD Phenom(tm) II X6 1090T Processor
* RAM: 16 GB
* Video: NVIDIA Corporation GF114 [GeForce GTX 560]

I hope it helps.


Warning: as this will modify your box, use it at your own risk!

#!/bin/bash
#
# This script will install E17 on a computer
# 
# You can use it to upgrade E17 whenever a new version comes out.
# (I used it from 0.17.0 to 0.17.3 without problems)
#
# For every successful build/install it creates a file_OK 
# and a file_ERROR is created for every failure.
#
#
#
# All info and credits: http://www.enlightenment.org/p.php?p=about/e17&l=en
#
# Gabriel Marques - snortt@gmail.com
#

# VERSION - Change whenever you want to upgrade E17 and its EFL
LIBS_VERSION="1.7.7"
E17_VERSION="0.17.3"
CONNMAN_VER="1.16"

# Where to get it 
E17_PKG_HOST="http://download.enlightenment.org/releases"
CONNMAN_SRC_URL="http://www.kernel.org/pub/linux/network/connman"

# Which format to get
LIBS_FILE_EXT="tar.gz"
E17_SRC_FILE_EXT="tar.gz"
CONNMAN_SRC_FILE_EXT="tar.gz"

# What to get 
E17_LIBS="eina eet evas embryo ecore eio edje efreet e_dbus evas_generic_loaders ethumb eeze emotion elementary"
E17_SRC_FILE="enlightenment-${E17_VERSION}.${E17_SRC_FILE_EXT}"
CONNMAN_SRC_FILE="connman-${CONNMAN_VER}.${CONNMAN_SRC_FILE_EXT}"

# ----------------------------------------
# You don't need to change anything ahead
# ----------------------------------------------------------------------------
SYS_DEPS="gcc doxygen libc6-dev make gdb wget libpam0g-dev libfreetype6-dev libpng-dev 
libjpeg-dev zlib1g-dev libdbus-1-dev libtiff-dev librsvg2-dev libgif-dev 
libcurl4-openssl-dev libasound2-dev libudev-dev libspectre-dev libpoppler-dev 
libraw-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libfribidi-dev 
libexif-dev liblua5.1-0-dev libx11-dev libxcursor-dev libxrender-dev libxrandr-dev 
libxfixes-dev libxdamage-dev libxcomposite-dev libxss-dev libxp-dev libxext-dev 
libxinerama-dev libxkbfile-dev libxtst-dev libxcb-shape0-dev libxcb-keysyms1-dev 
mesa-common-dev iptables-dev zlib1g-dev libfreetype6-dev fontconfig libfribidi-dev 
libpng12-dev  libpng++-dev libpnglite-dev libgif-dev libgiftiio-dev librsvg2-dev  
libx11-dev libxext-dev libxrender-dev libxcomposite-dev libxdamage-dev 
libxfixes-dev libxrandr-dev libxinerama-dev libxss-dev libxp-dev 
libxpm-dev libxcb-dpms0-dev libxext-dev libxcb-xtest0-dev 
libxdo-dev libxcursor-dev x11proto-print-dev libxp-dev libxkbfile-dev libxcb1-dev 
libxcb-composite0-dev libxcb-glx0-dev libxcb-render0-dev libxcb-screensaver0-dev 
libxcb-sync0-dev libxcb-xinerama0-dev libxcb-xprint0-dev libxcb-util0-dev 
libxcb-image0-dev libxcb-render-util0-dev libxcb-keysyms1-dev libxcb-shape0-dev 
libudev-dev  libdbus-1-dev libasound2-dev libpoppler-dev libraw-dev libspectre-dev 
libgstreamer0.10-dev libxine2-dev libvlc-dev libvlccore-dev  liblua5.2-dev 
libwebp-dev libbulletml-dev libmount-dev libssl-dev libdbus-c++-dev 
libdbus-glib-1-dev libdbusmenu-glib-dev libpulse-dev libblkid libblkid1-dev"

OPENGL_DEPS="libgl1-mesa-dev"

SLEEP_TIME="2"

if [ "$(id -u)" -ne "0" ]; then
    echo -e "You need to be root"
    exit 201
fi

echo -e "Updating system packages lists..."
apt-get update && apt-get dist-upgrade

echo -e "Installing system packages..."
apt-get install ${SYS_DEPS}

echo -e "Installing OpenGL packages..."
apt-get install ${OPENGL_DEPS}

echo -e " "
echo -e "Getting EFL"
for lib in ${E17_LIBS}
do
  if [ -f ${lib}-${LIBS_VERSION}.${LIBS_FILE_EXT} ]; then
    echo -e "File already exists: ${lib}-${LIBS_VERSION}.${LIBS_FILE_EXT}"
  else
    echo -e "Getting file: ${lib}-${LIBS_VERSION}.${LIBS_FILE_EXT}"
    touch ${lib}-${LIBS_VERSION}.${LIBS_FILE_EXT}_incomplete_download 
    wget ${E17_PKG_HOST}/${lib}-${LIBS_VERSION}.${LIBS_FILE_EXT} && \
      rm ${lib}-${LIBS_VERSION}.${LIBS_FILE_EXT}_incomplete_download 2> /dev/null 
  fi
done

echo -e " "
echo -e "Getting E17..."
if [ -f ${E17_SRC_FILE} ]; then
  echo -e "File already exists: ${E17_SRC_FILE}"
else
  echo -e "Getting file: ${E17_SRC_FILE}"
  touch ${E17_SRC_FILE}_incomplete_download 
  wget ${E17_PKG_HOST}/${E17_SRC_FILE} && \ 
    rm ${E17_SRC_FILE}_incomplete_download 2> /dev/null 
fi

# Now it's time to compile everything
export PATH=/usr/local/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
export CFLAGS="-O3 -fvisibility=hidden -ffast-math"

echo -e " "
echo -e "Building EFL..."
for lib in ${E17_LIBS}
do
  if [ "$lib" == "evas_generic_loaders" ]; then
    configure_opts="--disable-gnutls --disable-poppler"
  else
    configure_opts="--disable-gnutls"
  fi 

  # See if its already built/installed
  if [ -f ${lib}-${LIBS_VERSION}_OK ]; then
    echo -e "*** ${lib}-${LIBS_VERSION} is already built/installed ***"
    echo -e "\tRemove ${lib}-${LIBS_VERSION}_OK before rebuilding/reinstalling"
    echo -e "\tContinuing in $SLEEP_TIME seconds..."; sleep $SLEEP_TIME
    echo -e " "
  else
    tar zxf ${lib}-${LIBS_VERSION}.${LIBS_FILE_EXT}
    cd ${lib}-${LIBS_VERSION}
    ./configure ${configure_opts} && make && sudo make install && cd .. && \
      rm -rf ${lib}-${LIBS_VERSION} && touch ${lib}-${LIBS_VERSION}_OK || \
      touch ${lib}-${LIBS_VERSION}_ERROR

    # See if it worked or stop!
    if [ -f ${lib}-${LIBS_VERSION}_ERROR} ]; then
      exit
    fi 
  fi 
done

echo -e " "
echo -e "Configuring libraries dependencies..."
ldconfig

echo -e " "
echo -e "Building E17..."
if [ -f ${E17_SRC_FILE%.*.*}_OK ]; then
  echo -e "*** ${E17_SRC_FILE%.*.*} is already built/installed ***"
  echo -e "\tRemove ${E17_SRC_FILE%.*.*}_OK before rebuilding/reinstalling"
  echo -e "\tContinuing in $SLEEP_TIME seconds..."; sleep $SLEEP_TIME
  echo -e " "
else
  tar zxf ${E17_SRC_FILE}
  cd ${E17_SRC_FILE%.*.*}
  ./configure && make && sudo make install && cd .. && \
    rm -rf ${E17_SRC_FILE%.*.*} && touch ${E17_SRC_FILE%.*.*}_OK || \
    touch ${E17_SRC_FILE%.*.*}_ERROR

  # See if it worked or stop!
  if [ -f ${E17_SRC_FILE%.*.*}_ERROR ]; then
    exit
  fi 

  echo -e " "  
  echo -e "Inserting E17 on xsessions list..."
  if [ -f /usr/share/xsessions/enlightenment.desktop ]; then
    echo -e "Already there"
  else
    echo -e "Enlightenment is now on xsessions list"
    ln -s /usr/local/share/xsessions/enlightenment.desktop /usr/share/xsessions/enlightenment.desktop
  fi
fi 

echo -e " "
echo -e "Getting ConnMan..."
if [ -f ${CONNMAN_SRC_FILE} ]; then
  echo -e "File ${CONNMAN_SRC_FILE} already exists"
else
  echo -e "Getting file ${CONNMAN_SRC_FILE}" 
  touch ${CONNMAN_SRC_FILE}_incomplete_download 
  wget ${CONNMAN_SRC_URL}/${CONNMAN_SRC_FILE} && \
    rm ${CONNMAN_SRC_FILE}_incomplete_download 2> /dev/null 
fi 

echo -e " "
echo -e "Building ConnMan..."
if [ -f ${CONNMAN_SRC_FILE%.*.*}_OK ]; then
  echo -e "*** ${CONNMAN_SRC_FILE%.*.*} is already built/installed ***"
  echo -e "\tRemove ${CONNMAN_SRC_FILE%.*.*}_OK before rebuilding/reinstalling"
  echo -e "\tContinuing in $SLEEP_TIME seconds..."; sleep $SLEEP_TIME
  echo -e " "
else
  tar zxf ${CONNMAN_SRC_FILE}
  cd ${CONNMAN_SRC_FILE%.*.*}
  ./configure && make && sudo make install && cd .. && \
    rm -rf ${CONNMAN_SRC_FILE%.*.*} && touch ${CONNMAN_SRC_FILE%.*.*}_OK || \
    touch ${CONNMAN_SRC_FILE%.*.*}_ERROR

  # See if it worked or stop!
  if [ -f ${CONNMAN_SRC_FILE%.*.*}_ERROR ]; then
    exit
  fi 

  echo -e "Trying connman..."
  service network-manager stop
  /usr/local/sbin/connmand &

  echo -e "Adding ConnMan to system startup..."
  if grep '/usr/local/sbin/connmand' /etc/rc.local > /dev/null 2>&1; then
    echo -e "Already there"
  else
    echo -e "Adding ConnMan to /etc/rc.local"
    sed 's:exit 0:(sleep 9;/usr/local/sbin/connmand) \&\nexit 0:g' /etc/rc.local > /tmp/temp.$$
    cp /tmp/temp.$$ /etc/rc.local && rm /tmp/temp.$$
  fi
  
  echo -e "Disabling Network Manager"
  update-rc.d network-manager disable
fi 

echo -e "Have Fun! ;-)"

This is what you should get at the end of the process.



Tuesday, June 18, 2013

Thursday, June 13, 2013

Variadic Functions

Hi there again!
This time I'm gonna show you a very simple program to demonstrate how you can use variadic functions.
They are functions that accept a variable number of arguments and are very useful for many mathematical and logical operations programming.

 1 /* 
 2  *    Even being very basic, this program shows how to use variadic functions
 3  *
 4  *    Gabriel Marques 
 5  *    snortt@gmail.com
 6  *
 7  */
 8 
 9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 
13 /* This one is needed by variadic functions */
14 #include <stdarg.h>
15 
16 /* 
17  * Prototypes
18  * 
19  * As you can see, variadic functions are declared with a coma and the ellipsis (", ...")
20  */
21 void print_message(const char *, ...);
22 int sum_of(int, ...);
23 
24 int main(int argc, char *argv[]) {
25 
26     print_message("This is a test %s %c", "...", '\n');
27     printf("Sum of 1 2 3 = %d\n", sum_of(3,1,2,3));
28     
29     exit (EXIT_SUCCESS);
30 }
31 
32 void print_message(const char *str, ...) {
33     /* This creates the pointer to the args list*/
34     va_list args;
35     
36     /* This initializes the args list. Points to first element AFTER str */
37     va_start(args, str);
38 
39     /* Now we print all he args by making use of the proper function */
40     vprintf(str, args);
41 
42     /* Now we clean up the mess we made inside the program stack */
43     va_end(args);
44 }
45 
46 /* This function receives the count of values to be added and the values themselves */
47 int sum_of(int count, ...) {
48     
49     int i, sum = 0;
50     /* This creates the pointer to the args list*/
51     va_list args;
52 
53     /* This initializes the args list. Points to first element AFTER count */
54     va_start(args, count);
55 
56    for (i = 0; i < count; i++) {
57         /* Steps through the args list and adds each value it finds until it reaches the limit (count) */
58         sum += va_arg(args, int);
59     }
60     
61     /* Now we clean up the mess we made inside the program stack */
62     va_end(args);
63 
64     return sum;
65 }

I hope this quick text could help you!


Bitshifting

.: Bit shifting using C Language :.

This is a small, but yet funny program, that demonstrates how to use bit shifting for simple operations using the C Language.

Try to keep this in mind:

  • Move 1 bit to the left (<): multiply by 2
  • Move 1 bit to the right (>): divide by 2
For more information on Bit shifting and Bitwise operations take a look at:
Bitwise operations 



  1 /*
  2  * Flag bits test.
  3  * This program shows how to implement
  4  * flags operations using the eight bits
  5  * of a char variable.
  6  *
  7  * Gabriel Marques
  8  * snortt@gmail.com
  9  *
 10  */
 11 
 12 /*
 13  * Imagine you need to use flags to control
 14  * the state of something. Think of 8 flags,
 15  * for instance, for controlling 8 basic
 16  * functions of a super advanced robot! ;-)
 17  *
 18  * Things that require binary state, such as
 19  * Unit_Status, Engage_Battle, Force_Field, etc
 20  * require ON or OFF control.
 21  *
 22  * You could use variables to control each state,
 23  * but think of the huge waste you would have
 24  * as all of these states are binary values!
 25  *
 26  * char Unity_Status[] = "On"; <-- This would require
 27  * much more space than a simple 0 or 1 as it would
 28  * allocate memory for 3bytes! ('O', 'n', '\n').
 29  * And you would also need to deal with strings
 30  * comparison to test each status!
 31  *
 32  * Now, think of a single bit for each status.
 33  * As a char variable is 1 byte long, we would
 34  * have 8 bits to store information on a single char! :-)
 35  *
 36  * Using bit shifts we could manipulate each bit inside
 37  * our 1 byte (8 bits) char:
 38  *
 39  * You can see the operation and the bit it affects below:
 40  *
 41  *  1<<0 --> 00000001 << 0    00000001 --> Bit 0
 42  *  1<<1 --> 00000001 << 1    00000010 --> Bit 1
 43  *  1<<2 --> 00000001 << 2    00000100 --> Bit 2
 44  *  1<<3 --> 00000001 << 3    00001000 --> Bit 3
 45  *  1<<4 --> 00000001 << 4    00010000 --> Bit 4
 46  *  1<<5 --> 00000001 << 5    00100000 --> Bit 5
 47  *  1<<6 --> 00000001 << 6    01000000 --> Bit 6
 48  *  1<<7 --> 00000001 << 7    10000000 --> Bit 7
 49  *
 50  *
 51  * Let's take a look at a simple example, regarding our
 52  * super advanced robot and its behavior.
 53  *
 54  */
 55 
 56 #include <stdio.h>
 57 #include <stdlib.h>
 58 
 59 /* Definition of each funcionality flag */
 60 const unsigned char UNITY_STATUS             = (1<<0); /* Bit 0 */
 61 const unsigned char UNITY_ENGAGED_FOR_BATTLE = (1<<1); /* Bit 1 */
 62 const unsigned char UNITY_DEFLECTOR_SHIELD   = (1<<2); /* Bit 2 */
 63 const unsigned char UNITY_LASER_CANNON       = (1<<3); /* Bit 3 */
 64 const unsigned char UNITY_MISSLE_ARMED       = (1<<4); /* Bit 4 */
 65 const unsigned char UNITY_MOVE               = (1<<5); /* Bit 5 */
 66 const unsigned char UNITY_AI_SYSTEM_FAILURE  = (1<<6); /* Bit 6 */
 67 const unsigned char UNITY_GENERAL_FAILURE    = (1<<7); /* Bit 7 */
 68 
 69 /*
 70  * Now that we have the flags defined we can manipulate
 71  * them to test if our robot (UNITY) has some modes ON or OFF
 72  * as you will see later inside the main program.
 73  */
 74 
 75 int main() {
 76 
 77     unsigned char unity_control_flags;   /* This controls our robot ;-) */
 78     unity_control_flags = 0;             /* This disables everything */
 79 
 80     /* Let's turn ON our robot */
 81     unity_control_flags |= UNITY_STATUS;
 82 
 83     /* Let's make our unity start its deflector shield */
 84     unity_control_flags |= UNITY_DEFLECTOR_SHIELD;
 85 
 86     /* During a battle, let's suppose our system is damaged */
 87     unity_control_flags |= UNITY_GENERAL_FAILURE;
 88 
 89     /*
 90      * Now, let's test some status flags of our robot
 91      * We do this by testing if some bits are set to 1
 92      * what will result in a TRUE test.
 93      */
 94 
 95     /* Let's see if it is turned on*/
 96     if ( (unity_control_flags & UNITY_STATUS) != 0 )
 97         printf("Unity is on and waiting for instructions!\n");
 98     else
 99         printf("Unity is off!\n");
100 
101     /* Let's test if the unity has its shield on */
102     if ((unity_control_flags & UNITY_DEFLECTOR_SHIELD) != 0)
103         printf("Unity has its deflector shield on!\n");
104     else
105         printf("Deflector shield is down! Unity is vulnerable!\n");
106 
107     /* And we do the same test for a general system failure */
108     if ((unity_control_flags & UNITY_GENERAL_FAILURE) != 0)
109         printf("Warning! General system failure!\n");
110     else
111         printf("Unity system is healthy!\n");
112 
113     /*
114      * Now let's suppose we need to turn some features off
115      * Let's pretend we've just fixed your system failure problem.
116      * We need to reset the corresponding flag!
117      *
118      * We do this by using the flags variable AND NOT the
119      * desired functionality (~)
120      */
121      unity_control_flags &= ~UNITY_GENERAL_FAILURE;
122 
123      /* Let's check if it worked */
124      /* And we do the same test for a general system failure */
125      if ((unity_control_flags & UNITY_GENERAL_FAILURE) != 0)
126         printf("Warning! General system failure!\n");
127      else
128         printf("Unity system is healthy!\n");
129 
130 
131      return 0;
132 }

Program arguments on the command line

.: Program arguments on the command line :.

Although you can make your own routines to read and process the command line used to invoke your
program, everything is more practical when using the C library functions, as in the case of getopt_long ().

The example code that follows is a simple program that processes its arguments and shows on screen.


 1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <stdlib.h>
 4 
 5 #define _GNU_SOURCE
 6 #include <getopt.h>
 7 
 8 int main(int argc, char *argv[]) {
 9 
10  int opt;
11  int verbose_flag=0;
12 
13  struct option longopts[] = {
14  {"help", 0, NULL, 'h'},
15  {"file", 1, NULL, 'f'},
16  {"verbose",0,&verbose_flag,1},
17  {0,0,0,0}};
18 
19     while((opt = getopt_long(argc, argv, ":hf:", longopts, NULL)) != -1) {
20         switch(opt) {
21         case 'h':
22             printf("option: %c\n", opt);
23    printf("%s [-h|--help] [--verbose] [-f|--file <file>] [args]\n",argv[0]);
24             break;
25         case 'f':
26             printf("option %c filename: %s\n", opt, optarg);
27             break;
28         case ':':
29             printf("option %c needs a value\n", optopt);
30             break;
31         case '?':
32             printf("unknown option: %c\n", optopt);
33             break;
34         }
35     }
36 
37  if (verbose_flag != 0)
38   printf("Verbose mode ON!\n");
39  
40     for(; optind < argc; optind++) {
41   printf("optind: %d < argc: %d\n", optind, argc);
42         printf("argument: %s (argv[%d])\n", argv[optind], optind);
43   printf("\n");
44  }
45  
46  while (optind < argc) {
47   printf("optind: %d < argc: %d\n", optind, argc);
48         printf("argument: %s (argv[%d])\n", argv[optind], optind);
49   printf("\n");
50   optind++;
51  }
52 
53  printf("Out of the loop.\nflag: %d\noptind: %d e argc: %d\n", verbose_flag, optind, argc);
54 
55     exit(0);
56 }

Environment Variables


.: Printing the environment variables :.

Avoid modifying the environment directly. Prefer using functions like setenv and unsetenv.

Environment variables are accessed through the array of char pointers that represents the list of variables and their values​​.

The sample code bellow prints the environment variables and their values


 1 /* 
 2  * A demonstration on how to print the execution environment 
 3  * Never modify environ directly! Use setenv and unsetenv instead.
 4  */
 5 
 6 #include <stdio.h>
 7 #include <stdlib.h>
 8 
 9 /* The variable 'environ' keeps the environment. 
10  * An array of pointers to the variables list.
11  */
12 extern char **environ;
13 
14 int main() {
15  
16  char **var;
17  for (var = environ; *var != NULL; ++var)
18   printf("%s\n", *var);
19 
20  return 0;
21 }

Playing with the terminal buffer

.: Dealing with stdou and stderr :.

Hum ... so you've just wrote an awesome console app and it keeps joking with your face when it comes to writing its output to the screen.

Maybe the sample code bellow could help you a little.


 1 /* 
 2  * This program shows a simple stdout and stderr usage.
 3  *
 4  * Note that stdout uses a buffer and this can lead to undesired effects when 
 5  * printing, because it can occur while the buffer isn't empty. What a mess!
 6  * stderr doesn't use a buffer, so messages are print as they arrive.
 7  * 
 8  */
 9 
10 #include <stdio.h>
11 #include <stdlib.h>
12 
13 int main(int argc, char *argv[]) {
14 
15  int counter = 0;
16 
17  /* 
18   * Prints 5 dots without adjusting stdout buffer, one per second. 
19   * Note that it can happen (and probably will) of 5 points are printed 
20       * only once at the end of the program. stdout uses a buffer and 5 dots
21   * aren't enough to fill it! */
22  printf("\nTest 1/3. Using printf, no buffer adjustments. Wait 5 seconds\n");
23  while (counter++ <= 4) {
24   printf(".");
25   sleep(1);
26  }
27 
28  /* 
29   * Prints 5 dots, but this time adjusting the buffer, one per second.
30   * As we empty the buffer, its new content is processed immediately. 
31   */
32  counter = 0;
33  fflush(stdout);
34  printf("\nTest 2/3. Using printf, buffer adjustments. Note a dot per second.\n");
35  while (counter++ <= 4) {
36   fflush(stdout);
37   printf(".");
38   sleep(1);
39  }
40  
41  /* 
42   * Prints 5 dots using stderr, one per second.
43   * Note the lack of a buffer in stderr. Data is processed immediately. 
44   */
45  counter = 0;
46  fflush(stdout);
47  printf("\nTest 3/3. Using fprintf with stderr. Note a dot per second.\n");
48  while (counter++ <= 4) {
49   fprintf(stderr,".");
50   sleep(1);
51  }
52 
53 
54  return 0;
55 }

This is just the logo! Yes! I like my logo! :P


Tips and Tricks for OpenBSD - Post Login Messages

.: How to configure post login messages on OpenBSD :.

Again, Linux users who know the /etc/issue file are going to love this one!

Wait! OpenBSD doesn't have this file, but you're ok if you have the /etc/gettytab on your filesystem (or if you can create one!).

See the example:

# $OpenBSD: gettytab,v 1.4 2000/09/08 02:27:36 pjanzen Exp $
#
# Most of the table entries here are just copies of the old getty table.
# It is by no means certain, or even likely, that any of them are optimal
# for any purpose whatever. Nor is it likely that more than a couple are
# even correct.
#
# The default gettytab entry, used to set defaults for all other
# entries, and in cases where getty is called with no table name
#
# Original confs:
#default:\
# :np:im=\r\n%s/%m (%h) (%t)\r\n\r\n:sp#1200:
# --------------------
# Your new configs should look like these new lines bellow:
default:\
     :np:im=\r\nYourServerName %s/%m :: Hostname:(%h)\r\n\r\n:sp#1200:

I hope this could help anybody! See ya!

;-)

Tips and Tricks for OpenBSD - Terminal Configurations

.: How to configure the OpenBSD terminals :.

Linux users who had any contact with the /etc/inittab file are going to love this one! ;-)

This quick guide is gonna show you how to configure the number of virtual terminals on OpenBSD.

Before you go ahead and crash just after figuring you that BSD systems do NOT have inittab file, I should tell you this: BSD systems do NOT have inittab file! =)

But you're ok if you edit que /etc/ttys file!

So, hands on! Here is how you can control the number of virtual terminals.

#
# $OpenBSD: ttys,v 1.3 2008/01/09 17:39:42 miod Exp $
#
# name getty type status comments
#
console "/usr/libexec/getty std.9600" vt220 on secure
ttyC0 "/usr/libexec/getty std.9600" vt220 on secure
tty00 "/usr/libexec/getty std.9600" unknown off secure
tty01 "/usr/libexec/getty std.9600" unknown off secure
tty02 "/usr/libexec/getty std.9600" unknown off secure
tty03 "/usr/libexec/getty std.9600" unknown off secure
ttyp0 none network
ttyp1 none network
ttyp2 none network

Note that just the console and ttyC0 (accessed by Ctrl+Alt+F1) are active ;-)
Got the idea? Great! Now go configure your servers! ;-)


Tips and Tricks for OpenBSD - Custom Kernel

.: Custom Kernel on OpenBSD :.

If you want to discuss more about the BSD kernel implementation, sorry, but this is meant to be a quick guide for those who want to make simple adjustments and build a custom kernel. Feel free to go further.


Just follow the steps bellow:

  1. cd /usr/src 
  2. wget http://mirror/openbsd/4.9/sys.tar.gz 
  3. tar zxvf sys.tar.gz -C /usr/src 
  4. cd /usr/src/sys/arch/i386/conf 
  5. cp GENERIC  MYKERNEL (and edit MYKERNEL as you need ;-P)
  6. config MYKERNEL 
  7. cd ../compile/MYKERNEL 
  8. make depend && make && mv /bsd /bsd.original 
  9. cp bsd / 
  10. reboot
Hum! That was fast and easy, wasn't it? :)
Keep practicing with different configurations!

Patches Application

If you want to apply security/fixes patches, just go to http://www.openbsd.org/ and take a look at the erratas page for your chosen version of OpenBSD.

At the time of writing this post, 5.6 was the current version, so I'd point to http://www.openbsd.org/errata56.html

There you can grab more info about patches and the proper ways of applying them.

For a quick and easy tip, you basically can get all patches from a single tarball and extract them.

Then you'll find a structure of directories whose files are named as follows:
 
010_<what_to_fix>.patch.sig

For each file inside that dir, just use ``signify'', but keep in mind that some patches are for kernel and some are for system-wide programs. The example below uses signify to patch ``pipex''. Then you just recompile the patched kernel/program.

signify -Vep /etc/signify/openbsd-XY-base.pub -x 010_pipex.patch.sig \
        -m - | (cd /usr/src && patch -p0 
 
See ya!

Tips and Tricks for OpenBSD - Using GNU ls

.: How to use the GNU ls on OpenBSD :.

If you're a Linux lover, you certainly miss GNU ls when using BSD systems. Well, this post will help you to install and configure GNU ls on OpenBSD.

Just follow those really easy steps:

pkg_add -i -v gnuls
alias ls='gls --color=auto'
If you want to make it permanent, just add this inside your favorite shell configuration file:

# for BASH
echo "alias ls=gls --color=auto" >> .bashrc

There you go! :)

Tips and Tricks for OpenBSD - Shell Replacement

.: Shell Replacement :.

If you ever wondered how to replace the default shell of your newly installed OpenBSD, this post might help you! :)

After you have configured your packages repository, just fire the commands bellow:

pkg_add -i -v bash
chsh -s bash root # this sets bash as the default shell for root
chsh -s bash gabriel # this sets bash as the default shell for the user gabriel
I think you've got the idea!

=)