Discussion:
Societal debt ---- more stuff from the closet
(too old to reply)
KP Bhat
2007-10-07 03:46:25 UTC
Permalink
Like Nehru, like Andrew Carnegie, like Bill Gates, like Warren Buffet,
I have this intense urge to return somethingbackto thesociety.
Make no mistake. I am not saying that I am in the same league as the
people that I have mentioned ----- far from it. There is hardly
anything that I can giveback. I am a poor, low-life computer
programmer, not blessed with excessive riches ---- so no "monies" will
be forthcoming from this end. However, in my own humble way, I would
like to payback, and this is what I have decided to do.
Over the years, I have benefited a lot from the open source movement.
I have used lots of open source code, and to good effect. I am a
strong believer in the open source concept. I would like to show my
intense gratitude by releasing somethingbackto the developer
community, as a most humble offering. It will all happen under this
"thread". It is my sincere hope that someday, somewhere, someone will
find this stuff helpful.
I would like to make it clear that I am not doing this for monetary
gain, or to gain bragging rights. This is just my way of saying to
the open-source community: From the bottom of my heart, thank you very
very much.
Regards,
Bhat
Found some more stuff that might (or might not) benefit the open
source community

#!/bin/ksh
# Script to highlight mispelled words in a file
#
for word
do
rezult=`echo $1 | spell`
if [ "$rezult" != "" ]
then
echo $1: Wrong
fi
shift
done
KP Bhat
2007-10-07 03:47:43 UTC
Permalink
#!/bin/ksh
#
# This script operates on C/C++/IDL files and recursively searches
# for included header files. The directory list to be searched needs
to be
# provided.
#
# Caution:- Due to the recursive nature of this utility, error
checking is
# minimum.
#

##############################################################################
#
# Function to recursively search (in depth-first-search fashion) a
file for
# included header files
#
##############################################################################
function locate_includes
{
if [ ! -f $1 ]
then
return
fi

list_1=`grep "#include" $1`
if [ "$list_1" = "" ]
then
return
else
list_2=`echo $list_1 | sed 's/\"/ /g'`
list_3=`echo $list_2 | sed 's/</ /g'`
list_4=`echo $list_3 | sed 's/>/ /g'`

list_5=
flag=FALSE
for word in $list_4
do
if [ "$word" = "#include" ]
then
flag=TRUE
continue
fi

if [ "$flag" = "TRUE" ] #Flag set on previous iteration
then
list_5="$list_5 $word"
flag=FALSE
fi
done

for entry in $list_5
do
for this_directory in $DIR_LIST
do
if [ -f $this_directory/$entry ]
then
echo `basename $1`: $entry
$0 $this_directory/$entry
break
fi
done
done
fi
}

###############################################################################
#
# Main
#
###############################################################################

DIR_LIST="."

if [ "$#" = "0" ]
then
echo usage: `basename $0` \<file_name\> [-I\<include directory\> [-I
\<include directory\>] ]
exit 1
else
if [ "$1" = "-I" ]
then
echo usage: $0 \<file_name\> [-I\<include directory\> [-I
\<include directory\>] ]
exit 1
else
root=$1
shift
fi
fi

while [ "$1" != "" ]
do
if [ "$1" = "-I" ]
then
DIR_LIST="$DIR_LIST $2"
fi
shift
done

locate_includes $root $DIR_LIST
KP Bhat
2007-10-07 03:48:38 UTC
Permalink
#!/bin/ksh
# Script to list all files in a directory, except the designated file
name

if [ "$#" != 1 ]
then
echo Usage: $0 \<Pattern\>
exit 1
fi

ls -l *[!"$1"]
KP Bhat
2007-10-07 03:49:41 UTC
Permalink
#!/bin/ksh
# Script to remove all files in a directory, except the designated
file name

if [ "$#" != 1 ]
then
echo Usage: $0 \<Pattern\>
exit 1
fi

files_to_remove=`ls *[!"$1"]`
for file in $files_to_remove
do
'rm' -f $file
done
#
# Note:- I am doing this instead of directly doing something like
# 'rm' -f *[!"$1"] to give me an opportunity of viewing all the file
# names that match the criterion using lsbut
#
KP Bhat
2007-10-07 03:53:27 UTC
Permalink
//
// Program to compare two binary files
//

