selfjungle Just another WordPress weblog

19Jul/110

perl regexp – threat whole string as one line

s - Treat the whole string as one line, so that even /./ will match a "newline" character.

#!/usr/bin/perl

my $multiline =
  "In the town where I was born,\n" .
  "Lived a man who sailed to sea,\n" .
  "And he told us of his life,\n" .
  "In the land of submarines.";

if ($multiline =~ /born,.Lived/s) {
  print "found\n";   # found in deed
} else {
  print "not found\n"; 
}
Tagged as: No Comments
19Jul/110

xterm colors

The blue is too dark for my eyes, specially then it's used by colorgcc to highlight line numbers.

...and I just can't get angry with a cheerful orange cursorColor 😀

~./Xdefaults:

xterm*foreground:       #ffffff
xterm*background:       #000000
xterm*cursorColor:      orange
xterm*color4:           #526fcf
Tagged as: No Comments
19Jul/110

find and remove files

With rm and find:

rm $(find . -name *.gcda)

Or with the -exec paramter of find

find . -name *.gcda -exec rm -rf {} \;
Tagged as: No Comments
18Jul/110

git tag

Tag commit:

git tag <tagname> -m <msg> #lightweight
git tag -a <tagname> -m <msg> #annotated, preferred

Push tags (not done by default):

git push --tags <remote> <branch> #dangerous, avoid pushing all tags!
git push  <remote>  <tagname>

Fetch tags

git fetch --tags <remote> <branch> #or
git remote update <remote> 

Look up which branch has the tag

git branch --all --contains <tagname>

delete tags remote and local:

git push --delete origin <tagname>
git tag -d  <tagname>
Tagged as: No Comments
18Jul/110

choose default answers at make oldconfig

Linux command yes outputs parameter string.

yes "" | make oldconfig
Tagged as: No Comments
19Mar/110

Stringizing Operator, Token concatenation

After many years, even C shows new stuff:

Stringizing Operator

#define QUOTEME(x) #x

According to wiki:

Although macro expansion does not occur within a quoted string, the text of the macro arguments can be quoted and treated as a string literal by using the "#" directive (also known as the "Stringizing Operator").

the code

printf("%s\n", QUOTEME(1+2));

will expand to

printf("%s\n", "1+2");

Token concatenation

Token concatenation, also called token pasting, is one of the most subtle — and easy to abuse — features of the C macro preprocessor. Two arguments can be 'glued' together using ## preprocessor operator; this allows two tokens to be concatenated in the preprocessed code. This can be used to construct elaborate macros which act like a crude version of C++ templates.

For instance:

#define MYCASE(item,id) \
case id: \
  item##_##id = id;\
break

the code

MYCASE(widget,23);

will expand to

widget_23 = 23;
Tagged as: No Comments
4Mar/110

create signature (of hash) with openssl c api

This is just too long...

You can find another version where all return value is checked here

EDIT: There is another solution, with SHA256_x to create digest and RSA_sign to sign here

/*  gcc ./openssl_sign.c -lssl */

#include s<tdio.h>
#include <string.h>
#include <error.h>

#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include o<penssl/evp.h>

int pass_cb( char *buf, int size, int rwflag, void *u )
{
  int len;
  char tmp[1024];
  printf( "Enter pass phrase for '%s': ", (char*)u );
  scanf( "%s", tmp );
  len = strlen( tmp );

  if ( len <= 0 ) return 0;
  if ( len > size ) len = size;

  memset( buf, '\0', size );
  memcpy( buf, tmp, len );
  return len;
}

RSA* getRsaFp( const char* rsaprivKeyPath )
{
  FILE* fp;
  fp = fopen( rsaprivKeyPath, "r" );
  if ( fp == 0 ) {
    fprintf( stderr, "Couldn't open RSA priv key: '%s'. %s\n",
             rsaprivKeyPath, strerror(errno) );
    exit(1);
  }

  RSA *rsa = 0;
  rsa = RSA_new();
  rsa = PEM_read_RSAPrivateKey(fp, 0, pass_cb, (char*)rsaprivKeyPath);
  fclose( fp );
  return rsa;
}


int main( int argc, char* argv[] )
{
  if ( argc != 2 ) {
    fprintf( stderr, "Usage: %s <text to sign>\n", argv[0] );
    exit( 1 );
  }
  const char *clearText = argv[1];

  char rsaprivKeyPath[1024];
  sprintf( rsaprivKeyPath, "%s/.ssh/id_rsa",  getenv ("HOME") );

  SSL_load_error_strings();

  OpenSSL_add_all_algorithms();
  OpenSSL_add_all_ciphers();
  OpenSSL_add_all_digests();


  EVP_PKEY *evpKey = 0;
  evpKey = EVP_PKEY_new();

  RSA *rsa = 0;
  rsa = getRsaFp( rsaprivKeyPath );

  EVP_PKEY_set1_RSA( evpKey, rsa );

  EVP_MD_CTX* ctx = 0;
  ctx = EVP_MD_CTX_create();
  EVP_SignInit_ex( ctx, EVP_sha1(), 0 );
  EVP_SignUpdate( ctx, clearText, strlen( clearText ) );

  const int MAX_LEN = 1024;
  unsigned char sig[MAX_LEN];
  unsigned int sigLen;
  memset(sig, 0, MAX_LEN);

  EVP_SignFinal( ctx, sig, &sigLen, evpKey );

  printf( "Got signature: '%s'\n", sig );

  EVP_MD_CTX_destroy( ctx );
  RSA_free( rsa );
  EVP_PKEY_free( evpKey );
  ERR_free_strings();
  return 0;
}
Tagged as: No Comments
27Feb/110

