Jan 29, 2013

USB

lsusb
usb-devices

Jan 28, 2013

SSD

cat >> sysctl.conf << "EOF"
vm.swappiness=1
vm.vfs_cache_pressure=50
EOF

sudo cp sysctl.conf /etc/
And in /etc/fstab:
noatime,discard
https://wiki.archlinux.org/index.php/Solid_State_Drives
http://help.ubuntu.ru/wiki/ssd

Jan 17, 2013

C code auto formatting

indent -st na.c > na2.c

Jan 15, 2013

How to determine OS of the remote computer

Scan of a local network
nmap -O -sV 192.168.10.*
#...
#Nmap scan report for 192.168.10.100
#Host is up (0.074s latency).
#Nmap scan report for 192.168.10.102
#Host is up (0.0015s latency).
#Nmap scan report for 192.168.10.104
#Host is up (0.11s latency).
#...
Identify OS on remote host
sudo nmap -O -sV 192.168.10.20
#Starting Nmap 6.00 ( http://nmap.org ) at 2013-01-15 10:29 FET
#Nmap scan report for 192.168.10.20
#Host is up (0.00032s latency).
#Not shown: 980 closed ports
#PORT     STATE SERVICE        VERSION
#80/tcp   open  http           Microsoft HTTPAPI httpd 2.0 (SSDP/UPnP)
#89/tcp   open  http           Microsoft HTTPAPI httpd 2.0 (SSDP/UPnP)
#135/tcp  open  msrpc          Microsoft Windows RPC
#139/tcp  open  netbios-ssn
#443/tcp  open  skype2         Skype
#445/tcp  open  netbios-ssn
#912/tcp  open  vmware-auth    VMware Authentication Daemon 1.0 (Uses VNC, SOAP)
#1025/tcp open  msrpc          Microsoft Windows RPC
#1026/tcp open  msrpc          Microsoft Windows RPC
#1027/tcp open  msrpc          Microsoft Windows RPC
#1028/tcp open  msrpc          Microsoft Windows RPC
#1062/tcp open  msrpc          Microsoft Windows RPC
#1104/tcp open  memcache       memcached
#1192/tcp open  msrpc          Microsoft Windows RPC
#1248/tcp open  msrpc          Microsoft Windows RPC
#1433/tcp open  ms-sql-s       Microsoft SQL Server 2008 R2 10.50.2500; SP1
#2003/tcp open  http           Microsoft HTTPAPI httpd 2.0 (SSDP/UPnP)
#3389/tcp open  ms-wbt-server?
#8009/tcp open  ajp13          Apache Jserv (Protocol v1.3)
#8181/tcp open  http           Apache Tomcat/Coyote JSP engine 1.1
#MAC Address: 1C:6F:65:8C:34:B3 (Giga-byte Technology Co.)
#Device type: general purpose
#Running: Microsoft Windows 7|2008
#OS CPE: cpe:/o:microsoft:windows_7 cpe:/o:microsoft:windows_server_2008::sp1
#OS details: Microsoft Windows 7 or Windows Server 2008 SP1
#Network Distance: 1 hop
#Service Info: OS: Windows; CPE: cpe:/o:microsoft:windows
#
#OS and Service detection performed. Please report any incorrect results at #http://nmap.org/submit/ .
#Nmap done: 1 IP address (1 host up) scanned in 101.33 seconds
The same technique can be also used for all over the WAN remote hosts. Scanning for OS version on a remote host can be quite handy to you as an administrator. On the other hand, this technique can also be abused by hackers. They can target any host with their exploitation attack based on quite accurate information of a running OS and its patch level. Let this be just a quick reminder for all of us to keep all our systems up to date. http://how-to.linuxcareer.com/how-to-determine-os-of-the-remote-computer

Common Lisp links from Alexander Artemenko

http://dev.svetlyak.ru/link-roundup-25/

Jan 11, 2013

The Manual

man printf
# search:
man -k printf
apropos printf
# library functions section 3:
man 3 printf
Vi users can put the cursor on a word and use K to open that word’s manpage

Compiling C

-O3 indicates optimization level three, which tries every trick known to build faster code. If, when you run the debugger, you find that too many variables have been optimized out for you to follow what’s going on, then change this to -O0.
-Wall adds compiler warnings. This works for gcc, Clang, and icc. For icc, you might prefer -w1, which displays the compiler’s warnings, but not its remarks.
-Werror your compiler will treat warnings as errors.
-I adds the given path to the include search path, which the compiler searches for header files you #included in your code.
-L adds to the library search path. Order matters:
gcc -I/usr/local/include use_useful.c -o use_useful -L/usr/local/lib -luseful
If you have a file named specific.o that depends on the Libbroad library, and Libbroad depends on Libgeneral, then you will need: gcc specific.o -lbroad -lgeneral
-g adds symbols for debugging.
-std=gnu11 is gcc-specific, and specifies that gcc should allow code conforming to the C11 and POSIX standards

pkg-config
pkg-config --libs gsl libxml-2.0
# -lgsl -lgslcblas -lm -lxml2

pkg-config --cflags gsl libxml-2.0
# -I/usr/include/libxml2

