How to manipulate packages with dpkg (Enabling Muilti-Arch in Multi-Arch support) ?

$dpkg --print-architecture
amd64
$dpkg --print-foreign-architectures
$dpkg --add-architecture i386
dpkg: error: unable to create new file '/var/lib/dpkg/arch-new': Permission denied
$sudo dpkg --add-architecture i386
[sudo] password for jeffrin: 
$dpkg --print-foreign-architectures
i386
$sudo dpkg --add-architecture itest386
$dpkg --print-foreign-architectures
i386
itest386
$dpkg --remove-architecture itest386
dpkg: error: unable to create new file '/var/lib/dpkg/arch-new': Permission denied
$sudo dpkg --remove-architecture itest386
$dpkg --print-foreign-architectures
i386
$sudo dpkg --remove-architecture i386
$dpkg --print-foreign-architectures
$

How to write a program to solve project Euler problem 4

'''
This file is worked on from http://www.s-anand.net/euler.html , Solution of Problem 4 

A palindromic number reads the same both ways. The largest palindrome made from the product 
of two 2-digit numbers is 9009 = 91 x 99.

Find the largest palindrome made from the product of two 3-digit numbers.

'''

n = 0

for a in xrange(999,100,-1):
    for b in xrange(a,100,-1):
        x = a * b
        if ( x > n ):
            s = str( a * b )
            if s == s[::-1]:
                n = a * b
print n

Hacking with runit and sv ( sv – control and manage services monitored by runsv(8) ) commands

ABOUT runit

runit is a cross-platform Unix init scheme with service supervision, a replacement for sysvinit, and other init schemes. It runs on GNU/Linux, *BSD, MacOSX, Solaris, and can easily be adapted to other Unix operating systems

ABOUT sv

The sv program reports the current status and controls the state of services monitored by the runsv(8) supervisor.
services consists of one or more arguments, each argument naming a directory service used by runsv(8). If service doesn’t start with a dot or slash and doesn’t end with a slash, it is searched in the default services directory /service/, otherwise relative to the current directory.

A TYPICAL SHELL EXPOSURE
[bash]
$pwd
/etc/service
$ls
hello.sh
$mkdir new
mkdir: cannot create directory ‘new’: Permission denied
$sudo mkdir new
$mv hello.sh new/run
mv: cannot move ‘hello.sh’ to ‘new/run’: Permission denied
$sudo mv hello.sh new/run
$ls
new
$cd new/
$ls
run supervise
$ls -l
total 8
-rw-r–r– 1 root root 11 Oct 24 18:55 run
drwx—— 2 root root 4096 Oct 24 18:57 supervise
$cd supervise/
bash: cd: supervise/: Permission denied
$sudo cd supervise/
sudo: cd: command not found
$sudo sv start /etc/service/new
timeout: down: /etc/service/new: 1s, normally up, want up
$sudo sv status /etc/service/new
down: /etc/service/new: 0s, normally up, want up
$sudo sv restart /etc/service/new
timeout: down: /etc/service/new: 0s, normally up, want up
$sudo emacs /etc/service/new/run
$sudo sv restart /etc/service/new
timeout: down: /etc/service/new: 0s, normally up, want up
$sudo sv stop /etc/service/new
ok: down: /etc/service/new: 0s, normally up, want up
$sudo chmod +x /etc/service/new/run
$sudo sv start /etc/service/new
timeout: down: /etc/service/new: 0s, normally up, want up
$sudo sv start /etc/service/new/
timeout: down: /etc/service/new/: 1s, normally up, want up
$su
Password:
root>sv start /etc/service/new/
run run~ supervise/
root>sv start /etc/service/new/run
fail: /etc/service/new/run: unable to change to service directory: not a directory
root>sv start /etc/service/new/
timeout: down: /etc/service/new/: 1s, normally up, want up
root>exit
$echo $?
1
$sudo sv stop /etc/service/new/
ok: down: /etc/service/new/: 1s, normally up, want up
$echo $?
0
$sudo runit
– runit: fatal: must be run as process no 1.
$sudo runit enable
– runit: fatal: must be run as process no 1.
$

[/bash]
TYPICAL SOURCE CODE EXPOSURE
[c]