#include <iostream>
#include <fstream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

using namespace std;

///////////////////////////////////////////////////////////////////
// Definitions
///////////////////////////////////////////////////////////////////
#undef USAGE_OPTIONS
#define USAGE_OPTIONS " <file-1> <file-2>"

#define BLOCK_SIZE 1024

#define SUCCESS 0
#define FAIL 1

///////////////////////////////////////////////////////////////////
// Global Variables
///////////////////////////////////////////////////////////////////
ifstream ifstr1, ifstr2;

///////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////
void
closeStreams()
{
if(ifstr1.is_open()) ifstr1.close();
if(ifstr2.is_open()) ifstr2.close();
}


///////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////
void
compare(char ch1, char ch2, size_t offset)
{
if(ch1 != ch2)
{
cout << hex << "0x" << offset << " (" << dec << offset << "):\t\t"
<< hex << "0x" << (int) ch1 << " (" << dec << (int) ch1 <<
"), "
<< hex << "0x" << (int) ch2 << " (" << dec << (int) ch2 <<
")"
<< endl;
}
}


///////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////
main(int argc, char **argv)
{
int retVal;

if(argc < 3)
{
cerr << "usage: " << argv[0] << USAGE_OPTIONS << endl;
return FAIL;
}

struct stat file1Info, file2Info;

if(stat(argv[1], &file1Info) < 0)
{
cerr << "Cannot determine size of content file " << argv[1]
<< " ....exiting" << endl;
closeStreams();
return FAIL;
}

if(stat(argv[2], &file2Info) < 0)
{
cerr << "Cannot determine size of content file " << argv[2]
<< " ....exiting" << endl;
closeStreams();
return FAIL;
}

if(file1Info.st_ino == file2Info.st_ino)
return SUCCESS;


if(file1Info.st_size != file2Info.st_size)
{
cout << "Files " << argv[1] << " and " << argv[2] << " differ" <<
endl;
return SUCCESS;
}


ifstr1.open(argv[1], ios::in|ios::binary);
if(!ifstr1)
{
cerr << "Cannot open file " << argv[1] << " ....exiting" << endl;
closeStreams();
return FAIL;
}


ifstr2.open(argv[2], ios::in|ios::binary);
if(!ifstr2)
{
cerr << "Cannot open file " << argv[2] << " ....exiting" << endl;
closeStreams();
return FAIL;
}

size_t blocksToRead = file1Info.st_size / BLOCK_SIZE;
size_t bytesToRead = file1Info.st_size % BLOCK_SIZE;

char blockBuf1[BLOCK_SIZE], blockBuf2[BLOCK_SIZE];
for(size_t i = 0; i < blocksToRead; i++)
{
size_t offset = ifstr1.tellg();
ifstr1.read(blockBuf1, BLOCK_SIZE);

if(!ifstr1)
{
cerr << "Error reading file " << argv[1] << " ---- aborting\n";
closeStreams();
return FAIL;
}

ifstr2.read(blockBuf2, BLOCK_SIZE);

if(!ifstr2)
{
cerr << "Error reading file " << argv[2] << " ---- aborting\n";
closeStreams();
return FAIL;
}

if(memcmp(blockBuf1, blockBuf2, BLOCK_SIZE))
{
for(size_t j = 0; j < BLOCK_SIZE; j++)
compare(blockBuf1[j], blockBuf2[j], offset+j);
}
}


char ch1, ch2;
for(size_t i = 0; i < bytesToRead; i++)
{
size_t offset = ifstr1.tellg();
ifstr1.read(&ch1, 1);

if(!ifstr1)
{
cerr << "Error reading file " << argv[1] << " ---- aborting\n";
closeStreams();
return FAIL;
}

ifstr2.read(&ch2, 1);

if(!ifstr2)
{
cerr << "Error reading file " << argv[2] << " ---- aborting\n";
closeStreams();
return FAIL;
}

compare(ch1, ch2, ifstr1.tellg());
}

closeStreams();
return SUCCESS;
}
KP Bhat
2007-10-07 03:56:01 UTC
Permalink
//
// Program to "patch" (i.e. repair) a binary file byte-by-byte

