Sunday, October 25, 2009

Insidious C bugs

Here's a couple novel C bugs that I've encountered recently (using gcc). Man, it sucks when these happen.

1) No compiler warnings when not instantiating structs correctly

The following code will give no warning indicating that you have not, when declaring an array of structs using the curly bracket notation, instantiated all members of the struct:
#include <stdio.h>

int main()
{
struct a_struct {
int a;
int b;
} buggy[] = {
{ 1, 2, },
{ 3, /* oops! */ },
{ 5, 6, },
};

int i;
for(i = 0; i < sizeof(buggy) / sizeof(*buggy); i++)
{
printf("%i: %i %i\n", i, buggy[i].a, buggy[i].b);
}

return 0;
}
Here's the ouptut:
0: 1 2
1: 3 0
2: 5 6
Oops! It would have been nice for the compiler to tell us about this...

2) Forgetting to delete the semicolon when turning an assert into an if statement

Say you have
assert(cond);
But you want to make it an if statement to have a little more debugging output when it triggers. So you change it to:
if(!cond);  /* oops! */
{
dump_stats();
assert(0);
}
Whoops! Now the if statement has an empty body and the block between the curly braces will always execute!

3) Strange things you can't do within case statements

This isn't really insidious so much as annoying -- why are you allowed to do some things inside a case statement (that don't seem to make sense) but aren't allowed to do others (that seem to make much more sense)? For instance:

int main() {
int i = 0;

switch(i)
{
int j = 5; /* fine */
case 0:
char ch; /* error */
break;
case 1:
i = 1;
char ch2; /* fine */
break;
default:;
char ch3; /* fine */
break;
}

return i;
}
Note the semicolon following the 'default:' There's a good discussion of this problem here. Essentially, the first thing after a label cannot be a declaration. The error that the compiler gives here is usually pretty cryptic, too.

Similar links

Sunday, October 04, 2009

Youtube kung-fu on Linux

This is just a collection of commands that I have used in the past to edit video, especially for uploading to online video sites such as YouTube. This entry is small now, hopefully it grows as I figure out how to do more useful stuff.


# Cut a clip from a whole video file (-ss is base, -t is offset)
ffmpeg -sameq -i input.avi -ss 00:1:23 -t 00:02:35 output.avi

Wednesday, September 23, 2009

Mandating key-based logins with sshd

This page provides a pretty good summary of creating a public/private key pair on the client and adding the public key to the server's authorized_keys file.

On the server side, edit /etc/ssh/sshd_config and add the directive PasswordAuthentication no and then restart sshd with sudo /etc/init.d/ssh restart Anyone that tries to log in to your server without a public key in the server's authorized_keys file will now not even be given the chance to enter a password.

sshd_config manpage

Monday, September 21, 2009

xmonad

Xmonad is a sweet window manager. So sweet, in fact, that I've decided to use it. Here's a quick reference, basically the manpage rearranged (mod = alt by default) ... (also, remember to change the session login to "Run Xclient script" so that other things, like xmodmap, can be run in .xsession):

=== Launch ===
mod-shift-q -- Quit xmonad
mod-q -- Restart xmonad (and reload config file)
mod-shift-return -- Launch terminal
mod-shift-c -- Close the focused window
mod-p -- Launch dmenu

=== Layout ===
mod-space -- Rotate through the available layout algorithms
mod-shift-space -- Reset the layouts on the current workspace to default

=== Focus ===
mod-tab -- Move focus to the next window
mod-shift-tab -- Move focus to the previous window
mod-j -- Move focus to the next window
mod-k -- Move focus to the previous window
mod-m -- Move focus to the master window

=== Swap ===
mod-return -- Swap the focused window and the master window
mod-shift-j -- Swap the focused window with the next window
mod-shift-k -- Swap the focused window with the previous window

=== Resize ===
mod-h -- Shrink the master area
mod-l -- Expand the master area

=== Master Area (the area on the left) ===
mod-comma -- Increment the number of windows in the master area
mod-period -- Deincrement the number of windows in the master area

=== Workspace ===
mod-[1..9] -- Switch to workspace N
mod-shift-[1..9] -- Move client to workspace N

=== Multiple Screens ===
mod-{w,e,r} -- Switch to physical/Xinerama screens 1, 2, or 3
mod-shift-{w,e,r} -- Move client to screen 1, 2, or 3

=== Mouse ===
mod-button1 -- Set the window to floating mode and move by dragging
mod-button2 -- Raise the window to the top of the stack
mod-button3 -- Set the window to floating mode and resize by dragging
mod-t -- Push window back into tiling

