Sunday, September 18, 2016

Unix Internals - With C Programming Examples-2

Print all the environment variables using the envp argument in main() function.


/* Program to Print Environment Variables
 * Program Name : prenv.c
 *
 */
#include <stdio.h>
int main(int argc, char *argv[], char *envp[])
{
    int i = 0;

    while (envp[i]) {
        printf("Environment Variable : %s\n", envp[i]);
        i++;
    }
    return 0;
}



Print all the environment variables using extern variable environ.

/* Program to Print Environment Variables
 * Program Name : prenvv.c
 *
 */
#include <stdio.h>

extern char **environ;

int main(int argc, char *argv[])
{
    int i = 0;

    while (environ[i]) {
        printf("Environment Variable : %s\n", environ[i]);
        i++;
    }
    return 0;
}


The output of both the programs are same.

Program Output:


$ ./prenv
Environment Variable : _=./prenv
Environment Variable : HZ=100
Environment Variable : SSH_TTY=/dev/ttyp0
Environment Variable : PATH=/bin:/usr/bin:/usr/gnu/bin:/sbin:/usr/local/bin
Environment Variable : HUSHLOGIN=FALSE
Environment Variable : EDITOR=emacs
Environment Variable : SHELL=/bin/ksh
Environment Variable : HOME=/home/reemus
Environment Variable : TERM=xterm
Environment Variable : PWD=/home/reemus/prog/cprog
Environment Variable : TZ=EST5EDT
Environment Variable : ENV=/home/reemus/.kshrc
$


Thursday, September 15, 2016

Unix Internals - With C Programming Examples-1

For a long time I wanted to create a series of Blog post of what I have learned as Unix System Programming and its Internals with the help of plain simple to read examples.

Program: cmdargs.c


/* Program to read and display the command-line arguments */
#include <stdio.h>

int main(int argc, char *argv[])
/* argc - No. of Command-line Arguments passed to the program */
/* argv - Array of Strings - Command-line Arguments */
{
    int i;
    printf("No. of Command-line Arguments : %d\n", argc);
    for (i = 0; i < argc; i++) {
        printf("Arg[%d] = %s\n", i, argv[i]);
    }
    return 0;
}

Output:


$ ./cmdargs one two three
No. of Command-line Arguments : 4
Arg[0] = ./cmdargs
Arg[1] = one
Arg[2] = two
Arg[3] = three
$ 

As you can see the "argc" contains the number of command-line arguments passed to the program.

The very first command-line argument for the program is program name itself, followed by the other arguments passed.



Saturday, August 20, 2016

Books for Programmers (From Quora)



Books to be Read by Programmers
  1. Gödel Escher Bach by Douglas Hofstadter (cognitive scientist)
  2. The Metamagical Themas by Douglas Hofstadter
  3. The Art of Computer Programming by Donald Knuth
  4. Causality: Models, Reasoning and Inference by Judea Pearl
  5. Concepts, Techniques, and Models of Computer Programming by Peter Van Roy
  6. Purely Functional Data Structures by Okasaki
  7. The Art of Meta Object Protocol by Gregor Kiczales
  8. To Mock a Mockingbird by Raymond Smullyan

Friday, March 11, 2016

UltraEdit Compile And Execute Batch Files

Compile Batch File

@echo off

set FULL_FILENAME=%~1
set FILENAME=%~n1
set DIRNAME=%~dp1
set FILE_EXT=%~x1

echo Full File Name: %FULL_FILENAME%
echo File Name: %FILENAME%
echo File Directory: %DIRNAME%
echo File Extension: %FILE_EXT%

set CC_BIN=gcc
set CPP_BIN=g++
set GHC_BIN=ghc
set SCALA_BIN=scalac.bat

set CC_EXT=.c
set CPP_EXT=.c++
set GHC_EXT=.hs
set SCALA_EXT=.scala

REM set CPP_OPTIONS=-std=c++11
set CPP_OPTIONS=

IF /I %FILE_EXT% == %CPP_EXT% GOTO __CPPCOMPILE
IF /I %FILE_EXT% == %CC_EXT%  GOTO __CCOMPILE
IF /I %FILE_EXT% == %GHC_EXT% GOTO __HSKCOMPILE
IF /I %FILE_EXT% == %SCALA_EXT% GOTO __SCALACOMPILE
GOTO END

:__CCOMPILE
echo --- Compiling C Program ... ---
call %CC_BIN% %FULL_FILENAME%  -o %FILENAME%.exe & IF ERRORLEVEL 1 (echo. && echo "ERROR - Compilation Error - Please Fix !!!" ) ELSE echo "Compilation success !!!"
GOTO END

:__CPPCOMPILE
echo --- Compiling C++ Program ... ---
call %CPP_BIN% %FULL_FILENAME%  -o %FILENAME%.exe %CPP_OPTIONS% & IF ERRORLEVEL 1 (echo. && echo "ERROR - Compilation Error - Please Fix !!!" ) ELSE echo "Compilation success !!!"
GOTO END

:__HSKCOMPILE
echo --- Compiling Haskell Program ... ---
call %GHC_BIN% --make %FULL_FILENAME%  -o %FILENAME%.exe & IF ERRORLEVEL 1 (echo. && echo "ERROR - Compilation Error - Please Fix !!!" ) ELSE echo "Compilation success !!!"
GOTO END

:__SCALACOMPILE
echo --- Compiling Scala Program ... ---
call %SCALA_BIN% %FULL_FILENAME%  & IF ERRORLEVEL 1 (echo. && echo "ERROR - Compilation Error - Please Fix !!!" ) ELSE echo "Compilation success !!!"
GOTO END

:END

Execute Batch File

@echo off

set FULL_FILENAME=%~1
set FILENAME=%~n1
set DIRNAME=%~dp1
set FILE_EXT=%~x1

echo ++++++++++++++++++++++++++++++++++++++++
echo Executing  %FULL_FILENAME%  ...
echo ++++++++++++++++++++++++++++++++++++++++

set CC_BIN=gcc
set CPP_BIN=g++
set GHC_BIN=ghc
set RUBY_BIN=C:\Ruby21\bin\ruby.exe
set PYTHON_BIN=C:\Python27\python.exe
set SCALA_BIN=scala.bat


set CC_EXT=.c
set CPP_EXT=.c++
set GHC_EXT=.hs
set PY_EXT=.py
set RUBY_EXT=.rb
set SCALA_EXT=.scala

IF /I %FILE_EXT% == %CPP_EXT% GOTO __CPPEXEC
IF /I %FILE_EXT% == %CC_EXT%  GOTO __CEXEC
IF /I %FILE_EXT% == %GHC_EXT% GOTO __HSKEXEC
IF /I %FILE_EXT% == %PY_EXT% GOTO __PYTHONEXEC
IF /I %FILE_EXT% == %RUBY_EXT% GOTO __RUBYEXEC
IF /I %FILE_EXT% == %SCALA_EXT% GOTO __SCALAEXEC
GOTO END

:__CEXEC
call %FILENAME%.exe
GOTO END

:__CPPEXEC
call %FILENAME%.exe
GOTO END

:__HSKEXEC
call %FILENAME%.exe
GOTO END

:__PYTHONEXEC
call %PYTHON_BIN% %FULL_FILENAME%
GOTO END

:__RUBYEXEC
call %RUBY_BIN% %FULL_FILENAME%
GOTO END

:__SCALAEXEC
call %SCALA_BIN% %FILENAME%
GOTO END

:END
echo ++++++++++++++++++++++++++++++++++++++++
echo Execution - Completed !!!
pause