#include <iostream>
#include <fstream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

using namespace std;

///////////////////////////////////////////////////////////////////
// Definitions
///////////////////////////////////////////////////////////////////
#undef USAGE_OPTIONS
#define USAGE_OPTIONS " <file> <offset> <value>"

#define SUCCESS 0
#define FAIL 1

///////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////
main(int argc, char **argv)
{
int retVal;

if(argc < 4)
{
cerr << "usage: " << argv[0] << USAGE_OPTIONS << endl;
return FAIL;
}

struct stat fileInfo;

if(stat(argv[1], &fileInfo) < 0)
{
cerr << "Cannot determine size of content file " << argv[1]
<< " ....exiting" << endl;
return FAIL;
}

unsigned long offset;
long newLongValue;

offset = strtoul(argv[2], NULL, 0);
newLongValue = strtol(argv[3], NULL, 0);
if((newLongValue > 127) || (newLongValue < -127))
{
cerr << "The new value of the byte should lie between -127 and
127 ....exiting"
<< endl;
return FAIL;
}

char newByte = newLongValue;

if(offset > fileInfo.st_size)
{
cout << "Offset (" << offset << ") exceeds file size ....exiting"
<< endl;
return FAIL;
}

//ofstream ofstr(argv[1], ios::out|ios::binary);
fstream iofstr(argv[1], ios::in|ios::out|ios::binary);
if(!iofstr)
{
cerr << "Cannot open file " << argv[1] << " ....exiting" << endl;
return FAIL;
}

iofstr.seekp(offset);
if(!iofstr)
{
cerr << "Error in file seek operation to the desired
offset ....exiting"
<< endl;
iofstr.close();
return FAIL;
}

iofstr.write(&newByte, 1);
if(!iofstr)
{
cerr << "Error writing to file " << argv[1] << " ....exiting" <<
endl;
iofstr.close();
return FAIL;
}

iofstr.close();
return SUCCESS;
}
KP Bhat
2007-10-07 03:57:43 UTC
Permalink
###############################################################################
#
# This script recursively operates on two directories (designated
as the
# source and target directories) and compares their contents. The
script
# checks for file differences as well as missing and unexpected
files.
#
###############################################################################
#!/bin/ksh
#set -x

function compare
{
if [ ! -f $1 ]
then
return
fi

if [ ! -e $2 ]
then
echo "- Expected file $2 does not exist under the target
directory"
return
fi

delta=`cmp $1 $2 2>/dev/null`
if [ "$delta" != "" ]
then
echo "!= Files $1 and $2 differ"
fi
}


function find_diff
{
if [ -f $1 -a -f $2 ]
then
compare $1 $2
elif [ -d $1 -a -d $2 ]
then
for file in $1/*
do
other_file=`basename $file`
if [ -f $file ]
then
compare $file $2/$other_file
elif [ -d $file ]
then
$0 $file $2/$other_file
fi
done

for file in $2/*
do
other_file=`basename $file`
if [ ! -e $1/$other_file ]
then
echo "+ Unexpected file $file encountered under the target
directory"
fi
done
fi
}

if [ $# -lt 2 ]
then
echo "Usage: `basename $0` \<source directory\> \<target directory
\>"
exit 1
fi

find_diff $1 $2

exit 0
KP Bhat
2007-10-07 03:58:29 UTC
Permalink
#!/bin/ksh
# Script to rename symbolic links


if [ $# != 2 ]
then
echo $0: \<old_pattern\> \<new_pattern\>
exit 1
fi

for file in *
do
vble=`ls -l $file`
old_link=`echo $vble | awk '{print $NF}'`
new_link=`echo $old_link | sed "s%$1%$2%"`
if [ "$old_link" != "$new_link" ]
then
if [ -f $new_link ]
then
'rm' $file
echo Relinking $file to $new_link
ln -s $new_link $file
fi
fi
done

Loading...