=== Setup ===
sudo apt-get install xmonad dwm xlockmore xclock htop

=== ? ===
mod-shift-p -- Launch gmrun
mod-n -- Resize viewed windows to the correct size
mod-b -- Toggle the status bar gap

Here are some other useful utilities to replace functionality that is present in, say, GNOME on the command line:
How to install Xmobar

First, install the necessary packages:
sudo apt-get install libghc6-http-dev libghc6-zlib-dev libx11-dev libxft-dev

I hadn't installed any packages with cabal before, so I needed to set up that as well. I found it necessary to do the standard download, untar, runhaskell Setup configure && runhaskell Setup build && sudo runhaskell Setup install for the following packages:
  • http://hackage.haskell.org/packages/archive/HTTP/4000.0.8/HTTP-4000.0.8.tar.gz
  • http://haskell.org/cabal/release/cabal-1.6.0.2/Cabal-1.6.0.2.tar.gz
  • http://haskell.org/cabal/release/cabal-install-0.6.2/cabal-install-0.6.2.tar.gz
A cabal update initializes the package database and a cabal install xmobar grabs the package.

Put this in your xmonad.hs (changing the paths appropriately, of course):

import XMonad
import XMonad.Hooks.DynamicLog
import XMonad.Hooks.ManageDocks
import XMonad.Util.Run(spawnPipe)
import System.IO

main = do
xmproc <- spawnPipe "/home/dannyc/.cabal/bin/xmobar /home/dannyc/.xmonad/xmobarrc"
xmonad $ defaultConfig {
layoutHook = avoidStruts $ layoutHook defaultConfig
, logHook = dynamicLogWithPP $ xmobarPP
{ ppOutput = hPutStrLn xmproc
, ppTitle = xmobarColor "green" "" . shorten 50
}
}
And put this in your xmobarrc:
Config { font = "-*-Fixed-Bold-R-Normal-*-13-*-*-*-*-*-*-*"
, bgColor = "black"
, fgColor = "grey"
, position = TopW L 90
, commands = [ Run Weather "EGPF" ["-t"," <tempF>F","-L","64","-H","77",
"--normal","green","--high","red","--low","lightblue"] 36000
, Run Cpu ["-L","3","-H","50","--normal","green","--high","red"] 10
, Run Memory ["-t","Mem: <usedratio> 10
, Run Swap [] 10
, Run Date "%a %b %_d %l:%M" "date" 10
, Run StdinReader
]
, sepChar = "%"
, alignSep = "}{"
, template = "%StdinReader% }{ %cpu% | %memory% * %swap% <fc=#ee9a00>date%<fc> | %EGPF%"
}

There, you now have a bare-bones xmobar install. I figured this out by loosely following the instructions here. I don't know any haskell (yet) and this is my first time messing with the xmonad config, so I don't guarantee at all that I am doing things the Right Way. If your xmonad keeps locking up because of this configuration, see the big bold comment in the link above which points to here.

TODO
  • customize xmobar
  • some kind of screen-like titleing for workspaces
  • better fonts?
  • picture background on startup (/transparent windows?)

Monday, August 24, 2009

Video DownloadHelper Firefox extension + YouTube = Napster

Video DownloadHelper can download videos from YouTube (or other video sites) and convert the audio to mp3 (or other formats) using ffmpeg (the default). Pick the mp4 (high quality / high definition -- these are format 18 or 22) videos off of YouTube to get the highest quality audio (128 kbps).

Ah, this brings me back...

Saturday, July 25, 2009

Photo metadata

# get exiftool
sudo apt-get install libimage-exiftool-perl

# get libexif CLI tool
sudo apt-get install exif

# erase all metadata
exiftool -all='' img.jpg

# see what metadata tags your photo has
exif -l img.jpg

# see the values of the included metadata tags
exif img.jpg

EXIF specifications: http://www.exif.org/specifications.html
For PNGs: http://pmt.sourceforge.net/pngmeta/index.html

Thursday, July 23, 2009

And now, a demonstration in how ridiculously easy it is to hack stuff with Google

Inspired by this reddit thread (look here for help deciphering the Google syntax).

phpMyAdmin -- inurl:SELECT inurl:FROM inurl:WHERE intitle:phpmyadmin

HP Laserjet printers -- "identify the document you want to print by using either option shown below, then select the apply button."

More printers
-- "Web Image Monitor" location comment "device name"

Web cams
-- inurl:"viewerframe mode="

And, let's not forget the Google Hacking Database or how to use Google to crack MD5-hashed passwords!