gcc `pkg-config --cflags --libs gsl libxml-2.0` -o specific specific.c
# gcc -I/usr/include/libxml2 -lgsl -lgslcblas -lm -lxml2 -o specific specific.c
Runtime linking
export LD_LIBRARY_PATH=libpath:$LD_LIBRARY_PATH
# or 
LDADD=-Llibpath -Wl,-Rlibpath

Makefile

Rules:
$@
The full target filename. By target, I mean the file that needs to be built, such as a .o file being compiled from a .c file or a program made by linking .o files.
$*
The target file with the suffix cut off. So if the target is prog.o, $* is prog, and $*.c would become prog.c.
$<
The name of the file that caused this target to get triggered and made. If we are making prog.o, it is probably because prog.c has recently been modified, so $< is prog.c.

The full list of default rules and variables:
make -p > default_rules
# POSIX-standard make has a specific recipe for compiling a .o object file from a .c source code file:
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $*.c
Make template:
P=program_name
OBJECTS=
CFLAGS = -g -Wall -O3
LDLIBS=
CC=c99
$(P): $(OBJECTS)
Passing enviroment variable:
html:
    latex -interaction batchmode $(f)
    latex2html $(f).tex
...and execute:
f=tip make

Jan 10, 2013

Compiling C from stdin

allheads.h:
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <gsl/gsl_rng.h>
Paste this onto your command line, or add it to your .bashrc, .zshrc, or wherever applicable:
go_libs="-lm"
go_flags="-g -Wall -include allheads.h -O3"
alias go_c="c99 -xc '-' $go_libs $go_flags"
And now we can use a here document to paste short C programs onto the command line:
go_c << '---'
int main(){printf("Hello from the command line.\n");}
---
# and then run:
./a.out

Python shell command

Execute code:
python -c "print 'hi.'"
# or
echo "print 'hi.'" | python '-'
# equal to
echo "print 'hi.'" | python -- -
Execute multiline code:
python '-' <<"EOF"
lines=2
print "\nThis script is %i lines long.\n" %(lines,)
EOF
Using json.tool to validate and pretty-print:
curl https://app01.nutshell.com/api/v1/json | python -m json.tool

Jan 8, 2013

Python to Javascript converters

https://github.com/buchuki/pyjaco

http://pyjs.org/
pyjamas/bin/pyjscompile -o output.js source.py
--strict: this preserves maximum Python semantic compatibility (up to the point of including a copy of the source file to provide tracebacks exactly like Python does);
-O: this tries to balance Python compatibility with speed: turns off function argument checking, attribute checking, bound methods, and using classes for numbers.
--stupid-mode: produces the minimal possible code, at the cost of switching Python semantics with JS semantics.

Jan 7, 2013

Head First C - David Griffiths

Exit status of last program
echo $?

&x - location in memory of x variable

You can also call sizeof for a data type, such as sizeof(int).

You can declare a char pointer as const char * to prevent the code from using it to modify a string.

man strstr
info coreutils 'printf'

Q: But what if I want to pass negative numbers as command-line arguments like set_temperature -c -4? Won’t it think that the 4 is an option, not an argument?
A: In order to avoid ambiguity, you can split your main arguments from the options using --. So you would write set_temperature -c -- -4. getopt() will stop reading options when it sees the --, so the rest of the line will be read as simple arguments.

(./bermuda | ./geo2json) < spooky.csv > output.json

If you genuinely want to share variables, you should declare them in your header file and prefix them with the keyword extern: 
extern int passcode;

make takes away a lot of the pain of compiling files. But if you find that even it is not automatic enough, take a look at a tool called autoconf

The make tool can do far, far more than we have space to discuss here. To find out more about make and what it can do for you, visit the GNU Make Manual

Remember: when you're assigning struct variables, you are telling the computer to copy data.

If you use the typedef command, you can normally skip giving the struct a proper name. But in a recursive structure, you need to include a pointer to the same type. C syntax won't let you use the typedef alias, so you need to give the struct a proper name. That's why the struct here is called struct island.

The nm command lists the names that are stored inside the archive.
nm /usr/lib/libruby1.8-static.a

When you bind a socket to a port, the operating system will prevent anything else from rebinding to it for the next 30 seconds or so, and that includes the program that bound the port in the first place. Use reuse option (p. 477) 

MUT-EX = MUTually EXclusive.

The make tool knows quite a lot about C compilation, and it can use implicit rules to build files without you telling it exactly how.  For example, if you have a file called fred.c, you can compile it without a makefile by typing:
> make fred
cc fred.c -o fred

Purchase at Amazon

https://bitbucket.org/st1tch/head-first-c

Author repository: https://github.com/dogriffiths/HeadFirstC

Generate safe bug reports

…
from django.views.decorators.debug import sensitive_post_parameters

@sensitive_post_parameters("password")
def login_view(request):
    …
POST:<QueryDict: {u'csrfmiddlewaretoken': [u'F3d71EHWECfavaeK4H7nUTzLwgY07AHT'],
                  u'password': [u'********************'],
                  u'email': [u'aruseni.magiku@gmail.com']}>
…
from django.views.decorators.debug import sensitive_variables

@sensitive_variables("payment_card_id")
def process_payment(request):
    …
http://habrahabr.ru/post/164935/

Jan 3, 2013

JavaScript profiling, Console

console.dir(obj);

console.time("t1")
....
console.timeEnd("t1")

console.profile("p1")
...
console.profileEnd("p1")

console.trace()

console.count("my_fun_count")