scanf vor switch/case und in do/while – ein gcc bug?

Posted in Uncategorized by Julian Kessel - Jan 24, 2011

Mir ist neulich in einer schulischen Arbeit ein merkwürdiges Verhalten des C input managements – ich nenne es mal so,  aufgefallen. Im folgenden Code überrennt das Programm nämlich einfach den letzten scanf() vor einem switch/case Ausdruck sogar! wenn unmittelbar davor den input buffer (stdin) geflusht wird:

#include <stdio.h>
 
int main(){
	char op;
	float inp1,inp2,erg;
	do{
		printf("Bitte geben sie einen Operand ein: "); scanf("%f", &inp1);
		fflush(stdin);
		printf("Bitte geben sie einen weiteren Operand ein: "); scanf("%f", &inp2);
 
		printf("\n(A)ddieren\n(S)ubtrahieren\n(M)ultiplizieren\n(D)ividieren\n"); 
		fflush(stdin);
		scanf("%c",&op); //bug in gcc?
 
		switch(op){
			case 'a': case 'A':
				erg=inp1+inp2;
				break;
			case 's': case 'S':
				erg=inp1-inp2;
				break;
			case 'm': case 'M':
				erg=inp1*inp2;
				break;
			case 'd': case 'D':
				if(inp2!=0.0) erg=inp1+inp2;
				break;
			default: printf("Eingabefehler!\n"); break;
		}
		printf("Ergebnis: %.3f\n", erg);
		printf("Zum Abbrechen [e/E] drücken: ");
		scanf("%c", &op); //kleiner Variablen-reuse
	}
	while((op!='E')&&(op!='e'));
 
	return 0;
}

Die Lösung ist, ein Space vor den Format-String-Typ zu setzen, auf einmal wartet das Programm mit der Eingabe. Warum Ich im Titel und im Code gcc erwähne hat den Grund, dass sowohl mein Lehrer, als auch die meisten Mitschüler ein Altes borland (ca. v5.02) benutzen, bei dem dieses Verhalten nicht auftritt…

Tags: COMMENTS

As I followed this post I ran into this error: /libexec/ld-elf.so.1: Shared object "libintl.so.8" not found, required by "wget"

Luckily, there is a simple workaround for that, simply set a link to an older? version of the library:

<code>ln -s /usr/local/lib/libintl.so.9 /usr/local/lib/libintl.so.8</code>

Write this to the PostInit routines under System -> Advanced -> Command Scripts and you're done :)

Tags: COMMENTS

Creating piezoelectric oscillator symbol in LaTeX

Posted in Uncategorized by Julian Kessel - Sep 25, 2010

{\textSFx\textSFix \fbox{\begin{minipage}{5in}\vspace{10pt}\end{minipage}}\textSFviii\textSFx}

Tags: COMMENTS

I assume you've got a working configuration for any roadwarrior such as your laptop, so I explain only small adaptions. Anyway, for a basic configuration, see http://www.scribd.com/doc/8142908/pfSense-OpenVPN-Tutorial

The setup has been tested with a nexus one running Froyo (FRF91) and Cyanogenmod 6 RC3 and PFSense 1.2.3-RELEASE nanobsd on an ALIX board

Let's start with the PFSense box:
Check LZO Compression #for a slight better performance

Under "Custom Options"

add push "redirect-gateway" #since Cyanogenmod OpenVPN integration has problems with doing this itself…

