Saturday, March 30, 2013

DOS batch: renaming multiple files with a counter

In rena.bat:
echo off
cls
set i=0
setlocal ENABLEDELAYEDEXPANSION
for /r %%x in (%1) do (
 ren "%%x" "foo-"!i!".jpg" 
 set /a i=i+1
)
The above code renames all .jpg files to be prefixed with foo and with an incrementing counter. To use the code,
rena *.jpg

Thursday, March 28, 2013

A bit of git

Install git everywhere

sudo apt-get install git

Local Directory

git init
git add .
git commit -a -m "message foo"

Prepare the server

git init

Submit to server

git remote add origin url.git
git push origin master

Further Worthwhile Reading

Sunday, March 24, 2013

C Comments: FIXME, TODO, XXX

Gedit has these comments hightlighted in yellow (credit):
/* TODO: How about auto-correcting small spelling errors? */
/* FIXME: This won't work if the file is missing. */
/* XXX: This method badly needs refactoring: should switch by core type. */

Saturday, March 23, 2013

Checking Endianness with C

The piece of code is written on April 23, 2010.
#include <stdio.h>
int checkEndian(){
 int p = 0xffff0000;
 if(*(char*)&p==0xff) //char* pointer of p's first byte
   printf("Big Endian");
 else
   printf("Little Endian"); //my pc is little endian
 return 0;
}
int main(){
   checkEndian();
}

Appendix

If the value of integer variable (32 bits) is 0xffff0000 and your system is in big endian, it will be stored as binary in memory like:
low addr:  1111 1111
       |   1111 1111
       ↓   0000 0000
high addr: 0000 0000

Friday, March 22, 2013

Users and Groups

Linux is a native multi-accessible system.

Enable and disable root

sudo password root
sudo passwd -l root
To open a shell with root, just
su

Add a new group bar

sudo groupadd bar

Add a new user foo to a group bar

sudo useradd -G bar foo

Add an existing user foo to a group bar

sudo usermod -a -G bar foo

Print current settings

id bar
groups foo

Tuesday, March 19, 2013

Makefile: CFLAGS

By writing the following in the Makefile,
CFLAGS = "-FOO -BAR"
It is equivalent to write the below in the original C files.
#define FOO
#define BAR
So if you have these somewhere in your code,
#ifdef FOO
foo();
#endif

foo() will be executed.

GCC CFLAGS

Flags like -Wall -Wextra are used to pass an option to the compiler. The former means to generate all warnings, the latter extra warnings. Follow link.

gs: Merging Multiple PDFs

Preparation

sudo apt-get install gs pdftk

The Command

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=done.pdf ./*.pdf

A Sketch of GhostScript.

gs
    starts the Ghostscript program.
-dBATCH
    once Ghostscript processes the PDF files, it should exit.
    If you don't include this option, Ghostscript will just keep running.
-dNOPAUSE
    forces Ghostscript to process each page without pausing for user interaction.
-q 
    stops Ghostscript from displaying messages while it works
-sDEVICE=pdfwrite 
    tells Ghostscript to use its built-in PDF writer to process the files.
-sOutputFile=finished.pdf
    tells Ghostscript to save the combined PDF file with the specified name.