Sort an integer array, What language do you use?

Array data type, used in a programming language to specify a variable that can be indexed

During my experiment with array / hash variable, i’ve found this interesting website. The wiki explains how to sort the integers in the array by using a variety of languages. even some programming languages that I do not know at all.

I’ll pick a few examples of sorting functions described in the wiki

Sort an integer array using C language:

#include <stdlib.h>  /* qsort() */
#include <stdio.h>   /* printf() */

int intcmp(const void *aa, const void *bb)
{
	const int *a = aa; *b = bb;
	return (*a < *b) ? -1 : (*a > *b);
}

int main()
{
	int nums[5] = {2,4,3,1,2};
	qsort(nums, 5, sizeof(int), intcmp);
	printf("result: %d %d %d %d %d\n",
		nums[0], nums[1], nums[2], nums[3], nums[4]);
	return 0;
}

Counting Directory Size Recursively Using C Part 2

Even there’s an easier way calculating directory size ftw syscall/API

NAME
ftw, nftw - file tree walk

DESCRIPTION
ftw() walks through the directory tree that is located under the directory dirpath, and calls fn() once for each entry in the tree.
By default, directories are handled before the files and subdirectories they contain (pre-order traversal).

CONFORMING TO
POSIX.1-2001, SVr4, SUSv1.

example code size.c

Counting Directory Size Recursively Using C

I’ve just found this great articles at Jim Plank website. I know, it was lots easier using linux du command

Without any further nonsense talk, here we go. First we need download some source code at Jim Plank website

# mkdir prsize
# cd prsize
# wget http://www.cs.utk.edu/~plank/plank/classes/cs360/360/notes/Prsize/prsize8.c
# wget http://www.cs.utk.edu/~plank/plank/classes/cs360/360/src/dllist.c
# wget http://www.cs.utk.edu/~plank/plank/classes/cs360/360/notes/Dllists/dllist.h
# wget http://www.cs.utk.edu/~plank/plank/classes/cs360/360/src/jrb.c
# wget http://www.cs.utk.edu/~plank/plank/classes/cs360/360/include/jrb.h
# wget http://www.cs.utk.edu/~plank/plank/classes/cs360/360/src/jval.c
# wget http://www.cs.utk.edu/~plank/plank/classes/cs360/360/include/jval.h

prsize8.c is the main program, while dllist.c/h jrb.c/h jval.c/h is library that prsize8 statically linked in.

Now we need to create some static library.(Original website doesn’t explain how to create the libs)

# gcc -c dllist.c -o dllist.o
# gcc -c jrb.c -o jrb.o
# gcc -c jval.c -o jval.o

# ar rcs libdllist.a dllist.o
# ar rcs libjrb.a jrb.o
# ar rcs libjval.a jval.o

How To Use LIKE In MYSQL Stored Procedure Select Statement?

I write a stored procedure as below but always result nothing/incorect. [text] DELIMITER // DROP PROCEDURE IF EXISTS get_domain // CREATE PROCEDURE get_domain (IN domin VARCHAR(64)) BEGIN SELECT * FROM domain_senders WHERE domain LIKE '%domin%'; END// DELIMITER ; [/text] The procedure itself is successfully created.After looking around with google i've…

Zombie Process

A zombie, or defunct, process is a process that has terminated, but its parent process has not taken it out of the process table with the wait() family of system calls. This typically happens when a program forks/execs another program, but then doesn’t reap it properly. The easiest way to write the parent to properly reap its children is to setup a signal handler for SIGCHLD that calls wait().

See the man pages on your local system for signal and wait(2).

Zombie and defunct states of a process are different things. Zombie is created when a child dies and parent didn’t call wait. Defunct is created when parent dies before the child finishes.

Defunct processes can’t be killed since they are already dead. To make them disappear you have to kill their parent process…

It’s not possible to kill defunct processes.

A good way to kill lists of processes is:

$ ps axf | grep name-of-process | grep -v -grep | awk '{print "kill -9 ",$1}' | sh

gethostbyname() vs getaddrinfo()

In IPv4 environment(usually) we used to call function gethostbyname() to do DNS lookups.then the information is load into a struct hostent.

#include <netdb.h>
struct hostent *gethostbyname(const char *name);

struct  hostent {
        char    *h_name;        /* official name of host */
        char    **h_aliases;    /* alias list */
        int     h_addrtype;     /* host address type */
        int     h_length;       /* length of address */
        char    **h_addr_list;  /* list of addresses from name server */
};
#define h_addr  h_addr_list[0]  /* address, for backward compatibility */

So, the easiest way to get information from gethostbyname() call is by extracting hostent structure.

Querying Visitor IP address

There are many ways, scripting/language to obtain remote IP address of the user who is browsing our website.

PERL

#!/usr/bin/perl
use CGI;
print "Content-type: text/plain; charset=iso-8859-1\n\n";
my $q = new CGI;
print "<b>Your Remote IP Address :" . $q->remote_host() . "</b>";

PHP

<?php
echo "<b>Your Remote IP Address :" . $_SERVER['REMOTE_ADDR'] . "</b>";
?>

SDL + Visual C++

Kali ini ceritanya lagi pingin banget belajar coding yg berbau bau graphical. setelah bulak balik googling. dapet referensi dan contohnya.   graphical2 API nya pakai library SDL yg bisa di download di www.libsdl.org bahasa pemograman yg di pakai c++, dengan memakai IDE visual sutdio 6.0. contoh coding dan souce code…