Now to the Device (all options have to match the ones chosen on your pfsense wall, i think that's self-evident):
Under "Advanced" (press the menu button) choose the following:

Device to use: tun
LZO Compression: true
Redirect Gateway: FALSE #pfsense pushes this option to us
Cipher Algorithm: AES-256-CBC #since my uplink is 1Mb/s it doesn't really matter that I use something really secure, crypting on my nexus1 will do that with ease
Size of cipher key: default

Tags: COMMENTS

Cleaning up / Extending Timetable in LaTeX

Posted in LaTeX by Julian Kessel - Aug 23, 2010

The Class always had a “buggy” behavior for me, so I thought about some fixes for that problems and since I never discovered a better package, I made up fixes for that problems:

The snippet below is for printing the exact begin- and ending times of your lessons, since The class gives the timestring too little space, it grows slightly over the cell border. This “hack” has some exploit flavor.

...
\setprinttimestamps{2} %view also ending time
\settimestyle{\vspace{1mm}\tiny} %_HACK_ push down the Time entries
...

I also recommend adding the command below, if you haven’t already, IMHO the default value is too big.

\setbottomspace{2pt}
Tags: COMMENTS

Android Emulator Dev Tools App on real Device

Posted in Android, Programming by Julian Kessel - Jun 30, 2010

Recent Versions (>=6) of Cyanogenmod and others already include the tools mentioned below, Check that before reading.

Please setup your SDK correctly e.g. w/ this guide:Installing the SDK
There are some Apps preinstalled on the Android Emulator that comes w/ the SDK which you don’t find on the Froyo image or in the Market.

    Start an Emulator Instance and connect your phone in USB-Debugging mode.

Dev Tools” (Developer.apk) [set of tools to view network usage, connections to google servers, view packages install. locations and a whole lot more!]
Spare Parts” (SpareParts.apk) [gives you very detailed info about battery usage of specific hardware and lets you adjust the system's animations]
Custom Locale” (CustomLocale.apk) [quickly fake your locale setting]
“Speech Recorder” (SpeechRecorder.apk) [no guess...]

Ok, so here’s how to get those tools on your device:
Connect Your Android Dev phone via USB and boot the emulator;
run the following bash one-liner:

for file in "SpeechRecorder.apk" "CustomLocale.apk" "SpareParts.apk"; do adb -e pull /system/app/$file; adb -d install ./$file; done
Tags: COMMENTS

Hex Addition function

Posted in Uncategorized by Julian Kessel - May 02, 2010

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def add_hex(arr):
    result = 0x0
    index = 0
    while True:
        print index
        try:
            result = result + int(arr[index], 16) + int(arr[index+1], 16)
        except IndexError:
            try:
                result = result + int(arr[index], 16)
            except IndexError:
                pass
            result = hex(result)
            break
        index+=2
    return result
 
print add_hex2(['0x01', '0x05', '0x04', '0x09', '0x17'])
Tags: COMMENTS

Date subtraction SQL snippet for OpenOffice base

Posted in Programming by Julian Kessel - Oct 23, 2009

With this simple line condition in OpenOffice base’s hsqldb it is possible to subtract a date fragment, and in order to this, select the matching entries. My example is a query on a column containing birthdates in the german format DD-MM-YY.

It selects the entries (persons) who are under the age of 20.

SELECT “birthdate” AS “Birthdate” FROM “under20″ AS “under20″ WHERE (datediff(‘yy’, “BIRTHDATE”, CURRENT_DATE)) <= 20

Instead of  ‘yy’ it’s possible to take the following other values:

‘ms’=millisecond

‘ss’=second

‘mi’=minute

‘hh’=hour

‘dd’=day

‘mm’=month

Tags: , COMMENTS

Install additional packages on pfSense 1.2.3-RC1

Posted in Uncategorized by Julian Kessel - Oct 14, 2009

There is no package manager in pfSense 1.2.3-RC1, though you can install FreeBSD ports via “Diagnostics > Command”.

Type

pkg_add URLofthePackage.tbz

in the field and press return.

the packages can be found at ftp://ftp.freebsd.org/pub/FreeBSD/ports/packages/category (BTW. there’s a complete list at the parent directory)

In most cases you will have to use a SSH or serial connection to configure the programs. Please also keep in mind that more daemons slow down the boot process significantly, especially on embedded devices !

The pkg_add(1) utility uses fetch(1) to download the precompiled package and installs it automatically. On regular (bigger) FreeBSD systems you will pull a complete directory of ports into your local ports directory from the FreeBSD servers in use of the program cvsup. Since this is method of getting new programs was not meant to be used on embedded systems on which that dump just fills up the small flash disks, the manual way of getting the packages is preferred. Another reason why it’s necessary to use precompiled packages is that most of the embedded OS’ bring no compiler with themselves.

An experience I made while setting up a ftp server that the filesystem is read-only. The developers explain it is due to the limited writecycles on flashable media, so the volume gets mounted for updating the cfg file only. You have to use the web-based cmdline for executing file-modifying commands. That also includes SSH which isn’t even with root rights able to write something.

Tags: COMMENTS

BASH – generate a pseudo-random user agent

Posted in Programming by Julian Kessel - Oct 01, 2009

The following script will give you a user agent randomly out of a big list of all user agents that exist.

1
2
3
4
5
6
7
8
9
#!/bin/bash
 
userAgents=`curl -s http://www.user-agents.org/allagents.xml | grep -iB 5 '<Type>B</Type>' | grep -i '<String>' | cut -c 9- | sed 's/..........$//'`
maxLines=`echo "$userAgents" | wc -l | tr -d ' '`
randomUserAgent=$(echo "$userAgents" | sed -n $[ ( $RANDOM % ( $[ $maxLines - 1 ] + 1 ) ) + 1 ]p)
 
echo $randomUserAgent
 
</string>

The script searches for a type “B” in the xml document, which stands for Browser, if you want to include other types, write the letter in place of, or with one space seperated from “B”. It’s possible to specify more than 2 types.

B = Browser
C = Link-, bookmark-, server- checking D = Downloading tool
P = Proxy server, web filtering
R = Robot, crawler, spider
S = Spam or bad bot

Tags: COMMENTS