Compiling C programs

I find it simplest to compile my C programs on the command line. Once the program is entered via a text editor or the editor of choice I save it with a suffix of .c to identify it as a raw c file. Then from the command line I simply use

1
gcc /path-to-file/example.c

The file is then compiled and saved as a file called a.out which is the default name given to the compiled program. You can then run the program on the command line using ./a.out. Make sure the permissions are set for it to be an executable file. 755 is good if you want anyone on the system to be able to execute it.

Shebang

The shebang:

1
#!/bin/bash

should be included at the top of all bash scripts. The scripts should then be saved, (I like to use a .sh suffix e.g. myScript.sh). Once saved you then need to make the script executable, use chmod 700 myscript.sh or similar depending on your requirements. Once it’s executable you can run it by calling the script on the command line with.

1
./myscript.sh

if you are in the directory of the script or if you are in a different directory

1
/pathToScript/myscript.sh

Or you could make sure that you save your script into a folder that is in your shell path, that way you can just type

1
myscript.sh

For loop on the command line

The expression below does a number of things. It looks at every file in a directory, and for each file it calls the man page on that file. It then pipes the output of the man page into a process called man2html, which converts the manpage to html. It then writes an html file to standard out titled by the original file and appended with the .html suffix.

1
for FILE in *; do man $FILE | man2html > ~/desktop/html/$f.html; done

Using GitHub and Xcode

in a few easy steps you can easily set up Xcode 4’s Git source control with your GitHub repository with an existing project that had no automatic source control before.

  • Create a repository on GitHub
  • Create a new project on Xcode4 and don’t forget to check the checkbox to enable Git source control.

If the project is already created without git source control then follow these 4 instructions in the terminal:

  • Use the cd command to switch to your project folder, making it the current working directory.
  • Enter git init to create an empty repository.
  • Enter git add . to copy your project files into the repository.
  • Enter git commit - m "Initial commit" to commit all the files.

Now your projects are set up and under git version control you can commit from within Xcode

  • From Xcode, commit whenever you want.
  • Using Terminal on you Mac, go to the folder where you created your Xcode project and then type git remote add origin git@github.com:$USERNAME/$REPO.git
    (of course, replace $USERNAME/$REPO with your own)
    git push -u origin master
  • You should now see your project on GitHub. Now you can work in Xcode and commit whenever you want. To push your new commit to GitHub simply type in the Terminal git push

or select push from within Xcode

Hacking the Playstation PS1 Controller

Recently I was looking at using the PS1 controller as a means to control various other things. This came to mind as I was looking particularly at controlling a stepper motor. Having searched google a little I discovered that there were a few references to doing this.

Cutting the cable you will discover that there are 9 coloured cables within. These are defined as follows;

No Colour Description
1 Brown Data
2 Orange Command
3 Purple N/C
4 Black GND
5 Red Vcc
6 Yellow Att
7 Blue Clock
8 N/C
9 Green ACK

Jump starting a CPU power supply

This is a quick blog post about jumpstarting a power supply. I salvaged an IBM 36L8848 power supply out of the skip at work, as having a power supply for various projects will always be useful. When I got it home and plugged it in, all seemed fine, until I tested the voltage and realised that it wasn’t working. I then realised that the power supply talks to the motherboard of a PC and when the on switch of the PC is pressed this starts the power supply. If you look at the picture below, you will see on the motherboard connector a green and black wire next to each other. The green wire when measured is giving a voltage of three volts. When this is connected to ground, i.e. the black wire, the power supply springs to life. You will see in the picture that I have simply connected a wire in the connector between the green and black wires. This should be the same on the majority of PC power supplies.

The jump lead attached!

Public and Private Keys.

There are two different types of cryptography, there’s symmetric cryptography, where there’s only one key. So if you have a secret key and encrypt a message with it, only a person who knows the secret key can decrypt it.
Also there is asymmetric cryptography where there are two keys – a public key (which can be known by everyone) and a private key (which is kept secret to you).The way asymmetric cryptography works, and the reason apple use it is that you can use it to prove a message came from you. If you encrypt something with your secret private key and someone can decrypt the message with your public key to get something that makes sense, they know that you, being the only person who knows the private key, created the message.
In other words, it is “signed”. And this is the basic technique behind code signing all apps in iOS!

ImageMagick and mogrify

Two great command line commands for use with the imagemagick library. The first one will resize all jpg files within a folder to have a width of 1920 pixels. I chose 1920 as this is the width of HD television pictures.

1
mogrify -resize 1920 *.jpg

The following command will crop all pictures in a folder to 1920x1080 the +0+99 defines an x,y co-ord where the crop will start, in this case 0 pixels across and 99 pixels down. This basically equates to lopping 100 pixels off the bottom and 99 pixels off the top.

1
mogrify -crop 1920x1080+0+99 *.jpg

You need to be careful using the mogrify command with imagemagick as the processes are destructive an manipulate the original image file. So have a back up folder available. Or failing that you can use the convert command instead which will make copies and leave your original images untouched.

^