<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<?xml-stylesheet type="text/css" href="http://pdes-net.org/x-haui/styles/feed.css"?>
<title type="html">x-haui</title>
<link rel="alternate" type="text/html" href="http://pdes-net.org/x-haui/" />
<link rel="self" type="application/atom+xml" href="http://pdes-net.org/x-haui/atom.xml" />
<updated>2012-04-10T00:43:41+02:00</updated>
<author>
<name>haui</name>
<uri>http://pdes-net.org/x-haui/</uri>
</author>
<id>http://pdes-net.org/x-haui/</id>
<generator uri="http://nanoblogger.sourceforge.net" version="3.3">NanoBlogger</generator>
<entry>
<title type="html">Default paramters</title>
<author>
<name>haui</name>
</author>
<link rel="alternate" type="text/html" href="http://pdes-net.org/x-haui/archives/2012/04/10/default_paramters/index.html" />
<id>http://pdes-net.org/x-haui/archives/2012/04/10/default_paramters/index.html</id>
<published>2012-04-10T00:06:26+02:00</published>
<updated>2012-04-10T00:06:26+02:00</updated>
<category term="C++" />
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<![CDATA[<p>Recently I came across an interesting snippet of C++ code:
<pre style="white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffff;">
<span style="color: #c000c0" >#include </span><span style="color: #c00000" >&lt;iostream&gt;</span>
<span style="color: #008000">class</span> Base {
    <span style="color: #804000">public</span>:
    <span style="color: #008000">virtual void</span> message1(){ std::cout &lt;&lt; <span style="color: #c00000">&quot;Base message1&quot;</span> &lt;&lt; std::endl; }
    <span style="color: #008000">virtual void</span> message2(std::string param = <span style="color: #c00000">&quot;Base message2&quot;</span>){ std::cout &lt;&lt; param &lt;&lt; std::endl; }
};
<span style="color: #008000">class</span> Derived : <span style="color: #804000">public</span> Base {
    <span style="color: #804000">public</span>:
    <span style="color: #008000">virtual void</span> message1(){ std::cout &lt;&lt; <span style="color: #c00000">&quot;Derived message1&quot;</span> &lt;&lt; std::endl; }
    <span style="color: #008000">virtual void</span> message2(std::string param = <span style="color: #c00000">&quot;Derived message2&quot;</span>){ std::cout &lt;&lt; param &lt;&lt; std::endl; }
};
<span style="color: #008000">int</span> main(){
    Derived d;
    Base *base = &amp;d;
    base-&gt;message1();
    base-&gt;message2();
    <span style="color: #804000">return</span> <span style="color: #c00000">0</span>;
}
</pre>

If you compile this with g++ and run the produced binary you'll get the following output:
<pre>
Derived message1
Base message2
</pre>
At first glance this might look a little confusing. It seems like the correct overloaded function is only called for <i>message1</i> but
not for <i>mesage2</i>. However, if you change the function bodies as follows, you can see that the correct function is called both times:

<pre style="white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffff;">
<span style="color: #008000">class</span> Base {
    <span style="color: #804000">public</span>:
    <span style="color: #008000">virtual void</span> message1(){ std::cout &lt;&lt; <span style="color: #c00000">&quot;Base::message1 Base message1&quot;</span> &lt;&lt; std::endl; }
    <span style="color: #008000">virtual void</span> message2(std::string param = <span style="color: #c00000">&quot;Base message2&quot;</span>){ std::cout &lt;&lt; <span style="color: #c00000">&quot;Base::message2 &quot;</span> &lt;&lt; param &lt;&lt; std::endl; }
};
<span style="color: #008000">class</span> Derived : <span style="color: #804000">public</span> Base {
    <span style="color: #804000">public</span>:
    <span style="color: #008000">virtual void</span> message1(){ std::cout &lt;&lt; <span style="color: #c00000">&quot;Derived::message1 Derived message1&quot;</span> &lt;&lt; std::endl; }
    <span style="color: #008000">virtual void</span> message2(std::string param = <span style="color: #c00000">&quot;Derived message2&quot;</span>){ std::cout &lt;&lt; <span style="color: #c00000">&quot;Derived::message2 &quot;</span>&lt;&lt; param &lt;&lt; std::endl; }
};
</pre>

<pre>
Derived::message1 Derived message1
Derived::message2 Base message2
</pre>
As you can see, the correct overloaded functions of the derived class are called in both cases. The main problem stems from the default parameter for
<i>message2</i>. Default parameters for C++ functions are resolved at compile time depending on the static type (here: Base), whereas the correct function is
determined from the dynamic type (here: Derived). As a rule of thumb, you should never change the values for default parameters in
inherited functions. Although it's legal and the result is well defined, doing so will only lead to confusion and subtle errors.
Another approach to avoid this issue is to abstain from using default parameters for virtual functions at all. 
If you want to know more about this an many other quirks of C++ you should take a look at Scott Meyer's book 
<a href="http://www.aristeia.com/books.html">Effective C++</a>.
</p>]]>
</div>
</content>
</entry>
<entry>
<title type="html">Security through obscurity</title>
<author>
<name>haui</name>
</author>
<link rel="alternate" type="text/html" href="http://pdes-net.org/x-haui/archives/2012/01/29/security_through_obscurity/index.html" />
<id>http://pdes-net.org/x-haui/archives/2012/01/29/security_through_obscurity/index.html</id>
<published>2012-01-29T17:31:47+02:00</published>
<updated>2012-01-29T17:31:47+02:00</updated>
<category term="linux" />
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<![CDATA[<p>From the variety of available email clients, I found <a href="http://www.claws-mail.org">Claws Mail</a> to be my favorite (maybe 'cause after 6
years of Linux, I still haven't found the time to configure mutt...). Anyway, in today's posting I will not praise the advantages of Claws Mail, 
but rant a little about one of its "security" features. Like most programs, Claws Mail stores its configuration in a separate directory in the user's home folder.
This folder contains, among other things, all account information. Since Claws Mail doesn't offer any kind of password manger or "master password" 
one would think, that the passwords for the mail accounts are stored in plain text. However, the <i>accountrc</i> file contains base64-encoded strings of 
DES-encrypted passwords. At this point, one should wonder how the program can encrypt the passwords without asking the user for a password. The solution
is simple - the password is hardcoded into the binary. With this knowledge it's obvious that this approach is a clear case of
 <a href="http://en.wikipedia.org/wiki/Security_through_obscurity">security through obscurity</a>. 
Given the accountrc file and the binary everyone can easily decrypt the passwords, i.e. with <a href="http://pdes-net.org/x-haui/scripts/misc/claws_decrypt.tar.gz">this</a> 
standalone C program. <br>
If you're asking for more security than restrictive file permissions for your home folder can provide, you still got several options. Patch Claws Mail's 
sourcecode in order to use a real password safe for the storage of the passwords, use file encryption (either for your complete home folder, or just 
for ~/.claws-mail, e.g. with <a href="http://pdes-net.org/cobra/archives/2009/07/26/poor_mans_strongbox_-_and_beyond/index.html">encfs</a>), or switch to another email client.  </p>]]>
</div>
</content>
</entry>
<entry>
<title type="html">Toggle SSL</title>
<author>
<name>haui</name>
</author>
<link rel="alternate" type="text/html" href="http://pdes-net.org/x-haui/archives/2012/01/26/toggle_ssl/index.html" />
<id>http://pdes-net.org/x-haui/archives/2012/01/26/toggle_ssl/index.html</id>
<published>2012-01-26T00:02:49+02:00</published>
<updated>2012-01-26T00:02:49+02:00</updated>
<category term="linux" />
<category term="scripts" />
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<![CDATA[<p> To switch easily between the HTTP and HTTPS version of a website, I wrote a small plugin for <a href="http://vimperator.org">Vimperator</a> that can be found 
<a href="http://pdes-net.org/x-haui/scripts/togglessl.js">here</a>. Save it into <i>~/.vimperator/plugins/</i> and restart Firefox. You should now be able to switch between the 
HTTP and HTTPS version of a website by pressing <i>\h</i>.
</p>]]>
</div>
</content>
</entry>
<entry>
<title type="html">Advanced I/O redirection</title>
<author>
<name>haui</name>
</author>
<link rel="alternate" type="text/html" href="http://pdes-net.org/x-haui/archives/2011/06/22/advanced_io_redirection/index.html" />
<id>http://pdes-net.org/x-haui/archives/2011/06/22/advanced_io_redirection/index.html</id>
<published>2011-06-22T00:47:58+02:00</published>
<updated>2011-06-22T00:47:58+02:00</updated>
<category term="linux" />
<category term="scripts" />
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<![CDATA[<p>Recently I had to commit a bunch of changes via SVN. Cause it's really recommended to review all changes made in the working directory
<b>before</b> actually committing the data, I issued a <b>svn status | grep ^M</b> to see all files that have been modified
since the last commit. The result was a fairly long list of files and I wanted to check which changes where actually
made to each individual file. Of course, every SVN user knows about <b>svn diff</b> or even better
 <b>svn diff | <a href='http://pdes-net.org/cobra/archives/2009/08/30/less_is_not_more/index.html'>less</a></b>,
which gives a complete diff of all modified files.<br />
However, I don't really like this output...it just glues diff after diff together an if you scroll too fast, you will miss one or 
more small but important changes. That's why i wanted to have a mechanism, that shows one diffed file at a time until 
I explicitly  proceed to the next file. My first approach was a simple one-liner: <br />
<pre>svn status | grep ^M | awk '{print $2}' | while read l; do echo "****** $l ******"; svn diff "$l" ; read tmp; done</pre>
As you will notice, this doesn't really work - the two read commands take turns in reading the output of <b>svn status</b>.
One elegant solution for this includes the use of the shell builtin exec:
<pre>#!/bin/bash
exec 3<&0
svn status | grep ^M | awk '{print $2}' | while read l; do echo "****** $l ******"; svn diff "$l" | less ; read tmp <&3 ;done
</pre>
The line following the shebang creates a copy of the current
<a href='http://en.wikipedia.org/wiki/Standard_streams#Standard_input_.28stdin.29'>stdin</a>
(filehandle 0) and assigns it to a new filehandle 3 i.e. 0 and 3 both will read commands from the keyboard being the default in a 
newly created shell. In the next line filehandle 0 is redirected several times (remember: <b>a | b</b> redirects the stdout of a 
into the stdin of b), so the first <b>read</b> reads its lines from the awk command. The second read, however, reads its input from 
filehandle 3 which still has the value that filehandle 0 had in the beginning of the script, i.e. it reads the keyboard input 
(I also piped svn diff through less, but that's just a small enhancement which  is unrelated to the main problem).<br />
This is just a simple example for the powers of bash's redirection, <a href='http://tldp.org/LDP/abs/html/ioredirintro.html'>more complex ones</a> do exist ;)
</p>

  ]]>
</div>
</content>
</entry>
<entry>
<title type="html">Flashplayer issues</title>
<author>
<name>haui</name>
</author>
<link rel="alternate" type="text/html" href="http://pdes-net.org/x-haui/archives/2011/01/09/flashplayer_issues/index.html" />
<id>http://pdes-net.org/x-haui/archives/2011/01/09/flashplayer_issues/index.html</id>
<published>2011-01-09T17:59:28+02:00</published>
<updated>2011-01-09T17:59:28+02:00</updated>
<category term="linux" />
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<![CDATA[
<p>While older versions of Adobe's Flashplayer for Linux made content like Youtube videos accessible via 
the /tmp filesystem, the latest versions hide these files from the user by exploiting a feature of 
<a href="http://linux.die.net/man/2/unlink">unlink</a>:
<pre>If the name was the last link to a file but any processes
still have the file open the file will remain in existence until
the last file descriptor referring to it is closed.</pre>
In other words, the flashplayer creates a new file in /tmp, deletes the file right away with unlink but keeps the 
filehandle open, so the flashplayer process may still access the file. </br>
This however, may lead to confusion - <b>df</b> reveals that the free space on /tmp is shrinking, while <b>du</b> 
doesn't show any growing files at all.</br>
One way to fix this issue is simple - use library preloading to overwrite the original unlink function used by firefox:
Download <a href="http://pdes-net.org/x-haui/scripts/misc/unlink.tgz">the tgz-archive</a>, unpack it and <b>make</b> it. If the previous 
steps were successful, you should now have a file <b>unlink.so</b> available. The last step is to tell firefox 
(or more precisely the <a href="http://en.wikipedia.org/wiki/Dynamic_linker">dynamic linker</a>) to use the unlink 
function from this file rather than the one from your C Standard library: </br>
<pre>LD_PRELOAD=/path/to/unlink.so firefox</pre>
The LD_PRELOAD environment variable tells the dynamic linker to search for libraries in non-standard locations - in this case in 
our library file unlink.so. You might want to add an alias like the following to your environment, but for obvious reasons you shouldn't globally export LD_PRELOAD.</br>
<pre>alias ff="LD_PRELOAD=/path/to/unlink.so firefox"</pre>
</br>
</br>
Yet there is one drawback with this solution: even if you close firefox, the files in /tmp will persist, so you may want to delete them manually from time to time...]]>
</div>
</content>
</entry>
<entry>
<title type="html">Some lesser-known Bash tricks</title>
<author>
<name>haui</name>
</author>
<link rel="alternate" type="text/html" href="http://pdes-net.org/x-haui/archives/2010/12/17/some_lesser-known_bash_tricks/index.html" />
<id>http://pdes-net.org/x-haui/archives/2010/12/17/some_lesser-known_bash_tricks/index.html</id>
<published>2010-12-17T19:18:28+02:00</published>
<updated>2010-12-17T19:18:28+02:00</updated>
<category term="linux" />
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<![CDATA[<p>Though alternatives like the zsh exist, the <b>B</b>ourne-<b>a</b>gain <b>sh</b>ell is still the de facto standard among all Unix shells. Maybe that's why some people refer 
to it as the Windows of the shells - although there are now better alternatives around, 
most users still stick with it. I am one of these users - that's why today's blog entry is
about some useful, but little-known bash features. ;-)<br>
Note that you'll need at least Bash v4.0 for some of them.<br> 
The Bash built-in <b>shopt</b> allows you to (de)activate various variables in order to
control optional shell behavior. <b>shopt</b> called without an argument gives you an overview
of all available options. To activate a feature, simply issue <b>shopt -s OPTION</b> - 
if you'd like to deactivate the feature again, a <b>shopt -u OPTION</b> suffices. If you wonder
what's so tricky about this, just read on - basic knowledge of <b>shopt</b> is needed to
benefit from the following. <br>

Everybody knows about the extremely useful for-loop, which allows it to perform the same
command for all (or a subset of all) files in a directory. Its syntax is pretty much 
straightforward:
<pre>for file in *; do echo "Touching $file"; touch "$file"; done</pre>
This will touch every file in the current directory and tell you about it (not really 
a real world example, but you might get the point).
However sometimes you'd like to also work on the files in all subdirectories - two popular
solutions for this include the find command or recursion. 
As a rule, most users forget/don't know about the Bash's <b>globstar</b> option. If set 
(<b>shopt -s globstar</b>), you may use the following construct to also touch the files
found in all subdirectories.
<pre>for file in **/*; do echo "Touching $file"; touch "$file"; done</pre>
Just want to touch all mp3 files? Here you go:
<pre>for file in **/*.mp3; do echo "Touching $file"; touch "$file"; done</pre>
<br>
Considering the previous example, you may notice that globbing doesn't include
hidden files - which in most cases makes sense. Nevertheless, you can alter this standard
behavior, by enabling the <b>dotglob</b> option using <b>shopt</b>
<br>
<br>
While the above examples mostly cover batch processing, some options only influence
the interactive shell usage. If <b>cdspell</b> is set, the bash will generously 
ignore spelling mistakes in the directory component of a <b>cd</b> command:
<pre>user@host /var/tmp $ mkdir example_
user@host /var/tmp $ cd example
-bash: cd: example: No such file or directory
user@host /var/tmp $ shopt -s cdspell
user@host /var/tmp $ cd example
example_
user@host /var/tmp/example_ $
</pre>

<b>autocd</b> does a very similar job - if you issue a valid directory name without the 
prepended <b>cd</b>, you will automatically change to that directory. 
<br><br>
These are just some of the available options - <b>man bash</b> knows and explains them all,
so start reading.... ;-)

</p>]]>
</div>
</content>
</entry>
<entry>
<title type="html">Discontinuation of yaydl</title>
<author>
<name>haui</name>
</author>
<link rel="alternate" type="text/html" href="http://pdes-net.org/x-haui/archives/2010/11/27/discontinuation_of_yaydl/index.html" />
<id>http://pdes-net.org/x-haui/archives/2010/11/27/discontinuation_of_yaydl/index.html</id>
<published>2010-11-27T00:54:06+02:00</published>
<updated>2010-11-27T00:54:06+02:00</updated>
<category term="news" />
<category term="projects" />
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<![CDATA[<p>
If you're one of the few yaydl users out there, you might have noticed that I didn't put too much effort in the project recently. As I don't see any chance of maintaining yaydl in an appropriate way over the next months, I decided to discontinue the whole thing. Feel free to use the script as long as it works for you, but please don't email me any bug reports or the like.<br><br>
If you're looking for an alternative, I suggest you take a closer look at <a href="http://code.google.com/p/clive/">clive</a> or <a href="http://rg3.github.com/youtube-dl/">youtube-dl</a>.
<br>
</p>]]>
</div>
</content>
</entry>
<entry>
<title type="html">Vim tips</title>
<author>
<name>haui</name>
</author>
<link rel="alternate" type="text/html" href="http://pdes-net.org/x-haui/archives/2010/08/18/vim_tips/index.html" />
<id>http://pdes-net.org/x-haui/archives/2010/08/18/vim_tips/index.html</id>
<published>2010-08-18T15:53:02+02:00</published>
<updated>2010-08-18T15:53:02+02:00</updated>
<category term="vim" />
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<![CDATA[<p>Almost 4PM...time for some vim tips. :-)
<ul>
<li><b>autocmd</b><br />
Vim's powerful <i>autocmd</i> feature can be used to automatically 
perform certain commands when a specific event occurs. The events that can be used as triggers 
range from creating a new file to resizing vim's window. A complete list of available triggers
 can be obtained by typing <i>:help autocmd-events</i> in vim. So, how's this useful?<br />
Let's say you write most of your Perl scripts in vim, why should <b>you</b> insert the shebang and some other stuff 
 manually in a new file, when the editor can do this for you? The following two steps show you how it's done:
<ol><li>
Create a new file ~/.vim/skeletons/skeleton.pl containing a shebang for Perl 
as well as the recommended use strict/warnings statements:
<pre>
p=$(which perl); mkdir -p ~/.vim/skeletons; cat << EOF > ~/.vim/skeletons/skeleton.pl
#!$p
use strict;
use warnings;
EOF
</pre>
</li>
<li>
Put the following in your ~/.vimrc
<pre>autocmd BufNewFile *.pl 0r ~/.vim/skeletons/skeleton.pl | :normal G</pre>
</li>
</ol>
Now, when you're creating a new *.pl-file it is automatically prepended with the contents of ~/.vim/skeletons/skeleton.pl and vim starts at the end of the file.
Needless to say, that you can use multiple autocmd commands to support languages other than Perl.
</li>
<br />

<li><b>Syntax check</b><br />
Everyone knows about vim's :make command, but did you know that it's possible to set the 
make program for each file type separately?
<pre>autocmd FileType perl set makeprg=perl\ -c\ %\ $*</pre>
By adding this to your ~/.vimrc, :make will no longer invoke <i>make file</i> but <i>perl -c file</i> instead, when you're editing a Perl script. As usual, Perl is just an example - i.e. Ruby programmers might use <i>ruby -c</i> or the like.
</li>

<br />
<li><b>Y?</b><br />
There's some inconsistency between deleting and yanking in vim: <br />
<i>dd</i> deletes the current line, <i>D</i> deletes from the cursor to the end of the line.<br />
<i>yy</i> yanks the current line, but <i>Y</i> also yanks the current line...<br />
To yank all characters from the cursor position to the end of the line, you either need to type <i>y$</i>, or
add a custom mapping for <i>Y</i> to your ~/.vimrc:
<pre>map Y y$</pre>
</li>

<br />
<li><b>Matchit</b></li>
Typing % in normal mode finds the next item in the current line or under the cursor and jumps to its match. 
Items include c-style comments, parenthesis and some preprocessor statements. Unfortunately,
 there's no native support for HTML or Latex, but there's a handy little plugin,
 that adds support for these and many other languages: 
<a href="http://www.vim.org/scripts/script.php?script_id=39">Matchit</a>.
</li>
</ul>

Enough for one day....]]>
</div>
</content>
</entry>
<entry>
<title type="html">yaydl 1.5.2</title>
<author>
<name>haui</name>
</author>
<link rel="alternate" type="text/html" href="http://pdes-net.org/x-haui/archives/2010/07/24/yaydl_1_5_2/index.html" />
<id>http://pdes-net.org/x-haui/archives/2010/07/24/yaydl_1_5_2/index.html</id>
<published>2010-07-24T17:10:36+02:00</published>
<updated>2010-07-24T17:10:36+02:00</updated>
<category term="projects" />
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<![CDATA[<p><a href="${BASE_URL}yaydl.html">yaydl</a> 1.5.2 fixes the support for youtube....<br>
<div class="download"><a href= "http://pdes-net.org/x-haui/scripts/perl/yaydl_youtubedownloader/yaydl-1.5.2.tar.gz">Download the tar.gz</a>
<p>]]>
</div>
</content>
</entry>
<entry>
<title type="html">Bandwidth monitors</title>
<author>
<name>haui</name>
</author>
<link rel="alternate" type="text/html" href="http://pdes-net.org/x-haui/archives/2010/07/20/bandwidth_monitors/index.html" />
<id>http://pdes-net.org/x-haui/archives/2010/07/20/bandwidth_monitors/index.html</id>
<published>2010-07-20T22:08:42+02:00</published>
<updated>2010-07-20T22:08:42+02:00</updated>
<category term="linux" />
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<![CDATA[<p>There are many tools available, that allow you to monitor (among other things) the current downstream of your internet connection. Some of them, like <a href="http://dag.wieers.com/home-made/dstat/">dstat</a> and <a href="http://www.gropp.org/?id=projects&sub=bwm-ng">bwm-ng</a> are handy console applications, whereas others integrate nicely into your desktop. Two popular examples for this would be <a href="http://conky.sourceforge.net/">conky</a> or <a href="http://members.dslextreme.com/users/billw/gkrellm/gkrellm.html">gkrellm<a>.<br />
So, in general there's no real need for the following bash one-liner, unless you're just an ordinary user working on some poorly equipped linux box which doesn't offer any of the tools mentioned above. In that case, you'll be glad to have a dirty solution like the following available:
<pre>r=$(cat /sys/class/net/eth0/statistics/rx_bytes) ; while [ 1 ]; do n=$(cat /sys/class/net/eth0/statistics/rx_bytes); d=$(((n-r) / 1024 ));r=$n; echo "$d KB/s"; sleep 1;done</pre>
<br />
There is no need to mention,that <i>eth0</i> must be replaced by your primary interface's name.]]>
</div>
</content>
</entry>
</feed>