int main(int argc, char **argv) {
unsigned int i, done;
char *x;

progname =*argv;
for (i =str_len(*argv); i; –i) if ((*argv)[i -1] == ‘/’) break;
*argv +=i;
optprogname =progname =*argv;
service =argv;
services =1;
lsb =(str_diff(progname, "sv"));
if ((x =env_get("SVDIR"))) varservice =x;
if ((x =env_get("SVWAIT"))) scan_ulong(x, &wait);
while ((i =getopt(argc, (const char* const*)argv, "w:vV")) != opteof) {
switch(i) {
case ‘w’: scan_ulong(optarg, &wait);
case ‘v’: verbose =1; break;
case ‘V’: strerr_warn1(VERSION, 0);
case ‘?’: usage();
}
}
argv +=optind; argc -=optind;
if (!(action =*argv++)) usage(); –argc;
if (!lsb) { service =argv; services =argc; }
if (!*service) usage();

taia_now(&tnow); tstart =tnow;
if ((curdir =open_read(".")) == -1)
fatal("unable to open current directory");

act =&control; acts ="s";
if (verbose) cbk =✓
switch (*action) {
case ‘x’: case ‘e’:
acts ="x"; break;
case ‘X’: case ‘E’:
acts ="x"; kll =1; cbk =✓ break;
case ‘D’:
acts ="d"; kll =1; cbk =✓ break;
case ‘T’:
acts ="tc"; kll =1; cbk =✓ break;
case ‘t’:
if (!str_diff(action, "try-restart")) { acts ="tc"; cbk =✓ break; }
case ‘c’:
if (!str_diff(action, "check")) { act =0; acts ="C"; cbk =✓ break; }
case ‘u’: case ‘d’: case ‘o’: case ‘p’: case ‘h’:
case ‘a’: case ‘i’: case ‘k’: case ‘q’: case ‘1’: case ‘2’:
action[1] =0; acts =action; break;
case ‘s’:
if (!str_diff(action, "shutdown")) { acts ="x"; cbk =✓ break; }
if (!str_diff(action, "start")) { acts ="u"; cbk =✓ break; }
if (!str_diff(action, "stop")) { acts ="d"; cbk =✓ break; }
if (lsb && str_diff(action, "status")) usage();
act =&status; cbk =0; break;
case ‘r’:
if (!str_diff(action, "restart")) { acts ="tcu"; cbk =✓ break; }
if (!str_diff(action, "reload")) { acts ="h"; cbk =✓ break; }
usage();
case ‘f’:
if (!str_diff(action, "force-reload"))
{ acts ="tc"; kll =1; cbk =✓ break; }
if (!str_diff(action, "force-restart"))
{ acts ="tcu"; kll =1; cbk =✓ break; }
if (!str_diff(action, "force-shutdown"))
{ acts ="x"; kll =1; cbk =✓ break; }
if (!str_diff(action, "force-stop"))
{ acts ="d"; kll =1; cbk =✓ break; }
default:
usage();
}

servicex =service;
for (i =0; i < services; ++i) {
if ((**service != ‘/’) && (**service != ‘.’) && **service &&
((*service)[str_len(*service) -1] != ‘/’)) {
if ((chdir(varservice) == -1) || (chdir(*service) == -1)) {
fail("unable to change to service directory");
*service =0;
}
}
else
if (chdir(*service) == -1) {
fail("unable to change to service directory");
*service =0;
}
if (*service) if (act && (act(acts) == -1)) *service =0;
if (fchdir(curdir) == -1) fatal("unable to change to original directory");
service++;
}

[/c]

SOURCE CODE TAKEN FROM DEBIAN SOURCE PACKAGE NAMED runit

RELATED LINKS
http://smarden.org/runit/
http://smarden.org/runit/sv.8.html
https://en.wikipedia.org/wiki/Runit
https://docs.ansible.com/ansible/2.5/modules/runit_module.html

Project Euler Problem 2, solution internals related using ruby debugger

$ruby -rdebug euler-two.rb 
Debug.rb
Emacs support available.

/usr/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:57:        RUBYGEMS_ACTIVATION_MONITOR.enter
(rdb:1) r
/usr/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:57:        RUBYGEMS_ACTIVATION_MONITOR.enter
(rdb:1) 
r
/usr/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:57:        RUBYGEMS_ACTIVATION_MONITOR.enter
(rdb:1) next
/usr/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:143:    RUBYGEMS_ACTIVATION_MONITOR.exit
(rdb:1) next
euler-two.rb:3:n1, n2 = 1, 2
(rdb:1) next
euler-two.rb:4:sum = 0
(rdb:1) print n1
1nil
(rdb:1) print n2
2nil
(rdb:1) next
euler-two.rb:6:while n2 < 4000000
(rdb:1) print n2
2nil
(rdb:1) next
euler-two.rb:7:sum += (n2 % 2 == 0) ? n2 : 0
(rdb:1) next
euler-two.rb:7:sum += (n2 % 2 == 0) ? n2 : 0
(rdb:1) print sum
0nil
(rdb:1) next
euler-two.rb:8:n1, n2 = n2, (n1 + n2) # iterate through fibonacci seq
(rdb:1) print sum
2nil
(rdb:1) next
euler-two.rb:8:n1, n2 = n2, (n1 + n2) # iterate through fibonacci seq
(rdb:1) next
euler-two.rb:7:sum += (n2 % 2 == 0) ? n2 : 0
(rdb:1) next
euler-two.rb:7:sum += (n2 % 2 == 0) ? n2 : 0
(rdb:1) print sum
2nil
(rdb:1) next
euler-two.rb:8:n1, n2 = n2, (n1 + n2) # iterate through fibonacci seq
(rdb:1) print sum
2nil
(rdb:1) continue
/usr/lib/ruby/2.1.0/debug.rb:290:in `eval':undefined local variable or method `continue' for main:Object
	from /usr/lib/ruby/2.1.0/debug.rb:290:in `rescue in debug_eval'
	from /usr/lib/ruby/2.1.0/debug.rb:287:in `debug_eval'
	from /usr/lib/ruby/2.1.0/debug.rb:661:in `block in debug_command'
	from /usr/lib/ruby/2.1.0/debug.rb:400:in `catch'
	from /usr/lib/ruby/2.1.0/debug.rb:400:in `debug_command'
	from /usr/lib/ruby/2.1.0/debug.rb:859:in `trace_func'
	from /usr/lib/ruby/2.1.0/debug.rb:1085:in `block in '
	from euler-two.rb:8:in `'
(rdb:1) puts

nil
(rdb:1) puts sum
2
nil
(rdb:1) puts n1
2
nil
(rdb:1) puts n2
3
nil
(rdb:1) quit
Really quit? (y/n) y
$

Project Euler Problem 3, solution internals using python debugger

$python -m pdb euler-three.py 
> /home/jeffrin/beautifulwork/lib/euler-three.py(7)()
-> '''
(Pdb) r
6857
--Return--
> /home/jeffrin/beautifulwork/lib/euler-three.py(17)()->None
-> print n
(Pdb) next
--Return--
> (1)()->None
(Pdb) next
The program finished and will be restarted
> /home/jeffrin/beautifulwork/lib/euler-three.py(7)()
-> '''
(Pdb) next
> /home/jeffrin/beautifulwork/lib/euler-three.py(9)()
-> n = 600851475143
(Pdb) next
> /home/jeffrin/beautifulwork/lib/euler-three.py(11)()
-> i = 2
(Pdb) next
> /home/jeffrin/beautifulwork/lib/euler-three.py(12)()
-> while i * i  /home/jeffrin/beautifulwork/lib/euler-three.py(13)()
-> while n % i == 0:
(Pdb) print n
600851475143
(Pdb) print n % i
1
(Pdb) next
> /home/jeffrin/beautifulwork/lib/euler-three.py(15)()
-> i = i + 1
(Pdb) print i
2
(Pdb) next
> /home/jeffrin/beautifulwork/lib/euler-three.py(12)()
-> while i * i  /home/jeffrin/beautifulwork/lib/euler-three.py(13)()
-> while n % i == 0:
(Pdb) print n % i
2
(Pdb) next
> /home/jeffrin/beautifulwork/lib/euler-three.py(15)()
-> i = i + 1
(Pdb) continue
6857
The program finished and will be restarted
> /home/jeffrin/beautifulwork/lib/euler-three.py(7)()
-> '''
(Pdb) 

Project Euler Problem 2, solution internals using python debugger

$python -m pdb project-euler-2.py 
> /home/jeffrin/beautifulwork/lib/project-euler-2.py(13)()
-> '''
(Pdb) next
> /home/jeffrin/beautifulwork/lib/project-euler-2.py(15)()
-> cache = {}
(Pdb) next
> /home/jeffrin/beautifulwork/lib/project-euler-2.py(16)()
-> def fib(n):
(Pdb) next
> /home/jeffrin/beautifulwork/lib/project-euler-2.py(20)()
-> n = 0
(Pdb) next
> /home/jeffrin/beautifulwork/lib/project-euler-2.py(21)()
-> i = 0
(Pdb) next
> /home/jeffrin/beautifulwork/lib/project-euler-2.py(22)()
-> while fib(i)  /home/jeffrin/beautifulwork/lib/project-euler-2.py(23)()
-> if not fib(i) % 2: n = n + fib(i)
(Pdb) next
> /home/jeffrin/beautifulwork/lib/project-euler-2.py(24)()
-> i = i + 1
(Pdb) next
> /home/jeffrin/beautifulwork/lib/project-euler-2.py(22)()
-> while fib(i)  /home/jeffrin/beautifulwork/lib/project-euler-2.py(23)()
-> if not fib(i) % 2: n = n + fib(i)
(Pdb) next
> /home/jeffrin/beautifulwork/lib/project-euler-2.py(24)()
-> i = i + 1
(Pdb) continue
4613732
The program finished and will be restarted
> /home/jeffrin/beautifulwork/lib/project-euler-2.py(13)()
-> '''
(Pdb) 

http://www.bytemuse.com/post/fibonacci-spiral/

GDB backtrace For chromium hangup

(gdb) bt
#0  pthread_cond_timedwait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_timedwait.S:238
#1  0x00007fb512be9443 in ?? ()
#2  0x00007fb512bc9d0d in ?? ()
#3  0x00007fb512bacb0f in ?? ()
#4  0x00007fb512bbc7d1 in ?? ()
#5  0x00007fb512baa03a in ?? ()
#6  0x00007fb51537aea8 in ?? ()
#7  0x00007fb512b7d045 in ?? ()
#8  0x00007fb512b7de1e in ?? ()
#9  0x00007fb512b7cc30 in ?? ()
#10 0x00007fb512569698 in ChromeMain ()
#11 0x00007fb50a541b45 in __libc_start_main (main=0x7fb512569650 , argc=4, argv=0x7fffb310c918, 
    init=, fini=, rtld_fini=, stack_end=0x7fffb310c908) at libc-start.c:287
#12 0x00007fb512569545 in _start ()

How to write a program to multiply a number by 4 using a bitwise operator ?

/* Part of this program copied from http://www.sanfoundry.com/c-program-multiply-number-4-using-bitwise-operators/ */

#include <stdio.h>

multbitwise()
{

long number;
printf("Enter an integer: ");
scanf("%ld",&number);
number = number << 2;
printf("Result is %ld \n",number);
}

/* http://stackoverflow.com/questions/4456442/interview-multiplication-of-2-integers-using-bitwise-operators */
/* http://www.geeksforgeeks.org/multiply-two-numbers-without-using-multiply-division-bitwise-operators-and-no-loops/ */
/* http://en.wikipedia.org/wiki/Bitwise_operation */

Hacking with conversions of decimal and binary

$./app 
Sign detection [1] 
Power of two [2] 
Counting No. of bits set [3] 
Set or clear bits without branching[4] 
Find maximum value[5] 
Finding least common multiple[6] 
Finding the greatest commom divisor[7] 
Finding if a number is an Armstrong number or not[8] 
Finding if a given number is prime number[9]  
Finding the number of twin prime numbers[10] 
Finding if a number is palindrome or not[11] 
Finding prime factor of a number[12] 
Finding the value of M^N[13] 
Finding factorial of a number[14] 
Finding the sum of the series 1! + 2! +...+N! [15] 
Finding the sume of the series 1 + 2 +...+N [16] 
Finding Parity of an Integer [17] 
Finding the biggest among three numbers [18] 
Finding the sum of even and odd numbers up to a number N [19] 
Finding the sum and number of integers divisible by 5 [20] 
Swapping Values [21] 
Finding the decimal value for a binary [22] 
Finding the reverse of an integer [23] 
Finding a way for user authentication [24]: 
Finding the binary equivalent of a decimal number and count no. of 1's in binary number [25] 
Option: 25
Enter a decimal integer 
25
 Input number is 25
 It's binary equivalent is 11001 
No. of 1s in the binary number is 3
$./app 
Sign detection [1] 
Power of two [2] 
Counting No. of bits set [3] 
Set or clear bits without branching[4] 
Find maximum value[5] 
Finding least common multiple[6] 
Finding the greatest commom divisor[7] 
Finding if a number is an Armstrong number or not[8] 
Finding if a given number is prime number[9]  
Finding the number of twin prime numbers[10] 
Finding if a number is palindrome or not[11] 
Finding prime factor of a number[12] 
Finding the value of M^N[13] 
Finding factorial of a number[14] 
Finding the sum of the series 1! + 2! +...+N! [15] 
Finding the sume of the series 1 + 2 +...+N [16] 
Finding Parity of an Integer [17] 
Finding the biggest among three numbers [18] 
Finding the sum of even and odd numbers up to a number N [19] 
Finding the sum and number of integers divisible by 5 [20] 
Swapping Values [21] 
Finding the decimal value for a binary [22] 
Finding the reverse of an integer [23] 
Finding a way for user authentication [24]: 
Finding the binary equivalent of a decimal number and count no. of 1's in binary number [25] 
Option: 22
Enter a binary number : 11001
The binary number is : 11001 
It's decimal equivalent is:  25 
$