install cxxtest to gentoo

It is basically downloading the ebuild from the bgo-overlay - cxxtest and following the steps of this article at linuxreviews about how to install custom ebuilds with gentoo.

Which are in this case:

su

mkdir -p /usr/local/portage
echo PORTDIR_OVERLAY=/usr/local/portage >> /etc/portage/make.conf

mkdir -p  /usr/local/portage/metadata/
touch /usr/local/portage/metadata/layout.conf
echo "masters = gentoo" >> /usr/local/portage/metadata/layout.conf

mkdir -p /usr/local/portage/dev-util/cxxtest/
cd /usr/local/portage/dev-util/cxxtest/
wget http://gpo.zugaina.org/AJAX/Ebuild/2578437 -O cxxtest-3.10.1.ebuild
ebuild cxxtest-3.10.1.ebuild digest
emerge cxxtest
Tagged as: No Comments
20Feb/111

iwlist scan perl wrapper

The output of /sbin/iwlist scan is too much for me in most of the cases: I just want to know which WiFis are present, quality and open/passneeded state.

So here is a small perl script for it, the ESSIDs printed in descending order of quality which changed from 1-70 to 1-100.

#!/usr/bin/perl

use warnings;
use strict;

open(LIST, "/sbin/iwlist scan 2>&1 |") or die "Failed: $!\n";

my %wifis;
my $essid;
while () {
        if (/ESSID\:\"(.*)\"/) { $essid = $1; }
        elsif (/Quality=(\d*)\/70/) { $wifis{$essid}->{"quality"} = $1; }
        elsif (/Encryption key\:(\S*)/) { $wifis{$essid}->{"key"} = $1; }
}

sub by_quality {
        $wifis{$b}->{"quality"} <=> $wifis{$a}->{"quality"};
}

print "\n";
foreach $essid ( sort by_quality keys %wifis) {
        printf '%*s %*s   %-d', 30, 
                $essid, 
                6,  $wifis{$essid}->{"key"}=~/on/? "Pass" : "Open" , 
                int($wifis{$essid}->{"quality"}) / 70.0 * 100;
        print "\n";
}
print "\n";

A sample output I got at my flat:

cs0rbagomba@ramen ~ $ wifi_list 

                    gara_dlink   Pass   71
                     TP-Link01   Open   54
                           zaa   Pass   50
                         DBnet   Pass   50
                          anzo   Pass   47
                          3Com   Pass   22
                TP-LINK_9D27F4   Pass   21
                 TP-LINK_TOMEC   Pass   21
                           TNT   Pass   17
                   TimeCapsule   Pass   15
                       hpsetup   Open   14
                  Pannon Cargo   Pass   12
                       Airlive   Pass   10
                TP-LINK_DA3008   Open   10
                                 Open   8
     Szeretetre melto internet   Pass   8
                        KZSNET   Pass   8
                     CEO_iroda   Pass   8
                         Vani2   Pass   5
                      GIGABYTE   Open   4
                        csikos   Pass   2
                TP-LINK_E6395C   Pass   2
                      Dante_88   Pass   2
                        RG60SE   Open   2
                       default   Open   2
                        WIFI99   Pass   1
                         Zsoka   Pass   1
                           IKO   Pass   1

cs0rbagomba@ramen ~ $

PS: TP-Link01 Open - sharing is caring 🙂 default settings rulz

Tagged as: 1 Comment
15Feb/110

pdfnup: print 2 pages in 1 sheet

When I want to print 2 pages in 1 sheet, the contents of the pages become too small: inside the margin of the sheet, the 2 pages keep their margins too.

There is a nice tool called pdfnup (part of pdfjam), which not only help us get rid of the margin problem, but we can trim, shift and do whatever we want with a pdf document to create a more readable new one.

When I print books, most of the time this line is enogh:

pdfnup --nup 2x1 --paper a4paper --noautoscale true --outfile output.pdf input.pdf

There was only one case when some fine calibration was needed:

pdfnup --nup 2x1 --paper a4paper --trim '4.5cm 3.5cm 4.5cm 3.5cm' --outfile output.pdf input.pdf
Tagged as: No Comments