statvfs example.
Small script that calls statvfs for the filesystem of pwd.
#!/bin/sh
################################################################
# Test statvfs().
################################################################
cat > statvfs-test.c <<EOF
#include <stdio.h>
#include <stdlib.h>
#include <sys/statvfs.h>
#include <sys/types.h>
#include <errno.h>
/*
* struct statvfs {
* unsigned long f_bsize; / file system block size
* unsigned long f_frsize; / fragment size
* fsblkcnt_t f_blocks; / size of fs in f_frsize units
* fsblkcnt_t f_bfree; / # free blocks
* fsblkcnt_t f_bavail; / # free blocks for non-root
* fsfilcnt_t f_files; / # inodes
* fsfilcnt_t f_ffree; / # free inodes
* fsfilcnt_t f_favail; / # free inodes for non-root
* unsigned long f_fsid; / file system ID
* unsigned long f_flag; / mount flags
* unsigned long f_namemax; / maximum filename length
* };
*/
void echo_stat(struct statvfs *buff)
{
fprintf (stderr, "Block Size :%lu\n",buff->f_bsize );
fprintf (stderr, "Fragm Size :%lu\n",buff->f_frsize );
fprintf (stderr, "Blocks :%lu\n",buff->f_blocks );
fprintf (stderr, "Free blocks :%lu\n",buff->f_bfree );
fprintf (stderr, "Non root fb :%lu\n",buff->f_bavail );
fprintf (stderr, "Inodes :%lu\n",buff->f_files );
fprintf (stderr, "Free Inodes :%lu\n",buff->f_ffree );
fprintf (stderr, "Non root fi :%lu\n",buff->f_favail );
fprintf (stderr, "FS Id :%lu\n",buff->f_fsid );
fprintf (stderr, "Mount flags :%lu\n",buff->f_flag );
fprintf (stderr, "Max fn leng :%lu\n",buff->f_namemax );
}
int main(void) {
struct statvfs * buff;
extern int errno;
if ( !(buff = (struct statvfs *)
malloc(sizeof(struct statvfs)))) {
perror ("Failed to allocate memory to buffer.");
}
if (statvfs(getenv("PWD"), buff) < 0) {
perror("statvfs() has failed.");
} else {
fprintf(stderr,"statvfs() completed successfully. \n");
echo_stat(buff);
}
free(buff);
exit(EXIT_SUCCESS);
}
EOF
for word_length in 32 64; do
cc \
-m${word_length} \
statvfs-test.c \
-o statvfs-test-${word_length}
file statvfs-test-${word_length}
./statvfs-test-${word_length}
done
The best thing about growing up is you get to act like a kid when ever you feel like doing so. I freed the fish today. And it was swimming through all the windows in my Desktops, all day long. Now I feel good about it.
Another Year.
Art is a deception that creates real emotions, a lie that creates truth, and when you give yourself to those emotions, it creates magic.
-A Random youtube guy.
Watched a amazing film today, brilliant screenplay with heart-rending acting. Lesley Manville, plays the character of Mary, a single middle-aged, slim and attractive woman. Mary is a needy woman with an acutely urge to bring happiness into her life , trying to make a final sense of it. There are no contrived crescendos, there is no artificial gloss thrown in, just a melancholy, loosely staged chain of events, that builds to its raw realization of an aching sad life of Mary.
My Mother has always been a advocate of a simple, happy life. I see her so happy all the time. In the midst of a heated argument with her, I sometimes find her sleeping. She keeps to herself, works her daily chores, still I feel she connects to all the people around her with such perfect harmony. I have come across many persons like her, and most of the time I have been overwhelmed by their choices. On the contrary there are some individuals, who seem have a disregard for whatever life has to offer them, and surprisingly enough they seem to have an acute urge to acquire attention and remains perceptually melancholy. Its always nice to be around people who are calm and happy with whatever they have, people who are not constantly blaming ‘fate’ to have brought upon such misery onto them, still working hard to have a good life and to share the same happiness with others around them.
WLan Adhoc mode [Ubuntu]
A handy script, to switch into WLan Adhoc mode in Ubuntu 10.04 .
#!/bin/bash
##############################################################################
# Script to switch wlnan card into adhoc mode, the script is not robust as it
# serves the purpose of Experiment and is not meant to be used for production
# enviornments.
# Author: Sougata Santra
##############################################################################
ip_addr="" # Default transport layer address
sub_mask="255.255.255.0" # Default subnet mask
brod_addr="192.168.0.255" # Default broadcast address
channel=4 # Default channel of wireless communication
essid="adhocmesh" # Default essid for the adhoc network
key=1234567890
logical_name=""
file="/etc/network/interfaces"
##############################################################################
# Usage
##############################################################################
fn_usage()
{
cat << EOF
Usage:
[-i ] ( [-s][-b][-c][-e]) : -i is essential, other parameters are optional
where,
-i : IP address of the host.
-s : Subnet mask, defaults to 255.255.255.0
-b : Broadcast Address, defaults to 192.168.0.255
-c : Channel ( 1-13 ), default is set to 4.
-e : ESSID for the ad-hoc network, defaults to adhocmesh
EOF
}
##############################################################################
# @TODO Check Wlan card
##############################################################################
fn_checkStatus()
{
echo "Not Implemented"
}
##############################################################################
# Switch mode of wlan card
##############################################################################
fn_switchMode()
{
echo -e '\n\n' >> $file
echo "auto "${logical_name} >> $file
echo "iface "${logical_name}" inet static" >> $file
echo "pre-up iwconfig "${logical_name}" mode Ad-Hoc" >> $file
echo "address "${ip_addr} >> $file
echo "netmask "${sub_mask} >> $file
echo "broadcast "${brod_addr} >> $file
echo "wireless-mode ad-hoc" >> $file
echo "wireless-channel "${channel} >> $file
echo "wireless-essid "${essid} >> $file
echo "wireless-key "${key} >> $file
service network-manager stop
/etc/init.d/networking restart
/etc/init.d/networking start
echo "Changes done to config file"
}
# Main
while getopts hi:s:b:c:e: option
do case "$option" in
h) fn_usage
exit 0;;
i) ip_addr="$OPTARG";;
s) sub_addr="$OPTARG";;
b) brod_addr="$OPTARG";;
c) channel="$OPTARG";;
e) essid="$OPTARG";;
[?]) print >&2 "Usage: $0 [-i ip-address] [-s subnet-mask] \
[-b brodcast-address] [-c channel number][-e essid]"
exit 1;;
esac
done
if [ "a"${ip_addr} == "a" ];then
fn_usage
exit 1
fi
temp=`iwconfig 2>&1 | grep 802.11`
for logical_name in $temp
do
echo $logical_name
break
done
fn_switchMode
echo "Interface "${logical_name}
echo "Internet Address" ${ip_addr}
echo "Subnet MaskInternet" ${sub_mask}
echo "Broadcast Address" ${brod_addr}
echo "Channel" ${channel}
echo "Internet Address" ${essid}
Unamed Pipe Example
This code is not tested, and when I mean not tested. I mean not compiled. There is no documentation, probably because if you understand it, you won’t use it.
Problem:
A program with two processes: reader and doubler. The reader process reads lines from standard input and sends these lines to the doubler process through a pipe. The doubler process duplicates each line to the standard output (that is, writes each line twice). Use unnamed Unix pipes for IPC
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
/* SIGCHLD handler. */
static
void sigchld_hdl (int sig)
{
while (waitpid(-1, NULL, WNOHANG) > 0)
{
}
}
int
main()
{
int count, i;
int pfd[2];
char buff[256];
size_t nbytes = 100;
char *inputline;
struct sigaction act;
memset (&act, 0, sizeof(act));
act.sa_handler = sigchld_hdl;
if (sigaction(SIGCHLD, &act, 0))
{
perror ("sigaction");
return EXIT_FAILURE;
}
if(pipe(pfd))
perror("parent pipe creation");
switch(fork())
{
case 0: //child
close(0);
dup(pfd[0]);
close(pfd[0]);
close(pfd[1]);
for(;;)
{
if( (count = read(0, buff, sizeof(buff))) == 0)
exit(1);
write(1,buff,count);
write(1,buff,count);
}
return 0;
case -1: //error
perror("fork failed");
return EXIT_FAILURE;
default: //parent
close(1);
dup(pfd[1]);
close(pfd[1]);
close(pfd[0]);
for(;;)
{
if( (inputline = (char *) malloc (sizeof(char)* (nbytes + 1) )) == NULL )
{
perror("Failed to allocate memory");
return EXIT_FAILURE;
}
if( getline (&inputline, &nbytes, stdin) == -1)
{
perror("Failed to read line");
if ( inputline != NULL )
free(inputline);
return EXIT_FAILURE;
}
write(1,inputline, strlen(inputline));
free(inputline);
}
break;
}
while (sleep(1)) // Magic
{}
return EXIT_SUCCESS;
}
sudo su
My living makes me sit before my terminal more than any other person would normally do, its cold out here, the Baltic has frozen and so has the lakes, the roads, and the trees. I read most of the time, books sometimes, but when I get irked by the very sight of them, I read random stuff from the internet. I read what people write in their blogs, what they post in forums. I avoid post that tries to educate me. It has been a long standing battle between me and pedantic education. I am not a Liberal Arts Major, neither English is my first tongue, so what ever I write here is flawed, its vapid for someone trying to squeeze literary pleasure from it. I always tried to keep my other side separated from this blog. I don’t write pseudo codes explaining love algorithms. I see those sometimes in some Facebook posts. Its like hitting readers with a ‘Sledge Hammer’ to make them believe he/she is a Programmer. I don’t enjoy discussing computer programming with people who does not understand it, still here is a small post trying to summarize what I think Computer Programmers are:
So do you like simple things, like a saucepan, like a potato or a coconut, good, you are not a geek. Most of the things are complicated, no matter how much you fool yourself. The girl you like to hangout is complicated, you favorite shell is complicated, you favorite editor is complicated, linux kernel is complicated, compilers are complicated, system calls are complicated, window manager are complicated, packaging framework are complicated, path traversals algorithms are complicated, math is complicated, summation, series, permutation, combination, everything is complex and complicated. Interwoven, twisted and tied like a byzantine problem. Yet there are people who actually like these complicated things, they spend hours thinking about them, they spend hours hacking on them, they apparently fall in love with them, they tune it twist it, read about them until the point they seem pretty simple. Then they serve it to the lesser programmers as API ( Potato ), Documentation ( Casserole ). They do it for free, they do it for few stickers they proudly stick behind their favorite machine. They are hackers, they are geeks. Not the local guy who wears a dandy shirt, post “love.java” on FB and gets paid for calling some API that runs on some virtual machine.
” What a Hacker does for love, people won’t for money “
-fortune( Linux )
Hackers write geeky code comments , debate on irrelevant topics, exhausts precious IO cycles to write inodes with torantino films, listens to unorthodox music, leads their social life in electronic mails and IRC channels and laughs awkwardly loud while reading web comics. For every ‘love.java’ you write on social networking site, a hacker kills a Camel. Sill there is something really beautiful about them. In short they are like the Terminator, with lot of love.-( xkcd )
TL;DR : If you don’t know what LOVE is , you are not a Hacker.
EDIT: As pointed out by Ashish, the original quote.
% fortune -m ‘hacker does’
%% (fortunes)
A hacker does for love what others would not do for money.
About the Author: The author has some familiarity with Computer Science, while all along his degree, he played FPS games in his hostel, dreamed about Batman in his class room, discussed more raging national and regional issues like Bollywood, politics and girls with his friends . Then he pretend to be a software engineer writing meaningless software and living a meaningless life in Bangalore for few years. He chased the polestar to a cold country where he is again leading a meaningless life. He spends most of his time with two other computer programmers, cooks bad food and plays amateur cords in a guitar, he bought from a grocery store. He can even teach your dog who is not a software programmer, while he is still trying to program himself .
