<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Meherchilakalapudi..  writes for u....</title>
	<atom:link href="http://meherchilakalapudi.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://meherchilakalapudi.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Wed, 23 Sep 2009 14:22:26 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='meherchilakalapudi.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/329503a53ac233a4e8425d4d01183c49?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Meherchilakalapudi..  writes for u....</title>
		<link>http://meherchilakalapudi.wordpress.com</link>
	</image>
			<item>
		<title>Unix Administration</title>
		<link>http://meherchilakalapudi.wordpress.com/2009/08/19/unix-administration/</link>
		<comments>http://meherchilakalapudi.wordpress.com/2009/08/19/unix-administration/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 13:59:48 +0000</pubDate>
		<dc:creator>meherchilakalapudi</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[UnixAdministration]]></category>

		<guid isPermaLink="false">http://meherchilakalapudi.wordpress.com/2009/08/19/unix-administration/</guid>
		<description><![CDATA[What is Unix ?
UNIX is one of the most important operating system in use today, perhaps even the most important. Since its invention around the beginning of the 1970s it has been an object of continual research and development. UNIX is not popular because it is the best operating system one could imagine, but because [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meherchilakalapudi.wordpress.com&blog=5609619&post=687&subd=meherchilakalapudi&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>What is Unix ?<br />
UNIX is one of the most important operating system in use today, perhaps even the most important. Since its invention around the beginning of the 1970s it has been an object of continual research and development. UNIX is not popular because it is the best operating system one could imagine, but because it is an extremely flexible system which is easy to extend and modify. It is an ideal platform for developing new ideas.<br />
Much of the success of UNIX may be attributed to the rapid pace of its development (a development to which all of its users have been able to contribute) its efficiency at running programs and the many powerful tools which have been written for it over the years, such as the C programming language, make, shell, lex and yacc and many others. UNIX was written by programmers for programmers. It is popular in situations where a lot of computing power is required and for database applications, where timesharing is critical. In contrast to some operating systems, UNIX performs equally well on large scale computers (with many processors) and small computers which fit in your suitcase!<br />
All of the basic mechanisms required of a multi-user operating system are present in UNIX. During the last few years it has become ever more popular and has formed the basis of newer, though less mature, systems like NT. One reason for this that now computers have now become powerful enough to run UNIX effectively. UNIX places burdens on the resources of a computer, since it expects to be able to run potentially many programs simultaneously.<br />
If you are coming to UNIX from Windows or DOS you may well be used to using applications software or helpful interactive utilities to solve every problem. UNIX is not usually like this: the operating system has much greater functionality and provides the possibilities for making your own, so it is less common to find applications software which implements the same things. In UNIX you are usually asked to learn a language in order to express exactly what you want. This is much more powerful than menu systems, but it is harder to learn<br />
UNIX has long been in the hands of academics who are used to making their own applications or writing their own programs, whereas as the Windows world has been driven by businesses who are willing to spend money on software. For that reason commercial UNIX software is often very expensive and therefore not available at this college. On the other hand, the flexibility of UNIX means that it is easy to write programs and it is possible to fetch gigabytes of free software from the Internet to suit your needs. It may not look exactly like what you are used to on your PC, but then you have to remember that UNIX users are a different kind of animal altogether<br />
Like all operating systems, UNIX has many faults. The biggest problem for any operating system is that it evolves without being redesigned. Operating systems evolve as more and more patches and hacks are applied to solve day-to-day problems. The result is either a mess which works somehow (like UNIX) or a blank refusal to change (like DOS or the MacIntosh, prior to MacOS X, which is based on BSD UNIX). From a practical perspective, UNIX is important and successful because it is a multi-process system which<br />
“ has an enormous functionality built in, and the capacity to adapt itself to changing technologies,<br />
“ is relatively portable,<br />
“ is good at sharing resources (but not so good at security),<br />
“ has tools which are each developed to do one thing well,<br />
“ allows these tools to be combined in every imaginable way, using pipes and channeling of data streams,<br />
“ incorporates networking almost trivially, because all the right mechanisms are already there for providing services and sharing, building client-server pairs etc,.<br />
“ it is very adaptable and is often used to develop new ideas because of the rich variety of tools it possesses.<br />
UNIX has some problems: it is old, it contains a lot of rubbish which no one ever bothered to throw away. Although it develops quickly (at light speed compared to either DOS/Windows or MacIntosh) the user interface has been the slowest thing to change. UNIX is not user-friendly for beginners, it is user-friendly for advanced users: it is made for users who know about computing. It sometimes makes simple things difficult, but above all it makes things possible!<br />
The aim of this introduction is to<br />
“ introduce the unix system basics and user interface,<br />
“ develop the unix philosophy of using and combining tools,<br />
“ learn how to make new tools and write software,<br />
“ learn how to understand existing software.<br />
To accomplish this task, we must first learn something about the shell language (the way in which UNIX starts programs). Later we shall learn how to solve more complex problems using Perl and C. Each of these is a language which can be used to put UNIX to work. We must also learn when to use which tool, so that we do not waste time and effort. Typical uses for these different interfaces are<br />
shell<br />
Command line interaction, making scripts which performs simple jobs such as running programs, installing new software, simple system configuration and administration.<br />
perl<br />
Text interpretation, text formatting, output filters, mail robots, WWW cgi (common gateway interface) scripts in forms, password testing, simple database manipulation, simple client-server applications.<br />
C<br />
Nearly all of UNIX is written in C. Any program which cannot be solved quickly using shell or perl can be written in C. One advantage is that C is a compiled language and many simple errors can be caught at compile time.<br />
Much of UNIX’s recent popularity has been a result of its networking abilities: UNIX is the backbone of the Internet. No other widely available system could keep the Internet alive today. GNU/Linux is a free/open source re-write of the UNIX operating system, which many enhancements. While GNU/Linux is not “rocket science” to computer experts, it has distilled the essence of UNIX and placed it in the hands of everyone. It runs on wrist watches and mainframe computers. Like it or loathe it, GNU/Linux is probably the most important single development in computer operating systems for many years.<br />
Once you have mastered the UNIX interface and philosophy you will find that i) the PC and MacIntosh window environments might seem to be easy to use, but are simplistic and primitive by comparison; ii) UNIX is far from being the perfect operating system–it has a whole different set of problems and flaws.<br />
The operating system of the future will not be UNIX or GNU/Linux as we see it today (hopefully), nor will is be DOS or MacIntosh, but one thing is for certain: it will owe a lot to the UNIX operating system and will contain many of the tools and mechanisms we shall describe below.<br />
Flavours of Unix<br />
UNIX is not a single operating system. It has branched out in many different directions since it was introduced by AT&amp;T. The most important `fork()’ in its history happened early on when the university of Berkeley, California created the BSD (Berkeley Software Distribution), adding network support and the C-shell.<br />
Here are some of the most common implementations of unix.<br />
BSD:<br />
Berkeley, BSD<br />
SunOS:<br />
Sun Microsystems, BSD/sys 5<br />
Solaris:<br />
Sun Microsystems, Sys 5/BSD<br />
Ultrix:<br />
Digital Equipment Corporation, BSD<br />
OSF 1:<br />
Digital Equipment Corporation, BSD/sys 5<br />
HPUX:<br />
Hewlett-Packard, Sys 5<br />
AIX:<br />
IBM, Sys 5 / BSD<br />
IRIX:<br />
Silicon Graphics, Sys 5<br />
GNU/Linux:<br />
GNU, BSD/Posix<br />
How to use this reference<br />
This programming guide is something between a user manual and a tutorial. The information contained here should be sufficient to get you started with the unix system, but it is far from complete.<br />
To use this programming guide, you will need to work through the basics from each chapter. You will find that there is much more information here than you need straight away, so try not to be overwhelmed by the amount of material. Use the contents and the indices at the back to find the information you need. If you are following a one-semester UNIX course, you should probably concentrate on the following:<br />
“ The remainder of this introduction<br />
“ The detailed knowledge of the Bash shell<br />
“ A detailed knowledge of Perl, guided by chapter 6. This chapter provides pointers on how to get started in perl. It is not a substitute for the perl book.<br />
“ A sound appreciation of chapter 8 on network programming.<br />
The only way to learn UNIX is to sit down and try it. As with any new thing, it is a pain to get started, but once you are started, you will probably come to agree that UNIX contains a wealth of possibilities, perhaps more than you had ever though was possible or useful!<br />
One of the advantages of the UNIX system is that the entire UNIX manual is available on-line. You should get used to looking for information in the online manual pages. For instance, suppose you do not remember how to create a new directory, you could do the following:<br />
nexus% man -k dir</p>
<p>dir             ls (1)          – list contents of directories<br />
dirname         dirname (1)     – strip non-directory suffix from file name<br />
dirs            bash (1)        – bash built-in commands, see bash(1)<br />
find            find (1)        – search for files in a directory hierarchy<br />
ls              ls (1)          – list contents of directories<br />
mkdir           mkdir (1)       – make directories<br />
pwd             pwd (1)         – print name of current/working directory<br />
rmdir           rmdir (1)       – remove empty directories<br />
The `man -k’ command looks for a keyword in the manual and lists all the references it finds. The command `apropos’ is completely equivalent to `man -k’. Having discovered that the command to create a directory is `mkdir’ you can now look up the specific manual page on `mkdir’ to find out how to use it:<br />
man mkdir<br />
Some but no all of the UNIX commands also have a help option which is activated with the `-h’ or `–help’ command-line option.<br />
dax% mkdir –help<br />
Usage: mkdir [OPTION] DIRECTORY…</p>
<p>  -p, –parents     no error if existing, make parent directories as needed<br />
  -m, –mode=MODE   set permission mode (as in chmod), not 0777 – umask<br />
      –help        display this help and exit<br />
      –version     output version information and exit<br />
dax%</p>
<p>Never -Do’s in UNIX<br />
There are some things that you should never do in UNIX. Some of these will cause you more serious problems than others. You can make your own list as you discover more.<br />
“ You should NEVER EVER switch off the power on a UNIX computer unless you know what you are doing. A UNIX machine is not like a PC running DOS. Even when you are not doing anything, the system is working in the background. If you switch off the power, you could interrupt the system while it is writing to the disk drive and destroy your disk. You must also remember that several users might be using the system even though you cannot see them: they do not have to be sitting at the machine, they could be logged in over the network. If you switch off the power, you might ruin their valuable work.<br />
“ Once you have deleted a UNIX file using rm it is impossible to recover it! Don’t use wildcards with rm without thinking quite carefully about what you are doing! It has happened to very many users throughout the history of UNIX that one tries to type<br />
“ rm *~<br />
but instead, by a slip of the hand, one writes<br />
rm * ~<br />
UNIX then takes these wildcards in turn, so that the first command is rm * which deletes all of your files! BE CAREFUL!<br />
“ Don’t ever call a program or an important file `core’. Many scripts go around deleting files called `core’ because the, when a program crashes, UNIX dumps the entire kernel image to a file called `core’ and these files use up a lot of disk space. If you call a file `core’ it might get deleted!<br />
“ Don’t call test programs test. There is a UNIX command which is already called test and chances are that when you try to run your program you will start the UNIX command instead. This can cause a lot of confusion because the UNIX command doesn’t seem to do very much at all!<br />
What you should know before starting<br />
One library: several interfaces<br />
The core of unix is the library of functions (written in C) which access the system. Everything you do on a unix system goes through this set of functions. However, you can choose your own interface to these library functions. UNIX has very many different interfaces to its libraries in the form of languages and command interpreters.<br />
You can use the functions directly in C, or you can use command programs like `ls’, `cd’ etc. These functions just provide a simple user interface to the C calls. You can also use a variety of `script’ languages: C-shell, Bourne shell, Perl, Tcl, scheme. You choose the interface which solves your problem most easily.<br />
UNIX commands are files<br />
With the exception of a few simple commands which are built into the command interpreter (shell), all unix commands and programs consist of executable files. In other words, there is a separate executable file for each command. This makes it extremely simple to add new commands to the system. One simply makes a program with the desired name and places it in the appropriate directory.<br />
UNIX commands live in special directories (usually called bin for binary files). The location of these directories is recorded in a variable called path or PATH which is used by the system to search for binaries. We shall return to this in more detail in later chapters.<br />
Kernel and Shell<br />
Since users cannot command the kernel directly, UNIX has a command language known as the shell. The word shell implies a layer around the kernel. A shell is a user interface, or command interpreter.<br />
There are two main versions of the shell, plus a number of enhancements.<br />
/bin/sh<br />
The Bourne Shell. The shell is most often used for writing system scripts. It is part of the original unix system.<br />
/bin/csh<br />
The C-shell. This was added to unix by the Berkeley workers. The commands and syntax resemble C code. C-shell is better suited for interactive work than the Bourne shell.<br />
The program tcsh is a public-domain enhancement of the csh and is in common use. Two improved versions of the Bourne shell also exist: ksh, the Korn shell and bash, the Bourne-again shell.<br />
Although the shells are mainly tools for typing in commands (which are executable files to be loaded and run), they contain features such as aliases, a command history, wildcard-expansions and job control functions which provide a comfortable user environment.<br />
The role of C<br />
Most of the unix kernel and daemons are written in the C programming language (1). Calls to the kernel and to services are made through functions in the standard C library. The commands like chmod, mkdir and cd are all C functions. The binary files of the same name /bin/chmod, /bin/mkdir etc. are just trivial “wrapper” programs for these C functions.<br />
Until Solaris 2, the C compiler was a standard part of the UNIX operating system, thus C is the most natural language to program in in a UNIX environment. Some tools are provided for C programmers:<br />
dbx<br />
A symbolic debugger. Also gdb, xxgdb ddd.<br />
make<br />
A development tool for compiling large programs.<br />
lex<br />
A `lexer’. A program which generates C code to recognize words of text.<br />
yacc<br />
A `parser’. This is a tool which generates C code for checking the syntax of groups of textual words.<br />
rpcgen<br />
A protocol compiler which generates C code from a higher level language, for programming RPC applications.<br />
Stdin, stdout, stderr<br />
UNIX has three logical streams or files which are always open and are available to any program.<br />
stdin<br />
The standard input – file descriptor 0.<br />
stdout<br />
The standard output – file descriptor 1.<br />
stderr<br />
The standard error – file descriptor 2.<br />
The names are a part of the C language and are defined as pointers of type FILE.<br />
#include </p>
<p>/* FILE *stdin, *stdout, *stderr; */</p>
<p>fprintf(stderr,”This is an error message!\n”);</p>
<p>The names are `logical’ in the sense that they do not refer to a particular device, or a particular place for information to come from or go. Their role is analogous to the `.’ and `..’ directories in the filesystem. Programs can write to these files without worrying about where the information comes from or goes to. The user can personally define these places by redirecting standard I/O. This is discussed in the next chapter.<br />
A separate stream is kept for error messages so that error output does not get mixed up with a program’s intended output.<br />
The superuser (root)<br />
When logged onto a UNIX system directly, the user whose name is root has unlimited access to the files on the system. root can also become any other user without having to give a password. root is reserved for the system administrator or trusted users.<br />
Certain commands are forbidden to normal users. For example, a regular user should not be able to halt the system, or change the ownership of files (see next paragraph). These things are reserved for the root or superuser.<br />
In a networked environment, root has no automatic authority on remote machines. This is to prevent the system administrator of one machine in Canada from being able to edit files on another in China. He or she must log in directly and supply a password in order to gain access privileges. On a network where files are often accessible in principle to anyone, the username root gets mapped to the user nobody, who has no rights at all.<br />
The file hierarchy<br />
UNIX has a hierarchical filesystem, which makes use of directories and sub-directories to form a tree. The root of the tree is called the root filesystem or `/’. Although the details of where every file is located differ for different versions of unix, some basic features are the same. The main sub-directories of the root directory together with the most important file are shown in the figure. Their contents are as follows.<br />
`/bin’<br />
Executable (binary) programs. On most systems this is a separate directory to /usr/bin. In SunOS, this is a pointer (link) to /usr/bin.<br />
`/etc’<br />
Miscellaneous programs and configuration files. This directory has become very messy over the history of UNIX and has become a dumping ground for almost anything. Recent versions of unix have begun to tidy up this directory by creating subdirectories `/etc/mail’, `/etc/services’ etc!<br />
`/usr’<br />
This contains the main meat of UNIX. This is where application software lives, together with all of the basic libraries used by the OS.<br />
`/usr/bin’<br />
More executables from the OS.<br />
`/usr/local’<br />
This is where users’ custom software is normally added.<br />
`/sbin’<br />
A special area for statically linked system binaries. They are placed here to distinguish commands used solely by the system administrator from user commands and so that they lie on the system root partition where they are guaranteed to be accessible during booting.<br />
`/sys’<br />
This holds the configuration data which go to build the system kernel. (See below.)<br />
`/export’<br />
Network servers only use this. This contains the disk space set aside for client machines which do not have their own disks. It is like a `virtual disk’ for diskless clients.<br />
`/dev, /devices’<br />
A place where all the `logical devices’ are collected. These are called `device nodes’ in unix and are created by mknod. Logical devices are UNIX’s official entry points for writing to devices. For instance, /dev/console is a route to the system console, while /dev/kmem is a route for reading kernel memory. Device nodes enable devices to be treated as though they were files.<br />
`/home’<br />
(Called /users on some systems.) Each user has a separate login directory where files can be kept. These are normally stored under /home by some convention decided by the system administrator.<br />
`/var’<br />
System 5 and mixed systems have a separate directory for spooling. Under old BSD systems, /usr/spool contains spool queues and system data. /var/spool and /var/adm etc are used for holding queues and system log files.<br />
`/vmunix’<br />
This is the program code for the unix kernel (see below). On HPUX systems with file is called `hp-ux’. On linux it is called `linux’.<br />
`/kernel’<br />
On newer systems the kernel is built up from a number of modules which are placed in this directory.<br />
Every unix directory contains two `virtual’ directories marked by a single dot and two dots.<br />
ls -a<br />
.   ..<br />
The single dot represents the directory one is already in (the current directory). The double dots mean the directory one level up the tree from the current location. Thus, if one writes<br />
cd /usr/local<br />
cd ..<br />
the final directory is /usr. The single dot is very useful in C programming if one wishes to read `the current directory’. Since this is always called `.’ there is no need to keep track of what the current directory really is.<br />
`.’ and `..’ are `hard links’ to the true directories.<br />
Symbolic links<br />
A symbolic link is a pointer or an alias to another file. The command<br />
ln -s fromfile /other/directory/tolink<br />
makes the file fromfile appear to exist at /other/directory/tolink simultaneously. The file is not copied, it merely appears to be a part of the file tree in two places. Symbolic links can be made to both files and directories.<br />
A symbolic link is just a small file which contains the name of the real file one is interested in. It cannot be opened like an ordinary file, but may be read with the C call readlink() See section lstat and readlink. If we remove the file a symbolic link points to, the link remains — it just points nowhere.<br />
Hard links<br />
A hard link is a duplicate inode in the filesystem which is in every way equivalent to the original file inode. If a file is pointed to by a hard link, it cannot be removed until the link is removed. If a file has @math{n} hard links — all of them must be removed before the file can be removed. The number of hard links to a file is stored in the filesystem index node for the file.<br />
Getting started<br />
If you have never met unix, or another multiuser system before, then you might find the idea daunting. There are several things you should know.<br />
Logging in<br />
Each time you use unix you must log on to the system by typing a username and a password. Your login name is sometimes called an `account’ because some unix systems implement strict quotas for computer resources which have to be paid for with real money(2).</p>
<p>  login: mark<br />
  password:</p>
<p>Once you have typed in your password, you are `logged on’. What happens then depends on what kind of system you are logged onto and how. If you have a colour monitor and keyboard in front of you, with a graphical user interface, you will see a number of windows appear, perhaps a menu bar. You then use a mouse and keyboard just like any other system.<br />
This is not the only way to log onto unix. You can also log in remotely, from another machine, using the Secure Shell ssh program ( this replaces the now antiquated telnet and rlogin programs). If you use these programs, you will normally only get a text or command line interface (though graphical interfaces can easily be arranged).<br />
Once you have logged in, a short message will be printed (called Message of the Day or motd) and you will see the C-shell prompt: the name of the host you are logged onto followed by a percent sign, e.g.</p>
<p>Linux cube 2.2.19pre13 #2 Mon Feb 26 15:53:31 MET 2001 i686 unknown</p>
<p>   This is GNU/Linux – send problems to help@example.org</p>
<p>10:44pm  up 8 days, 13:34,  3 users,  load average: 0.08, 0.02, 0.01</p>
<p>There are 480 messages in your incoming mailbox.</p>
<p>Remember that every UNIX machine is a separate entity: it is not like logging onto a PC system where you log onto the `network’ i.e. the PC file server. Every UNIX machine is a server, or a client — more correctly a “peer” (equal partner). The network, in unix-land, has lots of players.<br />
The first thing you should do once you have logged on is to set a reliable password. A poor password might be okay on a PC which is not attached to a large network, but once you are attached to the Internet, you have to remember that the whole world will be trying to crack your password. Don’t think that no one will bother: some people really have nothing better to do. A password should not contain any word that could be in a list of words (in any language), or be a simple concatenation of a word and a number (e.g. mark123). It takes seconds to crack such a password. Choose instead something which is easy to remember. Feel free to use the PIN number from your bankers card in your password! This will leave you with fewer things to remember. e.g. Ma9876rk). Passwords can be up to eight characters long.<br />
Some sites allow you to change your password anywhere. Other sites require you to log onto a special machine to change your password:</p>
<p> dax%<br />
 dax% passwd<br />
 Change your password on host nexus<br />
 You cannot change it here<br />
 dax% rlogin nexus<br />
 password: ******</p>
<p> nexus% passwd<br />
 Changing password for mark<br />
 Enter login password: ********<br />
 Enter new password: ********<br />
 Reenter new passwd: ********</p>
<p>You will be prompted for your old password and your new password twice. If your network is large, it might take the system up to an hour or two to register the change in your password, so don’t forget the old one right away!<br />
Mouse buttons<br />
UNIX has three mouse buttons. On some PC’s running GNU/Linux or some other PC unix, there are only two, but the middle mouse button can be simulated by pressing both mouse buttons simultaneously. The mouse buttons have the following general functions. They may also have additional functions in special software.<br />
index finger<br />
This is used to select and click on objects. It is also used to mark out areas and copy by dragging. This is the button you normally use.<br />
middle finger<br />
Used to pull down menus. It is also used to paste a marked area somewhere at the mouse position.<br />
outer finger<br />
Pulls down menus.<br />
On a left-handed system right and left are reversed.<br />
E-mail<br />
Reading electronic mail on unix is just like any other system, but there are many programs to choose from. There are very old programs from the seventies such as<br />
mail<br />
and there are fully graphical mail programs such as<br />
tkrat<br />
mailtool<br />
Choose the program you like best. Not all of the programs support modern multimedia extensions because of their age. Some programs like tkrat have immediate mail notification alerts. To start a mail program you just type its name. If you have an icon-bar, you can click on the mail-icon.<br />
Simple commands<br />
Inexperienced computer users often prefer to use file-manager programs to avoid typing anything. With a mouse you can click your way through directories and files without having to type anything (e.g. the kfm or tkdesk programs). More experienced users generally find this to be slow and tedious after a while and prefer to use written commands. UNIX has many short cuts and keyboard features which make typed commands extremely fast and much more powerful than use of the mouse.<br />
Today the CDE, KDE and GNOME projects are the most important efforts to write graphical user interfaces for computers. The CDE (Common Desktop Environment) is a commercial program developed by IBM, Hewlett-Packard, Sun Microsystems and many other vendors. KDE (a German effort, a pun on CDE) and GNOME are free software window systems which have taken windowing to the next level. While they have borrowed and stolen many ideas from Windows’ innovative Windows 95 user interface, they have taken windowing beyond this.<br />
If you come from a Windows environment, the UNIX commands can be a little strange. It is a different way of thinking: using language to ask for exactly what you want, instead of pointing to a menu of limited choices. It is also a strange language. Because they stem from an era when keyboards had to be hit with hammer force, and machines were very slow, the UNIX command names are as short as possible, so they seem pretty cryptic. Some familiar ones which DOS borrowed from UNIX include,<br />
cd<br />
mkdir<br />
which change to a new directory and make a new directory respectively. To list the files in the current directory you use,<br />
ls<br />
To rename a file, you `move’ it:<br />
mv old-name new-name<br />
Text editing and word processing<br />
Text editing is one of the things which people spend most time doing on any computer. It is important to distinguish text editing from word processing. On a PC or MacIntosh, you are perhaps used to Word or WordPerfect for writing documents.<br />
UNIX has a Word-like program called lyx, and even several Office clones (e.g. Star Office soffice), but for the most part UNIX users do not use word processors. It is more common in the UNIX community to write all documents, regardless of whether they are letters, books or computer programs, using a non-formatting text editor. (UNIX word processors like Framemaker do exist, but they are very expensive. A version of MS-Word also exists for some unices.) Once you have written a document in a normal text editor, you call up a text formatter to make it pretty. You might think this strange, but the truth of the matter is that this two-stage process gives you the most power and flexibility–and that is what most UNIX folks like.<br />
For writing programs, or anything else, you edit a file by typing:</p>
<p> emacs myfile</p>
<p>emacs is one of dozens of text-editors. It is not the simplest or most intuitive, but it is the most powerful and if you are going to spend time learning an editor, it wouldn’t do any harm to make it this one. You could also click on emacs’ icon if you are relying on a window system. Emacs is almost certainly the most powerful text editor that exists on any system. It is not a word-processor, it is not for formatting printed documents, but it can be linked to almost any other program in order to format and print text. It contains a powerful programming language and has many intelligent features. We shall not go into the details of document formatting in this book, but only mention that programs like troff and Tex or Latex are used for this purpose to obtain typeset-quality printing. Text formatting is an area where UNIX folks do things differently to PC folks.<br />
The login environment<br />
UNIX began as a timesharing mainframe system in the seventies, when the only terminals available were text based teletype terminals or tty-s. Later, the Massachusetts Institute of Technology (MIT) developed the X-windows interface which is now a standard across UNIX platforms. Because of this history, the X-window system works as a front end to the standard UNIX shell and interface, so to understand the user environment we must first understand the shell.<br />
Shells<br />
A shell is a command interpreter. In the early days of UNIX, a shell was the only way of issuing commands to the system. Nowadays many window-based application programs provide menus and buttons to perform simple commands, but the UNIX shell remains the most powerful and flexible way of interacting with the system.<br />
After logging in and entering a password, the UNIX process init starts a shell for the user logging in. UNIX has several different kinds of shell to choose from, so that each user can pick his/her favourite command interface. The type of shell which the system starts at login is determined by the user’s entry in the passwd database. On most systems, the standard login shell is a variant of the C-shell.<br />
Shells provide facilities and commands which<br />
“ Start and stop processes (programs)<br />
“ Allow two processes to communicate through a pipe<br />
“ Allow the user to redirect the flow of input or output<br />
“ Allow simple command line editing and command history<br />
“ Define aliases to frequently used commands<br />
“ Define global “environment” variables which are used to configure the default behaviour of a variety of programs. These lie in an “associated array” for each process and may be seen with the `env’ command. Environment variables are inherited by all processes which are started from a shell.<br />
“ Provide wildcard expansion (joker notation) of filenames using `*,?,[]‘<br />
“ Provide a simple script language, with tests and loops, so that users can combine system programs to create new programs of their own.<br />
“ Change and remember the location of the current working directory, or location within the file hierarchy.<br />
The shell does not contain any more specific functions–all other commands, such as programs which list files or create directories etc., are executable programs which are independent of the shell. When you type `ls’, the shell looks for the executable file called `ls’ in a special list of directories called the command path (which is contained in the environment variable $PATH) and attempts to start this program. This allows such programs to be developed and replaced independently of the actual command interpreter.<br />
Each shell which is started can be customized and configured by editing a setup file. For the Bash shell this file is called `.bashrc’, and for the C-shell and its variants it is called `.profile’. (Note that files which begin with leading dots are not normally visible with the `ls’ command. Use `ls -a’ to view these.) Any commands which are placed in these files are interpreted by the shell before the first command prompt is issued. These files are typically used to define a command search path and terminal characteristics.<br />
On each new command line you can use the cursor keys to edit the line. The up-arrow browses back through earlier commands. CTRL-a takes you to the start of the line. CTRL-e takes you to the end of the line. The TAB can be used to save typing with the `completion’ facility See section Command/filename completion.<br />
Shell commands generally<br />
Shell commands are commands like cp, mv, passwd, cat, more, less, cc, grep, ps etc..<br />
One thing you can always bet on with Unix is that there is not just one way of doing things — there are so many standards, that there is often a bewildering array to choose from. UNIX has two main command shells. They are called sh (Bourne Shell) and csh C-shell. Their modern implementations are called Bash (Bourne Again Shell) and tcsh (T-C shell).<br />
Very few commands are actually built into the shell command line interpreter, in the same way that they are built into DOS. Rather commands are programs which exist as actual program files. When we type a command, the shell searches for a program with the same name and tries to execute it. This is very flexible, since anyone is free to write their own programs and therefore extend the command language of the system. The file must be executable, or a Command not found error will result. To see what actually happens when you type a command like gcc, try typing the following into a GNU/Linux system: (you can type this exactly as shown into a Bash shell)<br />
cube$ IFS=:</p>
<p>cube$ for dir in $PATH       # for every directory in the list path<br />
&gt;do<br />
&gt;  if [ -x $dir/gcc ]        # if the file is executable<br />
&gt;  then<br />
&gt;     echo Found $dir/gcc    # Print message found!<br />
&gt;     break                  # break out of loop<br />
&gt;  else<br />
&gt;     echo Searching $dir/gcc<br />
&gt;  fi<br />
&gt;done<br />
If you use C-shell (e.g. tcsh), try typing in the following C-shell commands directly into a C-shell.</p>
<p>nexus%  foreach dir ( $path )  # for every directory in the list path<br />
&gt;    if ( -x $dir/gcc ) then   # if the file is executable<br />
&gt;      echo Found $dir/gcc     # Print message found!<br />
&gt;      break                   # break out of loop<br />
&gt;    else<br />
&gt;      echo Searching $dir/gcc<br />
&gt;    endif<br />
&gt;  end<br />
The output of these command sequences is something like this:<br />
  Searching /usr/lang/gcc<br />
  Searching /usr/openwin/bin/gcc<br />
  Searching /usr/openwin/bin/xview/gcc<br />
  Searching /physics/lib/framemaker/bin/gcc<br />
  Searching /physics/motif/bin/gcc<br />
  Searching /physics/mutils/bin/gcc<br />
  Searching /physics/common/scripts/gcc<br />
  Found /physics/bin/gcc<br />
If you type<br />
  echo $PATH<br />
in Bourne Shell, or<br />
  echo $path<br />
in C-shell you will see the entire list of directories which are searched by the shell. If we had left out the `break’ command, we might have discovered that UNIX often has several programs with the same name, in different directories! For example,<br />
/bin/mail<br />
/usr/ucb/mail<br />
/bin/Mail</p>
<p>/bin/make<br />
/usr/local/bin/make.<br />
Also, different versions of UNIX have different conventions for placing the commands in directories, so the path list needs to be different for different types of UNIX machine. In Bash a few basic commands like cd and kill are built into the shell (as in DOS).<br />
You can find out which directory a command is stored in using<br />
type<br />
command. For example<br />
cube$ type cd<br />
cd is a shell builtin<br />
cube$ type mv<br />
mv is /bin/mv<br />
cube$<br />
type only searches the directories in $PATH and quits after the first match, so if there are several commands with the same name, you will only see the first of them using type.<br />
Finally, in the C-shell the command corresponding to type is built in and called which. In Bash which is a program:<br />
cube$ type which<br />
which is /usr/bin/which<br />
cube$ tcsh<br />
cube% which which<br />
which: shell built-in command.<br />
Take a look at the script /usr/bin/which. It is a script written in bash.<br />
Environment and shell variables<br />
Environment variables are variables which the shell keeps. They are normally used to configure the behaviour of utility programs like lpr (which sends a file to the printer) and mail (which reads and sends mail) so that special options do not have to be typed in every time you run these programs.<br />
Any program can read these variables to find out how you have configured your working environment. We shall meet these variables frequently. Here are some important variables<br />
PATH             # The search path for shell commands (bash)<br />
TERM             # The terminal type (bash and csh)<br />
DISPLAY          # X11 – the name of your display<br />
LD_LIBRARY_PATH  # Path to search for object and shared libraries<br />
HOSTNAME         # Name of this UNIX host<br />
PRINTER          # Default printer (lpr)<br />
HOME             # The path to your home directory (bash)<br />
PS1              # The default prompt for bash</p>
<p>path             # The search path for shell commands (csh)<br />
term             # The terminal type (csh)<br />
prompt           # The default prompt for csh<br />
home             # The path to your home directory (csh)</p>
<p>These variables fall into two groups. Traditionally the first group always have names in uppercase letters and are called environment variables, whereas variables in the second group have names with lowercase letters and are called shell variables– but this is only a convention. The uppercase variables are global variables, whereas the lower case variables are local variables. Local variables are not defined for programs or sub-shells started by the current shell, while global variables are inherited by all sub-shells.<br />
The Bash-shell and the C-shell use these conventions differently and not always consistently. You will see how to define these below. For now you just have to know that you can use the command env can be used in Bash shell to see all of the defined global environment variables while set lists both the global and the local variables.<br />
Wildcards<br />
Sometimes you want to be able to refer to several files in one go. For instance, you might want to copy all files ending in `.c’ to a new directory. To do this one uses wildcards. Wildcards are characters like * ? which stand for any character or group of characters. In card games the joker is a `wild card’ which can be substituted for any other card. Use of wildcards is also called filename substitution in the UNIX manuals, in the sections on sh and csh.<br />
The wildcard symbols are,<br />
`?’<br />
Match single character. e.g. ls /etc/rc.????<br />
`*’<br />
Match any number of characters. e.g. ls /etc/rc.*<br />
`[...]‘<br />
Match any character in a list enclosed by these brackets. e.g. ls [abc].C<br />
Here are some examples and explanations.<br />
`/etc/rc.????’<br />
Match all files in /etc whose first three characters are rc. and are 7 characters long.<br />
`*.c’<br />
Match all files ending in `.c’ i.e. all C programs.<br />
`*.[Cc]‘<br />
List all files ending on `.c’ or `.C’ i.e. all C and C++ programs.<br />
`*.[a-z]‘<br />
Match any file ending in .a, .b, .c, … up to .z etc.<br />
It is important to understand that the shell expands wildcards. When you type a command, the program is not invoked with an argument that contains * or ?. The shell expands the special characters first and invokes commands with the entire list of files which match the patterns. The programs never see the wildcard characters, only the list of files they stand for. To see this in action, you can type<br />
echo /etc/rc*<br />
which gives<br />
/etc/rc0 /etc/rc0.d /etc/rc1 /etc/rc1.d /etc/rc2 /etc/rc2.d /etc/rc3<br />
/etc/rc3.d /etc/rc5 /etc/rc6 /etc/rcS /etc/rcS.d<br />
All shell commands are invoked with a command line of this form. This has an important corollary. It means that multiple renaming cannot work!<br />
UNIX files are renamed using the mv command. In many microcomputer operating systems one can write<br />
rename *.x *.y<br />
which changes the file extension of all files ending in `.x’ to the same name with a `.y’ extension. This cannot work in UNIX, because the shell tries expands everything before passing the arguments to the command line.<br />
Regular expressions<br />
The wildcards belong to the shell. They are used for matching filenames. UNIX has a more general and widely used mechanism for matching strings, this is through regular expressions.<br />
Regular expressions are used by the egrep utility, text editors like ed, vi and emacs and sed and awk. They are also used in the C programming language for matching input as well as in the Perl programming language and lex tokenizer. Here are some examples using the egrep command which print lines from the file /etc/rc which match certain conditions. The construction is part of egrep. Everything in between these symbols is a regular expression. Notice that special shell symbols ! * &amp; have to be preceded with a backslash \ in order to prevent the shell from expanding them!</p>
<p># Print all lines beginning with a comment #<br />
egrep ‘(^#)’           /etc/rc</p>
<p># Print all lines which DON’T begin with #<br />
egrep ‘(^[^#])’        /etc/rc</p>
<p># Print all lines beginning with e, f or g.<br />
egrep ‘(^[efg])’       /etc/rc</p>
<p># Print all lines beginning with uppercase<br />
egrep ‘(^[A-Z])’       /etc/rc</p>
<p># Print all lines NOT beginning with uppercase<br />
egrep ‘(^[^A-Z])’      /etc/rc</p>
<p># Print all lines containing ! * &amp;<br />
egrep ‘([\!\*\&amp;])’     /etc/rc</p>
<p># All lines containing ! * &amp; but not starting #<br />
egrep ‘([^#][\!\*\&amp;])’ /etc/rc<br />
Regular expressions are made up of the following `atoms’.<br />
These examples assume that the file `/etc/rc’ exists. If it doesn’t exist on the machine you are using, try to find the equivalent by, for instance, replacing /etc/rc with /etc/rc* which will try to find a match beginning with the rc.<br />
`.’<br />
Match any single character except the end of line.<br />
`^’<br />
Match the beginning of a line as the first character.<br />
`$’<br />
Match end of line as last character.<br />
`[..]‘<br />
Match any character in the list between the square brackets.(see below).<br />
`*’<br />
Match zero or more occurrences of the preceding expression.<br />
`+’<br />
Match one or more occurrences of the preceding expression.<br />
`?’<br />
Match zero or one occurrence of the preceding expression.<br />
You can find a complete list in the UNIX manual pages. The square brackets above are used to define a class of characters to be matched. Here are some examples,<br />
“ If the square brackets contain a list of characters, $[a-z156]$ then a single occurrence of any character in the list will match the regular expression: in this case any lowercase letter or the numbers 1, 5 and 6.<br />
“ If the first character in the brackets is the caret symbol `^’ then any character except those in the list will be matched.<br />
“ Normally a dash or minus sign `-’ means a range of characters. If it is the first character after the `[' or after `[^' then it is treated literally.<br />
Nested shell commands and "<br />
The backwards apostrophes `...` can be used in all shells and also in the programming language Perl. When these are encountered in a string the shell tries to execute the command inside the quotes and replace the quoted expression by the result of that command. For example:</p>
<p>UNIX$ echo "This system's kernel type is `/usr/bin/file /boot/vmlinuz-2.2.19pre13`"<br />
This system's kernel type is /boot/vmlinuz-2.2.19pre13: Linux kernel x86 boot executable bzImage, version 2.2.19pre13</p>
<p>UNIX$ for file in `ls /local/ssl/misc/*`<br />
&gt; do<br />
&gt; echo I found a config file $file<br />
&gt; echo Its type is `/usr/bin/file $file`<br />
&gt; done<br />
I found a config file /local/ssl/misc/CA.pl<br />
Its type is /local/ssl/misc/CA.pl: perl script text<br />
I found a config file /local/ssl/misc/CA.sh<br />
Its type is /local/ssl/misc/CA.sh: Bourne shell script text<br />
I found a config file /local/ssl/misc/c_hash<br />
Its type is /local/ssl/misc/c_hash: Bourne shell script text<br />
I found a config file /local/ssl/misc/c_info<br />
Its type is /local/ssl/misc/c_info: Bourne shell script text<br />
I found a config file /local/ssl/misc/c_issuer<br />
Its type is /local/ssl/misc/c_issuer: Bourne shell script text<br />
I found a config file /local/ssl/misc/c_name<br />
Its type is /local/ssl/misc/c_name: Bourne shell script text<br />
I found a config file /local/ssl/misc/der_chop<br />
Its type is /local/ssl/misc/der_chop: perl script text<br />
This is how we insert the result of a shell command into a text string or variable.<br />
UNIX command overview<br />
Important keys<br />
TAB<br />
The TAB key is used by Bash and Emacs for "filename completion", i.e. when you are uncertain of the correct name of something, or simply can't be bothered to type it out, you can hit TAB to either finish off the word, or show you alternative choices. e.g. try in Bash<br />
cube$ loadTAB<br />
loadkeys    loadmeter   loadunimap<br />
This shows the possible completions of commands which match "load". Type one more letter and TAB, and the rest will be filled in.<br />
CTRL-A<br />
Jump to start of line. If `screen' is active, this prefixes all control key commands for `screen' and then the normal CTRL-A is replaced by CTRL-a a.<br />
CTRL-C<br />
Interrupt or break key. Sends signal 15 to a process.<br />
CTRL-D<br />
Signifies `EOF' (end of file) or shows expansion matches in command/filename completion See section Command/filename completion.<br />
CTRL-E<br />
Jump to end of line.<br />
CTRL-L<br />
Clear screen in newer shells and in emacs. Same as `clear' in the shell.<br />
CTRL-Z<br />
Suspend the present process, but do not destroy it. This sends signal 18 to the process.<br />
Alternative shells<br />
bash<br />
The Bourne Again shell, an improved sh.<br />
csh<br />
The standard C-shell.<br />
jsh<br />
The same as sh, with C-shell style job control.<br />
ksh<br />
The Korn shell, an improved sh.<br />
sh<br />
The original Bourne shell.<br />
sh5<br />
On ULTRIX systems the standard Bourne shell is quite stupid. sh5 corresponds to the normal Bourne shell on these systems.<br />
tcsh<br />
An improved C-shell.<br />
zsh<br />
An improved sh.<br />
Window based terminal emulators<br />
xterm<br />
The standard X11 terminal window.<br />
shelltool, cmdtool<br />
Openwindows terminals from Sun Microsystems. These are not completely X11 compatible during copy/paste operations.<br />
screen<br />
This is not a window in itself, but allows you to emulate having several windows inside a single (say) xterm window. The user can switch between different windows and open new ones, but can only see one window at a time See section Multiple screens.<br />
Remote shells and logins<br />
The best way to log onto another system is to use the Secure Shell command ssh. This replaces the now obsolete commands:<br />
rlogin<br />
Login onto a remote UNIX system.<br />
rsh<br />
Open a shell on a remote system (require access rights).<br />
telnet<br />
Open a connection to a remove system using the telnet protocol.<br />
These old commands are insecure andnote very flexible. The Secure Shell offers encryption, strong authentication and greater functionality. It can be used to run a single program on a remote machine, or to login on the remote machine.</p>
<p>cube$ ssh metaverse date<br />
cube$ ssh metaverse<br />
Text editors<br />
ed<br />
An ancient line-editor.<br />
vi<br />
Visual interface to ed. This is the only "standard" UNIX text editor supplied by vendors.<br />
emacs<br />
The most powerful UNIX editor. A fully configurable, user programmable editor which works under X11 and on tty-terminals.<br />
xemacs<br />
A pretty version of emacs for X11 windows.<br />
pico<br />
A tty-terminal only editor, comes as part of the PINE mail package.<br />
xedit<br />
A test X11-only editor supplied with X-windows.<br />
textedit<br />
A simple X11-only editor supplied by Sun Microsystems.<br />
File handling commands<br />
ls<br />
List files in specified directory (like dir on other systems).<br />
cp<br />
Copy files.<br />
mv<br />
Move or rename files.<br />
touch<br />
Creates an empty new file if none exists, or updates date and time stamps on existing files.<br />
rm, unlink<br />
Remove a file or link (delete).<br />
mkdir, rmdir<br />
Make or remove a directory. A directory must be empty in order to be able to remove it.<br />
cat<br />
Concatenate or join together a number of files. The output is written to the standard output by default. Can also be used to simply print a file on screen.<br />
lp, lpr<br />
Line printer. Send a file to the default printer, or the printer defined in the `PRINTER' environment variable.<br />
lpq, lpstat<br />
Show the status of the print queue.<br />
File browsing<br />
more<br />
Shows one screen full at a time. Possibility to search for a string and edit the file. This is like `type file | more' in DOS.<br />
less<br />
An enhanced version of more.<br />
mc<br />
Midnight commander, a free version of the `Norton Commander' PC utility for UNIX. (Only for non-serious UNIX users...)<br />
kfm<br />
A window based file manager with icons and all that nonsense.<br />
Ownership and granting access permission<br />
chmod<br />
Change file access mode.<br />
chown, chgrp<br />
Change owner and group of a file. The GNU version of chown allows both these operations to be performed together using the syntax chown owner.group file.<br />
acl<br />
On newer Unices, Access control lists allow access to be granted on a per-user basis rather than by groups.<br />
Extracting from and rebuilding files<br />
cut<br />
Extract a column in a table<br />
paste<br />
Merge several files so that each file becomes a column in a table.<br />
sed<br />
A batch text-editor for searching, replacing and selecting text without human intervention.<br />
awk<br />
A prerunner to the Perl language, for extracting and modifying textfiles.<br />
rmcr<br />
Strip carriage return (ASCII 13) characters from a file. Useful for converting DOS files to UNIX.<br />
Locating files<br />
find<br />
Search for files from a specified directory using various criteria.<br />
locate<br />
Fast search in a global file database for files containing a search-string.<br />
whereis<br />
Look for a command and its documentation on the system.<br />
Disk usage.<br />
du<br />
Show number of blocks used by a file or files.<br />
df<br />
Show the state of usage for one or more disk partitions.<br />
Show other users logged on<br />
users<br />
Simple list of other users.<br />
finger<br />
Show who is logged onto this and other systems.<br />
who<br />
List of users logged into this system.<br />
w<br />
Long list of who is logged onto this system and what they are doing.<br />
Contacting other users<br />
write<br />
Send a simple message to the named user, end with CTRL-D. The command `mesg n' switches off messages receipt.<br />
talk<br />
Interactive two-way conversation with named user.<br />
irc<br />
Internet relay chat. A conferencing system for realtime multi-user conversations, for addicts and losers.<br />
Mail senders/readers<br />
mail<br />
The standard (old) mail interface.<br />
Mail<br />
Another mail interface.<br />
elm<br />
Electronic Mail program. Lots of functionality but poor support for multimedia.<br />
pine<br />
Rumours (untrue) are that pine stands for Pine is Not Elm; it actually stands for nothing at all. Improved support for multimedia but very slow and rather stupid at times. Some of the best features of elm have been removed!<br />
mailtool<br />
Sun's openwindows client program.<br />
rmail<br />
A mail interface built into the emacs editor.<br />
netscape mail<br />
A mail interface built into the netscape navigator.<br />
zmail<br />
A commercial mail package.<br />
tkrat<br />
A graphical mail reader which supports most MIME types, written in tcl/tk. This program has a nice feel and allows you to create a searchable database of old mail messages, but has a hopeless locking mechanism.<br />
File transfer<br />
ftp<br />
The File Transfer program - copies files to/from a remote host.<br />
ncftp<br />
An enhanced ftp for anonymous login.<br />
Compilers<br />
cc<br />
The C compiler.<br />
CC<br />
The C++ compiler.<br />
gcc<br />
The GNU C compiler.<br />
g++<br />
The GNU C++ compiler.<br />
javac<br />
A generator of Java bytecode.<br />
java<br />
A Java Virtual Machine.<br />
ld<br />
The system linker/loader.<br />
ar<br />
Archive library builder.<br />
dbx<br />
A symbolic debugger.<br />
gdb<br />
The GNU symbolic debugger.<br />
xxgdb<br />
The GNU debugger with a window driven front-end.<br />
ddd<br />
A motif based front-end to the gdb debugger.<br />
Other interpreted languages<br />
perl<br />
Practical extraction an report language.<br />
tcl<br />
A perl-like language with special support for building user interfaces and command shells.<br />
php<br />
Personal Home Page Tools (officially "PHP: Hypertext Preprocessor"). A server-side HTML-embedded scripting language.<br />
scheme<br />
A lisp-like extensible scripting language from GNU.<br />
mercury<br />
A prolog-like language for artificial intelligence.<br />
Processes and system statistics<br />
ps<br />
List system process table.<br />
vmstat<br />
List kernel virtual-memory statistics.<br />
netstat<br />
List network connections and statistics.<br />
rpcinfo<br />
Show rpc information.<br />
showmount<br />
Show clients mounting local filesystems.<br />
System identity<br />
uname<br />
Display system name and operating system release.<br />
hostname<br />
Show the name of this host.<br />
domainname<br />
Show the name of the local NIS domain. Normally this is chosen to be the same as the BIND/DNS domain, but it need not be.<br />
nslookup<br />
Interrogate the DNS/BIND name service (hostname to IP address conversion).<br />
Internet resources<br />
archie, xarchie<br />
Search the internet ftp database for files.<br />
xrn, fnews<br />
Read news (browser).<br />
netscape, xmosaic<br />
Read world wide web (WWW) (browser).<br />
Text formatting and postscript<br />
tex, latex<br />
Donald Knuth's text formatting language, pronounced "tek" (the x is really a Greek "chi"). Used widely for technical publications. Compiles to dvi (device independent) file format.<br />
texinfo<br />
A hypertext documentation system using tex and "info" format. This is the GNU documentation system. This UNIX guide is written in texinfo!!!<br />
xdvi<br />
View a tex dvi file on screen.<br />
dvips<br />
Convert dvi format into postscript.<br />
ghostview, ghostscript<br />
View a postscript file on screen.<br />
Picture editors and processors<br />
xv<br />
Handles, edits and processes pictures in a variety of standard graphics formats (gif, jpg, tiff etc). Use xv -quit to place a picture on your root window.<br />
xpaint<br />
A simple paint program.<br />
xfig<br />
A line drawing figure editor. Produces postscript, tex, and a variety of other output formats.<br />
xmgr<br />
A graphing and analysis program.<br />
xsetroot<br />
Load an X-bitmap image into the screen (root window) background. Small images are tiled.<br />
Miscellaneous<br />
date<br />
Print the date and time.<br />
ispell<br />
Spelling checker.<br />
xcalc<br />
A graphical calculator.<br />
dc,bc<br />
Text-based calculators.<br />
xclock<br />
A clock!<br />
ping<br />
Send a "sonar" ping to see if another UNIX host is alive.<br />
Terminals<br />
In order to communicate with a user, a shell needs to have access to a terminal. UNIX was designed to work with many different kinds of terminals. Input/output commands in UNIX read and write to a virtual terminal. In reality a terminal might be a text-based Teletype terminal (called a tty for short) or a graphics based terminal; it might be 80-characters wide or it might be wider or narrower. UNIX take into account these possibility by defining a number of instances of terminals in a more or less object oriented way.<br />
Each user's terminal has to be configured before cursor based input/output will work correctly. Normally this is done by choosing one of a number of standard terminal types a list which is supplied by the system. In practice the user defines the value of the environment variable `TERM' to an appropriate name. Typical examples are `vt100' and `xterm'. If no standard setup is found, the terminal can always be configured manually using UNIX's most cryptic and opaque of commands: `stty'.<br />
The job of configuring terminals is much easier now that hardware is more standard. Users' terminals are usually configured centrally by the system administrator and it is seldom indeed that one ever has to choose anything other than `vt100' or `xterm'.<br />
The X window system<br />
Because UNIX originated before windowing technology was available, the user-interface was not designed with windowing in mind. The X window system attempts to be like a virtual machine park, running a different program in each window. Although the programs appear on one screen, they may in fact be running on UNIX systems anywhere in the world, with only the output being local to the user's display. The standard shell interface is available by running an X client application called `xterm' which is a graphical front-end to the standard UNIX textual interface.<br />
The `xterm' program provides a virtual terminal using the X windows graphical user interface. It works in exactly the same way as a tty terminal, except that standard graphical facilities like copy and paste are available. Moreover, the user has the convenience of being able to run a different shell in every window. For example, using the `rlogin' command, it is possible to work on the local system in one window, and on another remote system in another window. The X-window environment allows one to cut and paste between windows, regardless of which host the shell runs on.<br />
The components of the X-window system<br />
The X11 system is based on the client-server model. You might wonder why a window system would be based on a model which was introduced for interprocess communication, or network communication. The answer is straightforward.<br />
The designers of the X window system realized that network communication was to be the paradigm of the next generation of computer systems. They wanted to design a system of windows which would enable a user to sit at a terminal in Massachusetts and work on a machine in Tokyo -- and still be able to get high quality windows displayed on their terminal. The aim of X windows from the beginning is to create a distributed window environment.<br />
When I log onto my friend's Hewlett Packard workstation to use the text editor (because I don't like the one on my EUNUCHS workstation) I want it to work correctly on my screen, with my keyboard -- even though my workstation is manufactured by a different company. I also want the colours to be right despite the fact that the HP machine uses a completely different video hardware to my machine. When I press the curly brace key {, I want to see a curly brace, and not some hieroglyphic because the HP station uses a different keyboard.<br />
These are the problems which X tries to address. In a network environment we need a common window system which will work on any kind of hardware, and hide the differences between different machines as far as possible. But it has to be flexible enough to allow us to change all of the things we don't like -- to choose our own colours, and the kind of window borders we want etc. Other windowing systems (like Microsoft windows) ignore these problems and thereby lock the user to a single vendors products and a single operating system. (That, of course, is no accident.)<br />
The way X solves this problem is to use the client server model. Each program which wants to open a window on somebody's compute screen is a client of the X window service. To get something drawn on a user's screen, the client asks a server on the host of interest to draw windows for it. No client ever draws anything itself -- it asks the server to do it on its behalf. There are several reasons for this:<br />
" The clients can all talk a common `window language' or protocol. We can hide the difference between different kinds of hardware by making the machine-specific part of drawing graphics entirely a problem of implementing the server on the particular hardware. When a new type of hardware comes along, we just need to modify the server -- none of the clients need to be modified.<br />
" We can contact different servers and send our output to different hardware -- thus even though a program is running on a CPU in Tokyo, it can ask the server in Massachusetts to display its window for it.<br />
" When more than one window is on a user's display, it eventually becomes necessary to move the windows around and then figure out which windows are on top of which other windows etc. If all of the drawing information is kept in a server, it is straightforward to work out this information. If every client drew where it wanted to, it would be impossible to know which window was supposed to be on top of another.<br />
In X, the window manager is a different program to the server which does the drawing of graphics -- but the client-server idea still applies, it just has one more piece to its puzzle.<br />
How to set up X windows<br />
The X windows system is large and complex and not particularly user friendly. When you log in to the system, X reads two files in your home directory which decide which applications will be started what they will look like. The files are called<br />
.Xsession<br />
This file is a shell script which starts up a number of applications as background processes and exits by calling a window manager. Here is a simple example file<br />
#!/bin/bash<br />
#<br />
# .xsession file<br />
#<br />
#</p>
<p>PATH="/usr/bin:/bin:/local/gnu/bin:/usr/X11R6/bin"</p>
<p>#<br />
# List applications here, with &amp; at the end<br />
# so they run in the background<br />
#</p>
<p>  xterm -T NewTitle -sl 1000 -geometry 90x45+16+150 -sb &amp;<br />
  xclock &amp;<br />
  xbiff -geometry 80x80+510+0 &amp;<br />
  netscape -iconic&amp;</p>
<p># Start a window manager. Exec replaces this script with<br />
# the fvwm process, so that it doesn't exist as a separate<br />
# (useless) process.</p>
<p>  exec /local/bin/fvwm</p>
<p>.Xdefaults<br />
This file specifies all of the resources which X programs use. It can be used to change the colours used by applications, or font types etc. The subject of X-resources is a large one and we don't have time for it here. Here is a simple example, which shows how you can make your over-bright xterm and emacs windows less bright grey shade.<br />
xterm*background: LightGrey<br />
Emacs*background: grey92<br />
Xemacs*background: grey92<br />
X displays and authority<br />
In the terminology used by X11, every client program has to contact a display in order to open a window. A display is a virtual screen which is created by the X server on a particular host. X can create several separate displays on a given host, though most machines only have one.<br />
When an X client program wants to open a window, it looks in the UNIX environment variable `DISPLAY' for the IP address of a host which has an X server it can contact. For example, if we wrote<br />
DISPLAY="myhost:0"<br />
export DISPLAY<br />
the client would try to contact the X server on `myhost' and ask for a window on display number zero (the usual display). If we wrote<br />
DISPLAY="198.112.208.35:0"<br />
export DISPLAY<br />
the client would try to open display zero on the X server at the host with the IP address `198.112.208.35'.<br />
Clearly there must be some kind of security mechanism to prevent just anybody from opening windows on someone's display. X has two such mechanisms:<br />
xhost<br />
This mechanism is now obsolete. The `xhost' command is used to define a list of hosts which are allowed to open windows on the user's display. It cannot distinguish between individual users. i.e. the command xhost yourhost would allow anyone using yourhost to access the local display. This mechanism is only present for backward compatibility with early versions of X windows. Normally one should use the command xhost - to exclude all others from accessing the display.<br />
Xauthority<br />
The Xauthority mechanism has replaced the xhost scheme. It provides a security mechanism which can distinguish individual users, not just hosts. In order for a user to open a window on a display, he/she must have a ticket--called a "magic cookie". This is a binary file called `.Xauthority' which is created in the user's home directory when he/she first starts the X-windows system. Anyone who does not have a recent copy of this file cannot open windows or read the display of the user's terminal. This mechanism is based on the idea that the user's home directory is available via NFS on all hosts he/she will log onto, and thus the owner of the display will always have access to the magic cookie, and will therefore always be able to open windows on the display. Other users must obtain a copy of the file in order to open windows there. The command xauth is an interactive utility used for controlling the contents of the `.Xauthority' file. See the `xauth' manual page for more information.<br />
Multiple screens<br />
The window paradigm has been very successful in many ways, but anyone who has used a window system knows that the screen is simply not big enough for all the windows one would like! UNIX has several solutions to this problem.<br />
One solution is to attach several physical screens to a terminal. The X window system can support any number of physical screens of different types. A graphical designer might want a high resolution colour screen for drawing and a black and white screen for writing text, for instance. The disadvantage with this method is the cost of the hardware.<br />
A cheaper solution is to use a window manager such as `fwvm' which creates a virtual screen of unlimited size on a single monitor. As the mouse pointer reaches the edge of the true screen, the window manager replaces the display with a new "blank screen" in which to place windows. A miniaturized image of the windows on a control panel acts as a map which makes it possible to find the applications on the virtual screen.<br />
Yet another possibility is to create virtual displays inside a single window. In other words, one can collapse several shell windows into a single `xterm' window by running the program `screen'. The screen command allows you to start several shells in a single window (using CTRL-a CTRL-c) and to switch between them (by typing CTRL-a CTRL-n). It is only possible to see one shell window at a time, but it is still possible to cut and paste between windows and one has a considerable saving of space. The `screen' command also allows you to suspend a shell session, log out, log in again later and resume the session precisely where you left off.<br />
Here is a summary of some useful screen commands:<br />
screen<br />
Start the screen server.<br />
screen -r<br />
Resume a previously suspended screen session if possible.<br />
CTRL-a CTRL-c<br />
Start a new shell on top of the others (a fresh `screen') in the current window.<br />
CTRL-a CTRL-n<br />
Switch to the next `screen'.<br />
CTRL-a CTRL-a<br />
Switch to the last screen used.<br />
CTRL-a a<br />
When screen is running, CTRL-a is used for screen commands and cannot therefore be used in its usual shell meaning of `jump to start of line'. CTRL-a a replaces this.<br />
CTRL-a CTRL-d<br />
Detach the screen session from the current window so that it can be resumed later. It can be resumed with the `screen -r' command.<br />
CTRL-a ?<br />
Help screen.<br />
Files and access<br />
To prevent all users from being able to access all files on the system, UNIX records information about who creates files and also who is allowed to access them later.<br />
Each user has a unique username or loginname together with a unique user id or uid. The user id is a number, whereas the login name is a text string -- otherwise the two express the same information. A file belongs to user A if it is owned by user A. User A then decides whether or not other users can read, write or execute the file by setting the protection bits or the permission of the file using the command chmod.<br />
In addition to user identities, there are groups of users. The idea of a group is that several named users might want to be able to read and work on a file, without other users being able to access it. Every user is a member of at least one group, called the login group and each group has both a textual name and a number (group id). The uid and gid of each user is recorded in the file /etc/passwd (See chapter 6). Membership of other groups is recorded in the file /etc/group or on some systems /etc/logingroup.<br />
Protection bits<br />
The following output is from the command ls -lag executed on a SunOS type machine.</p>
<p>lrwxrwxrwx  1 root     wheel           7 Jun  1  1993 bin -&gt; usr/bin<br />
-r--r--r--  1 root     bin        103512 Jun  1  1993 boot<br />
drwxr-sr-x  2 bin      staff       11264 May 11 17:00 dev<br />
drwxr-sr-x 10 bin      staff        2560 Jul  8 02:06 etc<br />
drwxr-sr-x  8 root     wheel         512 Jun  1  1993 export<br />
drwx------  2 root     daemon        512 Sep 26  1993 home<br />
-rwxr-xr-x  1 root     wheel      249079 Jun  1  1993 kadb<br />
lrwxrwxrwx  1 root     wheel           7 Jun  1  1993 lib -&gt; usr/lib<br />
drwxr-xr-x  2 root     wheel        8192 Jun  1  1993 lost+found<br />
drwxr-sr-x  2 bin      staff         512 Jul 23  1992 mnt<br />
dr-xr-xr-x  1 root     wheel         512 May 11 17:00 net<br />
drwxr-sr-x  2 root     wheel         512 Jun  1  1993 pcfs<br />
drwxr-sr-x  2 bin      staff         512 Jun  1  1993 sbin<br />
lrwxrwxrwx  1 root     wheel          13 Jun  1  1993 sys-&gt;kvm/sys<br />
drwxrwxrwx  6 root     wheel         732 Jul  8 19:23 tmp<br />
drwxr-xr-x 27 root     wheel        1024 Jun 14  1993 usr<br />
drwxr-sr-x 10 bin      staff         512 Jul 23  1992 var<br />
-rwxr-xr-x  1 root     daemon    2182656 Jun  4  1993 vmUNIX</p>
<p>The first column is a textual representation of the protection bits for each file. Column two is the number of hard links to the file (See exercises below). The third and fourth columns are the user name and group name and the remainder show the file size in bytes and the creation date. Notice that the directories /bin and /sys are symbolic links to other directories.<br />
There are sixteen protection bits for a UNIX file, but only twelve of them can be changed by users. These twelve are split into four groups of three. Each three-bit number corresponds to one octal number.<br />
The leading four invisible bits gives information about the type of file: is the file a plain file, a directory or a link. In the output from ls this is represented by a single character: -, d or l.<br />
The next three bits set the so-called s-bits and t-bit which are explained below.<br />
The remaining three groups of three bits set flags which indicate whether a file can be read `r', written to `w' or executed `x' by (i) the user who created them, (ii) the other users who are in the group the file is marked with, and (iii) any user at all.<br />
For example, the permission<br />
Type Owner Group Anyone<br />
  d  rwx   r-x   ---<br />
tells us that the file is a directory, which can be read and written to by the owner, can be read by others in its group, but not by anyone else.<br />
Note about directories. It is impossible to cd to a directory unless the x bit is set. That is, directories must be `executable' in order to be accessible.<br />
Here are some examples of the relationship between binary, octal and the textual representation of file modes.<br />
Binary  Octal   Text</p>
<p> 001      1       x<br />
 010      2       w<br />
 100      4       r<br />
 110      6      rw-<br />
 101      5      r-x<br />
  -      644   rw-r--r--</p>
<p>It is well worth becoming familiar with the octal number representation of these permissions.<br />
Chmod<br />
The chmod command changes the permission or mode of a file. Only the owner of the file or the superuser can change the permission. Here are some examples of its use. Try them.<br />
# make read/write-able for everyone<br />
chmod a+w myfile   </p>
<p># add the 'execute' flag for directory<br />
chmod u+x mydir/  </p>
<p># open all files for everyone<br />
chmod 755 *       </p>
<p># set the s-bit on my-dir's group<br />
chmod g+s mydir/   </p>
<p># descend recursively into directory opening all files<br />
chmod -R a+r dir<br />
Umask<br />
When a new file gets created, the operating system must decide what default protection bits to set on that file. The variable umask decides this. umask is normally set by each user in his or her .cshrc file (see next chapter). For example<br />
umask 077    # safe<br />
umask 022    # liberal<br />
According the UNIX documentation, the value of umask is `XOR'ed (exclusive `OR') with a value of 666 &amp; umask for plain files or 777 &amp; umask for directories in order to find out the standard protection. Actually this is not quite true: `umask' only removes bits, it never sets bits which were not already set in 666. For instance<br />
umask               Permission</p>
<p>077                 600 (plain)<br />
077                 700 (dir)<br />
022                 644 (plain)<br />
022                 755 (dir)<br />
The correct rule for computing permissions is not XOR but `NOT AND'.<br />
Making programs executable<br />
A UNIX program is normally executed by typing its pathname. If the x execute bit is not set on the file, this will generate a `Permission denied' error. This protects the system from interpreting nonsense files as programs. To make a program executable for someone, you must therefore ensure that they can execute the file, using a command like<br />
chmod u+x filename<br />
This command would set execute permissions for the owner of the file;<br />
chmod ug+x filename<br />
would set execute permissions for the owner and for any users in the same group as the file. Note that script programs must also be readable in order to be executable, since the shell has the interpret them by reading.<br />
chown and chgrp<br />
These two commands change the ownership and the group ownership of a file. Only the superuser can change the ownership of a file on most systems. This is to prevent users from being able to defeat quota mechanisms. (On some systems, which do not implement quotas, ordinary users can give a file away to another user but not get it back again.) The same applies to group ownership.<br />
Making a group<br />
Normally users other than root cannot define their own groups. This is a weakness in UNIX from older times which no one seems to be in a hurry to change.<br />
s-bit and t-bit (sticky bit)<br />
The s and t bits have special uses. They are described as follows.<br />
Octal     Text       Name</p>
<p>4000      chmod u+s  Setuid bit<br />
2000      chmod g+s  Setgid bit<br />
1000      chmod +t   Sticky bit<br />
The effect of these bits differs for plain files and directories and differ between different versions of UNIX. You should check the manual page man sticky to find out about your system! The following is common behaviour.<br />
For executable files, the setuid bit tells UNIX that regardless of who runs the program it should be executed with the permissions and rights of owner of the file. This is often used to allow normal users limited access to root privileges. A setuid-root program is executed as root for any user. The setgid bit sets the group execution rights of the program in a similar way.<br />
In BSD UNIX, if the setgid bit is set on a directory then any new files created in that directory assume the group ownership of the parent directory and not the logingroup of the user who created the file. This is standard policy under system 5.<br />
A directory for which the sticky bit is set restrict the deletion of files within it. A file or directory inside a directory with the t-bit set can only be deleted or renamed by its owner or the superuser. This is useful for directories like the mail spool area and /tmp which must be writable to everyone, but should not allow a user to delete another user's files.<br />
(Ultrix) If an executable file is marked with a sticky bit, it is held in the memory or system swap area. It does not have to be fetched from disk each time it is executed. This saves time for frequently used programs like ls.<br />
(Solaris 1) If a non-executable file is marked with the sticky bit, it will not be held in the disk page cache -- that is, it is never copied from the disk and held in RAM but is written to directly. This is used to prevent certain files from using up valuable memory.<br />
On some systems (e.g. ULTRIX), only the superuser can set the sticky bit. On others (e.g. SunOS) any user can create a sticky directory.<br />
Bourne Again shell<br />
The Bourne Again shell (Bash) is the command interpreter which you use to run programs and utilities. It contains a simple programming language for writing tailor-made commands, and allows you to join together UNIX commands with pipes. It is a configurable environment, and once you know it well, it is the most efficient way of working with UNIX.<br />
The Bourne Again shell was written by the Free Software Foundation as a part of the GNU project and Bash is the default shell in most GNU/Linux distributions. Because of its command line editing features, it is much more efficient for interactive use than Bourne shell, the original UNIX shell. Most of the system scripts in UNIX are written in the Bourne shell. Although Bash includes many extensions and features not found in the Bourne shell, it maintains compatibility with it so that you can run Bourne shell scripts under Bash. On many GNU/Linux systems Bourne shell (`/bin/sh') is symbolically linked to Bash (`/bin/bash') so that the scripts that require the presence of the Bourne shell still run. If you want to write a platform independent shell script able to run on as many UNIX variants as possible, you should stick to Bourne shell syntax and avoid the Bash extensions.<br />
`~/.bashrc' and `~/.bash_profile' files<br />
When you log on to a GNU/Linux system and your login shell is defined in `/etc/passwd' to be Bash, it first executes commands in the `/etc/profile' file. It then searches for the `~/.bash_profile', `~/.bash_login' or `~/.profile' file, in this order, and executes commands in the first of these that is found and is readable. When a login exits, it executes commands in the `~/.bash_logout' file.<br />
When you start an non-login interactive Bash shell, it only executes commands in the `~/.bashrc' file, if it exists and is readable. However, this shell inherits any environment (exported) variables from the parent shell, so environment variables set in `/etc/profile' and `~/.bash_profile' are passed onto the non-login shells and later to its subshells.<br />
Here is a very simple example `~/.bashrc' file:<br />
#<br />
# .bashrc - read in by every bash that starts.<br />
#</p>
<p>umask 077             # Set the default file creation mask<br />
PATH="~/bin:$PATH"    # Inserts own bin directory first in PATH<br />
PS1="`uname`:\h\$ "  # prompt<br />
PS2="\h &gt; "          # prompt for foreach and while<br />
PRINTER=myprinter</p>
<p># Aliases are shortcuts to UNIX commands<br />
alias h=history<br />
alias ll="ls -l"<br />
alias cp='cp -i'<br />
alias rm='rm -i'<br />
alias c='ssh cube'</p>
<p>In order to make sure your `~/.bashrc' file is read when logging on with ssh to another machine, you may start your `~/.bash_profile' file like this:<br />
#<br />
# .bash_profile - read in every login.<br />
#</p>
<p>if [ -f ~/.bashrc ]<br />
then<br />
   source ~/.bashrc  # runs .bashrc as if they where<br />
                     # typed into this file<br />
fi<br />
Variables and export<br />
Shell variables are defined using the syntax<br />
VARIABLE=”username is”<br />
myname=”`whoami`”<br />
It is important that there be no space between the variable and the equals sign. These variables are then referred to using the dollar `$’ symbol.<br />
$ echo “My $VARIABLE $myname”<br />
My username is mark<br />
When assigning values to variables the dollar symbol is never used. By default these variables are local – that is they will not be passed on to programs and sub-shells running under the current shell. To make them global (so that child processes will inherit them) we use the command<br />
export VARIABLE<br />
This adds the variable to the process environment. Under Bash (but not under the old Bourne shell) it is also possible to declare a variable to be global on a single line by<br />
export GLOBALVAR=”global”<br />
The command<br />
set -a<br />
changes the default so that all variables, after the command are created global.<br />
Arrays or lists are often simulated in Bourne shell by sandwiching the colon `:’ symbol between items<br />
PATH=/bin:/usr/bin:/etc:/local/bin:.</p>
<p>LD_LIBARAY_PATH=/usr/lib:/usr/openwin/lib:/local/lib<br />
but there is no real facility for arrays in the Bourne shell. Note that the UNIX `cut’ command can be used to extract the elements of the list. Loops can also read such lists directly See section Loops in Bash. However, Bash version 2.x supports arrays as seen in the next section.<br />
The value of a variable is given by the dollar symbol. It is also possible to use curly braces around the variable name to `protect’ the variable from interfering text. For example:<br />
$ animal=worm<br />
$ echo book$animal<br />
bookworm<br />
$ thing=book<br />
$ echo $thingworm<br />
                       (nothing..)<br />
$ echo ${thing}worm<br />
bookworm</p>
<p>Default values can be given to variables in the Bourne shell. The following commands illustrate this.<br />
echo ${var-”No value set”}<br />
echo ${var=”Octopus”}<br />
echo ${var+”Forced value”}<br />
echo ${var?”No such variable”}<br />
The first of these prints out the contents of `$var’, if it is defined. If it is not defined the variable is substituted for the string “No value set”. The value of `var’ is not changed by this operation. It is only for convenience.<br />
The second command has the same effect as the first, but here the value of `$var’ is actually changed to “Octopus” if `$var’ is not set.<br />
The third version is slightly peculiar. If `$var’ is already set, its value will be forced to be “Forced value”, otherwise it is left undefined.<br />
Finally the last instance issues an error message “No such variable” if `$var’ is not defined.<br />
In Bash 2.x it is possible to extract parts of the string a variable is set to using the construction ${variable:offset:length}| as shown in the next example.<br />
var=”abcdefg”<br />
middle=${var:2:3}<br />
echo $middle<br />
cde<br />
An offset of 2 skips the first 2 characters and a string of length 3 is extracted from the middle of the string.<br />
Bash arrays<br />
The original Bourne shell does not have arrays. Bash version 2.x does have arrays, however. An array can be assigned from a string of words separated by whitespaces or the individual elements of the array can be set individually.<br />
colours=(red white green)<br />
colours[3]=”yellow”<br />
An element of the array must be referred to using curly braces.<br />
echo ${colours[1]}<br />
white<br />
Note that the first element of the array has index 0. The set of all elements is referred to by ${colours[*]}.<br />
echo ${colours[*]}<br />
red white green yellow<br />
echo ${#colours[*]}<br />
4<br />
As seen the number of elements in an array is given by ${#colours[*]}.<br />
Stdin, stdout, stderr and redirection to and from files<br />
When the shell starts up, it inherits three files: `stdin’, `stdout’, and `stderr’. Standard input normally comes from the keyboard. Standard output and standard error normally go to the screen. There are times you want to read input from a file or send output of errors to a file. This can be accomplished by using I/O redirection.<br />
In Bash and the Bourne shell, the standard input/output files are referred to by numbers rather than by names.<br />
stdin<br />
File number 0<br />
stdout<br />
File number 1<br />
stderr<br />
File number 2<br />
The default routes for these files can be changed by redirection. The output of the command echo is by default sent to the screen, that is the stdout with file number 1 is sent to the screen. Using redirection operators it is possible to redirect the standard out of echo to where we want it. We can send output to a file with the following command.<br />
echo “should be sent to a file” &gt; file.txt<br />
This creates a new file `file.txt’ containing the string ’should be sent to a file’. The redirection operator could have been given as 1&gt;, but it is understood that standard out is meant when skipping the number of the file handle. The single ‘&gt;’ always creates a new file, while ‘&gt;&gt;’ appends to the end of a file.<br />
If you had mistyped the command echo the result would have been:<br />
ehco “should be sent to a file” &gt; file.txt<br />
bash: ehco: command not found<br />
The standard error with file handle 2 is by default sent to the screen, independent of where standard out (1) is sent. If you like you can redirect stdout to another or the same file.<br />
ehco “should be sent to a file” &gt; file.txt 2&gt; error.txt<br />
cat error.txt<br />
bash: ehco: command not found<br />
There are several ways to send stderr to the same file as stdin is redirected to. The following three commands are equivalent.<br />
ehco “should be sent to a file” &gt;&amp; file.txt<br />
ehco “should be sent to a file” &gt;  file.txt 2&gt; file.txt<br />
ehco “should be sent to a file” &gt;  file.txt 2&gt;&amp;1<br />
The string 2&gt;&amp;1 means that stderr(2) should be sent to the same file as stdout(1). This is the only why to do this under the Bourne shell and this construction is therefore often seen in system shell scripts.<br />
Furthermore it is possible to force a command which by default takes standard input from the keyboard, to read input from a file by redirecting stdin. The mail-command expects input from keyboard, but the ‘&lt;’ redirection operator makes it send the password file to the user mark:<br />
/bin/mail mark &lt; /etc/passwd<br />
The following table summarizes the most important redirection operators:<br />
Redirection operator    What it does</p>
<p>                       Redirects output<br />
&gt;&gt;                      Appends output<br />
2&gt;                      Redirects error<br />
&gt;&amp;                      Redirects output and error (Bash only)<br />
2&gt;&amp;1                    Redirects error where output (1) is going</p>
<p>Pipes<br />
A pipe takes the output from the command on the left-hand side of the pipe symbol and sends it to the input of the command on the right-hand side of the pipe symbol. A pipeline can consist of several pipes and this makes pipes a very powerful tool. It enables us to combine all the small and efficient UNIX commands in any thinkable way. If you want to count the number of people logged on, you could save the output of the command who in the temporary file `tmp’, use wc -l to count the number of lines in `tmp’ and finally remove the temporary file.<br />
$ who &gt; tmp<br />
$ wc -l tmp<br />
      4 tmp<br />
$ rm tmp<br />
Using a pipe saves disk space and time: the stdout from who can be redirected to the stdin of wc -l through a pipe and there is no need for temporarily storing the output from who.<br />
$ who | wc -l<br />
      4<br />
Most UNIX-commands are constructed with piping in mind and this makes it possible to solve complex tasks easily, by joining commands along a pipeline. Consider the following pipeline:<br />
cat big.jpg | djpeg | pnmscale -pixels 150000 | cjpeg &gt; small.jpg<br />
The command cat sends the large JPEG-image to djpeg which decompresses it and sends the resulting bitmap to stdout. The stream of data floats through the next pipe to pnmscale which scales the bitmap image down to the given size. The scaled image is piped to the command cjpeg which compresses the standard input and finally produces a JPEG-image of reduced size which is stored in the file `small.jpg’.<br />
Command history<br />
The history feature in Bash means that you do not have to type commands over and over again. You can use the UP ARROW key to browse back through the list of commands you have typed previously and the keys LEFT ARROW and RIGHT ARROW to edit these commands.<br />
In addition there are a couple of commands which selects commands from the history list.<br />
`!!’<br />
Execute the last command again.<br />
`!4′<br />
Execute command number 4.<br />
The first of these simply repeats the last command. The second command gives an absolute number. The absolute command number can be seen by typing `history’.<br />
Command/filename completion<br />
In Bash you can save hours worth of typing errors by using the completion mechanism. This feature is based on the TAB key.<br />
The idea is that if you type half a filename and press TAB, the shell will try to guess the remainder of the filename. It does this by looking at the files which match what you have already typed and trying to fill in the rest. If there are several files which match, the shell sounds the “bell” or beeps. You can then type TAB twice to obtain a list of the possible alternatives. Here is an example: suppose you have just a single file in the current directory called `very_long_filename’, typing<br />
more TAB<br />
results in the following appearing on the command line<br />
more very_long_filename<br />
The shell was able to identify a unique file. Now suppose that you have two files called `very_long_filename’ and `very_big_filename’, typing<br />
more TAB<br />
results in the following appearing on the command line<br />
more very_<br />
and the shell beeps, indicating that the choice was not unique and a decision is required. Next, you type TAB twice(3) to see which files you have to choose from and the shell lists them and returns you to the command line, exactly where you were. You now choose `very_long_filename’ by typing `l’. This is enough to uniquely identify the file. Pressing the TAB key again results in<br />
more very_long_filename<br />
on the screen. As long as you have written enough to select a file uniquely, the shell will be able to complete the name for you.<br />
Completion also works on shell commands, but it is a little slower since the shell must search through all the directories in the command path to complete commands.<br />
Single and double quotes<br />
Two kinds of quotes can be used in shell apart from the backward quotes we mentioned above. The essential difference between them is that certain shell commands work inside double quotes but not inside single quotes. For example</p>
<p>cube$ echo /etc/rc*<br />
/etc/rc.boot /etc/rc0.d /etc/rc1.d /etc/rc2.d /etc/rc3.d /etc/rc4.d</p>
<p>cube$ echo “/etc/rc*”<br />
/etc/rc*</p>
<p>cube$ echo “`whoami`  — my name is $USER”<br />
mark  — my name is mark</p>
<p>cube$ echo ‘`whoami`  — my name is $USER’<br />
`whoami`  — my name is $USER</p>
<p>We see that the single quotes prevent variable substitution and sub-shells. Wildcards do not work inside either single or double quotes.<br />
Job control, break key, `fg’, `bg’<br />
So far we haven’t mentioned UNIX’s ability to multitask. In the Bourne shell (`sh’) there are no facilities for controlling several user processes. Bash provides some commands for starting and stopping processes. These originate from the days before windows and X11, so some of them may seem a little old-fashioned. They are still very useful nonetheless.<br />
Let’s begin by looking at the commands which are true for any shell. Most programs are run in the foreground or interactively. That means that they are connected to the standard input and send their output to the standard output. A program can be made to run in the background, if it does not need to use the standard I/O. For example, a program which generates output and sends it to a file could run in the background. In a window environment, programs which create their own windows can also be started as background processes, leaving standard I/O in the shell free.<br />
Background processes run independently of what you are doing in the foreground.<br />
UNIX Processes and BSD signals<br />
A background process is started using the special character `&amp;’ at the end of the command line.<br />
find / -name ‘*lib*’ -print &gt;&amp; output  &amp;<br />
The final `&amp;’ on the end of this line means that the job will be run in the background. Note that this is not confused with the redirection operator `&gt;&amp;’ since it must be the last character on the line. The command above looks for any files in the system containing the string `lib’ and writes the list of files to a file called `output’. This might be a useful way of searching for missing libraries which you want to include in your environment variable `LD_LIBRARY_PATH’. Searching the entire disk from the root directory `/’ could take a long time, so it pays to run this in the background.<br />
If we want to see what processes are running, we can use the `ps’ command. `ps’ without any arguments lists all of your processes, i.e. all processes owned by the user name you logged in with in the current shell. `ps’ takes many options, for instance `ps auxg’ will list all processes in gruesome detail (The “g” is for group, not gruesome!). `ps’ reads the kernel’s process tables directly.<br />
Processes can be stopped and started, or killed one and for all. The `kill’ command does this. There are, in fact, two versions of the `kill’ command. One of them is built into Bash and the other is not. If you use Bash then you will never care about the difference. We shall nonetheless mention the special features of Bash built-ins below. The kill command takes a number called a signal as an argument and another number called the process identifier or PID for short. Kill send signals to processes. Some of these are fatal and some are for information only. The two commands<br />
kill -15 127<br />
kill 127</p>
<p>are identical. They both send signal 15 to PID 127. This is the normal termination signal and it is often enough to stop any process from running.<br />
Programs can choose to ignore certain signals by trapping signals with a special handler. One signal they cannot ignore is signal 9.<br />
kill -9  127<br />
is a sure way of killing PID 127. Even though the process dies, it may not be removed from the kernel’s process table if it has a parent (see next section).<br />
Here is the complete list of signals which the Linux kernel send to processes in different circumstances.<br />
#define SIGHUP     1       /* Hangup (POSIX). */<br />
#define SIGINT     2       /* Interrupt (ANSI). */<br />
#define SIGQUIT    3       /* Quit (POSIX). */<br />
#define SIGILL     4       /* Illegal instruction (ANSI). */<br />
#define SIGTRAP    5       /* Trace trap (POSIX). */<br />
#define SIGABRT    6       /* Abort (ANSI). */<br />
#define SIGIOT     6       /* IOT trap (4.2 BSD). */<br />
#define SIGBUS     7       /* BUS error (4.2 BSD). */<br />
#define SIGFPE     8       /* Floating-point exception (ANSI). */<br />
#define SIGKILL    9       /* Kill, unblockable (POSIX). */<br />
#define SIGUSR1    10      /* User-defined signal 1 (POSIX). */<br />
#define SIGSEGV    11      /* Segmentation violation (ANSI). */<br />
#define SIGUSR2    12      /* User-defined signal 2 (POSIX). */<br />
#define SIGPIPE    13      /* Broken pipe (POSIX). */<br />
#define SIGALRM    14      /* Alarm clock (POSIX). */<br />
#define SIGTERM    15      /* Termination (ANSI). */<br />
#define SIGSTKFLT  16      /* Stack fault. */<br />
#define SIGCLD     SIGCHLD /* Same as SIGCHLD (System V). */<br />
#define SIGCHLD    17      /* Child status has changed (POSIX). */<br />
#define SIGCONT    18      /* Continue (POSIX). */<br />
#define SIGSTOP    19      /* Stop, unblockable (POSIX). */<br />
#define SIGTSTP    20      /* Keyboard stop (POSIX). */<br />
#define SIGTTIN    21      /* Background read from tty (POSIX). */<br />
#define SIGTTOU    22      /* Background write to tty (POSIX). */<br />
#define SIGURG     23      /* Urgent condition on socket (4.2 BSD). */<br />
#define SIGXCPU    24      /* CPU limit exceeded (4.2 BSD). */<br />
#define SIGXFSZ    25      /* File size limit exceeded (4.2 BSD). */<br />
#define SIGVTALRM  26      /* Virtual alarm clock (4.2 BSD). */<br />
#define SIGPROF    27      /* Profiling alarm clock (4.2 BSD). */<br />
#define SIGWINCH   28      /* Window size change (4.3 BSD, Sun). */<br />
#define SIGPOLL    SIGIO   /* Pollable event occurred (System V). */<br />
#define SIGIO      29      /* I/O now possible (4.2 BSD). */<br />
#define SIGPWR     30      /* Power failure restart (System V). */<br />
#define SIGSYS     31      /* Bad system call. */<br />
We have already mentioned 15 and 9 which are the main signals for users. Signal 1, or `HUP’ can be sent to certain programs by the superuser. For instance<br />
kill -1<br />
kill -HUP<br />
which forces `inetd’ to reread its configuration file. Sometimes it is useful to suspend a process temporarily and then restart it later.<br />
kill -20        # suspend process<br />
kill -18        # resume process<br />
Child Processes and zombies<br />
When you start a process from a shell, regardless of whether it is a background process or a foreground process, the new process becomes a child of the original shell. Remember that the shell is just a UNIX process itself. Moreover, if one of the children starts a new process then it will be a child of the child (a grandchild?)! Processes therefore form hierarchies. Several children can have a common parent.<br />
If we kill a parent, then (unless the child has detached itself from the parent) all of its children die too. If a child dies, the parent is not affected. Sometimes when a child is killed, it does not die but becomes “defunct” or a zombie process. This means that the child has a parent which is waiting for it to finish. If the parent has not yet been informed that the child has died, for example because it has been suspended itself, then the dead child is not removed from the kernel’s process table. When the parent wakes up and receives the message that the child has terminated, the process entry for the dead child can be removed.<br />
Bash builtins: `jobs’, `kill’, `fg’,`bg’, break key<br />
Now let’s look at some commands which are built into Bash for starting and stopping processes. Bash refers to user programs as `jobs’ rather than processes — but there is no real difference. The added bonus of Bash is that each shell has a job number in addition to its PID. The job numbers are simpler and are private for the shell, whereas the PIDs are assigned by the kernel and are often very large numbers which are difficult to to remember. When a command is executed in the shell, it is assigned a job number. If you never run any background jobs then there is only ever one job number: 1, since every job exits before the next one starts. However, if you run background tasks, then you can have several jobs “active” at any time. Moreover, by suspending jobs, Bash allows you to have several interactive programs running on the same terminal — the `fg’ and `bg’ commands allow you to move commands from the background to the foreground and vice-versa.<br />
Take a look at the following shell session.<br />
cube$ emacs myfile&amp;<br />
[3] 771<br />
cube$</p>
<p>  ( other commands … , edit myfile and close emacs )</p>
<p>When a background job is done, the shell prints a message at a suitable moment between prompts.<br />
[3]+  Done                    emacs myfile<br />
cube$<br />
This tells you that job number 1 finished normally. If the job exits abnormally then the word `Done’ may be replaced by some other message. For instance, if you kill the job, it will say<br />
cube$ kill %3<br />
cube$<br />
[3]+  Terminated              emacs myfile<br />
cube$<br />
You can list the jobs you have running using the `jobs’ command. The output looks something like<br />
cube$ jobs<br />
[1]   Terminated              xdvi unix<br />
[2]   Running                 xemacs unix.texinfo &amp;<br />
[3]   Running                 xterm -sb -sl 10000 &amp;<br />
[4]   Running                 ghostview &amp;<br />
[5]   Running                 netscape &amp;<br />
[6]   Running                 xterm -sb -sl 10000 &amp;<br />
[7]   Running                 xemacs fil &amp;<br />
[8]+  Stopped                 emacs unix.log<br />
[9]-  Running                 gimp &amp;<br />
To suspend a program which you are running in the foreground you can type CTRL-z (this is like sending a `kill -20′ signal from the keyboard). (4) You can suspend any number of programs and then restart them one at a time using `fg’ and `bg’. If you want job 5 to be restarted in the foreground, you would type<br />
fg %5<br />
When you have had enough of job 5, you can type CTRL-z to suspend it and then type<br />
fg %6<br />
to activate job 6. Provided a job does not want to send output to `stdout’, you can restart any job in the background, using a command like.<br />
bg %4<br />
This method of working was useful before windows were available. Using `fg’ and `bg’, you can edit several files or work on several programs without have to quit to move from one to another.<br />
See also some related commands for batch processing `at’, `batch’ and `atq’, `cron’.<br />
NOTE: CTRL-c sends a `kill -2′ signal, which send a standard interrupt message to a program. This is always a safe way to interrupt a shell command.<br />
Arithmetic in Bash<br />
In Bourne shell arithmetic is performed entirely `by proxy’. To evaluate an expression we call the `expr’ command or the `bc’ precision calculator. Here are some examples of `expr’<br />
a=`expr $a+1`                 # increment a<br />
a=`expr 4 + 10 \* 5`          # 4+10*5<br />
check = `expr $a \&gt; $b`       # true=1, false=0. True if $a &gt; $b<br />
`expr’ is very sensitive to spaces and backslash characters and this makes it a bit awkward to do arithmetic under the Bourne shell.<br />
Bash 2.0 provides a new and simpler way to do arithmetic using double parentheses. If you surround any integer arithmetic expression as in (( x = y + 1 )), you can perform most arithmetic operations with the same syntax as in Java and C.<br />
(( x = 1 ))<br />
echo $x<br />
1<br />
(( x++ ))<br />
(( y = 4*x ))<br />
echo $y<br />
8<br />
Note that you do not need to use the dollar symbol to refer to a variable within the double parentheses (but you may do it) and that spaces are allowed.<br />
(( sum = 2 ))<br />
(( total = 4*$sum + sum ))<br />
echo $total<br />
10<br />
The variables within double parentheses are throughout treated as integers. Assigning a float value like 2.5 to a variable results in an syntax error while assigning a string to a variable cause the string to be stored as zero.<br />
Scripts and arguments<br />
Scripts are created by making an executable file which begins with the sequence of characters<br />
#!/bin/bash<br />
This construction is quite general: any executable file which begins with a sequence<br />
#!myprogram -option<br />
will cause the shell to attempt to execute<br />
myprogam -option filename<br />
where filename is the name of the file.<br />
If a script is to accept arguments then these can be referred to as ` $1 $2 $3..$9′. There is a logical limit of nine arguments to a Bourne script, but Bash handles the next arguments as `${10}’. `$0′ is the name of the script itself.<br />
Here is a simple Bash script which prints out all its arguments.</p>
<p>#!/bin/bash<br />
#<br />
# Print all arguments (version 1)<br />
#</p>
<p>for arg in $*<br />
do<br />
  echo Argument $arg<br />
done</p>
<p>echo Total number of arguments was $#</p>
<p>The `$*’ symbol stands for the entire list of arguments and `$#’ is the total number of arguments.<br />
Another way of achieving the same is to use the `shift’ command. We shall meet this again in the Perl programming language. `shift’ takes the first argument from the argument list and deletes it, moving all of the other arguments down one number — this is how we can handle long lists of arguments in the Bourne shell.<br />
#!/bin/bash<br />
#<br />
#  Print all arguments (version 2)<br />
#</p>
<p>while ( true )<br />
do<br />
  arg=$1;<br />
  shift;<br />
  echo $arg was an argument;<br />
  if [ $# -eq 0 ]; then<br />
    break<br />
  fi<br />
done</p>
<p>Return codes<br />
All programs which execute in UNIX return a value through the C `return’ command. There is a convention that a return value of zero (0) means that everything went well, whereas any other value implies that some error occurred. The return value is usually the value returned in `errno’, the external error variable in C.<br />
Shell scripts can test for these values either by placing the command directly inside an `if’ test, or by testing the variable `$?’ which is always set to the return code of the last command. Some examples are given following the next two sections.<br />
Tests and conditionals<br />
Bash and the Bourne shell has an array of tests. They are written as follows. Notice that `test’ is itself not a part of the shell, but is a program which works out conditions and provides a return code. See the manual page on `test’ for more details.<br />
test -f file<br />
True if the file is a plain file<br />
test -d file<br />
True if the file is a directory<br />
test -r file<br />
True if the file is readable<br />
test -w file<br />
True if the file is writable<br />
test -x file<br />
True if the file is executable<br />
test -s file<br />
True if the file contains something<br />
test -g file<br />
True if setgid bit is set<br />
test -u file<br />
True if setuid bit is set<br />
test s1 = s2<br />
True if strings s1 and s2 are equal<br />
test s1 != s2<br />
True if strings s1 and s2 are unequal<br />
test x -eq y<br />
True if the integers x and y are numerically equal<br />
test x -ne y<br />
True if integers are not equal<br />
test x -gt y<br />
True if x is greater than y<br />
test x -lt y<br />
True if x is less than y<br />
test x -ge y<br />
True if x&gt;=y<br />
test x -le y<br />
True if x &lt;= y<br />
!<br />
Logical NOT operator<br />
-a<br />
Logical AND<br />
-o<br />
Logical OR<br />
Note that an alternate syntax for writing these commands if to use the square brackets, instead of writing the word test.<br />
 [ $x -lt $y ]   “==”    test $x -lt $y<br />
Just as with the arithmetic expressions, Bash 2.x provides a syntax for conditionals which are more similar to Java and C. While arithmetic C-like expressions can be used within double parentheses, C-like tests can be used within double square brackets.<br />
 [[ $var == &quot;OK&quot; || $var == &quot;yes&quot; ]]<br />
This C-like syntax is not allowed in the Bourne shell, but is equivalent to<br />
[ $var = &quot;OK&quot; -o $var = &quot;yes&quot; ]<br />
which is valid in both shells.<br />
Arithmetic C-like tests can be used within double parentheses so that under Bash 2.x the following tests are equivalent:<br />
 [ $x -lt $y ]   “==” (( x &lt; y ))<br />
Conditional structures<br />
The conditional structures have the following syntax.<br />
if UNIX-command<br />
then<br />
   command<br />
else<br />
   commands<br />
fi<br />
The `else’ clause is, of course, optional. As noted before, the first UNIX command could be anything, since every command has a return code. The result is TRUE if it evaluates to zero and false otherwise (in contrast to the conventions in most languages). Multiple tests can be made using<br />
if UNIX-command<br />
then<br />
   commands<br />
elif UNIX-command<br />
then<br />
   commands<br />
elif UNIX-command<br />
then<br />
   commands<br />
else<br />
   commands<br />
fi<br />
where `elif’ means `else-if’.<br />
The equivalent of the C-school’s `switch’ statement is a more Pascal-like `case’ structure.<br />
case UNIX-command-or-variable in</p>
<p>   wildcard1) commands ;;<br />
   wildcard2) commands ;;<br />
   wildcard3) commands ;;</p>
<p>esac<br />
This structure uses the wildcards to match the output of the command or variable in the first line. The first pattern which matches gets executed.<br />
Input from the user in Bash<br />
In shell you can read the value of a variable using the `read’ command, with syntax<br />
read variable<br />
This reads in a string from the keyboard and terminates on a newline character. Under the old Bourne shell another way to do this is to use the `input’ command to access a particular logical device. The keyboard device in the current terminal is `/dev/tty’, so that one writes<br />
variable = `line  /dev/tty<br />
  exit 1<br />
fi</p>
<p>echo “Looking for $1″</p>
<p>until users | grep -s $1<br />
do<br />
   sleep 60<br />
done</p>
<p>echo “!!! WAKE UP !!!” &gt; /dev/tty<br />
echo “User $1 just logged in” &gt; /dev/tty</p>
<p>This script uses `grep’ in `silent mode’ (-s option). i.e. grep never writes anything to the terminal. The only thing we are interested in is the return code the piped command produces. If `grep’ detects a line containing the username we are interested in, then the result evaluates to TRUE and the sleep-loop exits.<br />
Our final example is the kind of script which is useful for a system administrator. It transfers over the Network Information Service database files so that a slave server is up to date. All we have to do is make a list of the files and place it in a `for’ loop. The names used below are the actual names of the NIS maps, well known to system administrators.<br />
#!/bin/bash<br />
#<br />
# Update the NIS database maps on a client server. This program<br />
# shouldn’t have to be run, but sometimes things go wrong and we<br />
# have to force a download from the main sever.<br />
#<br />
PATH=/etc/yp:/usr/etc/yp:$PATH</p>
<p>MASTER=myNISserver</p>
<p>for map in auto.direct auto.master ethers.byaddr ethers.byname\<br />
           group.bygid group.byname hosts.byaddr hosts.byname\<br />
           mail.aliases netgroup.byhost netgroup.byuser netgroup\<br />
           netid.byname networks.byaddr networks.byname passwd.byname\<br />
           passwd.byuid priss.byname protocols.byname protocols.bynumber\<br />
           rpc.bynumber services.byname services usenetgroups.byname;<br />
do<br />
  ypxfr $1 -h $MASTER $map<br />
done</p>
<p>Procedures and traps<br />
One of the worthy features of the Bourne shell is that it allows you to define subroutines or procedures. Subroutines work just like subroutines in any other programming language. They are executed in same shell (not as a sub-process).<br />
Here is an interesting program which demonstrates two useful things at the same time. First of all, it shows how to make a hierarchical subroutine structure using the Bourne shell. Secondly, it shows how the `trap’ directive can be used to trap signals, so that Bourne shell programs can exit safely when they are killed or when CTRL-C is typed.<br />
#!/bin/bash<br />
#<br />
#  How to make a signal handler in Bourne Shell<br />
#  using subroutines<br />
#</p>
<p>#####################################################<br />
# Level 2<br />
#####################################################</p>
<p>ReallyQuit()<br />
{<br />
while true<br />
do<br />
  echo “Do you really want to quit?”<br />
  read answer</p>
<p>  case $answer in</p>
<p>     y* | Y* ) return 0;;<br />
     *)        echo “Resuming…”<br />
               return 1;;</p>
<p>  esac</p>
<p>done<br />
}</p>
<p>#####################################################<br />
# Level 1<br />
#####################################################</p>
<p>SignalHandler()</p>
<p>{<br />
if ReallyQuit           # Call a function<br />
then<br />
   exit 0<br />
else<br />
   return 0<br />
fi<br />
}</p>
<p>#####################################################<br />
# Level 0 : main program<br />
#####################################################</p>
<p>trap SignalHandler 2 15  # Trap kill signals 2 and 15</p>
<p>echo “Type some lines of text…”</p>
<p>while read text<br />
do</p>
<p>   echo “$text – CTRL-C to exit”</p>
<p>done</p>
<p>Note that the logical tree structure of this program is upside down (the highest level comes at the bottom). This is because all subroutines must be defined before they are used.<br />
This example concludes our survey of Bash and the Bourne shell.<br />
setuid and setgid scripts<br />
The superuser `root’ is the only privileged user in UNIX. All other users have only restricted access to the system. Usually this is desirable, but sometimes it is a nuisance.<br />
A setuid script is a script which has its setuid-bit set. When such a script is executed by a user, it is run with all the rights and privileges of the owner of the script. All of the commands in the script are executed as the owner of the file and not with the user-id of the person who ran the script. If the owner of the setuid script is `root’ then the commands in the script are run with root privileges!<br />
Setuid scripts are clearly a touchy security issue. When giving away one’s rights to another user (especially those of `root’) one is tempting hackers. Setuid scripts should be avoided.<br />
A setgid program is almost the same, but only the group id is set to that of the owner of the file. Often the effect is the same.<br />
An example of a setuid program is the `ps’ program. `ps’ lists all of the processes running in the kernel. In order to do this it needs permission to access the private data structures in the kernel. By making `ps’ setgid root, it allows ordinary users to be able to read as much as the writers of `ps’ thought fit, but no more.<br />
Naturally, only the superuser can make a file setuid or setgid root.<br />
Exercises<br />
1. Write an improved `which’ command in Bash.<br />
2. Make a counter program which records in a file how many times you log in to your account. You can call this in your .bashrc file.<br />
3. Make a Bourne shell script to kill all the processes owned by a particular user. (Note, that if you are not the superuser, you cannot kill processes owned by other users.)<br />
4. Write a script to replace the `rm’ command with something safer. Think about a way of implementing `rm’ so that it is possible to get deleted files back again in case of emergencies. This is not possible using the normal `rm’ command. Hint: save files in a hidden directory `.deleted’. Make your script delete files in the `.deleted’ directory if they are older than a week, so that you don’t fill up the disk with rubbish.<br />
5. Suppose you have a bunch of files with a particular file-extension: write a script in Bash to change the extension to something else. e.g. to change *.C into *.c. Give the old and new extensions as arguments to the script.<br />
6. Write a program in Bash to search for files in the current directory which contain a certain string. e.g. search for all files which contain the word “if”. Hint: use the “find” command.<br />
7. Use the manual pages to find out about the commands `at’, `batch’ and `atq’. Test these commands by executing the shell command `date’ at some time of your choice. Use the `-m’ option so that the result of the job is mailed to you.<br />
8. Write a script in Bash to list all of the files bigger than a certain size starting from the current directory, and including all subdirectories. This kind of program is useful for system administrators when a disk becomes full.<br />
C shell<br />
Programmers who are used to C or C++ often find it easier to program in C-shell because there are strong similarities between the two.<br />
.cshrc and .login files<br />
Most users run the C-shell `/bin/csh’ as their login environment, or these days, preferably the `tcsh’ which is an improved version of csh. When a user logs in to a UNIX system the C-shell starts by reading some files which configure the environment by defining variables like path.<br />
“ The file `.cshrc’ is searched for in your home directory. i.e. `~/.cshrc’. If it is found, its contents are interpreted by the C-shell as C-shell instructions, before giving you the command prompt(5).<br />
“ If and only if this is the login shell (not a sub-shell that you have started after login) then the file `~/.login’ is searched for and executed.<br />
With the advent of the X11 windowing system, this has changed slightly. Since the window system takes over the entire login procedure, users never get to run `login shells’, since the login shell is used up by the X11 system. On an X-terminal or host running X the `.login’ file normally has no effect.<br />
With some thought, the `.login’ file can be eliminated entirely, and we can put everything into the .cshrc file. Here is a very simple example `.cshrc’ file.<br />
#<br />
# .cshrc – read in by every csh that starts.<br />
#</p>
<p># Set the default file creation mask<br />
umask 077</p>
<p># Set the path<br />
set path=( /usr/local/bin /usr/bin/X11 /usr/ucb /bin /usr/bin . )</p>
<p># Exit here if the shell is not interactive<br />
if ( $?prompt == 0 ) exit</p>
<p># Set some variables</p>
<p>set noclobber notify filec nobeep<br />
set history=100<br />
set prompt=”`hostname`%”<br />
set prompt2 = “%m %h&gt;”    # tcsh, prompt for foreach and while</p>
<p>setenv PRINTER myprinter<br />
setenv LD_LIBRARY_PATH /usr/lib:/usr/local/lib:/usr/openwin/lib</p>
<p># Aliases are shortcuts to UNIX commands</p>
<p>alias passwd  yppasswd<br />
alias dir     ‘ls -lg \!* | more’<br />
alias sys     ‘ps aux | more’<br />
alias h       history<br />
It is possible to make a much more complicated .cshrc file than this. The advent of distributed computing and NFS (Network file system) means that you might log into many different machines running different versions of UNIX. The command path would have to be set differently for each type of machine.<br />
Defining variables with set, setenv<br />
We have already seen in the examples above how to define variables in C-shell. Let’s formalize this. To define a local variable — that is, one which will not get passed on to programs and sub-shells running under the current shell, we write<br />
set local = “some string”<br />
set myname = “`whoami`”<br />
These variables are then referred to by using the dollar `$’ symbol. i.e. The value of the variable `local’ is `$local’.<br />
echo $local $myname<br />
Global variables, that is variables which all sub-shells inherit from the current shell are defined using `setenv’<br />
setenv GLOBAL “Some other string”<br />
setenv MYNAME “`who am i`”<br />
Their values are also referred to using the `$’ symbol. Notice that set uses an `=’ sign while `setenv’ does not.<br />
Variables can be also created without a value. The shell uses this method to switch on and off certain features, using variables like `noclobber’ and `noglob’. For instance<br />
nexus% set flag<br />
nexus% if ($?flag) echo ‘Flag is set!’<br />
Flag is set!<br />
nexus% unset flag<br />
nexus% if ( $?flag ) echo ‘Flag is set!’<br />
nexus%<br />
The operator `$?variable’ is `true’ if variable exists and `false’ if it does not. It does not matter whether the variable holds any information.<br />
The commands `unset’ and `unsetenv’ can be used to undefine or delete variables when you don’t want them anymore.<br />
Arrays<br />
A useful facility in the C-shell is the ability to make arrays out of strings and other variables. The round parentheses `(..)’ do this. For example, look at the following commands.<br />
nexus% set array = ( a b c d )<br />
nexus% echo $array[1]<br />
a<br />
nexus% echo $array[2]<br />
b<br />
nexus% echo $array[$#array]<br />
d</p>
<p>nexus% set noarray = ( “a b c d” )<br />
nexus% echo $noarray[1]<br />
a b c d<br />
nexus% echo $noarray[$#noarray]<br />
a b c d<br />
The first command defines an array containing the elements `a b c d’. The elements of the array are referred to using square brackets `[..]‘ and the first element is `$array[1]‘. The last element is `$array[4]‘. NOTE: this is not the same as in C or C++ where the first element of the array is the zeroth element!<br />
The special operator `$#’ returns the number of elements in an array. This gives us a simple way of finding the end of the array. For example<br />
nexus% echo $#path<br />
23</p>
<p>nexus% echo “The last element in path is $path[$#path]“<br />
The last element in path is .<br />
To find the next last element we need to be able to do arithmetic. We’ll come back to this later.<br />
Pipes and redirection in csh<br />
The symbols<br />
                &gt;&gt;  &lt;’. For example,<br />
echo testing &gt; myfile<br />
produces a file called `myfile’ which contains the string `testing’. The single `&gt;’ (greater than) sign always creates a new file, whereas the double `&gt;&gt;’ appends to the end of a file, if it already exists. So the first of the commands<br />
echo blah blah &gt;&gt; myfile<br />
echo Newfile &gt; myfile<br />
adds a second line to `myfile’ after `testing’, whereas the second command writes over `myfile’ and ends up with just one line `Newfile’.<br />
Now suppose we mistype a command<br />
ehco test &gt; myfile<br />
The command `ehco’ does not exist and so the error message `ehco: Command not found’ appears on the terminal. This error message was sent to stderr — so even though we redirected output to a file, the error message appeared on the screen to tell us that an error occurred. Even this can be changed. `stderr’ can also be redirected by adding an ampersand `&amp;’ character to the `&gt;’ symbol. The command<br />
ehco test &gt;&amp; myfile<br />
results in the file `myfile’ being created, containing the error message `ehco: Command not found’.<br />
The input direction can be changed using the `&lt;’ symbol for example<br />
/bin/mail mark ’ command. If one tries to redirect output to an existing file, the following happens.<br />
UNIX% set noclobber<br />
UNIX% touch blah        # create an empty file blah<br />
UNIX% echo test &gt; blah<br />
blah: File exists.<br />
If you are nervous about overwriting files, then you can set `noclobber’ in your `.cshrc’ file. `noclobber’ can be overridden using the pling `!’ symbol. So<br />
UNIX% set noclobber<br />
UNIX% touch blah        # create an empty file blah<br />
UNIX% echo test &gt;! blah<br />
writes over the file `blah’ even though `noclobber’ is set.<br />
Here are some other combinations of redirection symbols<br />
`&gt;&gt;’<br />
Append, including `stderr’<br />
`&gt;&gt;!’<br />
Append, ignoring `noclobber’<br />
`&gt;&gt;&amp;!’<br />
Append `stdout’, `stderr’, ignore `noclobber’<br />
`&lt;&lt;’<br />
See below.<br />
The last of these commands reads from the standard input until it finds a line which contains a word. It then feeds all of this input into the program concerned. For example,<br />
nexus% mail mark &lt; Hello mark<br />
nexus 2&gt; Nothing much to say…<br />
nexus 2&gt; so bye<br />
nexus 2&gt;<br />
nexus 2&gt; quit<br />
Sending mail…<br />
Mail sent!<br />
The mail message contains all the lines up to, but not including `marker’. This method can also be used to print text verbatim from a file without using multiple echo commands. Inside a script one may write:<br />
cat &lt; myfile</p>
<p>pwd</p>
<p>This program changes the working directory to /etc and then executes a subshell which inside the brackets changes directory to /usr/bin and lists the files there. The output of this private shell are sent to a file `myfile’. At the end we print out the current working directory just to show that the `cd’ command in brackets had no effect on the main program.<br />
Normally both parentheses must be on the same line. If a subshell command line gets too long, so that the brackets are not on the same line, you have to use backslash characters to continue the lines,</p>
<p>(  command \<br />
   command \<br />
   command \<br />
)</p>
<p>Tests and conditions<br />
No programming language would be complete without tests and loops. C-shell has two kinds of decision structure: the `if..then..else’ and the `switch’ structure. These are closely related to their C counterparts. The syntax of these is<br />
if (condition) command</p>
<p>if (condition) then<br />
   command<br />
   command..<br />
else<br />
   command<br />
   command..<br />
endif<br />
switch (string)</p>
<p>  case one:<br />
              commands<br />
              breaksw</p>
<p>  case two:<br />
              commands<br />
              breaksw</p>
<p>  …</p>
<p>endsw</p>
<p>In the latter case, no commands should appear on the same line as a `case’ statement, or they will be ignored. Also, if the `breaksw’ commands are omitted, then control flows through all the commands for case 2, case 3 etc, exactly as it does in the C programming language.<br />
We shall consider some examples of these statements in a moment, but first it is worth listing some important tests which can be used in `if’ questions to find out information about files.<br />
`-r file’<br />
True if the file exists and is readable<br />
`-w file’<br />
True if the file exists and is writable<br />
`-x file’<br />
True if the file exists and is executable<br />
`-e file’<br />
True if the file simply exists<br />
`-z file’<br />
True if the file exists and is empty<br />
`-f file’<br />
True if the file is a plain file<br />
`-d file’<br />
True if the file is a directory<br />
We shall also have need of the following comparison operators.<br />
`==’<br />
is equal to (string comparison)<br />
`!=’<br />
is not equal to<br />
`&gt;’<br />
is greater than<br />
`=’<br />
is greater than or equal to<br />
`&lt;=’<br />
is less than or equal to<br />
`=~’<br />
matches a wildcard<br />
`!~’<br />
does not match a wildcard<br />
The simplest way to learn about these statements is to use them, so we shall now look at some examples.<br />
#!/bin/csh -f<br />
#<br />
#  Safe copy from  to<br />
#<br />
#</p>
<p>if ($#argv != 2) then</p>
<p>  echo “Syntax: copy  ”<br />
  exit 0</p>
<p>endif</p>
<p>if ( -f $argv[2] ) then</p>
<p>   echo “File exists. Copy anyway?”</p>
<p>   switch ( $ Makefile</p>
<p>echo “Making software. Type CTRL-C to abort and edit Makefile”</p>
<p>$MAKE software         # call make to build program<br />
chmod 755 software     # set correct protection</p>
<p>Loops in csh<br />
The C-shell has three loop structures: `repeat’, `while’ and `foreach’. We have already seen some examples of the `foreach’ loop.<br />
The structure of these loops is as follows<br />
repeat number-of-times command</p>
<p>while ( test expression )</p>
<p>   commands</p>
<p>end</p>
<p>foreach  control-variable  ( list-or-array )</p>
<p>   commands</p>
<p>end</p>
<p>The commands `break’ and `continue’ can be used to break out of the loops at any time. Here are some examples.<br />
repeat 2 echo “Yo!” | write mark<br />
This sends the message “Yo!” to mark’s terminal twice.<br />
repeat 5 echo `echo “Shutdown time! Log out now” | wall ; sleep 30` ; halt<br />
This example repeats the command `echo Shutdown time…’ five times at 30 second intervals, before shutting down the system. Only the superuser can run this command! Note the strange construction with `echo echo’. This is to force the repeat command to take two shell commands as an argument. (Try to explain why this works for yourself.)<br />
Input from the user</p>
<p># Test a user response</p>
<p>echo “Answer y/n (yes or no)”</p>
<p>set valid = false</p>
<p>while ( $valid == false )</p>
<p>   switch ( $ 6 &amp;&amp; $x  6 || $x &gt; 2 )       # Bitwise shift right<br />
@ back    = ( $var &lt;&lt; 2 )       # Bitwise shift left<br />
These operators are precisely those found in the C programming language.<br />
Examples<br />
The following script uses the operators in the last two sections to take a list of files with a given file extension (say `.doc’) and change it for another (say `.tex’). This is a partial solution to the limitation of not being able to do multiple renames in shell.<br />
#!/bin/csh -f<br />
#############################################################<br />
#<br />
# Change file extension for multiple files<br />
#<br />
#############################################################</p>
<p>if ($#argv &lt; 2) then<br />
  echo Syntax: chext oldpattern newextension<br />
  echo “e.g: chext *.doc tex “<br />
  exit 0<br />
endif</p>
<p>mkdir /tmp/chext.$user                 # Make a scratch area</p>
<p>set newext=”$argv[$#argv]“             # Last arg is new ext<br />
set oldext=”$argv[1]:e”</p>
<p>echo “Old extension was ($oldext)”&quot;<br />
echo “New extension ($newext) — okay? (y/n)”</p>
<p>switch( $ $argv[2]<br />
#<br />
###########################################################<br />
if (”`whoami`” != “root”) then<br />
  echo Permission denied<br />
  exit 0<br />
endif</p>
<p>if ( $#argv  2 ) then<br />
  echo Usage: KILL username lowest-pid<br />
  exit 0<br />
endif</p>
<p>if ( $argv[1] == “root”) then<br />
  echo No! Too dangerous — system will crash<br />
  exit 0<br />
endif</p>
<p>############################################################<br />
# Kill everything<br />
############################################################</p>
<p>if ( $#argv == 1 ) then</p>
<p>  set killarray = ( `ps aux |  awk ‘{ if ($1 == user) \<br />
{printf “%s “,$2}}’ user=$argv[1]` )</p>
<p>  foreach process ($killarray)</p>
<p>     kill -1 $process<br />
     kill -15 $process &gt; /dev/null<br />
     kill -9 $process &gt; /dev/null</p>
<p>     if (”`kill -9 $process | egrep -e ‘No such process’`” == “”) then<br />
        echo “Warning – $process would not die – try again”<br />
     endif<br />
  end</p>
<p>#############################################################<br />
# Start from a certain PID<br />
#############################################################</p>
<p>else if ( $#argv == 2 ) then</p>
<p>  set killarray = ( `ps aux |  awk ‘{ if ($1 == user &amp;&amp; $2 &gt; uid) \<br />
{printf “%s “,$2}}’ user=$argv[1] uid=$argv[2]` )</p>
<p>  foreach process ($killarray)</p>
<p>     kill -1 $process &gt; /dev/null<br />
     kill -15 $process<br />
     sleep 2<br />
     kill -9 $process &gt; /dev/null</p>
<p>     if (”`kill -9 $process | egrep -e ‘No such process’`” == “”) then<br />
        echo “Warning – $process would not die – try again”<br />
     endif<br />
  end</p>
<p>endif</p>
<p>This program would be better written in C or Perl.<br />
Summary: Limitations of shell programming<br />
To summarize the last two long and oppressive chapters we shall take a step back from the details and look at what we have achieved.<br />
The idea behind the shell is to provide a user interface, with access to the system’s facilities at a simple level. In the 70’s user interfaces were not designed to be user-friendly. The UNIX shell is not particularly use friendly, but it is very powerful. Perhaps it would have been enough to provide only commands to allow users to write C programs. Since all of the system functions are available from C, that would certainly allow everyone to do what anything that UNIX can do. But shell programming is much more immediate than C. It is an environment of frequently used tools. Also for quick programming solutions: C is a compiled language, whereas the shell is an interpreter. A quick shell program can solve many problems in no time at all, without having to compile anything.<br />
Shell programming is only useful for `quick and easy’ programs. To use it for anything serious is an abuse. Programming difficult things in shell is clumsy, and it is difficult to get returned-information (like error messages) back in a useful form. Besides, shell scripts are slow compared to real programs since they involve starting a new program for each new command.<br />
These difficulties are solved partly by Perl, which we shall consider next — but in the final analysis, real programs of substance need to be written in C. Contrary to popular belief, this is not more difficult than programming in the shell — in fact, many things are much simpler, because all of the shell commands originated as C functions. The shell is an extra layer of the UNIX onion which we have to battle our way through to get where we’re going.<br />
Sometimes it is helpful to be shielded from low level details — sometimes it is a hindrance. In the remaining chapters we shall consider more involved programming needs.<br />
Perl<br />
So far, we have been looking at shell programming for performing fairly simple tasks. Now let’s extend the idea of shell programming to cover more complex tasks like systems programming and network communications. Perl is a language which was designed to retain the immediateness of shell languages, but at the same time capture some of the flexibility of C. Perl is an acronym for Practical extraction and report language. In this chapter, we shall not aim to teach Perl from scratch — the best way to learn it is to use it! Rather we shall concentrate on demonstrating some principles.<br />
Sed and awk, cut and paste<br />
One of the reasons for using Perl is that it is extremely good at textfile handling–one of the most important things for UNIX users, and particularly useful in connection with CGI script processing on the World Wide Web. It has simple built-in constructs for searching and replacing text, storing information in arrays and retrieving them in sorted form. All of the these things have previously been possible using the UNIX shell commands<br />
sed<br />
awk<br />
cut<br />
paste<br />
but these commands were designed to work primarily in the Bourne shell and are a bit `awk’ward to use for all but the simplest applications.<br />
`sed’<br />
is a stream editor. It takes command line instructions, reads input from the stream stdin and produces output on stdout according to those instructions. `sed’ works line by line from the start of a textfile.<br />
`awk’<br />
is a pattern matching and processing language. It takes a textfile and reads it line by line, matching regular expressions and acting on them. `awk’ is powerful enough to have conditional instructions like `if..then..else’ and uses C’s `printf’ construction for output.<br />
`cut’<br />
Takes a line of input and cuts it into fields, separated by some character. For instance, a normal line of text is a string of words separated by spaces. Each word is a different field. `cut’ can be used, for instance, to pick out the third column in a table. Any character can be specified as the separator.<br />
`paste’<br />
is the logical opposite of cut. It concatenates @math{n} files, and makes each line in the file into a column of a table. For instance, `paste one two three’ would make a table in which the first column consisted of all lines in `one’, the second of all lines in `two’ and the third of all lines in `three’. If one file is longer than the others, then some columns have blank spaces.<br />
Perl unifies all of these operations and more. It also makes them much simpler.<br />
Program structure<br />
To summarize Perl, we need to know about the structure of a Perl program, the conditional constructs it has, its loops and its variables. In the latest versions of Perl (Perl 5), you can write object oriented programs of great complexity. We shall not go into this depth, for the simple reason that Perl’s strength is not as a general programming language but as a specialized language for textfile handling. The syntax of Perl is in many ways like the C programming language, but there are important differences.<br />
“ Variables do not have types. They are interpreted in a context sensitive way. The operators which acts upon variables determine whether a variable is to be considered a string or as an integer etc.<br />
“ Although there are no types, Perl defines arrays of different kinds. There are three different kinds of array, labelled by the symbols `$’, `@’ and `%’.<br />
“ Perl keeps a number of standard variables with special names e.g. `$_ @ARGV’ and `%ENV’. Special attention should be paid to these. They are very important!<br />
“ The shell reverse apostrophe notation `command` can be used to execute UNIX programs and get the result into a Perl variable.<br />
Here is a simple `structured hello world’ program in Perl. Notice that subroutines are called using the `&amp;’ symbol. There is no special way of marking the main program — it is simply that part of the program which starts at line 1.<br />
#!/local/bin/perl<br />
#<br />
# Comments<br />
#</p>
<p>&amp;Hello();<br />
&World;</p>
<p># end of main</p>
<p>sub Hello<br />
   {<br />
   print “Hello”;<br />
   }</p>
<p>sub World<br />
   {<br />
   print “World\n”;<br />
   }</p>
<p>The parentheses on subroutines are optional, if there are no parameters passed. Notice that each line must end in a semi-colon.<br />
Perl variables<br />
Scalar variables<br />
In Perl, variables do not have to be declared before they are used. Whenever you use a new symbol, Perl automatically adds the symbol to its symbol table and initializes the variable to the empty string.<br />
It is important to understand that there is no practical difference between zero and the empty string in perl — except in the way that you, the user, choose to use it. Perl makes no distinction between strings and integers or any other types of data — except when it wants to interpret them. For instance, to compare two variables as strings is not the same as comparing them as integers, even if the string contains a textual representation of an integer. Take a look at the following program.<br />
#!/local/bin/perl<br />
#<br />
# Nothing!<br />
#</p>
<p>print “Nothing == $nothing\n”;</p>
<p>print “Nothing is zero!\n” if ($nothing == 0);</p>
<p>if ($nothing eq “”)<br />
   {<br />
   print STDERR “Nothing is really nothing!\n”;<br />
   }</p>
<p>$nothing = 0;</p>
<p>print “Nothing is now $nothing\n”;<br />
The output from this program is</p>
<p>Nothing ==<br />
Nothing is zero!<br />
Nothing is really nothing!<br />
Nothing is now 0</p>
<p>There are several important things to note here. First of all, we never declare the variable `nothing’. When we try to write its value, perl creates the name and associates a NULL value to it i.e. the empty string. There is no error. Perl knows it is a variable because of the `$’ symbol in front of it. All scalar variables are identified by using the dollar symbol.<br />
Next, we compare the value of `$nothing’ to the integer `0′ using the integer comparison symbol `==’, and then we compare it to the empty string using the string comparison symbol `eq’. Both tests are true! That means that the empty string is interpreted as having a numerical value of zero. In fact any string which does not form a valid integer number has a numerical value of zero.<br />
Finally we can set `$nothing’ explicitly to a valid integer string zero, which would now pass the first test, but fail the second.<br />
As extra spice, this program also demonstrates two different ways of writing the `if’ command in perl.<br />
The default scalar variable.<br />
The special variable `$_’ is used for many purposes in Perl. It is used as a buffer to contain the result of the last operation, the last line read in from a file etc. It is so general that many functions which act on scalar variables work by default on `$_’ if no other argument is specified. For example,<br />
print;<br />
is the same as<br />
print $_;<br />
Array (vector) variables<br />
The complement of scalar variables is arrays. An array, in Perl is identified by the `@’ symbol and, like scalar variables, is allocated and initialized dynamically.</p>
<p>@array[0] = “This little piggy went to market”;<br />
@array[2] = “This little piggy stayed at home”;</p>
<p>print “@array[0] @array[1] @array[2]“;</p>
<p>The index of an array is always understood to be a number, not a string, so if you use a non-numerical string to refer to an array element, you will always get the zeroth element, since a non-numerical string has an integer value of zero.<br />
An important array which every program defines is<br />
@ARGV<br />
This is the argument vector array, and contains the commands line arguments by analogy with the C-shell variable `$argv[]‘.<br />
Given an array, we can find the last element by using the `$#’ operator. For example,<br />
$last_element = $ARGV[$#ARGV];<br />
Notice that each element in an array is a scalar variable. The `$#’ cannot be interpreted directly as the number of elements in the array, as it can in the C-shell. You should experiment with the value of this quantity — it often necessary to add 1 or 2 to its value in order to get the behaviour one is used to in the C-shell.<br />
Perl does not support multiple-dimension arrays directly, but it is possible to simulate them yourself. (See the Perl book.)<br />
Special array commands<br />
The `shift’ command acts on arrays and returns and removes the first element of the array. Afterwards, all of the elements are shifted down one place. So one way to read the elements of an array in order is to repeatedly call `shift’.<br />
$next_element=shift(@myarray);<br />
Note that, if the array argument is omitted, then `shift’ works on `@ARGV’ by default.<br />
Another useful function is `split’, which takes a string and turns it into an array of strings. `split’ works by choosing a character (usually a space) to delimit the array elements, so a string containing a sentence separated by spaces would be turned into an array of words. The syntax is<br />
@array = split;                       # works with spaces on $_<br />
@array = split(pattern,string);       # Breaks on pattern<br />
($v1,$v2…) = split(pattern,string); # Name array elements with scalars<br />
In the first of these cases, it is assumed that the variable `$_’ is to be split on whitespace characters. In the second case, we decide on what character the split is to take place and on what string the function is to act. For instance<br />
@new_array = split(”:”,”name:passwd:uid:gid:gcos:home:shell”);<br />
The result is a seven element array called `@new_array’, where `$new_array[0]‘ is `name’ etc.<br />
In the final example, the left hand side shows that we wish to capture elements of the array in a named set of scalar variables. If the number of variables on the lefthand side is fewer than the number of strings which are generated on the right hand side, they are discarded. If the number on the left hand side is greater, then the remainder variables are empty.<br />
Associated arrays<br />
One of the very nice features of Perl is the ability to use one string as an index to another string in an array. For example, we can make a short encyclopedia of zoo animals by constructing an associative array in which the keys (or indices) of the array are the names of animals, and the contents of the array are the information about them.</p>
<p>$animals{”Penguin”} = “A suspicious animal, good with cheese crackers…”;<br />
$animals{”dog”} = “Plays stupid, but could be a cover…”;</p>
<p>if ($index eq “fish”)<br />
   {<br />
   $animals{$index} = “Often comes in square boxes. Very cold.”;<br />
   }</p>
<p>An entire associated array is written `%array’, while the elements are `$array{$key}’.<br />
Perl provides a special associative array for every program called `%ENV’. This contains the environment variables defined in the parent shell which is running the Perl program. For example<br />
print “Username = $ENV{”USER”}\n”;</p>
<p>$ld = “LD_LIBRARY_PATH”;<br />
print “The link editor path is $ENV{$ld}\n”;</p>
<p>To get the current path into an ordinary array, one could write,<br />
@path_array= split(”:”,$ENV{”PATH”});<br />
Array example program<br />
Here is an example which prints out a list of files in a specified directory, in order of their UNIX protection bits. The least protected file files come first.<br />
#!/local/bin/perl<br />
#<br />
# Demonstration of arrays and associated arrays.<br />
# Print out a list of files, sorted by protection,<br />
# so that the least secure files come first.<br />
#<br />
# e.g.     arrays<br />
#          arrays *.C<br />
#<br />
############################################################</p>
<p>print “You typed in “,$#ARGV+1,” arguments to command\n”;</p>
<p>if ($#ARGV &lt; 1)<br />
   {<br />
   print “That’s not enough to do anything with!\n”;<br />
   }</p>
<p>while ($next_arg = shift(@ARGV))<br />
   {<br />
   if ( ! ( -f $next_arg || -d $next_arg))<br />
      {<br />
      print “No such file: $next_arg\n”;<br />
      next;<br />
      }</p>
<p>   ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size) = stat($next_arg);<br />
   $octalmode = sprintf(”%o”,$mode &amp; 0777);</p>
<p>   $assoc_array{$octalmode} .= $next_arg.<br />
            ” : size (”.$size.”), mode (”.$octalmode.”)\n”;<br />
   }</p>
<p>print “In order: LEAST secure first!\n\n”;</p>
<p>foreach $i (reverse sort keys(%assoc_array))<br />
   {<br />
   print $assoc_array{$i};<br />
   }</p>
<p>Loops and conditionals<br />
Here are some of the most commonly used decision-making constructions and loops in Perl. The following is not a comprehensive list — for that, you will have to look in the Perl bible: Programming Perl, by Larry Wall and Randal Schwartz. The basic pattern follows the C programming language quite closely. In the case of the `for’ loop, Perl has both the C-like version, called `for’ and a `foreach’ command which is like the C-shell implementation.<br />
if (expression)<br />
   {<br />
   block;<br />
   }<br />
else<br />
   {<br />
   block;<br />
   }</p>
<p>command if (expression);</p>
<p>unless (expression)<br />
   {<br />
   block;<br />
   }<br />
else<br />
   {<br />
   block;<br />
   }</p>
<p>while (expression)<br />
   {<br />
   block;<br />
   }</p>
<p>do<br />
   {<br />
   block;<br />
   }<br />
while (expression);</p>
<p>for (initializer; expression; statement)<br />
   {<br />
   block;<br />
   }</p>
<p>foreach variable(array)<br />
   {<br />
   block;<br />
   }</p>
<p>In all cases, the `else’ clauses may be omitted.<br />
Strangely, perl does not have a `switch’ statement, but the Perl book describes how to make one using the features provided.<br />
The for loop<br />
The for loop is exactly like that in C or C++ and is used to iterate over a numerical index, like this:</p>
<p>for ($i = 0; $i &lt; 10; $i++)<br />
   {<br />
   print $i, “\n”;<br />
   }</p>
<p>The foreach loop<br />
The foreach loop is like its counterpart in the C shell. It is used for reading elements one by one from a regular array. For example,</p>
<p>foreach $i ( @array )<br />
   {<br />
   print $i, “\n”;<br />
   }</p>
<p>Iterating over elements in arrays<br />
One of the main uses for `for’ type loops is to iterate over successive values in an array. This can be done in two ways which show the essential difference between for and foreach.<br />
If we want to fetch each value in an array in turn, without caring about numerical indices, the it is simplest to use the foreach loop.</p>
<p> @array = split(” “,”a b c d e f g”);</p>
<p> foreach $var ( @array )<br />
    {<br />
    print $var, “\n”;<br />
    }</p>
<p>This example prints each letter on a separate line. If, on the other hand, we are interested in the index, for the purposes of some calculation, then the for loop is preferable.</p>
<p> @array = split(” “,”a b c d e f g”);</p>
<p> for ($i = 0; $i &lt;= $#array; $i++)<br />
    {<br />
    print $array[$i], “\n”;<br />
    }</p>
<p>Notice that, unlike the for-loop idiom in C/C++, the limit is `$i &lt;= $#array’, i.e. `less than or equal to’ rather than `less than’. This is because the `$#’ operator does not return the number of elements in the array but rather the last element.<br />
Associated arrays are slightly different, since they do not use numerical keys. Instead they use a set of strings, like in a database, so that you can use one string to look up another. In order to iterate over the values in the array we need to get a list of these strings. The keys command is used for this.<br />
 $assoc{”mark”} = “cool”;<br />
 $assoc{”GNU”} = “brave”;<br />
 $assoc{”zebra”} = “stripy”;</p>
<p> foreach $var ( keys %assoc )<br />
    {<br />
    print “$var , $assoc{$var} \n”;<br />
    }<br />
The order of the keys is not defined in the above example, but you can choose to sort them alphabetically by writing<br />
 foreach $var ( sort keys %assoc )<br />
instead.<br />
Iterating over lines in a file<br />
Since Perl is about file handling we are very interested in reading files. Unlike C and C++, perl likes to read files line by line. The angle brackets are used for this, See section Files in perl. Assuming that we have some file handle `’, for instance `’, we can always read the file line by line with a while-loop like this.</p>
<p> while ($line = )<br />
    {<br />
    print $line;<br />
    }</p>
<p>Note that $line includes the end of line character on the end of each line. If you want to remove it, you should add a `chop’ command:</p>
<p> while ($line = )<br />
    {<br />
    chop $line;<br />
    print “line = ($line)\n”;<br />
    }</p>
<p>Files in perl<br />
Opening files is straightforward in Perl. Files must be opened and closed using — wait for it — the commands `open’ and `close’. You should be careful to close files after you have finished with them — especially if you are writing to a file. Files are buffered and often large parts of a file are not actually written until the `close’ command is received.<br />
Three files are, of course, always open for every program, namely `STDIN’, `STDOUT’and `STDERR’.<br />
Formally, to open a file, we must obtain a file descriptor or file handle. This is done using `open’;<br />
open (file_descrip,”Filename”);<br />
The angular brackets `’ are used to read from the file. For example,<br />
$line = ;<br />
reads one line from the file associated with `file_descrip’.<br />
Let’s look at some examples of filing opening. Here is how we can implement UNIX’s `cut’ and `paste’ commands in perl:<br />
#!/local/bin/perl<br />
#<br />
# Cut in perl<br />
#</p>
<p> # Cut second column</p>
<p>while ()<br />
   {<br />
   @cut_array = split;</p>
<p>   print “@cut_array[1]\n”;<br />
   }</p>
<p>This is the simplest way to open a file. The empty file descriptor `’ tells perl to take the argument of the command as a filename and open that file for reading. This is really short for `while($_=)’ with the standard input redirected to the named file.<br />
The `paste’program can be written as follows:<br />
#!/local/bin/perl<br />
#<br />
# Paste in perl<br />
#<br />
# Two files only, syntax : paste file 1file2<br />
#</p>
<p>open (file1,”@ARGV[0]“) || die “Can’t open @ARGV[0]\n”;<br />
open (file2,”@ARGV[1]“) || die “Can’t open @ARGV[1]\n”;</p>
<p>while (($line1 = ) || ($line2 = ))<br />
   {<br />
   chop $line1;<br />
   chop $line2;</p>
<p>   print “$line1 $line2\n”;    # tab character between<br />
   }</p>
<p>Here we see more formally how to read from two separate files at the same time. Notice that, by putting the read commands into the test-expression for the `while’ loop, we are using the fact that `’ returns a non-zero (true) value unless we have reached the end of the file.<br />
To write and append to files, we use the shell redirection symbols inside the `open’ command.<br />
open(fd,”&gt; filename”);    # open file for writing<br />
open(fd,”&gt;&gt; filename”);   # open file for appending<br />
We can also open a pipe from an arbitrary UNIX command and receive the output of that command as our input:<br />
open (fd,”/bin/ps aux | “);<br />
A simple perl program<br />
Let us now write the simplest perl program which illustrates the way in which perl can save time. We shall write it in three different ways to show what the short cuts mean. Let us implement the `cat’ command, which copies files to the standard output. The simplest way to write this is perl is the following:</p>
<p>#!/local/bin/perl</p>
<p>while ()<br />
   {<br />
    print;<br />
   }</p>
<p>Here we have made heavy use of the many default assumptions which perl makes. The program is simple, but difficult to understand for novices. First of all we use the default file handle  which means, take one line of input from a default file. This object returns true as long as it has not reached the end of the file, so this loop continues to read lines until it reaches the end of file. The default file is standard input, unless this script is invoked with a command line argument, in which case the argument is treated as a filename and perl attempts to open the argument-filename for reading. The print statement has no argument telling it what to print, but perl takes this to mean: print the default variable `$_’.<br />
We can therefore write this more explicitly as follows:</p>
<p>#!/local/bin/perl</p>
<p>open (HANDLE,”$ARGV[1]“);</p>
<p>while ()<br />
   {<br />
    print $_;<br />
   }<br />
Here we have simply filled in the assumptions explicitly. The command `’ now reads a single line from the named file-handle into the default variable `$_’. To make this program more general, we can eliminate the defaults entirely.</p>
<p>#!/local/bin/perl</p>
<p>open (HANDLE,”$ARGV[1]“);</p>
<p>while ($line=)<br />
   {<br />
    print $line;<br />
   }<br />
== and `eq’<br />
Be careful to distinguish between the comparison operator for integers `==’ and the corresponding operator for strings `eq’. These do not work in each other’s places so if you get the wrong comparison operator your program might not work and it is quite difficult to find the error.<br />
chop<br />
The command `chop’ cuts off the last character of a string. This is useful for removing newline characters when reading files etc. The syntax is<br />
chop;         # chop $_;</p>
<p>chop $scalar; # remove last character in $scalar<br />
Perl subroutines<br />
Subroutines are indicated, as in the example above, by the ampersand `&amp;’ symbol. When parameters are passed to a Perl subroutine, they are handed over as an array called `@_’. Which is analogous to the `$_’ variable. Here is a simple example:<br />
#!/local/bin/perl</p>
<p>$a=”silver”;<br />
$b=”gold”;</p>
<p>&amp;PrintArgs($a,$b);</p>
<p># end of main</p>
<p>sub PrintArgs</p>
<p>   {<br />
   ($local_a,$local_b) = @_;</p>
<p>   print “$local_a, $local_b\n”;<br />
   }</p>
<p>die – exit on error<br />
When a program has to quit and give a message, the `die’ command is normally used. If called without an argument, Perl generates its own message including a line number at which the error occurred. To include your own message, you write<br />
die “My message….”;<br />
If the string is terminated with a `\n’ newline character, the line number of the error is not printed, otherwise Perl appends the line number to your string.<br />
When opening files, it is common to see the syntax:<br />
open (filehandle,”Filename”) || die “Can’t open…”;<br />
The logical `OR’ symbol is used, because `open’ returns true if all goes well, in which case the right hand side is never evaluated. If `open’ is false, then die is executed. You can decide for yourself whether or not you think this is good programming style — we mention it here because it is common practice.<br />
The stat() idiom<br />
The UNIX library function stat() is used to find out information about a given file. This function is available both in C and in Perl. In perl, it returns an array of values. Usually we are interested in knowing the access permissions of a file. stat() is called using the syntax</p>
<p>@array = stat (”filename”);</p>
<p>or alternatively, using a named array</p>
<p>($device,$inode,$mode) = stat(”filename”);</p>
<p>The value returned in the mode variable is a bit-pattern, See section Protection bits. The most useful way of treating these bit patterns is to use octal numbers to interpret their meaning.<br />
To find out whether a file is readable or writable to a group of users, we use a programming idiom which is very common for dealing with bit patterns: first we define a mask which zeroes out all of the bits in the mode string except those which we are specifically interested in. This is done by defining a mask value in which the bits we want are set to 1 and all others are set to zero. Then we AND the mask with the mode string. If the result is different from zero then we know that all of the bits were also set in the mode string. As in C, the bitwise AND operator in perl is called `&amp;’.<br />
For example, to test whether a file is writable to other users in the same group as the file, we would write the following.</p>
<p>$mask = 020;   # Leading 0 means octal number</p>
<p>($device,$inode,$mode) = stat(”file”);</p>
<p>if ($mode &amp; $mask)<br />
   {<br />
   print “File is writable by the group\n”;<br />
   }</p>
<p>Here the 2 in the second octal number means “write”, the fact that it is the second octal number from the right means that it refers to “group”. Thus the result of the if-test is only true if that particular bit is true. We shall see this idiom in action below.<br />
Perl example programs<br />
The passwd program and `crypt()’ function<br />
Here is a simple implementation of the UNIX `passwd’ program in Perl.<br />
#!/local/bin/perl<br />
#<br />
# A perl version of the passwd program.<br />
#<br />
# Note – the real passwd program needs to be much more<br />
# secure than this one. This is just to demonstrate the<br />
# use of the crypt() function.<br />
#<br />
#############################################################</p>
<p>print “Changing passwd for $ENV{’USER’} on $ENV{’HOST’}\n”;</p>
<p>system ’stty’,&#8217;-echo’;<br />
print “Old passwd: “;</p>
<p>$oldpwd = ;<br />
chop $oldpwd;</p>
<p>($name,$coded_pwd,$uid,$gid,$x,$y,$z,$gcos,$home,$shell)<br />
                                 = getpwnam($ENV{”USER”});</p>
<p>if (crypt($oldpwd,$coded_pwd) ne $coded_pwd)<br />
   {<br />
   print “\nPasswd incorrect\n”;<br />
   exit (1);<br />
   }</p>
<p>$oldpwd = “”;                         # Destroy the evidence!</p>
<p>print “\nNew passwd: “;</p>
<p>$newpwd = ;</p>
<p>print “\nRepeat new passwd: “;</p>
<p>$rnewpwd = ;</p>
<p>chop $newpwd;<br />
chop $rnewpwd;</p>
<p>if ($newpwd ne $rnewpwd)<br />
   {<br />
   print “\n Incorrectly typed. Password unchanged.\n”;<br />
   exit (1);<br />
   }</p>
<p>$salt = rand();<br />
$new_coded_pwd = crypt($newpwd,$salt);</p>
<p>print “\n\n$name:$new_coded_pwd:$uid:$gid:$gcos:$home:$shell\n”;</p>
<p>Example with `fork()’<br />
The following example uses the `fork’ function to start a daemon which goes into the background and watches the system to which process is using the greatest amount of CPU time each minute. A pipe is opened from the BSD `ps’ command.<br />
#!/local/bin/perl<br />
#<br />
# A fork() demo. This program will sit in the background and<br />
# make a list of the process which uses the maximum CPU average<br />
# at 1 minute intervals. On a quiet BSD like system this will<br />
# normally be the swapper (long term scheduler).<br />
#</p>
<p>$true = 1;<br />
$logfile=”perl.cpu.logfile”;</p>
<p>print “Max CPU logfile, forking daemon…\n”;</p>
<p>if (fork())<br />
   {<br />
   exit(0);<br />
   }</p>
<p>while ($true)<br />
   {<br />
   open (logfile,”&gt;&gt; $logfile”) || die “Can’t open $logfile\n”;<br />
   open (ps,”/bin/ps aux |”) || die “Couldn’t open a pipe from ps !!\n”;</p>
<p>   $skip_first_line = ;<br />
   $max_process = ;<br />
   close(ps);</p>
<p>   print logfile $max_process;<br />
   close(logfile);<br />
   sleep 60;</p>
<p>   ($a,$b,$c,$d,$e,$f,$g,$size) = stat($logfile);</p>
<p>   if ($size &gt; 500)<br />
      {<br />
      print STDERR “Log file getting big, better quit!\n”;<br />
      exit(0);<br />
      }<br />
   }</p>
<p>Example reading databases<br />
Here is an example program with several of the above features demonstrated simultaneously. This following program lists all users who have home directories on the current host. If the home area has sub-directories, corresponding to groups, then this is specified on the command line. The word `home’ causes the program to print out the home directories of the users.<br />
#!/local/bin/perl<br />
##################################################################<br />
#<br />
# allusers – list all users on named host, i.e. all<br />
#            users who can log into this machine.<br />
#<br />
# Syntax: allusers group<br />
#         allusers mygroup home<br />
#         allusers myhost group home<br />
#<br />
# NOTE : This command returns only users who are registered on<br />
#        the current host. It will not find users which cannot<br />
#        be validated in the passwd file, or in the named groups<br />
#        in NIS. It assumes that the users belonging to<br />
#        different groups are saved in subdirectories of<br />
#        /home/hostname.<br />
#<br />
##################################################################</p>
<p>&amp;arguments();</p>
<p>die “\n” if ( ! -d “/home/$server” );</p>
<p>$disks = `/bin/ls -d /home/$server/$group`;</p>
<p>foreach $home (split(/\s/,$disks))<br />
   {<br />
   open (LS,”cd $home; /bin/ls $home |”) || die “allusers: Pipe didn’t open”;</p>
<p>   while ()<br />
      {<br />
      $exists = “”;<br />
      ($user) = split;<br />
      ($exists,$pw,$uid,$gid,$qu,$cm,$gcos,$dir)=getpwnam($user);</p>
<p>      if ($exists)<br />
         {<br />
         if ($printhomes)<br />
            {<br />
            print “$dir\n”;<br />
            }<br />
         else<br />
            {<br />
            print “$user\n”;<br />
            }<br />
         }<br />
      }<br />
   close(LS);<br />
   }</p>
<p>########################################################</p>
<p>sub arguments<br />
   {<br />
   $printhomes = 0;<br />
   $group = “*”;<br />
   $server = `/bin/hostname`;<br />
   chop $server;</p>
<p>   foreach $arg (@ARGV)<br />
      {<br />
      if (substr($arg,0,1) eq “u”)<br />
         {<br />
         $group = $arg;<br />
         next;<br />
         }</p>
<p>      if ($arg eq “home”)<br />
         {<br />
         $printhomes = 1;<br />
         next;<br />
         }</p>
<p>      $server= $arg;     #default is to interpret as a server.<br />
      }<br />
   }</p>
<p>Pattern matching and extraction<br />
Perl has regular expression operators for identifying patterns. The operator</p>
<p>     /regular expression/</p>
<p>returns true of false depending on whether the regular expression matches the contents of $_. For example</p>
<p>  if (/perl/)<br />
     {<br />
     print “String contains perl as a substring”;<br />
     }</p>
<p>  if (/(Sat|Sun)day/)<br />
     {<br />
     print “Weekend day….”;<br />
     }<br />
The effect is rather like the grep command. To use this operator on other variables you would write:</p>
<p>  $variable =~ /regexp/</p>
<p>Regular expression can contain parenthetic sub-expressions, e.g.</p>
<p>  if (/(Sat|Sun)day (..)th (.*)/)<br />
     {<br />
     $first = $1;<br />
     $second = $2;<br />
     $third = $3;<br />
     }<br />
in which case perl places the objects matched by such sub-expressions in the variables $1, $2 etc.<br />
Searching and replacing text<br />
The `sed’-like function for replacing all occurances of a string is easily implemented in Perl using<br />
while ()<br />
   {<br />
   s/$search/$replace/g;<br />
   print output;<br />
   }<br />
This example replaces the string inside the default variable. To replace in a general variable we use the operator `=~’, with syntax:<br />
$variable =~ s/search/replace/<br />
Here is an example of some of this operator in use. The following is a program which searches and replaces a string in several files. This is useful program indeed for making a change globally in a group of files! The program is called `file-replace’.<br />
#!/local/bin/perl<br />
##############################################################<br />
#<br />
# Look through files for findstring and change to newstring<br />
# in all files.<br />
#<br />
##############################################################</p>
<p>#<br />
# Define a temporary file and check it doesn’t exist<br />
#</p>
<p>$outputfile = “tmpmarkfind”;<br />
unlink $outputfile;</p>
<p>#<br />
# Check command line for list of files<br />
#</p>
<p>if ($#ARGV &lt; 0)<br />
   {<br />
   die “Syntax: file-replace [file list]\n”;<br />
   }</p>
<p>print “Enter the string you want to find (Don’t use quotes):\n\n:”;<br />
$findstring=;<br />
chop $findstring;</p>
<p>print “Enter the string you want to replace with (Don’t use quotes):\n\n:”;<br />
$replacestring=;<br />
chop $replacestring;</p>
<p>#</p>
<p>print “\nFind: $findstring\n”;<br />
print “Replace: $replacestring\n”;<br />
print “\nConfirm (y/n)  “;<br />
$y = ;<br />
chop $y;</p>
<p>if ( $y ne “y”)<br />
   {<br />
   die “Aborted — nothing done.\n”;<br />
   }<br />
else<br />
   {<br />
   print “Use CTRL-C to interrupt…\n”;<br />
   }</p>
<p>#<br />
# Now shift default array @ARGV to get arguments 1 by 1<br />
#</p>
<p>while ($file = shift)<br />
   {<br />
   if ($file eq “file-replace”)<br />
      {<br />
      print “Findmark will not operate on itself!”;<br />
      next;<br />
      }</p>
<p>   #<br />
   # Save existing mode of file for later<br />
   #</p>
<p>   ($dev,$ino,$mode)=stat($file);</p>
<p>   open (INPUT,$file) || warn “Couldn’t open $file\n”;<br />
   open (OUTPUT,”&gt; $outputfile”) || warn “Can’t open tmp”;</p>
<p>   $notify = 1;</p>
<p>   while ()<br />
      {<br />
      if (/$findstring/ &amp;&amp; $notify)<br />
         {<br />
         print “Fixing $file…\n”;<br />
         $notify = 0;<br />
         }<br />
      s/$findstring/$replacestring/g;<br />
      print OUTPUT;<br />
      }</p>
<p>   close (OUTPUT);</p>
<p>   #<br />
   # If nothing went wrong (if outfile not empty)<br />
   # move temp file to original and reset the<br />
   # file mode saved above<br />
   #</p>
<p>   if (! -z $outputfile)<br />
      {<br />
      rename ($outputfile,$file);<br />
      chmod ($mode,$file);<br />
      }<br />
   else<br />
      {<br />
      print “Warning: file empty!\n.”;<br />
      }<br />
   }<br />
Similarly we can search for lines containing a string. Here is the grep program written in perl<br />
#!/local/bin/perl<br />
#<br />
# grep as a perl program<br />
#</p>
<p># Check arguments etc</p>
<p>while ()<br />
   {<br />
   print if (/$ARGV[1]/);<br />
   }</p>
<p>The operator `/search-string/’ returns true if the search string is a substring of the default variable $_. To search an arbitrary string, we write<br />
 …. if (teststring =~ /search-string/);<br />
Here teststring is searched for occurrances of search-string and the result is true if one is found.<br />
In perl you can use regular expressions to search for text patterns. Note however that, like all regular expression dialects, perl has its own conventions. For example the dollar sign does not mean “match the end of line” in perl, instead one uses the `\n’ symbol. Here is an example program which illustrates the use of regular expressions in perl:<br />
#!/local/bin/perl<br />
#<br />
# Test regular expressions in perl<br />
#<br />
# NB – careful with \ $ * symbols etc. Use ” quotes since<br />
#      the shell interprets these!<br />
#</p>
<p>open (FILE,”regex_test”);</p>
<p>$regex = $ARGV[$#ARGV];</p>
<p>print “Looking for $ARGV[$#ARGV] in file…\n”;</p>
<p>while ()<br />
   {<br />
   if (/$regex/)<br />
      {<br />
      print;<br />
      }<br />
   }</p>
<p>#<br />
# Test like this:<br />
#<br />
#  regex ‘.*’       – prints every line (matches everything)<br />
#  regex ‘.’        – all lines except those containing only blanks<br />
#                     (. doesn’t match ws/white-space)<br />
#  regex ‘[a-z]‘    – matches any line containing lowercase<br />
#  regex ‘[^a-z]‘   – matches any line containg something which is<br />
#                     not lowercase a-z<br />
#  regex ‘[A-Za-z]‘ – matches any line containing letters of any kind<br />
#  regex ‘[0-9]‘    – match any line containing numbers<br />
#  regex ‘#.*’      – line containing a hash symbol followed by anything<br />
#  regex ‘^#.*’     – line starting with hash symbol (first char)<br />
#  regex ‘;\n’      – match line ending in a semi-colon<br />
#</p>
<p>Try running this program with the test data on the following file which is called `regex_test’ in the example program.</p>
<p># A line beginning with a hash symbol</p>
<p>JUST UPPERCASE LETTERS</p>
<p>just lowercase letters</p>
<p>Letters and numbers 123456</p>
<p>123456</p>
<p>A line ending with a semi-colon;</p>
<p>Line with a comment # COMMENT…</p>
<p>Example: convert mail to WWW pages<br />
Here is an example program which you could use to automatically turn a mail message of the form<br />
From: Newswire<br />
To: Mail2html<br />
Subject: Nothing happened</p>
<p>On the 13th February at kl. 09:30 nothing happened. No footprints<br />
were found leading to the scene of a terrible murder, no evidence<br />
of a struggle …. etc etc</p>
<p>into an html-file for the world wide web. The program works by extracting the message body and subject from the mail and writing html-commands around these to make a web page. The subject field of the mail becomes the title. The other headers get skipped, since the script searches for lines containing the sequence “colon-space” or `: ‘. A regular expression is used for this.<br />
#!/local/bin/perl<br />
#<br />
# Make HTML from mail<br />
#</p>
<p>&amp;BeginWebPage();<br />
&amp;ReadNewMail();<br />
&amp;EndWebPage();</p>
<p>##########################################################</p>
<p>sub BeginWebPage</p>
<p>{<br />
    print “\n”;<br />
    print “\n”;<br />
}</p>
<p>##########################################################</p>
<p>sub EndWebPage</p>
<p>{<br />
    print “\n”;<br />
    print “\n”;<br />
}</p>
<p>##########################################################</p>
<p>sub ReadNewMail</p>
<p>{<br />
while ()<br />
   {<br />
   if (/Subject:/)   # Search for subject line<br />
      {<br />
      # Extract subject text…</p>
<p>      chop;<br />
      ($left,$right) = split(”:”,$_);<br />
      print “<H1> $right </H1>\n”;<br />
      next;<br />
      }<br />
   elsif (/.*: .*/)   # Search for – anything: anything<br />
      {<br />
      next;           # skip other headers<br />
      }</p>
<p>   print;<br />
   }<br />
}</p>
<p>Generate WWW pages automagically<br />
The following program scans through the password database and build a standardized html-page for each user it finds there. It fills in the name of the user in each case. Note the use of the `&lt; $outputfile”) || die “Can’t open $outputfile\n”;</p>
<p>   &MakePage;</p>
<p>   close (OUT);<br />
   }</p>
<p>####################################################################<br />
# Level 1<br />
####################################################################</p>
<p>sub MakePage</p>
<p>{<br />
print OUT &lt;&lt;ENDMARKER;</p>
<p>$FullName{$user}’s Home Page<br />
<H1>$FullName{$user}’s Home Page</H1></p>
<p>Hi welcome to my home page. In case you hadn’t<br />
got it yet my name is: $FullName{$user}…</p>
<p>I study at <a href="http://www.iu.hio.no">H&oslash;gskolen i Oslo</a>.</p>
<p>ENDMARKER<br />
}</p>
<p>Other supported functions<br />
Perl has very many functions which come directly from the C library. To give a taster, a few are listed here. The Perl book contains a comprehensive description of these.<br />
Fork<br />
The standard UNIX fork command for spawning new processes.<br />
Sockets<br />
Support for network socket communication.<br />
Directories<br />
Directory opening and handling routines.<br />
Databases<br />
Reading from the password files and the host databases is supported through the standard C functions `getpasswdbyname’ etc. dressed up to look like Perl.<br />
Crypt<br />
The password encryption function.<br />
Regexp<br />
Regular expressions and pattern matching, search and replace functions as in `sed’.<br />
Operators<br />
Perl has the full set of C’s logical operators.<br />
File testing<br />
Tests from the shell like `if (-f file)’.<br />
Here are some of the most frequently used functions<br />
chmod<br />
Change the file mode of a file. e.g. chmod 755,filename<br />
chdir<br />
Change the current working directory. e.g. chdir /etc<br />
stat<br />
Get info about permissions, ownership and type of a file.<br />
open<br />
Open a file for reading, `&gt;’ writing, `|’ as a pipe.<br />
close<br />
Close an open file handle.<br />
system<br />
Execute a shell command as a child process. e.g. system “ls”;<br />
split<br />
Split a string variable into an array of elements, by searching for a special character (space or `:’ etc.) e.g. @array=split(”:”,$string).<br />
rename<br />
Rename a file. e.g. rename old name new-name<br />
mkdir<br />
Make a new directory. mkdir newdir<br />
shift<br />
Read the first element of an array and delete it, shifting all the array elements down by one. (e.g. $first=shift(@array);).<br />
chop<br />
Chops off the last character of a string. Often used for deleting the end-of-line character when reading from a file.<br />
oct<br />
Interprets a number as octal (converts to decimal). e.g. $decimal = oct(755);<br />
kill<br />
Send a kill signal to a list of processes. e.g. kill -9, pid1,pid2…<br />
You should explore Perl’s possibilities yourself. Perl is a good alternative to the shell which has much of the power of C and is therefore ideal for simple and more complex system programming tasks. If you intend to be a system administrator for UNIX systems, you could do much worse than to read the Perl book and learn Perl inside out.<br />
Summary<br />
The Practical Extraction and Report Language is a powerful tool which goes beyond shell programming, but which retains much of the immediateness of shell programming in a more formal programming environment.<br />
The success of Perl has led many programmers to use it exclusively. In the next section, I would like to argue that programming directly in C is not much harder. In fact it has advantages in the long run. The power of Perl is that it is as immediate as shell programming. If you are inexperienced, Perl is a little easier than C because many features are ready programmed into the language, but with time one also builds up a repertoire of C functions which can do the same tricks.<br />
Exercises<br />
1. Write a program which prints out all of its arguments alphabetically together with the first and the last, and the number of arguments.<br />
2. Write a program which prints out the pathname of the home directory for a given user. The user’s login name should be given as an argument.<br />
3. Write a program called `search-replace’ which looks for a given string in a list of files and replaces it with a new string. You should be able to specify a list of files using ordinary UNIX wildcards. e.g. `search-replace search-string replace-string *.text’. This is a dangerous operation! What if the user types the strings incorrectly? How can you may the program safer?<br />
4. Write a program which opens a pipe from `ps’ and computes the total cpu-time used by each user. Print the results in order of maximum to minimum. Hint: use an associated array to store the information.<br />
5. Write a program which forks and goes into the background. Make the program send you mail when some other user of your choice logs in. Use sleep to check only every few minutes.<br />
6. Open a pipe from `find’ and collect statistics over how many files there are in all of your sub-directories.<br />
Project<br />
Write a program which checks the `sanity’ of your UNIX system.<br />
1. Check that the password file /etc/passwd is not writable by general users.<br />
2. Check that the processes `cron’ and `sendmail’ are running.<br />
3. Check that, if the file `/etc/exports’ or `/etc/dfs/dfstab’ exists, the nfsd daemon is running.<br />
4. Check that if the filesystem table `/etc/fstab’ (or its equivalent on non-BSD systems) contains NFS mounted filesystems, the `biod’ or `nfsiod’ daemon is running.<br />
5. Check that the file `/etc/resolv.conf’ contains the correct domain name. It may or may not be the same as that returned by the shell command `domainname’. If it is not the same, you should print the message `NIS domain has different name to DNS domain’.<br />
WWW and CGI programming<br />
CGI stands for the Common Gateway Interface. It is the name given to scripts which can be executed from within pages of the world wide web. Although it is possible to use any language in CGI programs (hence the word `common’), the usual choice is Perl, because of the ease with which Perl can handle text.<br />
The CGI interface is pretty unintelligent, in order to be as general as possible, so we need to do a bit of work in order to make scripts work.<br />
Permissions<br />
The key thing about the WWW which often causes a lot of confusion is that the W3 service runs with a user ID of `nobody’. The purpose of this is to ensure that nobody has the right to read or write files unless they are opened very explicitly by the user who owns them.<br />
In order for files to be readable on the WWW, they must have file mode `644′ and they must lie in a directory which has mode `755′. In order for a CGI program to be executable, it must have permission `755′ and in order for such a program to write to a file in a user’s directory, it must be possible for the file to be created (if necessary) and everyone must be able to write to it. That means that files which are written to by the WWW must have mode `666′ and must either exist already or lie in a directory with permission `777′(6).<br />
Protocols<br />
CGI script programs communicate with W3 browsers using a very simple protocol. It goes like this:<br />
“ A web page sends data to a script using the `forms’ interface. Those data are concatenated into a single line. The data in separate fields of a form are separated by `&amp;’ signs. New lines are replaced by the text `%0D%0A’, which is the DOS ASCII representation of a newline, and spaces are replaced by `+’ symbols.<br />
“ A CGI script reads this single line of text on the standard input.<br />
“ The CGI script replies to the web browser. The first line of the reply must be a line which is tells the browser what mime-type the data are sent in. Usually, a CGI script replies in HTML code, in which case the first line in the reply must be:<br />
“   Content-type: text/html<br />
This must be followed by a blank line.<br />
HTML coding of forms<br />
To start a CGI program from a web page we use a form which is a part of the HTML code enclosed with the parentheses</p>
<p>     …</p>
<p>The method `post’ means that the data which get typed into this form will be piped into the CGI program via its standard input. The `action’ specifies which program you want to start. Note that you cannot simply use the absolute path of the file, for security reasons. You must use something called a `script alias’ to tell the web browser where to find the program. If you do not have a script alias defined for you personally, then you need to get one from your system administrator. By using a script alias, no one from outside your site can see where your files are located, only that you have a `cgi-bin’ area somewhere on your system.<br />
Within these parentheses, you can arrange to collect different kinds of input. The simplest kind of input is just a button which starts the CGI program. This has the form</p>
<p>This code creates a button. When you click on it the program in your `action’ string gets started. More generally, you will want to create input boxes where you can type in data. To create a single line input field, you use the following syntax:</p>
<p>This creates a single line text field of width 40 characters. This is not the limit on the length of the string which can be typed into the field, only a limit on the amount which is visible at any time. It is for visual formatting only. The NAME field is used to identify the data in the CGI script. The string you enter here will be sent to the CGI script in the form `variable-name=value of input…’. Another type of input is a text area. This is a larger box where one can type in text on several lines. The syntax is:</p>
<p>which means: create a text area of fifty rows by fifty columns with a prompt to the left of the box. Again, the size has only to do with the visual formatting, not to do with limits on the amount of text which can be entered.<br />
As an example, let’s create a WWW page with a complete form which can be used to make a guest book, or order form.</p>
<p>Example form</p>
<p><H1>Write in my guest book…</H1><br />
<HR></p>
<p><H2>Please leave a comment using the form below.</H2><P></p>
<p>Your Name/e-mail:  <BR><BR></p>
<p><P></p>
<p><P></p>
<p><P></p>
<p>The reset button clears the form. When the submit button is pressed, the CGI program is activated.<br />
Perl and the web<br />
Interpreting data from forms<br />
To interpret and respond to the data in a form, we must write a program which satisfies the protocol above, See section Protocols. We use perl as a script language. The simplest valid CGI script is the following:</p>
<p>#!/local/bin/perl</p>
<p> #<br />
 # Reply with proper protocol<br />
 #</p>
<p>print “Content-type: text/html\n\n”;</p>
<p> #<br />
 # Get the data from the form …<br />
 #</p>
<p>$input = ;</p>
<p> #<br />
 # … and echo them back<br />
 #</p>
<p>print $input, “\n Done! \n”;</p>
<p>Although rather banal, this script is a useful starting point for CGI programming, because it shows you just how the input arrives at the script from the HTML form. The data arrive all in a single, enormously long line, full of funny characters. The first job of any script is to decode this line.<br />
Before looking at how to decode the data, we should make an important point about the protocol line. If a web browser does not get this `Content-type’ line from the CGI script it returns with an error:</p>
<p>500 Server Error</p>
<p>The server encountered an internal error or misconfiguration and was<br />
unable to complete your request.</p>
<p>Please contact the server administrator, and inform them of the time<br />
the error occurred, and anything you might have done that may have<br />
caused the error.</p>
<p>Error: HTTPd: malformed header from script www/cgi-bin/comment.pl</p>
<p>Before finishing your CGI script, you will probably encounter this error several times. A common reason for getting the error is a syntax error in your script. If your program contains an error, the first thing a browser gets in return is not the `Content-type’ line, but an error message. The browser does not pass on this error message, it just prints the uninformative message above.<br />
If you can get the above script to work, then you are ready to decode the data which are sent to the script. The first thing is to use perl to split the long line into an array of lines, by splitting on `&amp;’. We can also convert all of the `+’ symbols back into spaces. The script now looks like this:<br />
#!/local/bin/perl</p>
<p> #<br />
 # Reply with proper protocol<br />
 #</p>
<p>print “Content-type: text/html\n\n”;</p>
<p> #<br />
 # Get the data from the form …<br />
 #</p>
<p>$input = ;</p>
<p> #<br />
 # … and echo them back<br />
 #</p>
<p>print “$input\n\n\n”;</p>
<p>$input =~ s/\+/ /g;</p>
<p> #<br />
 # Now split the lines and convert<br />
 #</p>
<p>@array = split(’&amp;’,$input);</p>
<p>foreach $var ( @array )<br />
   {<br />
   print “$var\n”;<br />
   }</p>
<p>print “Done! \n”;</p>
<p>We now have a series of elements in our array. The output from this script is something like this:</p>
<p>variable1=Mark+Burgess&amp;variable2=%0D%0AI+just+called+to+say+ (wrap)<br />
….%0D%0A…hey+pig%2C+nothing%27s+working+out+the+way+I+planned<br />
variable1=Mark Burgess variable2=%0D%0AI just called to say  (wrap)<br />
….%0D%0A…hey pig%2Cnothing%27s working out the way I planned Done!</p>
<p>As you can see, all control characters are converted into the form `%XX’. We should now try to do something with these. Since we are usually not interested in keeping new lines, or any other control codes, we can simply null-out these with a line of the form</p>
<p>$input =~ s/%..//g;</p>
<p>The regular expression `%..’ matches anything beginning with a percent symbol followed by two characters. The resulting output is then free of these symbols. We can then separate the variable contents from their names by splitting the input. Here is the complete code:<br />
#!/local/bin/perl</p>
<p> #<br />
 # Reply with proper protocol<br />
 #</p>
<p>print “Content-type: text/html\n\n”;</p>
<p> #<br />
 # Get the data from the form …<br />
 #</p>
<p>$input = ;</p>
<p> #<br />
 # … and echo them back<br />
 #</p>
<p>print “$input\n\n\n”;</p>
<p>$input =~ s/%..//g;</p>
<p>$input =~ s/\+/ /g;</p>
<p>@array = split(’&amp;’,$input);</p>
<p>foreach $var ( @array )<br />
   {<br />
   print “$var<br />”;<br />
   }</p>
<p>print “<br />
<hr />\n”;</p>
<p>($name,$variable1) = split(”variable1=”,$array[0]);<br />
($name,$variable2) = split(”variable2=”,$array[1]);</p>
<p>print “<br />var1 = $variable1<br />”;<br />
print “<br />var2 = $variable2<br />”;</p>
<p>print “<br />Done! \n”;</p>
<p>and the output<br />
variable1=Mark+Burgess&amp;variable2=%0D%0AI+just+called+to+say (wrap)<br />
+….%0D%0A…hey+pig%2C+nothing%27s+working+out+the+way+I+planned<br />
variable1=Mark Burgess<br />
variable2=I just called to say …….hey pig nothings working (wrap)<br />
 out the way I planned</p>
<p>var1 = Mark Burgess</p>
<p>var2 = I just called to say …….hey pig nothings working out  (wrap)<br />
the way I planned</p>
<p>Done!<br />
A complete guestbook example in perl<br />
Let us now use this technique to develop a guest book application. Based on the code above, analyze the following code.<br />
#!/local/bin/perl<br />
####################################################################<br />
#<br />
# Guest book<br />
#<br />
####################################################################</p>
<p>$guestbook_page = “/iu/nexus/ud/mark/www/tmp/cfguest.html”;</p>
<p>$tmp_page = “/iu/nexus/ud/mark/www/tmp/guests.tmp”;</p>
<p>$remote_host = $ENV{”REMOTE_HOST”};</p>
<p>print “Content-type: text/html\n\n”;<br />
print “<br />
<hr />\n”;<br />
print “Thank you for submitting your comment!</p>
<p>\n”;<br />
print “best wishes,</p>
<p>”;<br />
print “-Mark</p>
<p>”;<br />
print “Return to <a href="http://www.iu.hio.no/~mark/menu.html">menu</a>\n”;</p>
<p>$input = ;</p>
<p>$input =~ s/%..//g;</p>
<p>$input =~ s/\+/ /g;</p>
<p>@array = split(’&amp;’,$input);</p>
<p>($skip,$name) = split(”var1=”,$array[0]);<br />
($skip,$message) = split(”var2=”,$array[1]);</p>
<p>if (! open (PAGE, $guestbook_page))<br />
   {<br />
   print “Content-type: text/html\n\n”;<br />
   print “couldn’t open guestbook page file!”;<br />
   }</p>
<p>if (! open (TMP, “+&gt;$tmp_page”))<br />
   {<br />
   print “Content-type: text/html\n\n”;<br />
   print “couldn’t open temporary output file!”;<br />
   }</p>
<p>while ($line = )<br />
   {<br />
   if ($line =~ /<br />
<h3>Number of entries: (..)/)<br />
      {<br />
      $entry_no = $1;<br />
      $entry_no++;<br />
      $line = “<br />
<h3>Number of entries: $entry_no </h3>
<p>\n”;<br />
      }</p>
<p>    if ($line =~ //)<br />
       {<br />
       $date = `date +”%A, %b %d %Y”`;<br />
       print TMP “<b>Entry $date from host: $remote_host</b>\n
<p>\n”;<br />
       print TMP “From: $name\n
<p>\n”;<br />
       print TMP $message;<br />
       print TMP “\n<br />
<hr />\n”;<br />
       }</p>
<p>    print TMP “$line”;<br />
    }</p>
<p>close PAGE;<br />
close TMP;</p>
<p>if (! rename ($tmp_page, $guestbook_page))<br />
   {<br />
   print “Oops! Rename operation failed!\n”;<br />
   }</p>
<p>chmod (0600, $guestbook_page);</p>
<p>This script works by reading through the old guest book file, opening a new copy of the guest book file and appending a new messages at the end. The end of the message section (not counting the `’ tags) is marked by a comment line.</p>
<p>Note that a provisional guest book file has to exist in the first place. The script writes to a new file and then swaps the new file for the old one. The guest book file looks something like this:</p>
<p>Comments</p>
<h1>My guest book</h1>
<p><b>Entry no.  Wednesday, Feb 28 1996<br />
 from host: dax</b></p>
<p>
From: Mark.Burgess@iu.hio.no</p>
<p>
Just to start the ball rolling….</p>
<hr />
<p><b>Entry no.  Tuesday, Mar 26 1996<br />
 from host: enterprise.subspace.net</b></p>
<p>
From: spock@enterprise</p>
<p>
Registering a form of energy never before encountered.</p>
<address><a href="//www.iu.hio.no/~mark”">Mark<br />
Burgess</a> – Mark.Burgess@iu.hio.no </p>
<p>The directory in which this file lies needs to be writable to the user nobody (the WWW user) and the files within need to be deletable by nobody but no one else. Some users try to make guest book scripts setuid-themselves in order to overcome the problem that httpd runs with uid nobody, but this opens many security issues. In short it is asking for trouble. Unfortunately an ordinary user cannot use chown in order to give access only to the WWW user nobody, so this approach needs the cooperation of the system administrator. Nevertheless this is the most secure approach. Try to work through this example step for step.<br />
PHP and the web<br />
The PHP language makes the whole business of web programming rather simpler than perl. It hides the business of translating variables from forms into new variables in a CGI program and it even allows you to embed active code into you HTML pages. PHP has special support for querying data in an SQL database like MySQL or Oracle. PHP documentation lives at @uref{http://www.php.net}.<br />
Embedded PHP<br />
PHP code can be embedded inside HTML pages provided your WWW server is configurered with PHP support. PHP code lives inside a tag with the general form</p>
<p>For example, we could use this to import one file into another and print out a table of numbers:</p>
<p>&lt;?php</p>
<p>include “file.html”</p>
<p>for ($i = 0; $i &lt; 10; $i++)<br />
   {<br />
   print “Counting $i<br />”;<br />
   }</p>
<p>?&gt;</p>
<p>This makes it easy to generate WWW pages with a fixed visual layout:<br />
&lt;?php<br />
#<br />
# Standard layout<br />
#</p>
<p># Set $title, $comment and $contents</p>
<p>##########################################################################</p>
<p>print “\n”;<br />
print “<img src="img/header.gif">”;</p>
<p>print “<br />
<h1>”$title</h1>
<p>”;<br />
print “<em>$comment</em>”;<br />
print “<br />
<blockquote>\n”;</p>
<p>include $contents;</p>
<p>print (”</p></blockquote>
<p>\n”);<br />
print (”\n”);<br />
print (”\n”);</p>
<p>Variables are easily set by calling PHP code in the form of a CGI program from a form.<br />
PHP and forms<br />
PHP is particularly good at dealing with forms, as a CGI scripting language. Consider the following form:</p>
<p>    Name:  <br />
    Email: <br />
    Preferred language:</p>
<p>        English<br />
        Norwegian<br />
        Gobbledigook</p>
<p>This produces a page into which one types a name and email address and chooses a language from a list of three possible choices. When the user clicks on a button marked by the file `image.gif’ the form is posted. Here is a program which unravels the data sent to the CGI program:</p>
<p>#!/local/bin/php</p>
<p>&lt;?php<br />
#<br />
# A CGI program which handles a form<br />
# Variables are translated automatically<br />
#</p>
<p>$title = “This page title”;<br />
$comment = “This pages talks about the following…..”;</p>
<p>##########################################################################</p>
<p>echo “”;<br />
echo “<br />
<h1>$title</h1>
<p>”;<br />
echo “<em>$comment</em>”;<br />
echo “<br />
<blockquote>\n”;</p>
<p>###</p>
<p> echo “Your name is $personal[name]</p>
<p>”;<br />
 echo “Your email is $personal[email]</p>
<p>”;</p>
<p> echo “Language options: “;<br />
 echo “<br />
<table> “;</p>
<p> for ($i = 0; strlen($language[$i]) &gt; 0; $i++)<br />
    {<br />
    echo “<br />
<tr>
<td bgcolor="#ff0000">Variable language[$i] = $language[$i]</td>
</tr>
<p>”;<br />
    }</p>
<p> if ($language[0] == “Norwegian”)<br />
    {<br />
    echo “Hei alle sammen
<p>”;<br />
    }<br />
 else<br />
    {<br />
    echo “Greetings everyone, this page will be in English
<p>”;<br />
    }</p>
<p> echo “</table>
<p> “;</p>
<p>###</p>
<p>echo (”</p></blockquote>
<p>\n”);<br />
echo (”\n”);<br />
echo (”\n”);<br />
?&gt;</p>
<p>A complete PHP guestbook<br />
If your web-server supports PHP there is no need for separate CGI-scripts handling form output. A single PHP-script can create the form and handle the output simultaneously. In addition this script can be placed wherever the web-server is able to read HTML files. PHP defines a special variable $PHP_SELF which provides the action= assignment of the form with the script itself. Moreover such PHP-scripts checks whether the user has submitted any data by checking if the variables of the form is set with the command isset(). The following code shows how easily a guestbook can be made using PHP compared to the Perl-code shown in a previous section. See section A complete guestbook example in perl.</p>
<p>My Guestbook</p>
<h1>Welcome to my Guestbook</h1>
<h2>Please write me a little note below</h2>
<p>&lt;form action=”” method=”POST”&gt;</p>
<p>&lt;?php<br />
  $file = “/iu/nexus/ud/haugerud/www/cgi-out/guestbook.txt”;<br />
  if(isset($note))<br />
    {<br />
    $date = date(”F j, Y, G:i”);<br />
    $buffer = “<br />
<h3>Message sent from IP-address $REMOTE_ADDR</h3>
<p>\n”;<br />
    $buffer .= “<br />
<h4>$date</h4>
<p>\n”;<br />
    $buffer .= nl2br($note).’<br />’;</p>
<p>    $handle = fopen ($file, “r”);<br />
    while (!feof($handle))<br />
        {<br />
     $buffer .= fread($handle,4096);<br />
     }<br />
    fclose($handle);</p>
<p>    $outhandle=fopen ($file,”w”);<br />
    fputs($outhandle,$buffer);<br />
    fclose($outhandle);<br />
   }<br />
?&gt;</p>
<h2>The entries so far:</h2>
<p>C programming<br />
This section is not meant to teach you C. It is a guide to using C in UNIX and it is assumed that you have a working knowledge of the language. See the GNU C-Tutorial for an introduction to basics.<br />
Shell or C?<br />
In the preceding chapters we have been looking at ways to get simple programming tasks done. The immediateness of the script languages is a great advantage when we just want to get a job done as quickly as possible. Scripts lend themselves to simple system administration tasks like file processing, but they do not easily lend themselves to more serious programs.<br />
Although some system administrators have grown to the idea that shell programming is easier, I would argue that this is not really true. First of all, most of the UNIX shell commands are just wrapper programs for C function calls. Why use the wrapper when you can use the real thing? Secondly, the C function calls return data in pointers and structures which are very easy to manipulate, whereas piping the output of shell programs into others can be a very messy and awkward way of working. Here are some of the reasons why we also need a more traditional programming language like C.<br />
1. The shell languages do not allow us to create an acceptable user-interface, like X-windows, or the curses (cursor manipulation) library. They are mainly intended for file-processing. (Though recently the Tk library has provided a way of creating user interfaces in Tcl and Perl.)<br />
2. Shell commands read their input line-by-line. Not all input is generated in this simple way — we also need to be able to read through lines i.e. the concept of a data stream.<br />
3. More advanced data structures are needed for most applications, such as linked lists and binary trees, acyclic graphs etc.<br />
4. Compilers help to sort out simple typographical and logical errors by compile-time checking source code.<br />
5. Compiled code is faster than interpreted code.<br />
6. Many tools have been written to help in the programming of C code (dbx, lex, yacc etc.).<br />
C program structure<br />
The form of a C program<br />
A C program consists of a set of function, beginning with the main program:</p>
<p>main ()   /* This is a comment */</p>
<p>{</p>
<p>Commands …</p>
<p>}</p>
<p>The source code of a C program can be divided into several text files. C compiles all functions separately; the linker ld joins them all up at the end. This means that we can plan out a strategy for writing large programs in a clear and efficient manner.<br />
NOTE: C++ style comments `//…’ are not allowed by most C compilers.<br />
Macros and declarations<br />
Most UNIX systems now have ANSI C compatible compilers, but this has not always been the case. Most UNIX programs written in a version of C which is older than the ANSI standard, so you will need an appreciation of old Kernighan and Ritchie C conventions for C programming. See for example my C book.<br />
An obvious difference between ANSI C and K&amp;R C is that the C++ additions to the language are not included. Here are some useful points to remember.<br />
“ K&amp;R C does not allow `const’ data, it uses the C preprocessor with `#define’ instead. i.e. instead of<br />
“<br />
“ const int blah = 1;<br />
“<br />
use<br />
#define blah 1<br />
Remember that the hash symbol `#’ must be the first character on a line under UNIX.<br />
“ K&amp;R C doesn’t use function prototypes or declarations of the form:<br />
“<br />
“ void function (char *string, int a, int b)<br />
“<br />
“ {<br />
“ }<br />
“<br />
Instead one writes:</p>
<p>void function (string, a, b)</p>
<p>char *string;<br />
int a,b;</p>
<p>{<br />
}</p>
<p>Several files<br />
Most UNIX programs are very large and are split up into many files. Remember, when you split up programs into several files, you must declare variables as `extern’ in file A if they are really declared in file B. in which you want to use them. This tells the compiler that it should not try to create local storage for the variable, because this was already done in another file.<br />
A note about UNIX system calls and standards<br />
Most of the system calls in UNIX return data in the form of `struct’ variables. Sometimes these are structures used by the operating system itself — in other cases they are just put together so that programmers can handle a packet of data in a convenient way.<br />
If in doubt, you can find the definitions of these structures in the relevant include files under `/usr/include’.<br />
Since UNIX comes in many flavours the system calls are not always compatible and may have different options and arguments. Because of this there is a number of standardizing organizations for UNIX. One of them is POSIX which is an organization run by the major UNIX vendors. Programs written for UNIX are now expected to be POSIX compliant. This is not something you need to think about at the level of this course, but you should certainly remember that there exist programming standards and that these should be adhered to. The aim is to work towards a single standard UNIX.<br />
Compiling: `cc’, `ld’ and `a.out’<br />
The C compiler on the UNIX system is traditionally called `cc’ and has always been a traditional part of every UNIX environment. Recently several UNIX vendors have stopped including the C compiler as a part of their operating systems and instead sell a compiler separately. Fortunately there is a public domain Free Software version of the compiler called `gcc’ (the GNU C compiler). We shall use this in all the examples.<br />
To compile a program consisting of several files of code, we first compile all of the separate pieces without trying to link them. There are therefore two stages: first we turn `.c’ files into `.o’ files. This compiles code but does not fix any address references. Then we link all `.o’ files into the final executable, including any libraries which are used.<br />
Let’s suppose we have files `a.c’, `b.c’ and `c.c’. We write:</p>
<p>  gcc -c a.c b.c c.c</p>
<p>This creates files `a.o’, `b.o’ and `c.o’. Next we link them into one file called `myprog’.</p>
<p>  gcc -o myprog  a.o b.o c.o</p>
<p>If the naming option `-o myprog’ is not used, the link `ld’ uses the default name a.out for the executable file.<br />
Libraries and `LD_LIBRARY_PATH’<br />
The resulting file is called `myprog’ and includes references only to the standard library `libc’. If we wish to link in the math library `libm’ or the cursor movement library `libcurses’ — or in general, a library called `libBLAH’ , we need to use the `-l’ directive.</p>
<p>gcc -o myprog files.o  -lm -lcurses -lBLAH</p>
<p>The compiler looks for a suitable library in all of the directories listed in the environment variable `LD_LIBRARY_PATH’. Alternatively we can add a directory to the search path by using the `-L’. option:</p>
<p>gcc -o myprog files.o -L/usr/local/lib -lm -lcurses -lBLAH</p>
<p>Include files<br />
Normally the compiler looks for include files only in the directory `/usr/include’. We can add further paths to search using the `-I’ option.</p>
<p>gcc -o myprog file.c  -I/usr/local/include -I/usr/local/X11/include</p>
<p>Previously, UNIX libraries have been in `a.out’ code format, but recent releases of UNIX have gone over to a more efficient and flexible format called ELF (executable and linking format).<br />
Shared and static libraries<br />
Libraries are collections of C functions which the operating system creators have written for our convenience. The source code for such a library is just the source for a collection of functions — there is no main program.<br />
There are two kinds of library used by modern operating systems: archive libraries or static libraries and shared libraries or dynamical libraries. An archive library has a name of the form</p>
<p> libname.a</p>
<p>When an archive library is linked to a program, it is appended lock, stock and barrel to the program code. This uses a lot of disk space and makes the size of the compiled program very large. Shared libraries (shared objects `so’ or shared archives `sa’ generally have names of the form)</p>
<p>  libname.so<br />
  libname.sa</p>
<p>often with version numbers appended. When a program is linked with a shared library the code is not appended to the program. Instead pointers to the shared objects are created and the library is loaded at runtime, thus avoiding the problem of having to store the library effectively multiple times on the disk.<br />
To make an archive library we compile all of the functions we wish to include in the library</p>
<p>gcc -c function1.c function2.c …</p>
<p>and then join the files using the `ar’ command.</p>
<p>ar rcv libMYLIB.a function1.o<br />
ar rcv libMYLIB.a function2.o</p>
<p>To make a shared library one provides an option to the linker program. The exact method is different in different operating systems, so you should look at the manual page for ld on your system. Under SunOS 4 we take the object files `*.o’ and run</p>
<p> ld -o libMYLIB.so.1.1 -assert pure-text *.o</p>
<p>Under HPUX, we write</p>
<p> ld -b -o libMYLIB.so.1.1 *.o</p>
<p>With the GNU linker, you write</p>
<p> ld -shared -o libMYLIB.so.1.1 *.o</p>
<p>NOTE: when you add a shared library to the system under SunOS or GNU/Linux you must run the command `ldconfig’, making sure that the path to the library is included in `LD_LIBRARY_PATH’. SunOS and GNU/Linux use a cache file `/etc/ld.so.cache’ to keep current versions of libraries. GNU/Linux also uses a configuration file called `/etc/ld.so.conf’.<br />
Knowing about important paths: directory structure<br />
It is important to understand how the C compiler finds the files it needs. We have already mentioned the `-I’ and `-L’ options to the compilation command line. In general, all system include files can be found in the directory `/usr/include’ and subdirectories of this directory. All system libraries can be found in `/usr/lib’.<br />
Many packages build their own libraries and keep the relevant files in separate directories so that if the system gets reinstalled, they do not get deleted. This is true for example of the X-windows system. The include and library files for this are typically kept in directories which look something like `/usr/local/X11R5/include’ and `/usr/X11R6/lib’. That means that we need to give all of this information to the compiler. Compiling a program becomes a complicated task in many cases so we need some kind of script to help us perform the task. The UNIX tool make was designed for this purpose.<br />
Make<br />
Nowadays compilers are often sold with fancy user environments driven by menus which make it easier to compile programs. UNIX has similar environments but all of them use shell-based command line compilation beneath the surface. That is because UNIX programmers are used to writing large and complex programs which occupy many directories and subdirectories. Each directory has to be adapted or configured to fit the particular flavour of UNIX system it is being compiled upon. Interactive user environments are very poor at performing this kind of service. UNIX solves the problem of compiling enormous trees of software (such as the UNIX system itself!) by using a compilation language called `make’. Such language files can be generated automatically by scripts, allowing very complex programs to configure and compile themselves from a single control script.<br />
Compiling large projects<br />
Typing lines like</p>
<p>  cc -c file1.c file2.c …<br />
  cc -o target file1.o ….</p>
<p>repeatedly to compile a complicated program can be a real nuisance. One possibility would therefore be to keep all the commands in a script. This could waste a lot of time though. Suppose you are working on a big project which consists of many lines of source code — but are editing only one file. You really only want to recompile the file you are working on and then relink the resulting object file with all of the other object files. Recompiling the other files which hadn’t changed would be a waste of time. But that would mean that you would have to change the script each time you change what you need to compile.<br />
A better solution is to use the `make’ command. `make’ was designed for precisely this purpose. To use `make’, we create a file called `Makefile’ in the same directory as our program. `make’ is a quite general program for building software. It is not specifically tied to the C programming language— it can be used in any programming language.<br />
A `make’ configuration file, called a `Makefile’, contains rules which describe how to compile or build all of the pieces of a program. For example, even without telling it specifically, make knows that in order to go from `prog.c’ to `prog.o’ the command `cc -c prog.c’ must be executed. A Makefile works by making such associations. The Makefile contains a list of all of the files which compose the program and rules as to how to get to the finished product from the source.<br />
The idea is that, to compile a program, we just have to type make. `make’ then reads the Makefile and compiles all of the parts which need compiling. It does not recompile files which have not changed since the last compilation! How does it do this? `make’ works by comparing the time-stamp on the file it needs to create with the time-stamp on the file which is to be compiled. If the compiled version exists and is newer than its source then the source does not need to be recompiled.<br />
To make this idea work in practice, `make’ has to know how to go through the steps of compiling a program. Some default rules are defined in a global configuration file, e.g.</p>
<p>/usr/include/make/default.mk</p>
<p>Let’s consider an example of what happens for the the three files `a.c’, `b.c’ and `c.c’ in the example above — and let’s not worry about what the Makefile looks like yet.<br />
The first time we compile, only the `.c’ files exist. When we type `make’, the program looks at its rules and finds that it has to make a file called `myprog’. To make this it needs to execute the command</p>
<p>  gcc -o myprog  a.o b.o c.o</p>
<p>So it looks for `a.o’ etc and doesn’t find them. It now goes to a kind of subroutine and looks to see if it has any rules for making files called `.o’ and it discovers that these are made by compiling with the `gcc -c’ option. Since the files do not exist, it does this. Now the files `a.o b.o c.o’ exist and it jumps back to the original problem of trying to make `myprog’. All the files it needs now exist and so it executes the command and builds `myprog’.<br />
If we now edit `a.c’, and type `make’ once again — it goes through the same procedure as before but now it finds all of the files. So it compares the dates on the files — if the source is newer than the result, it recompiles.<br />
By using this recursive method, `make’ only compiles those parts of a program which need compiling.<br />
Makefiles<br />
To write a Makefile, we have to tell `make’ about dependencies. The dependencies of a file are all of those files which are required to build it. Thus, the dependencies of `myprog’ are `a.o’, `b.o’ and `c.o’. The dependencies of `a.o’ are simply `a.c’, the dependencies of `b.o’ are `b.c’ and so on.<br />
A Makefile consists of rules of the form:<br />
target : dependencies<br />
TAB                   rule;</p>
<p>The target is the thing we want to build, the dependencies are like subroutines to be executed first if they do not exist. Finally the rule is to be executed if all if the dependencies exist; it takes the dependencies and turns them into the target. There are two important things to remember:<br />
“ The file names must start on the first character of a line.<br />
“ There must be a TAB character at the beginning of every rule or action. If there are spaces instead of tabs, or no tab at all, `make’ will signal an error. This bizarre feature can cause a lot of confusion.<br />
Let’s look at an example Makefile for a program which consists of two course files `main.c’ and `other.c’ and which makes use of a library called `libdb’ which lies in the directory `/usr/local/lib’. Our aim is to build a program called database:</p>
<p>#<br />
# Simple Makefile for `database’<br />
#</p>
<p># First define a macro</p>
<p>OBJ = main.o other.o</p>
<p>CC = gcc<br />
CFLAGS = -I/usr/local/include<br />
LDFLAGS = -L/usr/local/lib -ldb<br />
INSTALLDIR = /usr/local/bin</p>
<p>#<br />
# Rules start here. Note that the $@ variable becomes the name of the<br />
# executable file. In this case it is taken from the ${OBJ} variable<br />
#</p>
<p>database: ${OBJ}<br />
          ${CC} -o $@ ${OBJ} ${LDFLAGS}</p>
<p>#<br />
# If a header file changes, normally we need to recompile everything.<br />
# There is no way that make can know this unless we write a rule which<br />
# forces it to rebuild all .o files if the header file changes…<br />
#</p>
<p>${OBJ}: ${HEADERS}</p>
<p>#<br />
# As well as special rules for special files we can also define a<br />
# “suffix rule”. This is a rule which tells us how to build all files<br />
# of a certain type. Here is a rule to get .o files from .c files.<br />
# The $&lt; variable is like $? but is only used in suffix rules.<br />
#</p>
<p>.c.o:<br />
          ${CC} -c ${CFLAGS} $&lt;</p>
<p>#######################################################################<br />
# Clean up<br />
#######################################################################</p>
<p> #<br />
 # Make can also perform ordinary shell command jobs<br />
 # “make tidy” here performs a cleanup operation<br />
 #</p>
<p>clean:<br />
         rm -f ${OBJ}<br />
         rm -f y.tab.c lex.yy.c y.tab.h<br />
         rm -f y.tab lex.yy<br />
         rm -f *% *~ *.o<br />
         rm -f mconfig.tab.c mconfig.tab.h a.out<br />
         rm -f man.dvi man.aux man.log man.toc<br />
         rm -f cfengine.tar.gz cfengine.tar cfengine.tar.Z<br />
         make tidy<br />
         rm -f cfengine</p>
<p>install: ${INSTALLDIR}/database<br />
        cp database ${INSTALLDIR}/database</p>
<p>The Makefile above can be invoked in several ways.</p>
<p>make<br />
make database<br />
make clean<br />
make install</p>
<p>If we simple type `make’ i.e. the first of these choices, `make’ takes the first of the rules it finds as the object to build. In this case the rule is `database’, so the first two forms above are equivalent.<br />
On the other hand, if we type<br />
make clean<br />
then execution starts at the rule for `clean’, which is normally used to remove all files except the original source code. Make `install’ causes the compiled program to be installed at its intended destination.<br />
`make’ uses some special variables (which resemble the special variables used in Perl — but don’t confuse them). The most useful one is `$@’ which represents the current target — or the object which `make’ would like to compile. i.e. as `make’ checks each file it would like to compile, `$@’ is set to the current filename.<br />
$@<br />
This evaluates to the current target i.e. the name of the object you are currently trying to build. It is normal to use this as the final name of the program when compiling<br />
$?<br />
This is used only outside of suffix rules and means the name of all the files which must be compiled in order to build the current target.<br />
target: file1.o file2.o<br />
TAB cc -o $@ $?<br />
$&lt;<br />
This is only used in suffix rules. It has the same meaning as `$?’ but only in suffix rules. It stands for the pre-requisite, or the file which must be compiled in order to make a given object.<br />
Note that, because `make’ has some default rules defined in its configuration file, a single-file C program can be compiled very easily by typing<br />
make filename.c<br />
This is equivalent to<br />
cc -c filename.c<br />
cc -o filename filename.o<br />
New suffix rules for C++<br />
Standard rules for C++ are not often built into UNIX systems at the time of writing, but we can create them in our own Makefiles very easily. Here we shall use the GNU compiler g++’s conventions for C++ files. Here is a sample Makefile for using C++. Note that the `.SUFFIXES’ command must be used to declare new endings or file extensions.<br />
##################################################################<br />
#<br />
# This is the Makefile for g++<br />
#<br />
##################################################################</p>
<p>OBJ = cpp-prog.o X.o Y.o Z.o</p>
<p>CCPLUS = g++</p>
<p>.SUFFIXES: .C .o .h</p>
<p>#<br />
# Program Rules<br />
#</p>
<p>filesys: ${OBJ}<br />
         $(CCPLUS) -o filesys $(OBJ)</p>
<p>#<br />
#  Extra dependencies on the header file<br />
# (if the header file changes, we need to rebuild *.o)<br />
#</p>
<p>cpp-prog.o: filesys.h<br />
X.o: filesys.h<br />
Y.o: filesys.h<br />
Z.o: filesys.h</p>
<p>#<br />
# Suffix rules<br />
#</p>
<p>.C.o:<br />
         $(CCPLUS) -c $&lt;</p>
<p>The general rule here tells make that a `.o’ file can be created from a `.C’ file by executing the command `$(CCPLUS) -c’. (This is identical to the C case, except for the name of the compiler). The extra dependencies tell make that, if we change the header file `filesys.h’, then we must recompile all the files which read in `filesys.h’, since this could affect all of these. Finally, the highest level rule says that to make `filesys’ from the `.o’ files, we have to run `$(CCPLUS) -o filesys *.o’.<br />
The argv, argc and envp parameters<br />
When we write C programs which reads command line arguments, they are fed to us as an array of strings called the argument vector. The mechanisms for the C-shell and Perl are derived from the C argument vector. To read in the command line, we write</p>
<p>main (argc,argv,envp)</p>
<p>int argc;<br />
char *argv[], *envp[];</p>
<p>{<br />
printf (”The first argument was %s\n”,argv[1]);<br />
}<br />
Argument zero is the name of the program itself and `argv[argc-1]‘ is the last argument. The above definitions are in Kernighan and Ritchie C style. In ANSI C, the arguments can be declared using prototype:<br />
main (int argc, char **argv)</p>
<p>{</p>
<p>}<br />
The array of strings `envp[]‘ is a list of values of the environment variables of the system, formatted by<br />
NAME=value<br />
This gives C programmers access to the shell’s global environment.<br />
Environment variables in C<br />
In addition to the `envp’ vector, it is possible to access the environment variables through the call `getenv()’. This is used as follows; suppose we want to access the shell environment variable `$HOME’.<br />
char *string;</p>
<p>string = getenv(”HOME”);<br />
`string’ is now a pointer to static but public data. You should not use `string’ as if it were you’re own property because it will be used again by the system. Copy it’s contents to another string before using the data.<br />
char buffer[500];</p>
<p>strcpy (buffer,string);<br />
Files and directories<br />
All of the regular C functions from the standard library are available to UNIX programmers. The standard functions only address the issue of reading and writing to files however, they do not deal with operating system specific attributes such as file permissions and file types. Nor is there a mechanisms for obtaining lists of files within a directory. The reason for these omissions is that they are operating system dependent. To find out about these other attributes POSIX describes some standard UNIX system calls.<br />
opendir, readdir<br />
Files and directories are handled by functions defined in the header file `dirent.h’. In earlier UNIX systems the file `dir.h’ was used — and the definitions were slightly different, but not much. To get a list of files in a directory we must open the directory and read from it — just like a file. (A directory is just a file which contains data on its entries). The commands are<br />
opendir<br />
closedir<br />
readdir<br />
See the manual pages for dirent. These functions return pointers to a dirent structure which is defined in the file `/usr/include/dirent.h’. Here is an example ls command which lists the contents of the directory `/etc’. This header defines a structure</p>
<p>struct dirent<br />
   {<br />
   off_t                d_off;  /* offset of next disk dir entry */<br />
   unsigned long        d_fileno; /* file number of entry */<br />
   unsigned short       d_reclen; /* length of this record */<br />
   unsigned short       d_namlen; /* length of string in d_name */<br />
   char                 d_name[255+1]; /* name (up to MAXNAMLEN + 1) */<br />
   };</p>
<p>which can be used to obtain information from the directory nodes.</p>
<p>#include<br />
#include </p>
<p>main ()</p>
<p>{ DIR *dirh;<br />
  struct dirent *dirp;<br />
  static char mydir[20] = “/etc”;</p>
<p>if ((dirh = opendir(mydir)) == NULL)<br />
   {<br />
   perror(”opendir”);<br />
   return;<br />
   }</p>
<p>for (dirp = readdir(dirh); dirp != NULL; dirp = readdir(dirh))<br />
   {<br />
   printf(”Got dir entry: %s\n”,dirp-&gt;d_name);<br />
   }</p>
<p>closedir(dirh);<br />
}</p>
<p>Notice that reading from a directory is like reading from a file with fgets(), but the entries are filenames rather than lines of text.<br />
stat ()<br />
To determine the file properties or statistics we use the function call `stat()’ or its corollary `lstat()’. Both these functions find out information about files (permissions, owner, filetype etc). The only difference between them is the way in which they treat symbolic links. If `stat’ is used on a symbolic link, it stats the file the link points to rather than the link itself. If `lstat’ is used, the data refer to the link. Thus, to detect a link, we must use `lstat’, See section lstat and readlink.<br />
The data in the `stat’ structure are defined in the file `/usr/include/sys/stat.h’. Here are the most important structures.</p>
<p>struct stat<br />
   {<br />
   dev_t        st_dev;             /* device number*/<br />
   ino_t        st_ino;             /* file inode */<br />
   mode_t       st_mode;            /* permission */<br />
   short        st_nlink;           /* Number of hardlinks to file */<br />
   uid_t        st_uid;             /* user id */<br />
   gid_t        st_gid;             /* group id */<br />
   dev_t        st_rdev;<br />
   off_t        st_size;            /* size in bytes */<br />
   time_t       st_atime;           /* time file last accessed */<br />
   time_t       st_mtime;           /* time file contents last modified */<br />
   time_t       st_ctime;           /* time last attribute change */<br />
   long         st_blksize;<br />
   long         st_blocks;<br />
   };</p>
<p>lstat and readlink<br />
The function `stat()’ treats symbolic links as though they were the files they point to. In other words, if we use `stat()’ to read a symbolic link, we end up reading the file the link points to and not the link itself— we never see symbolic links. To avoid this problem, there is a different version of the stat function called `lstat()’ which is identical to `stat()’ except that it treats links as links and not as the files they point to. This means that we can test whether a file is a symbolic link, only if we use `lstat()’. (See the next paragraph.)<br />
Once we have identified a file to be a symbolic link, we use the `readlink()’ function to obtain the name of the file the link points to.</p>
<p>#define bufsize 512<br />
char buffer[bufsize];</p>
<p>readlink(”/path/to/file”,buffer,bufsize);</p>
<p>The result is returned in the string buffer.<br />
stat() test macros<br />
As we have already mentioned, the UNIX mode bits contain not only information about what permissions a file has, but also bits describing the type of file — whether it is a directory or a link etc. There are macros defined in UNIX to extract this information from the `st_mode’ member of the `stat’ structure. They are defined in the `stat.h’ headerfile. Here are some examples.<br />
#define S_ISBLK(m)    /* is block device */<br />
#define S_ISCHR(m)    /* is character device */<br />
#define S_ISDIR(m)    /* is directory */<br />
#define S_ISFIFO(m)   /* is fifo pipe/socket */<br />
#define S_ISREG(m)    /* is regular (normal) file */</p>
<p>#define S_ISLNK(m)    /* is symbolic link */  /* Not POSIX */<br />
#define S_ISSOCK(m)   /* is a lock */</p>
<p>#define S_IRWXU  /* rwx, owner */<br />
#define  S_IRUSR /* read permission, owner */<br />
#define  S_IWUSR /* write permission, owner */<br />
#define  S_IXUSR /* execute/search permission, owner */<br />
#define S_IRWXG  /* rwx, group */<br />
#define  S_IRGRP /* read permission, group */<br />
#define  S_IWGRP /* write permission, grougroup */<br />
#define  S_IXGRP /* execute/search permission, group */<br />
#define S_IRWXO  /* rwx, other */<br />
#define  S_IROTH /* read permission, other */<br />
#define  S_IWOTH /* write permission, other */<br />
#define  S_IXOTH /* execute/search permission, other */</p>
<p>These return true or false when acting on the mode member. Here is an example See section Example filing program.</p>
<p>struct stat statvar;</p>
<p>stat(”file”,&amp;statvar);</p>
<p>/* test return values */</p>
<p>if (S_ISDIR(statvar.st_mode))<br />
   {<br />
   printf(”Is a directory!”);<br />
   }</p>
<p>Example filing program<br />
The following example program demonstrates the use of the directory functions in dirent and the stat function call.<br />
/********************************************************************/<br />
/*                                                                  */<br />
/* Reading directories and `statting’ files                         */<br />
/*                                                                  */<br />
/********************************************************************/</p>
<p>#include<br />
#include<br />
#include<br />
#include </p>
<p>#define DIRNAME “/.”<br />
#define bufsize 255</p>
<p>/********************************************************************/</p>
<p>main ()</p>
<p>{ DIR *dirh;<br />
  struct dirent *dirp;<br />
  struct stat statbuf;<br />
  char *pathname[bufsize];<br />
  char *linkname[bufsize];</p>
<p>if ((dirh = opendir(DIRNAME)) == NULL)<br />
   {<br />
   perror(”opendir”);<br />
   exit(1);<br />
   }</p>
<p>for (dirp = readdir(dirh); dirp != NULL; dirp = readdir(dirh))<br />
   {<br />
   if (strcmp(”.”,dirp-&gt;d_name) == 0 || strcmp(”..”,dirp-&gt;d_name) == 0)<br />
      {<br />
      continue;<br />
      }</p>
<p>   if (strcmp(”lost+found”,dirp-&gt;d_name) == 0)<br />
      {<br />
      continue;<br />
      }</p>
<p>   sprintf(pathname,”%s/%s”,DIRNAME,dirp-&gt;d_name);</p>
<p>   if (lstat(pathname,&amp;statbuf) == -1)                /* see man stat */<br />
     {<br />
     perror(”stat”);<br />
     continue;<br />
     }</p>
<p>   if (S_ISREG(statbuf.st_mode))<br />
      {<br />
      printf(”%s is a regular file\n”,pathname);<br />
      };</p>
<p>   if (S_ISDIR(statbuf.st_mode))<br />
      {<br />
      printf(”%s is a directory\n”,pathname);<br />
      }</p>
<p>   if (S_ISLNK(statbuf.st_mode))<br />
      {<br />
      bzero(linkname,bufsize);                         /* clear string */<br />
      readlink(pathname,linkname,bufsize);<br />
      printf(”%s is a link to %s\n”,pathname,linkname);<br />
      }</p>
<p>   printf(”The mode of %s is %o\n\n”,pathname,statbuf.st_mode &amp; 07777);<br />
   }</p>
<p>closedir(dirh);<br />
}</p>
<p>Process control, fork(), exec(), popen() and system<br />
There is a number of ways in which processes can interact with one another and in which we can control their behaviour. We shall not go into great detail in this course, only provide examples for reference.<br />
The UNIX `fork()’ function is used to create child processes. This is the basis of all `heavyweight’ multitasking under UNIX. Here is a simple example of fork in which we start a child process from within a program and wait for it to finish. Note that the code for the parent and the child is is the same file. The only thing that distinguishes parent from child is the value returned by the fork function.<br />
When `fork()’ is called, it duplicates the entire current process so that two parallel processes are then running. The only difference between these is that the child process (the copy) gets a return value of zero from `fork()’, whereas the parent gets a return value equal to the process identifier (pid) of the child. This value can be used by the parent to send messages or to wait for the child. Here we show a simple example in which the `wait(NULL)’ command is used to wait for the last child spawned by the parent.<br />
/**************************************************************/<br />
/*                                                            */<br />
/*  A brief demo of the UNIX process duplicator fork().       */<br />
/*                                                            */<br />
/**************************************************************/</p>
<p>#include </p>
<p>/***************************************************************/</p>
<p>main ()</p>
<p>{ int pid, cid;</p>
<p>pid = getpid();</p>
<p>printf (”Fork demo! I am the parent (pid = %d)\n”,pid);</p>
<p>if (! fork())<br />
   {<br />
   cid = getpid();<br />
   printf (”I am the child (cid = %d) of (pid=%d)\n”,cid,pid);<br />
   ChildProcess();<br />
   exit(0);<br />
   }</p>
<p>printf(”Parent waiting here for the child…\n”);</p>
<p>wait(NULL);</p>
<p>printf(”Child finished, parent quitting too!\n”);<br />
}</p>
<p>/**************************************************************/</p>
<p>ChildProcess()</p>
<p>{ int i;</p>
<p>for (i = 0; i &lt; 10; i++)<br />
   {<br />
   printf (”%d…\n”,i);<br />
   sleep(1);<br />
   }<br />
}</p>
<p>Another possibility is that we might want to execute a program and wait to find out what the result of the program is before continuing. There are two ways to do this. The first is a variation on the theme above and uses fork().<br />
Let’s create a function which runs a shell command from within a C program, and determines its return value. We make the result a boolean (integer) value, so that the function returns `true’ if the shell command exits normally, See section Return codes.<br />
if (ShellCommandReturnsZero(shell-command))<br />
   {<br />
   printf (”Command %s went ok\n”,shell-command);<br />
   }</p>
<p>To do this we first have to fork a new process and then use one of the exec commands to load a new code image on top of the new process. shell commands from C This sounds complicated, but it is necessary because of the way UNIX handles processes. If we had no use for the return value, we could simply execute a shell command using the system(”shell command”) function, (which does all this for us) but when system() exits, we can only tell if the command was executed successfully or unsuccessfully–we learn nothing about what actually failed (the shell or command which was executed under the shell?) If we require detailed information about what happened to the child process then we need to do the following.<br />
#include<br />
#include </p>
<p>  /* Send complete command as a string */<br />
  /* including all arguments           */</p>
<p>ShellCommandReturnsZero(comm)</p>
<p>char *comm;</p>
<p>{ int status, i, argc;<br />
  pid_t pid;<br />
  char arg[maxshellargs][bufsize];<br />
  char **argv;</p>
<p>/* Build argument array for execv call*/</p>
<p>for (i = 0; i &lt; maxshellargs; i++)<br />
   {<br />
   bzero (arg[i],bufsize);<br />
   }</p>
<p>argc = SplitCommand(comm,arg);</p>
<p>if ((pid = fork()) &lt; 0)<br />
   {<br />
   FatalError(”Failed to fork new process”);<br />
   }<br />
else if (pid == 0)                     /* child */<br />
   {<br />
   argv = malloc((argc+1)*sizeof(char *));</p>
<p>   for (i = 0; i = maxshellargs-1)<br />
      {<br />
      yyerror(”Too many arguments in embedded script”);<br />
      FatalError(”Use a wrapper”);<br />
      }</p>
<p>   while (*sp == ‘ ‘ || *sp == ‘\t’)<br />
      {<br />
      sp++;<br />
      }</p>
<p>   switch (*sp)<br />
      {<br />
      case ‘\”‘: sscanf (++sp,”%[^\"]“,buff);<br />
                 break;<br />
      case ‘\”: sscanf (++sp,”%[^\']“,buff);<br />
                 break;<br />
      default:   sscanf (sp,”%s”,buff);<br />
                 break;<br />
      }</p>
<p>   for (j = 0; j &lt; bufsize; j++)<br />
      {<br />
      arg[i][j] = buff[j];<br />
      }</p>
<p>   sp += strlen(arg[i]);<br />
   i++;<br />
   }<br />
return (i);<br />
}</p>
<p>In this example, the script waits for the exit signal from the child process before continuing. The return value from the child is available from the wait function with the help of a set of macros defined in `/usr/include/sys/wait.h’. The value is given by WTERMSIG(status).<br />
In the final example, we can open a pipe to a process directly in a C program as though it were a file, by using the function popen(). Pipes may be opened for reading or for writing, in exactly the same way as a file is opened. The child process is automatically synchronized with the parent using this method. Here is a program which opens a UNIX command for reading (both stdout and stderr) from the child process are piped into the program. Notice that the syntax used in this call is that used by the Bourne shell, since this is build deeply into the UNIX execution design.</p>
<p>#define bufsize 1024</p>
<p>FILE *pp;<br />
char VBUFF[bufsize];</p>
<p>…</p>
<p>if ((pp = popen( “/sbin/mount -va 2&lt;&amp;1″,”r”)) == NULL)<br />
   {<br />
   printf(”Failed to open pipe\n”);<br />
   return errorcode;<br />
   }</p>
<p>while (!feof(pp))<br />
   {<br />
   fgets(VBUFF,bufsize,pp);</p>
<p>   /* Just write the output to stdout */</p>
<p>   printf (”Pipe read: %s\n”,VBUFF);<br />
   }</p>
<p>pclose(pp);</p>
<p>A more secure popen()<br />
One problem with the popen() system call is that it uses a shell to execute the command it obtains a pipe to. In the past this has been used to allow UNIX security breaches, using a so-called IFS attack which can trick the shell into executing a program with the name of the first node in the directory of the executable. For instance,if the pipe was to open the program `/bin/ps’, this coudl be tricked into executing a program in the current working directory of the process called `bin’ with argument `ps’.<br />
The solution is not to use a shell at all, but to replace popen() with a version which calls exec() directly. Here is a safe version from the source code of cfengine:</p>
<p>#define bufsize      4096<br />
#define maxshellargs 20</p>
<p>pid_t *CHILD;<br />
int    MAXFD = 20; /* Max number of simultaneous pipes */</p>
<p>/***************************************************************/</p>
<p>FILE *cfpopen(command, type)</p>
<p>char *command, *type;</p>
<p> { char arg[maxshellargs][bufsize];<br />
   int i, argc, pd[2];<br />
   char **argv;<br />
   pid_t pid;<br />
   FILE *pp = NULL;</p>
<p> if ((*type != ‘r’ &amp;&amp; *type != ‘w’) || (type[1] != ”))<br />
    {<br />
    errno = EINVAL;<br />
    return NULL;<br />
    }</p>
<p> if (CHILD == NULL)   /* first time */<br />
    {<br />
    if ((CHILD = calloc(MAXFD,sizeof(pid_t))) == NULL)<br />
       {<br />
       return NULL;<br />
       }<br />
    }</p>
<p> if (pipe(pd) &lt; 0)  /* Create a pair of descriptors to this process */<br />
    {<br />
    return NULL;<br />
    }</p>
<p> if ((pid = fork()) == -1)<br />
    {<br />
    return NULL;<br />
    }</p>
<p> if (pid == 0)<br />
    {<br />
    switch (*type)<br />
       {<br />
       case ‘r’:</p>
<p>                 close(pd[0]);        /* Don’t need output from parent */</p>
<p>                 if (pd[1] != 1)<br />
                    {<br />
                    dup2(pd[1],1);    /* Attach pp=pd[1] to our stdout */<br />
                    dup2(pd[1],2);    /* Merge stdout/stderr */<br />
                    close(pd[1]);<br />
                    }</p>
<p>                 break;</p>
<p>       case ‘w’:</p>
<p>                 close(pd[1]);</p>
<p>                 if (pd[0] != 0)<br />
                    {<br />
                    dup2(pd[0],0);<br />
                    close(pd[0]);<br />
                   }<br />
       }</p>
<p>    for (i = 0; i  0)<br />
          {<br />
          close(CHILD[i]);<br />
          }</p>
<p>       argc = SplitCommand(command,arg);<br />
       argv = (char **) malloc((argc+1)*sizeof(char *));</p>
<p>       if (argv == NULL)<br />
          {<br />
          FatalError(”Out of memory”);<br />
          }</p>
<p>       for (i = 0; i &lt; argc; i++)<br />
          {<br />
          argv[i] = arg[i];<br />
          }</p>
<p>       argv[i] = (char *) NULL;</p>
<p>       if (execv(arg[0],argv) == -1)<br />
          {<br />
          sprintf(OUTPUT,”Couldn’t run %s”,arg[0]);<br />
          CfLog(cferror,OUTPUT,”execv”);<br />
          }</p>
<p>       _exit(1);<br />
       }<br />
    }<br />
 else<br />
    {<br />
    switch (*type)<br />
       {<br />
       case ‘r’:</p>
<p>                 close(pd[1]);</p>
<p>                 if ((pp = fdopen(pd[0],type)) == NULL)<br />
                    {<br />
                    return NULL;<br />
                    }<br />
                 break;</p>
<p>       case ‘w’:</p>
<p>                 close(pd[0]);</p>
<p>                 if ((pp = fdopen(pd[1],type)) == NULL)<br />
                    {<br />
                    return NULL;<br />
                    }<br />
       }</p>
<p>    CHILD[fileno(pp)] = pid;<br />
    return pp;<br />
    }<br />
 }</p>
<p>/***************************************************************/</p>
<p>cfpclose(pp)</p>
<p>FILE *pp;</p>
<p>{ int fd, status;<br />
  pid_t pid;</p>
<p>Debug(”cfpclose(pp)\n”);</p>
<p>if (CHILD == NULL)  /* popen hasn’t been called */<br />
   {<br />
   return -1;<br />
   }</p>
<p>fd = fileno(pp);</p>
<p>if ((pid = CHILD[fd]) == 0)<br />
   {<br />
   return -1;<br />
   }</p>
<p>CHILD[fd] = 0;</p>
<p>if (fclose(pp) == EOF)<br />
   {<br />
   return -1;<br />
   }</p>
<p>Debug(”cfpopen – Waiting for process %d\n”,pid);</p>
<p>#ifdef HAVE_WAITPID</p>
<p>while(waitpid(pid,&amp;status,0) &lt; 0)<br />
   {<br />
   if (errno != EINTR)<br />
      {<br />
      return -1;<br />
      }<br />
   }</p>
<p> return status;</p>
<p>#else</p>
<p> if (wait(&amp;status) != pid)<br />
    {<br />
    return -1;<br />
    }<br />
 else<br />
    {<br />
    if (WIFSIGNALED(status))<br />
       {<br />
       return -1;<br />
       }</p>
<p>    if (! WIFEXITED(status))<br />
       {<br />
       return -1;<br />
       }</p>
<p>    return (WEXITSTATUS(status));<br />
    }<br />
#endif<br />
}</p>
<p>/*******************************************************************/<br />
/* Command exec aids                                               */<br />
/*******************************************************************/</p>
<p>SplitCommand(comm,arg)</p>
<p>char *comm, arg[maxshellargs][bufsize];</p>
<p>{ char *sp;<br />
  int i = 0, j;<br />
  char buff[bufsize];</p>
<p>for (sp = comm; sp = maxshellargs-1)<br />
      {<br />
      CfLog(cferror,”Too many arguments in embedded script”,”&#8221;);<br />
      FatalError(”Use a wrapper”);<br />
      }</p>
<p>   while (*sp == ‘ ‘ || *sp == ‘\t’)<br />
      {<br />
      sp++;<br />
      }</p>
<p>   switch (*sp)<br />
      {<br />
      case ”: return(i-1);</p>
<p>      case ‘\”‘: sscanf (++sp,”%[^\"]“,arg[i]);<br />
                 break;<br />
      case ‘\”: sscanf (++sp,”%[^\']“,arg[i]);<br />
                 break;<br />
      case ‘`’:  sscanf (++sp,”%[^`]“,arg[i]);<br />
                 break;<br />
      default:   sscanf (sp,”%s”,arg[i]);<br />
                 break;<br />
      }</p>
<p>   sp += strlen(arg[i]);<br />
   i++;<br />
   }</p>
<p>return (i);<br />
}</p>
<p>Traps and signals<br />
Processes can receive signals from the UNIX kernel at any time. Some of these signals terminate the execution of the program. This can cause problems if the program is in the middle of critical activity such as writing to a file. For that reason we can trap signals and provide our own routine for handling them in a special way.<br />
A signal handler is made by calling the function `signal()’ for each signal and by specifying a pointer to a function which will be called in the event of a signal. For example:</p>
<p>main ()</p>
<p>{ int HandleSignal();</p>
<p>signal(SIGTERM,HandleSignal);<br />
}</p>
<p>HandleSignal()</p>
<p>{<br />
/* Tidy up and exit cleanly */</p>
<p>exit(0);<br />
}</p>
<p>`SIGTERM’ is the usual signal sent by the command `kill’. There are many other signals which can be sent to programs. Here is list. You have to decide for yourself whether or not you want to provide your own signal handling function. To ignore a signal, you write</p>
<p>signal(SIGtype,SIG_IGN);</p>
<p>To remove a signal handler and re-activate a signal, you write</p>
<p>signal(SIGtype,SIG_DFL);</p>
<p>Regular expressions<br />
A regular expression is a pattern for matching strings of text. We have met regular expressions earlier in connection with the shell and Perl. Naturally these earlier encounters have their roots in C functions for handling expressions. A regular expression is used by first `compiling’ it into a convenient data structure. Then a matching function is used to compare the expression with a test string. In this example program we show how a regular expression typed in as an argument to the program is found within strings of input entered on the keyboard.</p>
<p>#include<br />
#include </p>
<p>main (argc,argv)</p>
<p>int argc;<br />
char **argv;</p>
<p>{<br />
 char buffer[1024];<br />
 regex_t rx;<br />
 regmatch_t match;<br />
 size_t nmatch = 1;</p>
<p> if (regcomp(&amp;rx, argv[1], REG_EXTENDED) != 0)<br />
    {<br />
    perror(”regcomp”);<br />
    return;<br />
    }</p>
<p> while (!feof(stdin))<br />
    {<br />
    fgets(buffer,1024,stdin);</p>
<p>    if (regexec(&amp;rx,buffer,1,&amp;match,0) == 0)<br />
       {<br />
       printf(”Matched:(%s) at %d to %d”,buffer,match.rm_so,match.rm_eo);<br />
       }</p>
<p>    }</p>
<p>regfree(&amp;rx);<br />
}</p>
<p>Here is an example of its use. The output of the program is in italics</p>
<p>% a.out xyz<br />
this is a string<br />
another string<br />
an xyz string<br />
Matched: (an xyz string<br />
) at 3 to 6<br />
another xyz zyxxyz string<br />
Matched: (another xyz xyz string<br />
) at 8 to 11</p>
<p>% a.out ‘xyz|abc’<br />
This is a string<br />
An abc string<br />
Matched: (An abc string<br />
) at 3 to 6<br />
Or an xyz string<br />
Matched: (Or an xyz string<br />
) at 6 to 9</p>
<p>If you don’t want the match data set &amp;pm to NULL. To get an exact match rather than a substring check that the bounds are 0 and strlen(argv[1])-1.<br />
DES encryption<br />
Encryption with the SSLeay library, compile with command</p>
<p>  gcc crypto.c -I/usr/local/ssl/include -L/usr/local/ssl/lib -lcrypto</p>
<p>Example of normal triple DES encryption which works only on an 8-byte buffer:</p>
<p>/*****************************************************************************/<br />
/*                                                                           */<br />
/* File: crypto.c                                                            */<br />
/*                                                                           */<br />
/* Compile with:  gcc program.c  -lcrypto   (SSLeay)                         */<br />
/*                                                                           */<br />
/*****************************************************************************/</p>
<p>#include<br />
#include </p>
<p>#define bufsize 1024</p>
<p>    /* Note how this truncates to 8 characters */</p>
<p>main ()</p>
<p>{ char in[bufsize],out[bufsize],back[bufsize];<br />
  des_cblock key1,key2,key3,seed = {0xFE,0xDC,0xBA,0×98,0×76,0×54,0×32,0×10};<br />
  des_key_schedule ks1,ks2,ks3;</p>
<p>strcpy(in,”1 2 3 4 5 6 7 8 9 a b c d e f g h i j k”);</p>
<p>des_random_seed(seed);</p>
<p>des_random_key(key1);<br />
des_random_key(key2);<br />
des_random_key(key3);</p>
<p>des_set_key((C_Block *)key1,ks1);<br />
des_set_key((C_Block *)key2,ks2);<br />
des_set_key((C_Block *)key3,ks3);<br />
des_ecb3_encrypt((C_Block *)in,(C_Block *)out,ks1,ks2,ks3,DES_ENCRYPT);</p>
<p>printf(”Encrypted [%s] into [%s]\n”,in,out);</p>
<p>des_ecb3_encrypt((C_Block *)out,(C_Block *)back,ks1,ks2,ks3,DES_DECRYPT);</p>
<p>printf(”and back to.. [%s]\n”,back);<br />
}</p>
<p>Triple DES, chaining mode, for longer strings (which must be a multiple of 8 bytes):</p>
<p>/*****************************************************************************/<br />
/*                                                                           */<br />
/* File: crypto.c                                                            */<br />
/*                                                                           */<br />
/* Compile with:  gcc program.c  -lcrypto   (SSLeay)                         */<br />
/*                                                                           */<br />
/*****************************************************************************/</p>
<p>#include<br />
#include </p>
<p>#define bufsize 1024</p>
<p>    /* This can be used on arbitrary length buffers */</p>
<p>main ()</p>
<p>{ char in[bufsize],out[bufsize],back[bufsize],workvec[bufsize];<br />
  des_cblock key1,key2,key3,seed = {0xFE,0xDC,0xBA,0×98,0×76,0×54,0×32,0×10};<br />
  des_key_schedule ks1,ks2,ks3;</p>
<p>strcpy(in,”1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z”);</p>
<p>des_random_seed(seed);</p>
<p>des_random_key(key1);<br />
des_random_key(key2);<br />
des_random_key(key3);</p>
<p>des_set_key((C_Block *)key1,ks1);<br />
des_set_key((C_Block *)key2,ks2);<br />
des_set_key((C_Block *)key3,ks3);</p>
<p>/* This work vector can be intialized t anything …*/</p>
<p>memset(workvec,0,bufsize);</p>
<p>des_ede3_cbc_encrypt((C_Block *)in,(C_Block *)out,(long)strlen(in),<br />
           ks1,ks2,ks3,(C_Block *)workvec,DES_ENCRYPT);</p>
<p>printf(”Encypted [%s] into [something]\n”,in);</p>
<p>/* .. but this must be initialized the same as above */</p>
<p>memset(workvec,0,bufsize);</p>
<p>/* Note that the length is the original length, not strlen(out) */</p>
<p>des_ede3_cbc_encrypt((C_Block *)out,(C_Block *)back,(long)strlen(in),<br />
           ks1,ks2,ks3,(C_Block *)workvec,DES_DECRYPT);</p>
<p>printf(”and back to.. [%s]\n”,back);<br />
}</p>
<p>Device control: ioctl<br />
The C function `ioctl’ (I/O control) is used to send special control commands to devices like the disk and the network interface. The syntax of the function is<br />
int ioctl(fd, request, arg)<br />
int fd, request;<br />
long arg;<br />
The first parameter is normally as device handle or socket descriptor. The second is a control parameter. Lists of valid control parameters are normally defined in the system `include’ files for a particular device. They are device and system dependent so you need a local manual and som detective work to find out what they are. The final parameter is a pointer to a variable which receives return data from the device.<br />
`ioctl’ commands are device specific, by their nature. The commands for the ethernet interface device are only partially standardized, for example. We could read the ethernet device (which is called `le0′ on a Sun workstation), using the following command:</p>
<p># include       /* Typical includes for internet */<br />
# include<br />
# include<br />
# include<br />
# include<br />
# include<br />
# include<br />
# include </p>
<p>struct ifreq IFR;<br />
int sk;<br />
struct sockaddr_in sin;</p>
<p>strcpy(IFR.ifr_name,”le0″);<br />
IFR.ifr_addr.sa_family = AF_INET;</p>
<p>if ((sk = socket(AF_INET,SOCK_DGRAM,IPPROTO_IP)) == -1)<br />
   {<br />
   perror(”socket”);<br />
   exit(1);<br />
   }</p>
<p>if (ioctl(sk,SIOCGIFFLAGS, (caddr_t) &amp;IFR) == -1)<br />
   {<br />
   perror (”ioctl”);<br />
   exit(1);<br />
   }<br />
We shall not go into the further details of `ioctl’, but simply note its role in system programming.<br />
Database example (Berkeley db)</p>
<p>DBT key,value;<br />
DB *dbp;<br />
DBC *dbcp;<br />
db_recno_t recno;</p>
<p>if ((errno = db_open(CHECKSUMDB,DB_BTREE, DB_CREATE, 0664, NULL, NULL, &amp;dbp)) != 0)<br />
   {<br />
   sprintf(OUTPUT,”cfd: couldn’t open checksum database %s\n”,CHECKSUMDB);<br />
   CfLog(cferror,OUTPUT,”db_open”);<br />
   return false;<br />
   }</p>
<p>bzero(&amp;value,sizeof(value));<br />
bzero(&amp;key,sizeof(key));      </p>
<p>key.data = filename;<br />
key.size = strlen(filename)+1;<br />
value.data = dbvalue;<br />
value.size = sizeof(dbvalue);</p>
<p>if ((errno = dbp-&gt;del(dbp,NULL,&amp;key,0)) != 0)<br />
   {<br />
   CfLog(cferror,”&#8221;,”db_store”);<br />
   }</p>
<p>key.data = filename;<br />
key.size = strlen(filename)+1;</p>
<p>if ((errno = dbp-&gt;put(dbp,NULL,&amp;key,&amp;value,0)) != 0)<br />
   {<br />
   CfLog(cferror,”put failed”,”db-&gt;put”);<br />
   }     </p>
<p>if ((errno = dbp-&gt;get(dbp,NULL,&amp;key,&amp;value,0)) == 0)<br />
   {<br />
   /* Not found … */<br />
   return;<br />
   }</p>
<p>dbp-&gt;close(dbp,0);</p>
<p>Text parsing tools: `lex’ and `yacc’<br />
This section is a taster only. You only need to know what lex and yacc are, not how they work.<br />
`lex’ and `yacc’ are two tools for the C programmer who wishes to make a text parser. A text parser is a program which reads a text file and interprets the symbols in it. Every programming language must include a text parser, for instance.<br />
The `yacc’ (yet another compiler compiler) program generates C code which parses a textfile, given a description of the syntax rules for the file. In other words, we define the logical structure of the text file, according to the way we wish to interpret it and give the rules to `yacc’. `yacc’ produces C code from this which does the job.<br />
`lex’ is a `lexer’. It is normally used together with `yacc’. `lex’ tokenizes or identifies symbols in a file. What that means is that it reads in a file and matches types of string in the file which are defined in terms of regular expressions by the programmer, and returns symbolic values for those strings.<br />
Although `lex’ can be used by independently of `yacc’, it is normally used to identify the different types of string which define the syntax of a file. For example, suppose `yacc’ was parsing a C program. On the beginning of a line, it might expect to find either a variable name or a preprocessor symbol. A variable name is just a string consisting of characters from the set `0-9a-Z_’, whereas a preprocessor command always starts with the character `#’. `yacc’ passes control to `lex’ which reads the file and matches the first object on the line. If it finds a variable, it returns to `yacc’ a token which is a number or value corresponding to `variable’. Similarly, if it finds a preprocessor command, it returns a token for that. If it doesn’t match either type it returns something else and `yacc’ signals a syntax error.<br />
Here is a `yacc’ file which parses a file consisting of lines of the form a+b, where $a$ and $b$ are numbers — any other syntax is incorrect. We could have used this later in the example program for the client-server example, See section Socket streams.<br />
You can learn more about lex and yacc in “Lex and Yacc”, J. Levine, T. Mason and D. Brown, O’Reilly and Assoc.<br />
%{<br />
/*******************************************************************/<br />
/*                                                                 */<br />
/*  PARSER for a + b protocol                                      */<br />
/*                                                                 */<br />
/* The section between the single %’s gets copied verbatim into    */<br />
/* the resulting C code yacc generates — including this comment!  */<br />
/*                                                                 */<br />
/*******************************************************************/</p>
<p>#include </p>
<p>extern char *yytext;</p>
<p>%}</p>
<p>%token NUMBER PLUS</p>
<p>%%</p>
<p>specification:       { yyerror(”Warning: invalid statement”);}<br />
                     | statement;</p>
<p>statement:            NUMBER PLUS NUMBER;</p>
<p>The lexer to go with this parser generates the tokens NUMBER and PLUS used by `yacc’:<br />
%{<br />
/*******************************************************************/<br />
/*                                                                 */<br />
/*  LEXER for a + b protocol                                       */<br />
/*                                                                 */<br />
/* Returns token types NUMBER and PLUS to yacc, one at a time      */<br />
/*                                                                 */<br />
/*******************************************************************/</p>
<p>#include “y.tab.h”       /* yacc produces this — need this line! */</p>
<p>%}</p>
<p>number    [0-9]+<br />
plus      [+]</p>
<p>%%</p>
<p>number                 {<br />
                       return NUMBER;<br />
                       }</p>
<p>plus                   {<br />
                       return PLUS;<br />
                       }</p>
<p>.                      {<br />
                       return yytext[0];<br />
                       }</p>
<p>%%</p>
<p>/* EOF */</p>
<p>The main program which uses `yacc’ and `lex’ looks like this:</p>
<p>extern FILE *yyin;</p>
<p>main ()</p>
<p>{</p>
<p>if ((yyin = fopen(”My_Input_File”,”r”)) == NULL)      /* Open file */<br />
   {<br />
   printf(”Can’t open file\n”);<br />
   exit (1);<br />
   }</p>
<p>while (!feof(yyin))<br />
   {<br />
   yyparse();<br />
   }</p>
<p>fclose (yyin);<br />
}<br />
Exercises<br />
1. Write a daemon program with a signal handler which makes a log of the heaviest (maximum cpu) process running, every five minutes. The program should exit if the log file becomes greater than 5-kbytes.<br />
2. Rewrite in C the perl program which lists all the files in the current directory containing a certain string.<br />
3. Write a version of `more’ which prints control characters safely. See the `cat -e’ command.<br />
4. Write a Makefile to create a shared library from a number of object files.<br />
Network Programming<br />
Client-server communication is the basis of modern operating system technology. The UNIX socket mechanism makes stream-based communication virtually transparent.<br />
Socket streams<br />
Analogous to filestreams are sockets or TCP/IP network connections. A socket is a two-way (read/write) pseudo-file node. An open socket stream is like an open file-descriptor. Berkeley sockets are part of the standard C library.<br />
There are two main kinds of socket: TCP/IP sockets and UNIX domain sockets. UNIX sockets can be used to provide local interprocess communication using a filestream communication protocol. TCP/IP sockets open file descriptors across the network. A TCP/IP socket is a file stream associated with an IP address and a port number. We write to a socket descriptor just as with a file descriptor, either with write() or using send().<br />
When sending binary data over a network we have to be careful about machine level representations of data. Operating systems (actually the hardware they run on) fall into two categories known as big endian and little endian. The names refer to the byte-order of numerical representations. The names indicate how large integers (which require say 32 bits or more) are stored in memory. Little endian systems store the least significant byte first, while big endian systems store the most significant byte first. For example, the representation of the number 34,677,374 has either of these forms.</p>
<p>          ———————————–<br />
  Big    |   2    |   17   |  34    |   126  |<br />
          ———————————–</p>
<p>          ———————————–<br />
  Little |   126  |   34   |  17    |   2    |<br />
          ———————————–</p>
<p>Obviously if we are transferring data from one host to another, both hosts have to agree on the data representation otherwise there would be disastrous consequences. This means that there has to be a common standard of network byte ordering. For example, Solaris (SPARC hardware) uses network byte ordering (big endian), while GNU/Linux (Intel hardware) uses the opposite (little endian). This means that Intel systems have to convert the format every time something is transmitted over the network. UNIX systems provide generic functions for converting between host-byteorder and network-byteorder for small and long integer data:</p>
<p>   htonl,  htons,  ntohl, ntohs</p>
<p>Here we list two example programs which show how to make a client-server pair. The server enters a loop, and listens for connections from any clients (the generic address `INADDR_ANY’ is a wildcard for any address on the current local network segment). The client program sends requests to the server as a protocol in the form of a string of the type `a + b’. Normally `a’ and `b’ are numbers, in which case the server returns their sum to the client. If the message has the special form `halt + *’, where the star is arbitrary, then the server shuts down. Any other form of message results in an error, which the server signals to the client.<br />
The basic structure of the client-server components in terms of system calls is this:</p>
<p>Client:</p>
<p>  socket()             Create a socket<br />
  connect()            Contact a server socket (IP + port)</p>
<p>    while (?)<br />
       {<br />
       send()          Send to server<br />
       recv()          Receive from server<br />
       }</p>
<p>Server:</p>
<p>   socket()            Create a socket<br />
   bind()              Associates the socket with a fixed address<br />
   listen()            Create a listen queue</p>
<p>   while()<br />
      {<br />
      reply=accept()   Accept a connection request<br />
      recv()           Receive from client<br />
      send()           Send to client<br />
      }</p>
<p>/**********************************************************************/<br />
/*                                                                    */<br />
/* The client part of a client-server pair. This simply takes two     */<br />
/* numbers and adds them together, returning the result to the client */<br />
/*                                                                    */<br />
/* Compiled with:                                                     */<br />
/*                   cc server.c                                      */<br />
/*                                                                    */<br />
/* User types:                                                        */<br />
/*                   3 + 5                                            */<br />
/*                   a + b                                            */<br />
/*                   halt + server                                    */<br />
/**********************************************************************/</p>
<p>#include<br />
#include<br />
#include<br />
#include<br />
#include </p>
<p>#define PORT 9000                   /* Arbitrary non-reserved port */<br />
#define HOST “nexus.iu.hio.no”<br />
#define bufsize 20</p>
<p>/**********************************************************************/<br />
/* Main                                                               */<br />
/**********************************************************************/</p>
<p>main (argc,argv)</p>
<p>int argc;<br />
char *argv[];</p>
<p>{ struct sockaddr_in cin;<br />
  struct hostent *hp;<br />
  char buffer[bufsize];<br />
  int sd;</p>
<p>if (argc != 4)<br />
   {<br />
   printf(”syntax: client a + b\n”);<br />
   exit(1);<br />
   }</p>
<p>if ((hp = gethostbyname(HOST)) == NULL)<br />
   {<br />
   perror(”gethostbyname: “);<br />
   exit(1);<br />
   }</p>
<p>memset(&amp;cin,0,sizeof(cin));             /* Another way to zero memory */</p>
<p>cin.sin_family = AF_INET;<br />
cin.sin_addr.s_addr = ((struct in_addr *)(hp-&gt;h_addr))-&gt;s_addr;<br />
cin.sin_port = htons(PORT);</p>
<p>printf(”Trying to connect to %s = %s\n”,HOST,inet_ntoa(cin.sin_addr));</p>
<p>if ((sd = socket(AF_INET,SOCK_STREAM,0)) == -1)<br />
   {<br />
   perror(”socket”);<br />
   exit(1);<br />
   }</p>
<p>if (connect(sd,&amp;cin,sizeof(cin)) == -1)<br />
   {<br />
   perror(”connect”);<br />
   exit(1);<br />
   }</p>
<p>sprintf(buffer,”%s + %s”,argv[1],argv[3]);</p>
<p>if (send(sd,buffer,strlen(buffer),0) == -1)<br />
   {<br />
   perror (”send”);<br />
   exit(1);<br />
   }</p>
<p>if (recv(sd,buffer,bufsize,0) == -1)<br />
   {<br />
   perror(”recv”);<br />
   exit (1);<br />
   }</p>
<p>printf (”Server responded with %s\n”,buffer);</p>
<p>close (sd);<br />
unlink(”./socket”);<br />
}</p>
<p>/**********************************************************************/<br />
/*                                                                    */<br />
/* The server part of a client-server pair. This simply takes two     */<br />
/* numbers and adds them together, returning the result to the client */<br />
/*                                                                    */<br />
/* Compiled with:                                                     */<br />
/*                   cc server.c                                      */<br />
/*                                                                    */<br />
/**********************************************************************/</p>
<p>#include<br />
#include<br />
#include<br />
#include<br />
#include </p>
<p>#define PORT 9000<br />
#define bufsize 20<br />
#define queuesize 5<br />
#define true 1<br />
#define false 0</p>
<p>/**********************************************************************/<br />
/* Main                                                               */<br />
/**********************************************************************/</p>
<p>main ()</p>
<p>{ struct sockaddr_in cin;<br />
  struct sockaddr_in sin;<br />
  struct hostent *hp;<br />
  char buffer[bufsize];<br />
  int sd, sd_client, addrlen;</p>
<p>memset(&amp;sin,0,sizeof(sin));       /* Another way to zero memory */<br />
sin.sin_family = AF_INET;<br />
sin.sin_addr.s_addr = INADDR_ANY;          /* Broadcast address */<br />
sin.sin_port = htons(PORT);</p>
<p>if ((sd = socket(AF_INET,SOCK_STREAM,0)) == -1)<br />
   {<br />
   perror(”socket”);<br />
   exit(1);<br />
   }</p>
<p>if (bind(sd,&amp;sin,sizeof(sin)) == -1)  /* Must have this on server */<br />
   {<br />
   perror(”bind”);<br />
   exit(1);<br />
   }</p>
<p>if (listen(sd,queuesize) == -1)<br />
   {<br />
   perror(”listen”);<br />
   exit(1);<br />
   }</p>
<p>while (true)<br />
  {<br />
  if ((sd_client = accept(sd,&amp;cin,&amp;addrlen)) == -1)<br />
      {<br />
      perror(”accept”);<br />
      exit(1);<br />
      }</p>
<p>   if (recv(sd_client,buffer,sizeof(buffer),0) == -1)<br />
      {<br />
      perror(”recv”);<br />
      exit(1);<br />
      }</p>
<p>   if (!DoService(buffer))<br />
      {<br />
      break;<br />
      }</p>
<p>   if (send(sd_client,buffer,strlen(buffer),0) == -1)<br />
      {<br />
      perror(”send”);<br />
      exit(1);<br />
      }</p>
<p>   close (sd_client);<br />
   }</p>
<p>close (sd);<br />
printf(”Server closing down…\n”);<br />
}</p>
<p>/**************************************************************/</p>
<p>DoService(buffer)</p>
<p>char *buffer;</p>
<p>  /* This is the protocol section. Here we must */<br />
  /* check that the incoming data are sensible  */</p>
<p>{ int a=0,b=0;</p>
<p>printf(”Received: %s\n”,buffer);<br />
sscanf(buffer,”%d + %d\n”,&amp;a,&amp;b);</p>
<p>if (a &gt; 0 &amp;&amp; b&gt; 0)<br />
   {<br />
   sprintf(buffer,”%d + %d = %d”,a,b,a+b);<br />
   return true;<br />
   }<br />
else<br />
   {<br />
   if (strncmp(”halt”,buffer,4) == 0)<br />
     {<br />
     sprintf(buffer,”Server closing down!”);<br />
     return false;<br />
     }<br />
   else<br />
     {<br />
     sprintf(buffer,”Invalid protocol”);<br />
     return true;<br />
     }<br />
   }<br />
}</p>
<p>In the example we use `streams’ to implement a typical input/output behaviour for C. A stream interface is a so-called reliable protocol. There are other kinds of sockets too, called unrealiable, or UDP sockets. Features to notice on the server are that we must bind to a specific address. The client is always implicitly bound to an address since a socket connection always originates from the machine on which the client is running. On the server however we want to know which addresses we shall be receiving requests from. In the above example we use the generic wildcard address `INADDR_ANY’ which means that any host can connect to the server. Had we been more specific, we could have limited communication to two machines only.<br />
By calling `listen()’ we set up a queue for incoming connections. Rather than forking a separate process to handle each request we set up a queue of a certain depth. If we exceed this depth then new clients rtying to connect will be refused connection.<br />
The `accept’ call is the mechanism which extracts a `reply handle’ from the socket. Using the handle obtained from this call we can reply to the client without having to open a special socket explicitly.<br />
An improved server side connection can be setup, reading the service name from `/etc/services’ and setting reusable socket options to avoid busy signals, like this:</p>
<p>  struct sockaddr_in cin, sin;<br />
  struct servent *server;<br />
  int sd, addrlen = sizeof(cin);<br />
  int portnumber, yes=1;</p>
<p>  if ((server = getservbyname(service-name,”tcp”)) == NULL)<br />
      {<br />
      CfLog(cferror,”Couldn’t get cfengine service”,”getservbyname”);<br />
      exit (1);<br />
      }</p>
<p>   bzero(&amp;cin,sizeof(cin));</p>
<p> /*  Service returns network byte order */</p>
<p>   sin.sin_port = (unsigned short)(server-&gt;s_port);<br />
   sin.sin_addr.s_addr = INADDR_ANY;<br />
   sin.sin_family = AF_INET;</p>
<p>   if ((sd = socket(AF_INET,SOCK_STREAM,0)) == -1)<br />
      {<br />
      CfLog(cferror,”Couldn’t open socket”,”socket”);<br />
      exit (1);<br />
      }</p>
<p>   if (setsockopt (sd, SOL_SOCKET, SO_REUSEADDR,<br />
                      (char *) &amp;yes, sizeof (int)) == -1)<br />
      {<br />
      CfLog(cferror,”Couldn’t set socket options”,”sockopt”);<br />
      exit (1);<br />
      }</p>
<p>   if (bind(sd,(struct sockaddr *)&amp;sin,sizeof(sin)) == -1)<br />
      {</p>
<p>      }</p>
<p> /* etc */</p>
<p>Multithreading a server<br />
All the arguments must be collected into a struct, since only one argument pointer can be sent to the pthread functions.</p>
<p>#include </p>
<p>SpawnCfGetFile(args)</p>
<p>struct cfd_thread_arg *args;</p>
<p>{ pthread_t tid;<br />
  void *CfGetFile();</p>
<p>pthread_attr_init(&amp;PTHREADDEFAULTS);<br />
pthread_attr_setdetachstate(&amp;PTHREADDEFAULTS,PTHREAD_CREATE_DETACHED);</p>
<p>if (pthread_create(&amp;tid,&amp;PTHREADDEFAULTS,CfGetFile,args) != 0)<br />
   {<br />
   CfLog(cferror,”pthread_create failed”,”create”);<br />
   CfGetFile(args);<br />
   }</p>
<p>pthread_attr_destroy(&amp;PTHREADDEFAULTS);<br />
}</p>
<p>/***************************************************************/</p>
<p>void *CfGetFile(args)</p>
<p>struct cfd_thread_arg *args;</p>
<p>{ pthread_mutex_t mutex;</p>
<p>if (pthread_mutex_lock(&amp;mutex) != 0)<br />
   {<br />
   CfLog(cferror,”pthread_mutex_lock failed”,”pthread_mutex_lock”);<br />
   free(args-&gt;replyfile);  /* from strdup in each thread */<br />
   DeleteConn(args-&gt;connect);<br />
   free((char *)args);<br />
   return NULL;<br />
   }</p>
<p>ACTIVE_THREADS++;   /* Global variable */</p>
<p>if (pthread_mutex_unlock(&amp;mutex) != 0)<br />
   {<br />
   CfLog(cferror,”pthread_mutex_unlock failed”,”unlock”);<br />
   }</p>
<p> /* send data */</p>
<p>if (pthread_mutex_lock(&amp;mutex) != 0)<br />
   {<br />
   CfLog(cferror,”pthread_mutex_lock failed”,”pthread_mutex_lock”);<br />
   return;<br />
   }</p>
<p>ACTIVE_THREADS–;</p>
<p>if (pthread_mutex_unlock(&amp;mutex) != 0)<br />
   {<br />
   CfLog(cferror,”pthread_mutex_unlock failed”,”unlock”);<br />
   }</p>
<p>#endif</p>
<p>return NULL;<br />
}</p>
<p>System databases<br />
The C library calls which query the databases are, amongst others,<br />
getpwnam            get password data by name<br />
getpwuid            get password data by uid<br />
getgrnam            get group data by name<br />
gethostent          get entry in hosts database<br />
getnetgrent         get entry in netgroups database<br />
getservbyname       get servive by name<br />
getservbyport       get service by port<br />
get protobyname     get protocol by name<br />
For a complete list and how to use these, see the UNIX manual.<br />
The following example shows how to read the password file of the system. The functions used here can be used regardless of whether the network information service (NIS) is in use. The data are returned in a structure which is defined in `/usr/include/pwd.h’.<br />
/******************************************************************/<br />
/*                                                                */<br />
/* Read the passwd file by name and sequentially                  */<br />
/*                                                                */<br />
/******************************************************************/</p>
<p>#include<br />
#include </p>
<p>main ()</p>
<p>{ uid_t uid;<br />
   struct passwd *pw;</p>
<p>uid = getuid();</p>
<p>pw = getpwuid(uid);</p>
<p>printf (”Your login name is %s\n”,pw-&gt;pw_name);</p>
<p>printf (”Now here comes the whole file!\n\n”);</p>
<p>setpwent();</p>
<p>while (getpwent())<br />
   {<br />
   printf (”%s:%s:%s\n”,pw-&gt;pw_name,pw-&gt;pw_gecos,pw-&gt;pw_dir);<br />
   }</p>
<p>endpwent();<br />
}</p>
<p>DNS – The Domain Name Service<br />
The second network database service is that which converts host and domain names into IP numbers and vice versa. This is the domain name service, usually implemented by the BIND (Berkeley Internet Name Domain) software. The information here concerns version 4.9 of this software.<br />
gethostbyname()<br />
This is perhaps the most important function form hostname lookup. `gethostbyname()’ gets its information either from files, NIS or DNS. Its behaviour is configured by the files mentioned above, See section DNS – The Domain Name Service. It is used to look up the IP address of a named host (including domain name if DNS is used). On the configurable systems described above, the full list of servers is queried until a reply is obtained. The order in which the different services are queried is important here since DNS returns a fully qualified name (host name plus domain name) whereas NIS and the `/etc/hosts’ file database return only a hostname.<br />
gethostbyname returns data in the form of a pointer to a static data structure. The syntax is<br />
#include </p>
<p>struct hostent *hp;</p>
<p>hp = gethostbyname(”myhost.domain.country”)<br />
The resulting structure varies on different implementations of UNIX, but the `old BSD standard’ is of the form:</p>
<p>struct  hostent<br />
   {<br />
   char    *h_name;        /* official name of host */<br />
   char    **h_aliases;    /* alias list */<br />
   int     h_addrtype;     /* host address type */<br />
   int     h_length;       /* length of address */<br />
   char    **h_addr_list;  /* list of addresses from name server */<br />
   };</p>
<p>#define h_addr  h_addr_list[0]  /* address, for backward compatiblity */</p>
<p>The structure contains a list of addresses and or aliases from the nameserver. The interesting quantity is usually extracted by means of the macro `h_addr’ whcih gives the first value in the address list, though officially one should examine the whole list now.<br />
This value is a pointer which can be converted into a text form by the following hideous type transformation:</p>
<p>#include<br />
#include<br />
#include </p>
<p>struct sockaddr_in sin;</p>
<p>cin.sin_addr.s_addr = ((struct in_addr *)(hp-&gt;h_addr))-&gt;s_addr;</p>
<p>printf(”IP address = %s\n”,inet_ntoa(cin.sin_addr));</p>
<p>See the client program in the first section of this chapter for an example of its use.<br />
C support for NFS<br />
The support for NFS mounting in the standard C library is through two sources. NFS is based on the Sun’s RPC system, so the basic calls are only instances of standard RPC protocols.<br />
The C functions in the standard input/output library can be used to access NFS filesystems. Since NFS imitates the UNIX filesystem as closely as possible, NFS filesystems can be mounted in exactly the same way as ordinary filesystems. Unfortunately, the C functions which perform the mount operation in UNIX and depressingly non-standard. They differ on almost every implementation of UNIX.<br />
The basic function which mounts a filesystem, in `mount’ (see man (2) mount). The mount table is stored in a file /etc/mtab on BSD systems (again the name varies wildly from UNIX to UNIX, mnttab on HPUX for instance). The file /etc/rmtab on an NFS server contains a list of remote-mounted filesystems which are mounted by remote clients. C functions exist which can read the filesystem tables and place the resulting data in C struct types. Alas, these struct defintions are also quite different on different systems. See `/usr/include/sys/mount.h’, so the user wishing to write system-independent code is confounded at the lowest level.<br />
Exercises<br />
1. Use `gethostbyname()’ to make a simple program like `nslookup’ which gives the internet address of a named host.<br />
2. Modify the client server example above to make a `remote ls’ command called `rls’. You should be able to use the syntax<br />
3. rls (options) hostname:/path/to/file<br />
Summary of programming idioms.<br />
True and false</p>
<p># C shell</p>
<p>  True   – non-zero/non-empty value<br />
  False  – zero or null string</p>
<p># Bourne shell</p>
<p>  True   – 0 returned by shell command<br />
  False  – non-zero returned by shell command</p>
<p>  ( Note that “test” converts from C shell style to Bourne shell)</p>
<p># Perl</p>
<p>  True   – non-zero/non-empty value<br />
  False  – zero or null string</p>
<p>/* C */</p>
<p>  True   – non zero integer<br />
  False  – zero integer<br />
Input from tty<br />
# C shell</p>
<p>   $&lt;</p>
<p># Bourne shell</p>
<p>   line<br />
   read</p>
<p># Perl</p>
<p>/* C */</p>
<p>   scanf</p>
<p>Redirection of I/O</p>
<p># C  shell</p>
<p>  command  &gt;  file<br />
  command  &gt;&amp; file<br />
  command  &gt;&gt; file<br />
  command1 |  command2</p>
<p># Bourne shell</p>
<p>  command  &gt; file<br />
  command  &gt; file 2&gt;&amp;1<br />
  command  &gt;&gt; file<br />
  command1 | command2</p>
<p># Perl</p>
<p>  open (HANDLE,”&gt;file”)<br />
  open (HANDLE,”&gt;file 2&gt;&amp;1″)<br />
  open (HANDLE,”&gt;&gt;file”)<br />
  open (HANDLE,”command1 |”)<br />
  open (HANDLE,”| command2″)</p>
<p>/* C */</p>
<p>  fopen (”file”,”w”); printf(..)<br />
  fopen (”file”,”w”); printf(..); fprintf(stderr,..)<br />
  fopen (”file”,”a”); printf(..)<br />
  popen (”command1″,”r”)<br />
  popen (”command2″,”w”)</p>
<p>Loops and tests</p>
<p>/* C */ Shell</p>
<p>  foreach end         if then else endif<br />
  while end           switch case breaksw endsw<br />
  repeat</p>
<p># Bourne shell</p>
<p>  while do done       if then else fi<br />
  until do done       case in esac<br />
  for in do done</p>
<p># Perl</p>
<p>  while               if then else<br />
  for                 unless else<br />
  foreach<br />
  until<br />
  do while<br />
  do until</p>
<p>/* C */</p>
<p>  while               if then else<br />
  do while            switch case<br />
  for</p>
<p>Arguments from command line<br />
# C shell</p>
<p>  $argv[]<br />
  $#argv</p>
<p># Bourne Shell</p>
<p>  $1, $2, $3…  $*<br />
  $#</p>
<p># Perl</p>
<p>  $ARGV[]<br />
  $#ARGV</p>
<p>/* C */</p>
<p>  char argv[][]<br />
  int  argc<br />
Arithmetic<br />
# C shell</p>
<p>    a = $b + $c</p>
<p># Bourne shell</p>
<p>   a = `expr $b + $c`</p>
<p># Perl</p>
<p>   $a = $b + $c;</p>
<p>/* C */</p>
<p>   a = b + c;<br />
Numerical comparison<br />
# C shell</p>
<p>   if ( $x == $y ) then<br />
   endif</p>
<p># Bourne shell</p>
<p>   if [ $x -eq $y ]; then<br />
   fi</p>
<p># Perl</p>
<p>   if ( $x == $y )<br />
      {<br />
      }</p>
<p>/* C */</p>
<p>   if ( x == y )<br />
      {<br />
      }<br />
String comparison</p>
<p># C shell</p>
<p>   if ( $x == $y ) then<br />
   endif</p>
<p># Bourne shell</p>
<p>   if [ $x = $y ]; then<br />
   fi</p>
<p># Perl</p>
<p>   if ( $x eq $y ) then<br />
      {<br />
      }</p>
<p>/* C */</p>
<p>   if (strcmp(x,y) == 0)<br />
      {<br />
      }</p>
<p>Opening a file</p>
<p># C shell, Bourne shell – cannot be done (pipes only)</p>
<p># Perl</p>
<p>    open (READ_HANDLE,”filename”);<br />
    open (WRITE_HANDLE,”&gt; filename”);<br />
    open (APPEND_HANDLE,”&gt;&gt; filename”);</p>
<p>/* C */</p>
<p>    FILE *fp;</p>
<p>    fp = fopen (”file”,”r”);<br />
    fp = fopen (”file”,”w”);<br />
    fp = fopen (”file”,”a”);</p>
<p>Opening a directory</p>
<p># C shell</p>
<p>   foreach dir ( directory/* )<br />
      …<br />
   end</p>
<p># Bourne shell</p>
<p>   for dir in directory/* ;<br />
   do<br />
     …<br />
   done</p>
<p># Perl</p>
<p>   opendir (HANDLE,”directory”) || die;</p>
<p>   while ($entry = readdir(HANDLE))<br />
      {<br />
      }</p>
<p>   closedir(HANDLE);</p>
<p># C</p>
<p>   #include<br />
   DIR *dirh;<br />
   struct dirent *dirp;</p>
<p>   if ((dirh = opendir(name)) == NULL)<br />
      {<br />
      perror(”opendir”)<br />
      exit(1);<br />
      }</p>
<p>   for (dirp = readdir(dirh); dirp != NULL; dirp = readdir(dirh))<br />
      {<br />
      …  /* dirp-&gt;d_name points to child */<br />
      }</p>
<p>   closedir(dirh);</p>
<p>Testing file types</p>
<p># C shell</p>
<p>    if ( -f file )  # plain file<br />
    if ( -d file )  # directory</p>
<p># Bourne shell</p>
<p>    if [ -f file ]  # plain file<br />
    if [ -d file ]  # directory</p>
<p># Perl</p>
<p>    if ( -f file )  # plain file<br />
    if ( -d file )  # directory<br />
    if ( -l file )  # symbolic link</p>
<p>/* C */</p>
<p>     #include </p>
<p>     struct stat statvar;</p>
<p>     stat(”file”, &amp;statvar);</p>
<p>     if (S_ISREG(statvar.mode))  /* plain file */<br />
     if (S_ISDIR(statvar.mode))  /* directory  */</p>
<p>     lstat(”file”, &amp;statvar);</p>
<p>     if (S_ISLNK(statvar.mode))  /* symbolic link */</p>
Posted in Uncategorized, UnixAdministration  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/meherchilakalapudi.wordpress.com/687/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/meherchilakalapudi.wordpress.com/687/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/meherchilakalapudi.wordpress.com/687/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/meherchilakalapudi.wordpress.com/687/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/meherchilakalapudi.wordpress.com/687/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/meherchilakalapudi.wordpress.com/687/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/meherchilakalapudi.wordpress.com/687/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/meherchilakalapudi.wordpress.com/687/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/meherchilakalapudi.wordpress.com/687/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/meherchilakalapudi.wordpress.com/687/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meherchilakalapudi.wordpress.com&blog=5609619&post=687&subd=meherchilakalapudi&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://meherchilakalapudi.wordpress.com/2009/08/19/unix-administration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/08b95690d7ec04fc15567da451c993a3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">meherchilakalapudi</media:title>
		</media:content>

		<media:content url="img/header.gif" medium="image" />
	</item>
		<item>
		<title>RMI(Remote Method Invoking)</title>
		<link>http://meherchilakalapudi.wordpress.com/2009/08/17/rmiremote-method-invoking/</link>
		<comments>http://meherchilakalapudi.wordpress.com/2009/08/17/rmiremote-method-invoking/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 13:34:05 +0000</pubDate>
		<dc:creator>meherchilakalapudi</dc:creator>
				<category><![CDATA[RMI(Remote Method Invoking)]]></category>

		<guid isPermaLink="false">http://meherchilakalapudi.wordpress.com/?p=682</guid>
		<description><![CDATA[                                   
                                                RMI    
                                    [RemoteMethodInvocation]
 
Three main Distributed Technologies which are in use are:
 

Java RMI(Remote Method Invocation) from Sun Micro Systems.  
CORBA(Common Object Request Broker Architecture) from OMG(ObjectManagementGroup)
DCOM(Distributed Component Object Model) from Microsoft

 
Distributed Computing is started with RPC(Remote Procedure Call) technology.
 
A special S/W called PortMapper takes the client request and Connect him to the requested Application. so [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meherchilakalapudi.wordpress.com&blog=5609619&post=682&subd=meherchilakalapudi&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>                                   </p>
<p>                                                <strong>RMI    </strong></p>
<p><strong>                                    [RemoteMethodInvocation]</strong></p>
<p><strong> </strong></p>
<p><strong>Three main Distributed Technologies which are in use are:</strong></p>
<p><strong> </strong></p>
<ol>
<li><strong>Java RMI(Remote Method Invocation) from Sun Micro Systems.  </strong></li>
<li><strong>CORBA(Common Object Request Broker Architecture) from OMG(ObjectManagementGroup)</strong></li>
<li><strong>DCOM(Distributed Component Object Model) from Microsoft</strong></li>
</ol>
<p><strong> </strong></p>
<p><strong>Distributed Computing is started with RPC(Remote Procedure Call) technology.</strong></p>
<p><strong> </strong></p>
<p><strong>A special S/W called PortMapper takes the client request and Connect him to the requested Application. so client must remember the port number.</strong></p>
<p><strong> </strong></p>
<p><strong>The Server does not maintain the state.</strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong>Advantages:</strong></p>
<p><strong>      </strong></p>
<ol>
<li><strong>1.      </strong><strong>Every Object will have a Unique name. so that client can remember easily</strong></li>
<li><strong>2.      </strong><strong>we can send and receive objects through network</strong></li>
<li><strong>3.      </strong><strong>RMI separates Interfaces from Implementations.</strong></li>
</ol>
<p><strong> </strong></p>
<p><strong> The stubs and skeletons performing the marshaling and unmarshaling of data</strong></p>
<p><strong>A Remote stub uses a remote reference to carry out a remote method invocation to a remote object.</strong></p>
<p><strong>Transport layer makes the stream based network connections over  TCP/IP between JVMs .It provides basic connectivity.</strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong>             </strong></p>
<p><strong> </strong></p>
<p><strong>            </strong></p>
<p><strong>                                    </strong></p>
<p><strong>RMI allows object to object communication between different virtual machines(JVM). JVM can be distinct entities located on the same or separate computer yet one JVM can invokes methods belonging to an object stored in another JVM. This enables application to call object methods located remotely, sharing resources and processing load across systems. if both communication objects are written java programming langs we use RMI</strong></p>
<p><strong>RMI is easier to understand and convenient to use.</strong></p>
<p><strong> </strong></p>
<p><strong>        In java, distributed object model, a remote object is one whose methods can be invoked from another java virtual machine Potentially on different host.  An object of this type can be described by one or more remote interface which are java interfaces that declare the methods of remote object.</strong></p>
<p><strong> </strong></p>
<p><strong> Remote method invocation is the action of invoking a methods of a remote interface on a remote object.</strong></p>
<p><strong> </strong></p>
<p><strong><span style="text-decoration:underline;">Client</span>:</strong></p>
<p><strong>          </strong></p>
<p><strong>The JVM  that calls the remote object is usually referred to as a client.</strong></p>
<p><strong> </strong></p>
<p><strong><span style="text-decoration:underline;">Server</span>: </strong></p>
<p><strong> </strong></p>
<p><strong>The JVM that contains the remote object is usually referred to as a server.</strong></p>
<p><strong> </strong></p>
<p><strong>Remote object is  always accessed via its remote interface. In other words, the client invokes the methods on the object only after casting the reference remote interface.</strong></p>
<p><strong> </strong></p>
<p><strong><span style="text-decoration:underline;">Stubs and skeleton layer:</span></strong></p>
<p><strong> </strong></p>
<p><strong>The stub and skeleton acts as an interface between an application and rest of the RMI system. Its purpose is to transfer data to the remote reference layer via marshalling and unmarshalling. Marshalling refers to process of converting data or object being transferred into byte stream and unmarshalling is convert stream into an object or data. This conversion is achieved via object serialization.</strong></p>
<p><strong> </strong></p>
<p><strong>The following list the sequence of events performed by the stub:</strong></p>
<p><strong> </strong></p>
<p><strong>1. Initiates the connection with a remote virtual machine containing the remote object.</strong></p>
<p><strong>2. Marshall the parameters to the remote virtual machine.</strong></p>
<p><strong>3. Wait for the result of the method invocation.</strong></p>
<p><strong>4. Unmarshall the return value or exception returned.</strong></p>
<p><strong>5. Return the value to the caller.</strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong><span style="text-decoration:underline;">Skeleton:</span></strong></p>
<p><strong> </strong></p>
<p><strong> The following list the sequence of the events performed by the skeleton.</strong></p>
<p><strong> </strong></p>
<p><strong>1. Unmarshall the parameter for the remote method.</strong></p>
<p><strong>2. Invokes the method on the actual remote object implementation.</strong></p>
<p><strong>3. Marshall the result to the caller.</strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong>The jdk contains the rmic tool that creates the class files for stubs and skeleton</strong></p>
<p><strong> </strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;">Locating Remote Object</span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong>Clients find the remote services by using the naming or directory services.</strong></p>
<p><strong>RMI naming service, a registry is a remote object that serves as  a directory Service for client. The behavior of the registry is defined by the interface java.rmi.registry.Registry. RMI registry runs on each machine that hosts remote object accepts queries for services by defined on the port 1099.</strong></p>
<p><strong> </strong></p>
<p><strong> In simple the remote object is associate with a name in this registry.  Anytime the client invokes methods on this remote object it obtains to a </strong></p>
<p><strong>reference to it by looking up the name. Naming class specially locates objects in the RMI registry.</strong></p>
<p><strong> </strong></p>
<p><strong>Client connects to the Remote Registry by using the computer name and port no.</strong></p>
<p><strong> </strong></p>
<p><strong>Naming class is used to bind the Objects to RMI Registry</strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> Method                                                                              Description</strong></p>
<p><strong>Public static void bind(String name, Remote obj)         Binds the remote </strong></p>
<p><strong>object  to a string name.</strong></p>
<p><strong> </strong></p>
<p><strong>public static string [] list (String name)                    Returns an array of names </strong></p>
<p><strong>                                                                                       bounded to the registry.</strong></p>
<p><strong> </strong></p>
<p><strong>Pubic static  Remote lookup(String name)               Return a reference a stub  </strong></p>
<p><strong>                                                                                       for the remote object                          </strong></p>
<p><strong>                                                                              associated with specified name.</strong></p>
<p><strong> </strong></p>
<p><strong>public static void rebind (String name, Remote obj) Rebinds the specified </strong></p>
<p><strong> </strong></p>
<p><strong>name if it is already in use to a new remote object. </strong></p>
<p><strong> </strong></p>
<p><strong>Public static void unbind (String name)           Removes the binding with the </strong></p>
<p><strong> </strong></p>
<p><strong>specified name.</strong></p>
<p><strong> </strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;">Developing application with RMI:</span></strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong>Writing client server application using RMI involves six basic steps:</strong></p>
<p><strong> </strong></p>
<p><strong>1. Defining a remote interface.</strong></p>
<p><strong>2. Implementing the remote interface.</strong></p>
<p><strong>3. Writing the client that uses the remote object.</strong></p>
<p><strong>4. Generating stubs and skeletons.</strong></p>
<p><strong>5. Starting the registry and registering object.</strong></p>
<p><strong>6. Running the server and client.</strong></p>
<p><strong> </strong></p>
<p><strong>                                    </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong>                                                                                                            </strong></p>
<p><strong>            </strong></p>
Posted in RMI(Remote Method Invoking)  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/meherchilakalapudi.wordpress.com/682/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/meherchilakalapudi.wordpress.com/682/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/meherchilakalapudi.wordpress.com/682/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/meherchilakalapudi.wordpress.com/682/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/meherchilakalapudi.wordpress.com/682/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/meherchilakalapudi.wordpress.com/682/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/meherchilakalapudi.wordpress.com/682/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/meherchilakalapudi.wordpress.com/682/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/meherchilakalapudi.wordpress.com/682/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/meherchilakalapudi.wordpress.com/682/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meherchilakalapudi.wordpress.com&blog=5609619&post=682&subd=meherchilakalapudi&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://meherchilakalapudi.wordpress.com/2009/08/17/rmiremote-method-invoking/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/08b95690d7ec04fc15567da451c993a3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">meherchilakalapudi</media:title>
		</media:content>
	</item>
		<item>
		<title>Jsp(java Server Pages)</title>
		<link>http://meherchilakalapudi.wordpress.com/2009/08/17/jspjava-server-pages/</link>
		<comments>http://meherchilakalapudi.wordpress.com/2009/08/17/jspjava-server-pages/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 13:30:50 +0000</pubDate>
		<dc:creator>meherchilakalapudi</dc:creator>
				<category><![CDATA[Jsp(java server Pages)]]></category>

		<guid isPermaLink="false">http://meherchilakalapudi.wordpress.com/?p=680</guid>
		<description><![CDATA[ 
 
 
JSP
                                [Java Server Page]
 
JavaServerPage is a simple,yet powerful technology for creating and maintaining dynamic webpages.A JSP is a HTML page with extension .jsp. we can include java executable code  in JSP pages.a JSP is nothing but a servlet after compilation.This enables the webapplication programmer to create dynamic content by using predifined components and by interacting [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meherchilakalapudi.wordpress.com&blog=5609619&post=680&subd=meherchilakalapudi&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p> </p>
<p> </p>
<p> </p>
<p align="center">JSP</p>
<p>                                [Java Server Page]</p>
<p> </p>
<p>JavaServerPage is a simple,yet powerful technology for creating and maintaining dynamic webpages.A JSP is a HTML page with extension .jsp. we can include java executable code  in JSP pages.a JSP is nothing but a servlet after compilation.This enables the webapplication programmer to create dynamic content by using predifined components and by interacting with components using server side scripting . JSP can reuse javabeans and custom tag libraries for complex and dynamic functionality.</p>
<p> </p>
<p>programmer tend to use JSPs most of the content sent to the client is fixed template data and only a small portion of the content is generated dynamically with java code.</p>
<p> </p>
<p>Comparisions with CGI</p>
<p> </p>
<ul>
<li>JSP can maintain state on the server between requests(since it can ue Servlet sessions)</li>
<li> Spawns a new thread for each request</li>
<li> Does not have to be loaded each time, once it has been initiated</li>
<li> Runs in a ready-loaded JVM as an extension to the web server</li>
</ul>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p>Comparing  with ASP:</p>
<p> </p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="197" valign="top"> </td>
<td width="197" valign="top">         JSP</td>
<td width="197" valign="top">          ASP</td>
</tr>
<tr>
<td width="197" valign="top">Platforms</td>
<td width="197" valign="top">All major web paltforms</td>
<td width="197" valign="top">Microsoft</td>
</tr>
<tr>
<td width="197" valign="top">Base Language</td>
<td width="197" valign="top">Java</td>
<td width="197" valign="top"> Jscript or VBScript</td>
</tr>
<tr>
<td width="197" valign="top">Components</td>
<td width="197" valign="top">JSP tags,JavaBeans or Enterprise JavaBeans</td>
<td width="197" valign="top">COM/DCOM</td>
</tr>
<tr>
<td width="197" valign="top">Code Interpretation</td>
<td width="197" valign="top">Once</td>
<td width="197" valign="top">Each instance</td>
</tr>
</tbody>
</table>
<p> </p>
<p> </p>
<p>Comparing with Servlets:</p>
<p> </p>
<ul>
<li>While writing servlet programming logic is simple but output format is lengthy,</li>
</ul>
<p>Where as in JSP we write logic and execute directly on the server</p>
<ul>
<li>In servlets we can’t separate dynamic content from java code</li>
</ul>
<p>Where as in JSP clear separation of dynamic content from java code is possible</p>
<ul>
<li>In servlet we can’t generate complex HTML page,</li>
</ul>
<p>     Where as in JSP we can directly include HTML</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p>The Server component that executes the JSPs is referred as JSPContainer.</p>
<p> </p>
<p>when JSP enabled sever receives the first request for a jsp, the jsp container translates that jsp into a java servlet that handles the current request and future requests to the jsp. If there are any errors in transalting into  the new servlet these errors result in translation-time errors.The jsp container places the java statements that implements the JSPs response in method _jspService() at translation time. If the new servlet compiles properly, the jspcontainer invokes method _jspService()  to process  the request .the jsp may respond directly to the request or invoke other webapplication components to assists the processing the request. Any errors that occur during the request processing are known as requesttime errors;</p>
<p> </p>
<p><span style="text-decoration:underline;">JSP Elements</span></p>
<p><span style="text-decoration:underline;"> </span></p>
<ul>
<li>Scripting Elements</li>
<li>Directives</li>
<li>Actions</li>
</ul>
<p><span style="text-decoration:underline;"> </span></p>
<p><span style="text-decoration:underline;"> </span></p>
<p><span style="text-decoration:underline;">Scripting Elements</span></p>
<p> </p>
<p>JSP scripting elements allow java code -variable or method declarations,scriplets, and expressions to be inserted into our JSP page.</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p>1)Declarations:</p>
<p>       </p>
<p>        A declarations is block of java code in a JSP that is used to defiene class-wide variables and methods in generated servlet. Declarations are initialized when the jsp page is initialized, and have instance scope in the generated servlet,so that anything defined in a declarations is available throughout the JSP to other declarations,expressions,and code.Adeclaration block is enclosed between &lt;%! and %&gt; and do not write anything to the outputstream. the syntax is:</p>
<p> </p>
<p>&lt;%! Java variable and method declarations %&gt;</p>
<p>Ex:</p>
<p>        &lt;%! int no=10;</p>
<p>                String %&gt;</p>
<p> </p>
<p>2)Scriptlets:</p>
<p> </p>
<p>        A scriptlet is a block of java code that is executed during the request procesing time, and is enclosed between &lt;% and %&gt; tags.It produces output for the client.All the code apperaring between the &lt;% and  %&gt; tags in the jsp gets put into the service() method of the servlet as is, in the order in which it appears.It&#8217;s syntax is:</p>
<p> </p>
<p>        &lt;% valid java code statements %&gt;</p>
<p>        Ex:</p>
<p>                &lt;% out.println(new java.util.Date().toString()); %&gt;</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p>3)Expressions:</p>
<p> </p>
<p>        An expression is a shorthand notation for a scriplet that sends the value of a java expression back to the client.The expression is evaluated at HTTP request processing time, and the result is converted into string and displayed.An expression is enclosed between &lt;%= and %&gt; tags.It&#8217;s syntax is</p>
<p>        &lt;%= java expression to be evaluated %&gt;</p>
<p>        Ex:</p>
<p>                &lt;%=&#8221;Java Expression&#8221; %&gt;</p>
<p> </p>
<p><span style="text-decoration:underline;">JSPDirectives:</span></p>
<p><span style="text-decoration:underline;"> </span></p>
<p>A JSP directive affects the overall structure of the servlet class.Directives begin with &lt;%@ and end with %&gt;, and the general syntax is:</p>
<p> </p>
<p>&lt;%@ directivename attribute=&#8221;value&#8221; attribute=&#8221;value&#8221; %&gt;</p>
<p> </p>
<p>There are three main directives that can be used in a JSP:</p>
<p> </p>
<p>1)The page directive</p>
<p>2)The include directive</p>
<p>3)The taglib directive</p>
<p> </p>
<p>The page directive:</p>
<p> </p>
<p>        The page directive is used to define and manipulate a number of important attributes that affect the whole jsp page.A page contains any number of page directives in any order, any where in the  JSP page.The General syntax of page directive is:</p>
<p>&lt;%@ page ATTRIBUTES %&gt;</p>
<p>where the valid attributes are the following name value pairs</p>
<p> </p>
<ul>
<li>language <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> efines the scripting language to be used. default value is Java</li>
<li>extends: The value is a fully qualified class name of the superclass that the generated class,into which this jsp page is translated, must extended.omitted by default</li>
<li>import: Comma separated list of packages or classes, with the same meaning as import statements in java classes</li>
<li>session: Specifies whether the page participates in an HTTP session.when true the implicit object named session is available and can be used to  access the current/new session for the page.</li>
</ul>
<p>If false,the page does not participate in a session and the implicit session object is unavailable     .</p>
<ul>
<li>buffer: Specifies the buffering model for the output stream to the client. if the value is none, no buffering occurs and all output is written directly through to the ServletResponse by a PrintWriter.</li>
</ul>
<p>if a buffer size is specified,out is buffered witha buffer size not less than that value.</p>
<ul>
<li>autoFlush: If true, the output buffer to the client is flushed qutomatically when it is full.   </li>
</ul>
<p>If false, a runtime exception is raised to indicate a buffer overflow</p>
<ul>
<li>isThreadSafe: Defines the level of thread safety implemented in the page. if the value is true the JSPengine may send multiple client requests tothe page at the same time.</li>
</ul>
<p>If the value is false then the JSP container queues up client requests sent to the page for processing, and process them one at a time, in the order in which they are recieved.</p>
<p> </p>
<ul>
<li>errorPage: Defines a URL to another JSP page within the current webapplications,which is invoked if a checked or unchecked exception is thrown.The page implementation catches the instance   of the throwable object and passes it to the error page.</li>
<li>isErrorPage: Indicates if the current JSP page is intended to another JSP page&#8217;s error page.</li>
<li>contentType: Defines the character encoding for the JSP and the MIME type for the response of the JSP page.The default value of MIME  type is text/html.</li>
</ul>
<p>       </p>
<p><span style="text-decoration:underline;">The include directive:</span></p>
<p> </p>
<p>The include directive instructs the container to include the contents of a resource in the current JSP, inserting it inline, in the JSP page.The specified file must be accessible and available to the JSP container.The syntax of the include directive is:</p>
<p> </p>
<p>        &lt;%@ include file=&#8221;Filename&#8221; %&gt;</p>
<p> </p>
<p>        Ex: &lt;%@ include file=&#8221;/jsp-examples/hello.jsp&#8221; %&gt;</p>
<p>       </p>
<p>the contents of the included file is parsed by the JSP only at translation time, that is when the JSP page is compilied into a servlet.</p>
<p> </p>
<p>       </p>
<p><span style="text-decoration:underline;">Implicit Objects:</span></p>
<p><span style="text-decoration:underline;"> </span></p>
<ul>
<li>request: The request object represents the HttpServletRequest instance to access the incoming request.</li>
<li>response: The response object is the HttpServletResponse instance that represents the servlets&#8217;s response to the request.</li>
<li>pageContext: the pageContext provides a single point of access to many of the page attributes and is convenient place to put shared data within the page.</li>
<li>session: The session object represents the session created for the requesting client.</li>
<li>application: The application object represents the servlet context,obtained from the servlet configurations object.</li>
<li>out: The out object is the object that writes into the outputstream to the client.</li>
<li>config: The config object is the ServletConfig for this JSP page, and has page scope</li>
</ul>
<p> </p>
<p><span style="text-decoration:underline;"> </span></p>
<p><span style="text-decoration:underline;">Standard Actions(XMLTags):</span></p>
<p><span style="text-decoration:underline;"> </span></p>
<p><span style="text-decoration:underline;">JSPTags</span>                                                           <span style="text-decoration:underline;">XMLTags</span></p>
<p><span style="text-decoration:underline;"> </span></p>
<p>&lt;%! declarations ; %&gt;                                                                                                                  &lt;jsp:declarations&gt; </p>
<p>                                                                                &#8230;&#8230;.</p>
<p>                                                                                                                                                        &lt;/jsp:declarations&gt;</p>
<p> </p>
<p>&lt;%= expressions %&gt;                                                                                                                    &lt;jsp:expression&gt;</p>
<p>                                                                        &#8230;&#8230;.</p>
<p>                                                                                                                                                        &lt;/jsp:expression&gt;</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p>&lt;% scriplets %&gt;                                  &lt;jsp:scriplets &gt;</p>
<p>                                                                        &#8230;&#8230;.</p>
<p>                                                                                                                                                        &lt;/jsp:scriplets&gt;</p>
<p> </p>
<p>&lt;%@ directive %&gt;               &lt;jsp:directive attribute=&#8221;value&#8221; /&gt;</p>
<p>       </p>
<p>&lt;%@ include %&gt;          &lt;jsp:include page=&#8221;URL&#8221;   /&gt;</p>
<p> </p>
<p>&lt;% response.sendRedirect %&gt;      &lt;jsp:forward   page=&#8221;URL&#8221; /&gt;</p>
<p>                                &lt;jsp:param name=”attribute” value=”value”&gt;</p>
<p>                                                &lt;jsp:plugin&gt;</p>
<p>                                                &lt;jsp:useBean&gt;</p>
<p>                                                &lt;jsp:setProperty&gt;</p>
<p>                                                &lt;jsp:getProperty&gt;</p>
<p> </p>
<p><span style="text-decoration:underline;"> </span></p>
<p><span style="text-decoration:underline;"> </span></p>
<p><span style="text-decoration:underline;"> </span></p>
<p><span style="text-decoration:underline;"> </span></p>
<p><span style="text-decoration:underline;"> </span></p>
<p><span style="text-decoration:underline;">&lt;jsp:include&gt;</span></p>
<p> </p>
<p>        It is so convenient to be able to include the contents of othre files into a jsp page.This makes it easier to use standard HTML headers and footers or to share common code among multiple JSP pages.</p>
<p>Ex:</p>
<p>&lt;jsp:include page=&#8221;url&#8221; /&gt;</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p><span style="text-decoration:underline;">&lt;jsp:forward&gt;</span></p>
<p>       </p>
<p>        This action is used to forward the  request to another JSP page.It has a single attribute i.e.,page which shared consists of a related URL.</p>
<p>Ex:</p>
<p>a)     &lt;jsp:forward page=&#8221;url&#8221; /&gt;</p>
<p> </p>
<p>b)     &lt;jsp:forward page=&#8221;url&#8221; &gt;</p>
<p>        &lt;jsp:param value=&#8221;value&#8221; /&gt;</p>
<p>        &lt;/jsp:forward&gt;</p>
<p> </p>
<p><span style="text-decoration:underline;">&lt;jsp:useBean&gt;</span></p>
<p><span style="text-decoration:underline;"> </span></p>
<p>Syntax:</p>
<p> </p>
<p>&lt;jsp:useBean   id=&#8221;name&#8221;  scope=&#8221;page/request/session/application&#8221; /&gt;</p>
<p> </p>
<p>id: This usually means instantiate an object of the class specified by class, and bind it to a varaible with the name specified by id.</p>
<p> </p>
<p>class:It designates the full package name of the bean</p>
<p> </p>
<p>scope:It indicates the context in which the bean should be made available</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p>                                                                                                                 1.page: Bean is only available to the current page</p>
<p>                                                                                                                 2.request: Bean is only available for the current client request</p>
<p>                                                                                                                 3.session: Bean is available to all the pages during the life of the current</p>
<p> </p>
<p>         HttpSession</p>
<p>                                                                                                                </p>
<p> </p>
<p>4.application: Bean is available to all the pages that share the same servlet</p>
<p>                                                                                          context</p>
<p> </p>
<p><span style="text-decoration:underline;">&lt;jsp:setProperty&gt;</span></p>
<p><span style="text-decoration:underline;"> </span></p>
<p>This action give values to properties of the beans</p>
<p> </p>
<p>syntax:</p>
<p>                                                                                                                 &lt;jsp:setProperty property=”propertyname”</p>
<p>                                                                                                                                  property=&#8221;propertyName&#8221;</p>
<p>   value=&#8221;propertyValue&#8221; /&gt;</p>
<p> </p>
<p><span style="text-decoration:underline;">&lt;jsp:getProperty&gt;</span>                                                                                                                                                                                                                                           </p>
<p> </p>
<p>This action retrieves the values of a bean property,converts it into a string and insert it into the output.</p>
<p> </p>
<p>syntax:</p>
<p>                                                                                                                 &lt;jsp:getProperty property=”propertyname”&gt;</p>
<p> </p>
<p> </p>
<p> </p>
<p>&lt;jsp:plugin&gt;</p>
<p> </p>
<p>The &lt;jsp:plugin&gt; action provides easy support for including Java applets in JSP</p>
<p>Generated page. It is used to generate browser-specific HTML tags that results in the download of the Java Plugin-in software,if required ,followd by the execution of the applet or JavaBean component that is specified in the tag.</p>
<p> </p>
<p>Syntax:</p>
<p> &lt;jsp:plugin type=”bean/applet” code=”objectcode”</p>
<p> </p>
<p>codebase=”objectcodebase”</p>
<p>height=”height”</p>
<p>width=”width”&gt;</p>
<p>&lt;jsp:params&gt;</p>
<p>     &lt;jsp:param name=”paramname” value=”paramvalue” /&gt;</p>
<p>          &lt;jsp:param name=”paramname” value=”paramvalue” /&gt;</p>
<p>        …….</p>
<p>&lt;/jsp:params&gt;</p>
<p>&lt;jsp:fallback &gt;Alternative text to display&lt;/jsp:fallback&gt;</p>
<p>&lt;/jsp:plugin&gt;</p>
<p> </p>
<p>                                                                                                  </p>
<p>                                                                                                  </p>
<p>                                                                                                                </p>
<p> </p>
<p> </p>
<p> </p>
<p>                                                                                                                 </p>
<p>                                                                                                                                           property=&#8221;propertyof the bean&#8221; /&gt;</p>
<p>                                                                                                                                                                                                                                                       </p>
<p> </p>
<p> </p>
<p> </p>
<p>       </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p align="center"> </p>
Posted in Jsp(java server Pages)  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/meherchilakalapudi.wordpress.com/680/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/meherchilakalapudi.wordpress.com/680/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/meherchilakalapudi.wordpress.com/680/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/meherchilakalapudi.wordpress.com/680/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/meherchilakalapudi.wordpress.com/680/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/meherchilakalapudi.wordpress.com/680/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/meherchilakalapudi.wordpress.com/680/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/meherchilakalapudi.wordpress.com/680/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/meherchilakalapudi.wordpress.com/680/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/meherchilakalapudi.wordpress.com/680/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meherchilakalapudi.wordpress.com&blog=5609619&post=680&subd=meherchilakalapudi&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://meherchilakalapudi.wordpress.com/2009/08/17/jspjava-server-pages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/08b95690d7ec04fc15567da451c993a3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">meherchilakalapudi</media:title>
		</media:content>
	</item>
		<item>
		<title>Java FAQ&#8217;s</title>
		<link>http://meherchilakalapudi.wordpress.com/2009/07/16/java-faqs/</link>
		<comments>http://meherchilakalapudi.wordpress.com/2009/07/16/java-faqs/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 14:35:48 +0000</pubDate>
		<dc:creator>meherchilakalapudi</dc:creator>
				<category><![CDATA[Java_FAQ's]]></category>

		<guid isPermaLink="false">http://meherchilakalapudi.wordpress.com/?p=678</guid>
		<description><![CDATA[FREQUENTLY ASKED QUESTIONS (JAVA) IN INTERVIEWS
for 1)What is OOPs?
Ans: Object oriented programming organizes a program around its data,i.e.,objects and a set of well defined interfaces to that data.An object-oriented program can be characterized as data controlling access to code.
2)what is the difference between Procedural and OOPs?
Ans: a) In procedural program, programming logic follows certain procedures [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meherchilakalapudi.wordpress.com&blog=5609619&post=678&subd=meherchilakalapudi&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>FREQUENTLY ASKED QUESTIONS (JAVA) IN INTERVIEWS</p>
<p>for 1)What is OOPs?</p>
<p>Ans: Object oriented programming organizes a program around its data,i.e.,objects and a set of well defined interfaces to that data.An object-oriented program can be characterized as data controlling access to code.</p>
<p>2)what is the difference between Procedural and OOPs?</p>
<p>Ans: a) In procedural program, programming logic follows certain procedures and the instructions are executed one after another. In OOPs program, unit of program is object, which is nothing but combination of data and code.</p>
<p>b) In procedural program,data is exposed to the whole program whereas in OOPs program,it is accessible with in the object and which in turn assures the security of the code.</p>
<p>3)What are Encapsulation, Inheritance and Polymorphism?</p>
<p>Ans: Encapsulation is the mechanism that binds together code and data it manipulates and keeps both safe from outside interference and misuse.Inheritance is the process by which one object acquires the properties of another object.Polymorphism is the feature that allows one interface to be used general class actions.</p>
<p>4)What is the difference between Assignment and Initialization?</p>
<p>Ans: Assignment can be done as many times as desired whereas initialization can be done only once.</p>
<p>5)What are Class, Constructor and Primitive data types?</p>
<p>Ans: Class is a template for multiple objects with similar features and it is a blue print for objects. It defines a type of object according to the data the object can hold and the operations the object can perform. Constructor is a special kind of method that determines how an object is initialized when created.</p>
<p>Primitive data types are 8 types and they are:</p>
<p>byte, short, int, long</p>
<p>float, double</p>
<p>boolean</p>
<p>char</p>
<p> </p>
<p>6)What is an Object and how do you allocate memory to it?</p>
<p>Ans: Object is an instance of a class and it is a software unit that combines a structured set of data with a set of  operations for inspecting and manipulating that data. When an object is created using new operator, memory is allocated to it.</p>
<p>7)What is the difference between constructor and method?  Ans: Constructor will be automatically invoked when an object is created whereas method has to be called explicitly.</p>
<p>8)What are methods and how are they defined?</p>
<p>Ans: Methods are functions that operate on instances of classes in which they are defined. Objects can communicate with each other using methods and can call methods in other classes.</p>
<p>Method definition has four parts.</p>
<p>they are name of the method,</p>
<p> type of object or primitive type the method returns,</p>
<p>a list of parameters and the body of the method. A method’s signature is a combination of the first three parts mentioned above.</p>
<p>9)What is the use of bin and lib in JDK?</p>
<p>Ans: Bin contains all tools such as javac, appletviewer, awt tool, etc., whereas lib contains API and all packages.</p>
<p>10)What is casting?</p>
<p>Ans: Casting is used to convert the value of one type to another.</p>
<p>11)How many ways can an argument be passed to a subroutine and explain them?</p>
<p>Ans: An argument can be passed in two ways. They are passing by value and passing by reference.Passing by value: This method copies the value of an argument into the formal parameter of the subroutine.Passing by reference: In this method, a reference to an argument (not the value of the argument) is passed to the parameter.</p>
<p>12)What is the difference between an argument and a parameter?</p>
<p>Ans: While defining method, variables passed in the method are called parameters. While using those methods, values passed to those variables are called arguments.</p>
<p>13)What are different types of access modifiers?</p>
<p>Ans: public: Any thing declared as public can be accessed from anywhere.</p>
<p>private: Any thing declared as private can’t be seen outside of its class.</p>
<p>protected: Any thing declared as protected can be accessed by classes in the same package and subclasses in the other packages.</p>
<p>default modifier : Can be accessed only to classes in the same package.</p>
<p>14)What is final, finalize() and finally?</p>
<p>Ans: final : final keyword can be used for class, method and variables.A final class cannot be subclassed and it prevents other programmers from subclassing a secure class to invoke insecure methods.A final method can’ t be overriddenA final variable can’t change from its initialized value.finalize( ) : finalize( ) method is used just before an object is destroyed and can be called just prior to garbage collecollection   finally : finally, a key word used in exception handling, creates a block of code that will be executed after a try/catch block has completed and before the code following the try/catch block. The finally block will execute whether or not an exception is thrown. this For example, if a method opens a file upon exit, then you will not want the code that closes the file to be bypassed by the exception-handling mechanism. This finally keyword is designed to address</p>
<p>contingency.      </p>
<p>15)What is UNICODE?</p>
<p>Ans: Unicode is used for internal representation of characters and strings and it uses 16 bits to represent each other.</p>
<p>16)What is Garbage Collection and how to call it explicitly?</p>
<p>Ans: When an object is no longer referred to by any variable, java automatically reclaims memory used by that object. This is known as garbage collection.System.gc() method may be used to call it explicitly.</p>
<p>17)What is  finalize() method ?</p>
<p>Ans: finalize () method is used just before an object is destroyed and can be called just prior to garbage collection.</p>
<p>18)What are Transient and Volatile Modifiers?</p>
<p>Ans: Transient: The transient modifier applies to variables only and it is not stored as part of its object’s Persistent state. Transient variables are not serialized.Volatile: Volatile modifier applies to variables only and it tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of the program.</p>
<p>19)What is method overloading and method overriding?</p>
<p>Ans: Method overloading: When a method in a class having the same method name with different arguments is said to be method overloading.</p>
<p>Method overriding : When a method in a class having the same method name with same arguments is said to be method overriding.</p>
<p>20)What is difference between overloading and overriding?</p>
<p>Ans: a) In overloading, there is a relationship between methods available in the same class whereas in overriding, there is relationship between a superclass method and subclass method.</p>
<p>b)    Overloading does not block inheritance from the superclass whereas overriding blocks inheritance from the superclass.</p>
<p>c)     In overloading, separate methods share the same name whereas in overriding,subclass method replaces the superclass.</p>
<p>d)    Overloading must have different method signatures whereas overriding  must have same signature.</p>
<p> </p>
<p>21) What is meant by Inheritance and what are its advantages?</p>
<p>Ans: Inheritance is the process of inheriting all the features from a class. The advantages of inheritance are reusability of code and accessibility of variables and methods of the super class by subclasses.</p>
<p>22)What is the difference between this() and super()?</p>
<p>Ans: this() can be used to invoke a constructor of the same class whereas super() can be used to invoke a super class constructor.</p>
<p>23)What is the difference between superclass and subclass?</p>
<h2>Ans: A super class is a class that is inherited whereas sub class is a classthat does the inheriting.</h2>
<p>24) What modifiers may be used with top-level class?</p>
<p>Ans: public, abstract and final can be used for top-level class.</p>
<p>25)What are inner class and anonymous class?</p>
<p>Ans: Inner class  : classes defined in other classes, including those defined in methods are called  inner classes. An inner class can have any accessibility including private.Anonymous class : Anonymous class is a class defined inside a method without a name and is instantiated and declared in the same place and cannot have explicit constructors.</p>
<p>26)What is a package?</p>
<p>Ans: A package is a collection of classes and interfaces that provides a high-level layer of access protection and name space management.</p>
<h3>27)          What is a reflection package? </h3>
<p>Ans: java.lang.reflect package has the ability to analyze itself in runtime.</p>
<h3>28)          What is interface and its use?</h3>
<p>Ans:Interface is similar to a class which may contain method’s signature only but not bodies and  it is</p>
<p>a formal set of method and constant declarations that must be defined by the class that implements it.</p>
<p>Interfaces are useful for:</p>
<p>a)Declaring methods that one or more classes are expected to implement</p>
<p>b)Capturing similarities between unrelated classes without forcing a class relationship.</p>
<p>c)Determining an object’s programming interface without revealing the actual body of the class.</p>
<h3>29)          What is an abstract class?</h3>
<p>Ans: An abstract class is a class designed with implementation gaps for subclasses to fill in and is deliberately incomplete. </p>
<h3>30)          What is the difference between Integer and int?</h3>
<p>Ans: a) Integer is a class defined in the java.lang package, whereas int is a primitive data type defined in the Java language itself. Java does not automatically convert from one to the other.</p>
<p>b)    Integer can be used as an argument for a method that requires an object, whereas int can be used for</p>
<p>calculations.</p>
<h3>31)           What is a cloneable interface and how many methods does it contain?</h3>
<p>Ans- It is not having any method because it is a TAGGED or MARKER interface.</p>
<h3>32)          What is the difference between abstract class and interface?</h3>
<p>Ans: a) All the methods declared inside an interface are abstract whereas abstract class must have at least one abstract method and others may be concrete or abstract.</p>
<p>b) In abstract class, key word abstract must be used for the methods whereas interface we need not use that keyword for the methods.</p>
<p>c)     Abstract class must have subclasses whereas interface can’t have subclasses.</p>
<p> </p>
<h3>33)          Can you have an inner class inside a method and what variables can you access?</h3>
<p>Ans: Yes, we can have an inner class inside a method and final variables can be accessed.</p>
<h3>34)          What is the difference between String and String Buffer?</h3>
<p>Ans: a) String objects are constants and immutable whereasStringBuffer objects are not.</p>
<p>b) String class supports constant strings whereas StringBuffer class supports growable and modifiable strings.</p>
<h3>35)          What is the difference between Array and vector?</h3>
<p>Ans: Array is a set of related data type and static whereas vector is a growable array of objects and dynamic.</p>
<h3>36)          What is the difference between exception and error?</h3>
<p>Ans: The exception class defines mild error conditions that your program encounters.Ex: Arithmetic Exception, FilenotFound exception Exceptions can occur when try to open the file, which does not exist</p>
<ul>
<li>the network connection is disrupted</li>
<li>operands being manipulated are out of prescribed ranges</li>
<li>the class file you are interested in loading is missing</li>
</ul>
<p>The error class defines serious error conditions that you should not  attempt to recover from. In most cases it is advisable to let the program terminate when such an error is encountered.</p>
<p>Ex: Running out of memory error, Stack overflow error.</p>
<h3>37)          What is the difference between process and thread?</h3>
<p>Ans: Process is a program in execution whereas thread is a separate path of execution in a program.</p>
<h3>38)          What is multithreading and what are the methods for inter-thread communication and what is the class in which these methods are defined?</h3>
<p>Ans: Multithreading is the mechanism in which more than one thread run independent of each other within the process.</p>
<p>wait (), notify () and notifyAll() methods can be used for inter-thread communication and these methods are in Object class.</p>
<p>wait( ) : When a thread executes a call to wait( ) method, it surrenders the object lock and enters into a waiting state.</p>
<p>notify( ) or notifyAll( ) : To remove a thread from the waiting state, some other thread must make a call to  notify( ) or notifyAll( ) method on the same object.</p>
<p>39)          What is the class and interface in java to create thread and which is the most advantageous method?</p>
<p>Ans: Thread class and Runnable interface can be used to create threads and using Runnable interface is the most advantageous method to create threads because we need not extend thread class here.</p>
<h3>40)          What are the states associated in the thread?</h3>
<p>Ans: Thread contains ready, running, waiting and dead states.</p>
<h3>41)           What is synchronization?</h3>
<p>Ans: Synchronization is the mechanism that ensures that only one thread is accessed the resources at a time.</p>
<h3>42)          When you will synchronize a piece of your code?</h3>
<p>Ans: When you expect your code will be accessed by different threads and these threads may change a particular data causing data corruption.</p>
<h3>43)          What is deadlock?</h3>
<p>Ans: When two threads are waiting each other and can’t precede the program is said to be deadlock.</p>
<h3>44)          What is daemon thread and which method is used to create the daemon thread?</h3>
<p>Ans: Daemon thread is a low priority thread which runs intermittently in the back ground doing the garbage collection operation for the java runtime system. setDaemon method is used to create a daemon thread.</p>
<h3>45)          Are there any global variables in Java, which can be accessed by other part of your program?</h3>
<p>Ans: No, it is not the main method in which you define variables. Global variables is not possible because concept of encapsulation is eliminated here.</p>
<p>46)What is an applet?</p>
<p>Ans: Applet is a dynamic and interactive program that runs inside a web page displayed by a java capable browser.</p>
<p>47)What is the difference between applications and applets?</p>
<p>Ans: a)Application must be run on local machine whereas applet needs no explicit installation on local machine.</p>
<p>b)Application must be run explicitly within a java-compatible virtual machine whereas applet loads and runs itself automatically in a java-enabled browser.</p>
<p>d)Application starts execution with its main method whereas applet starts execution with its init method.</p>
<p>e)Application can run with or without graphical user interface whereas applet must run within a graphical user interface.</p>
<p>48)How does applet recognize the height and  width?</p>
<p>Ans:Using getParameters()  method.</p>
<p> </p>
<p>49)When do you use codebase in applet?</p>
<p>Ans:When the applet class file is not in the same directory, codebase is used.</p>
<p>50)What is the lifecycle of an applet?</p>
<p>Ans:init( ) method        -  Can be called when an applet is first loaded</p>
<p>         start( ) method      -  Can be called each time an applet is started</p>
<p>         paint( ) method     -  Can be called when the applet is minimized or maximized</p>
<p>         stop( ) method      -  Can be used when the browser moves off the applet’s page</p>
<p>destroy( ) method -  Can be called when the browser is finished with the applet</p>
<p>51)How do you set security in applets?</p>
<p>Ans: using  setSecurityManager() method</p>
<p> </p>
<h4>52)          What is an event and what are the models available for event handling?</h4>
<p>Ans: An event is an event object that describes a state of change in a source. In other words, event occurs when an</p>
<p>action is generated, like pressing button, clicking mouse, selecting a list, etc.</p>
<p>There are two types of models for handling events and they are:</p>
<h5>a)             event-inheritance model and b) event-delegation model</h5>
<h4>53)          What are the advantages of the event-delegation  model over the event-inheritance model?</h4>
<p>Ans: The event-delegation model has two advantages over the event-inheritance model. They are:</p>
<p>a)It enables event handling by objects other than the ones that generate the events. This allows a clean separation between a component’s design and its use.</p>
<p>b)It performs much better in applications where many events are generated. This performance improvement is due to the fact that the event-delegation model does not have to be repeatedly process unhandled events as is the case of the event-inheritance.</p>
<p>54)What is source and listener ?</p>
<p>Ans: source : A source is an object that generates an event. This occurs when the internal state of that object changes in some way.</p>
<p>listener : A listener is an object that is notified when an event occurs. It has two major requirements. First, it must have been registered with one or more sources to receive notifications about specific types of events. Second, it must implement methods to receive and process these notifications. </p>
<h4>55)          What is adapter class?</h4>
<p>Ans: An adapter class provides an empty implementation of all methods in an event listener interface. Adapter classes are useful when you want to receive and process only some of the events that are handled by a particular event listener interface. You can define a new class to act listener by extending one of the adapter classes and implementing only those events in which you are interested.For example, the MouseMotionAdapter class has two methods, mouseDragged( )and mouseMoved(). The</p>
<p>signatures of these empty are exactly as defined in the  MouseMotionListener interface. If you are interested in only mouse drag events, then you could simply extend MouseMotionAdapter and implement mouseDragged( ) .</p>
<p>56)What is meant by controls and what are different types of controls in AWT?</p>
<p>Ans: Controls are components that allow a user to interact with your application and the AWT supports the following types of controls:Labels, Push Buttons, Check Boxes, Choice Lists, Lists, Scrollbars, Text Components.These controls are subclasses of Component.</p>
<h4>57)          What is the difference between choice and list?</h4>
<p>Ans: A Choice is displayed in a compact form that requires you to pull it down to see the list of available choices and only one item may be selected from a choice.A List may be displayed in such a way that several list items are visible and it supports the selection of one or more list items.</p>
<h4>58)          What is the difference between scrollbar and scrollpane?</h4>
<p>Ans: A Scrollbar is a Component, but not a Container whereas Scrollpane is a Conatiner and handles its own events and perform its own scrolling.</p>
<p>59)          What is a layout manager and what are different types of layout managers available in java.awt?</p>
<p>Ans: A layout manager is an object that is used to organize components in a container. The different layouts are available are FlowLayout, BorderLayout, CardLayout, GridLayout and GridBagLayout,Null layout/Custom layout</p>
<h4>60)          How are the elements of different layouts organized?</h4>
<p>Ans: FlowLayout: The elements of a FlowLayout are organized in a top to bottom, left to right fashion. BorderLayout: The elements of a BorderLayout are organized at the borders (North, South, East and West) and the center of a container.</p>
<p>CardLayout: The elements of a CardLayout are stacked, on top of the other, like a deck of cards.GridLayout: The elements of a GridLayout are of equal size and are laid out using the square of a grid. GridBagLayout: The elements of a GridBagLayout are organized according to a grid. However, the elements are of different size and may occupy more than one row or column of the grid. In addition, the rows and columns may have different sizes.</p>
<h4>61)           Which containers use a Border layout as their default layout?      </h4>
<p>Ans:  Window, Frame and Dialog classes use a BorderLayout as their layout.</p>
<h4>62)          Which containers use a Flow layout as their default layout?</h4>
<p>Ans: Panel and Applet classes use the FlowLayout as their default layout.</p>
<h4>63)          What are wrapper classes?</h4>
<p>Ans: Wrapper classes are classes that allow primitive types to be accessed as objects.</p>
<h4>64)          What are Vector, Hashtable, LinkedList and Enumeration?</h4>
<p>Ans: Vector : The Vector class provides the capability to implement a growable array of objects.Hashtable : The Hashtable class implements a Hashtable data structure. A Hashtable indexes and stores objects in a dictionary using hash codes as the object’s keys. Hash codes are integer values that identify objects.</p>
<p>LinkedList: Removing or inserting elements in the middle of an array can be done using LinkedList. A</p>
<p>LinkedList stores each object in a separate link whereas an array stores object references in consecutive locations.</p>
<p>Enumeration: An object that implements the Enumeration interface generates a series of elements, one at a time. It has two methods, namely hasMoreElements( ) and nextElement( ). HasMoreElemnts( ) tests if this enumeration has more elements and nextElement method returns successive elements of the series.</p>
<h4>65)          What is the difference between set and list?</h4>
<p>Ans: Set stores elements in an unordered way but does not contain duplicate elements, whereas list stores elements in an ordered way but may contain duplicate elements.</p>
<h4>66)          What is a stream and what are the types of Streams and classes of the Streams?</h4>
<p>Ans: A Stream is an abstraction that either produces or consumes information. There are two types of Streams and they are:</p>
<p>Byte Streams: Provide a convenient means for handling input and output of bytes.Character  Streams:  Provide a convenient means for handling input &amp; output of characters.Byte Streams classes: Are defined by using two abstract classes, namely InputStream and OutputStream.Character Streams classes: Are defined by using two abstract classes, namely Reader and Writer.</p>
<h4>67)          What is the difference between Reader/Writer and InputStream/Output Stream?</h4>
<p>Ans: The Reader/Writer class is character-oriented and the InputStream/OutputStream class is byte-oriented.</p>
<h4>68)          What is an I/O filter?</h4>
<p>Ans: An I/O filter is an object that reads from one stream and writes to another, usually altering the data in some way as it is passed from one stream to another.</p>
<h4>69)          What is serialization and deserialization?</h4>
<p>Ans: Serialization is the process of writing the state of an object to a byte stream.Deserialization is the process of restoring these objects.</p>
<h4>70)          What is JDBC?</h4>
<p>Ans: JDBC is a set of Java API for executing SQL statements. This API consists of a set of  classes and interfaces</p>
<p>to enable programs to write pure Java Database applications.</p>
<p>71)           What are drivers available?</p>
<p>Ans:  a)     JDBC-ODBC Bridge driver</p>
<p>       b)     Native API Partly-Java driver</p>
<p>c)     JDBC-Net Pure Java driver</p>
<p>d)    Native-Protocol Pure Java driver</p>
<h4>72)          What is the difference between JDBC and ODBC?</h4>
<p>Ans:  a)     ODBC is for Microsoft and JDBC is for Java applications.</p>
<p>b)            ODBC can’t be directly used with Java because it uses a C interface.</p>
<h5>c)             ODBC  makes use of pointers which have been removed totally from Java.</h5>
<p>d)    ODBC mixes simple and advanced features together and has complex options for simple queries. But JDBC is designed to keep things simple while allowing advanced capabilities when required.</p>
<p>e)     ODBC requires manual installation of the ODBC driver manager and driver on all client machines. JDBC drivers are written in Java and JDBC code is automatically installable, secure, and portable on all platforms.</p>
<p>f)     JDBC API is a natural Java interface and is built on ODBC. JDBC retains some of the basic features of ODBC.</p>
<h4>73)          What are the types of JDBC Driver Models and explain them?</h4>
<p>Ans: There are two types of  JDBC Driver Models and they are:</p>
<p>a)     Two tier model and b) Three tier modelTwo tier model: In this model, Java applications interact directly with the database. A JDBC driver is required to communicate with the particular database management system that is being accessed. SQL statements are sent to the database and the results are given to user. This model is referred to as client/server configuration where user is the client and the machine that has the database is called as the server.</p>
<p>Three tier model: A middle tier is introduced in this model. The functions of this model are:</p>
<p>a)     Collection of SQL statements from the client and handing it over to the database,</p>
<p>b)    Receiving results from database to the client and</p>
<p>c)     Maintaining control over accessing and updating of the above.</p>
<h4>74)          What are the steps involved for making a connection with a database or how do you connect to a database?</h4>
<p>Ans: a)    Loading the driver : To load the driver, Class.forName( ) method is used.</p>
<p>                     Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);</p>
<p>                When the driver is loaded, it registers itself with the java.sql.DriverManager class as an available  database driver.</p>
<p>b)    Making a connection with database : To open a connection to a given database, DriverManager.getConnection( ) method is used.</p>
<p>Connection con = DriverManager.getConnection (“jdbc:odbc:somedb”, “user”,  “password”);</p>
<p>c)     Executing SQL statements : To execute a SQL query, java.sql.statements class is used.  </p>
<p>createStatement( ) method of Connection to obtain a new Statement object.</p>
<p>Statement stmt  = con.createStatement( );</p>
<p>A query that returns data can be executed using the executeQuery( ) method of  Statement. This method</p>
<p>executes the statement and returns a java.sql.ResultSet that encapsulates the retrieved data:</p>
<p>ResultSet rs = stmt.executeQuery(“SELECT * FROM some table”);</p>
<p>d)    Process the results : ResultSet returns one row at a time. Next( ) method of ResultSet  object can be called to move to the next row.  The getString( ) and getObject( ) methods are used for retrieving  column values:</p>
<p>while(rs.next( ) ) {</p>
<p>String event = rs.getString(“event”);</p>
<p>Object count = (Integer) rs.getObject(“count”);</p>
<h4>75)          What type of driver did you use in project?</h4>
<p>Ans: JDBC-ODBC Bridge driver (is a driver that uses native(C language) libraries and makes calls to an existing</p>
<p>ODBC driver to access a database engine).</p>
<h4>76)          What are the types of statements in JDBC?</h4>
<p>Ans: Statement                &#8212; To be used createStatement() method for executing single SQL statement</p>
<p>PreparedStatement  &#8212; To be used preparedStatement() method for executing same SQL statement over and</p>
<p>over</p>
<p>CallableStatement    &#8212; To be used prepareCall( ) method for multiple SQL statements over and over</p>
<p> </p>
<h4>77)          What is stored procedure?</h4>
<p>Ans: Stored procedure is a group of SQL statements that forms a logical unit and performs a particular task. Stored Procedures are used to encapsulate a set of operations or queries to execute on database. Stored procedures can be compiled and executed with different parameters and results and may have any combination of input/output parameters.</p>
<h4>78)          How to create and call stored procedures?</h4>
<p>Ans:   To create stored procedures: Create procedure procedurename (specify in, out and in out parameters)</p>
<h3>BEGIN</h3>
<p>Any multiple SQL statement;</p>
<p>END;</p>
<p>To call stored procedures:</p>
<p>CallableStatement  csmt  = con.prepareCall(“{call procedure name(?,?)}”);</p>
<p>csmt.registerOutParameter(column no., data type);</p>
<p>csmt.setInt(column no., column name)</p>
<p>csmt.execute( ); </p>
<p>        </p>
<h4>&#8212;&#8212;79)                What is servlet?</h4>
<p>Ans: Servlets are modules that extend request/response-oriented servers, such as java-enabled web servers.For example, a servlet might be responsible for taking data in an HTML order-entry form and applying the business logic used to update a company’s order database.</p>
<h4>80)          What are the classes and  interfaces for servlets?</h4>
<p>Ans: There are two packages in servlets and they are javax.servlet and javax.servlet.http.</p>
<p>Javax.servlet contains:</p>
<p>               Interfaces                       Classes</p>
<p>               Servlet                            Generic Servlet</p>
<p>               ServletRequest               ServletInputStream</p>
<p>               ServletResponse             ServletOutputStream</p>
<p>               ServletConfig                 ServletException</p>
<p>               ServletContext                UnavailableException</p>
<p>SingleThreadModel</p>
<p>Javax.servlet.http contains:</p>
<p>               Interfaces                                 Classes</p>
<p>               HttpServletRequest                  Cookie</p>
<p>               HttpServletResponse                HttpServlet</p>
<p>               HttpSession                               HttpSessionBindingEvent</p>
<p>               HttpSessionContext                  HttpUtils</p>
<p>HttpSessionBindingListener</p>
<h4>81)           What is the difference between an applet and a servlet?</h4>
<p>Ans: a)       Servlets are to servers what applets are to browsers.</p>
<p>   b)    Applets must have graphical user interfaces whereas servlets have no graphical                                                                              user interfaces.</p>
<p> </p>
<h4>82)          What is the difference between doPost and doGet methods?</h4>
<p>Ans: a) doGet() method is used to get information, while doPost( ) method is used for posting information.</p>
<p>b) doGet() requests can’t send large amount of information and is limited to 240-255 characters. However, doPost( )requests passes all of its data, of unlimited length.</p>
<p>c)     A doGet( ) request is appended to the request URL in a query string and this allows the exchange is visible to the client, whereas a doPost() request passes directly over the  socket connection as part of its HTTP request body and the exchange are invisible to the client.</p>
<h4>83)          What is the life cycle of a servlet?</h4>
<p>Ans: Each Servlet has the same life cycle:</p>
<p>a)     A server loads and initializes the servlet by init( ) method.</p>
<p>b)    The servlet  handles zero or more client’s requests through service( ) method.</p>
<p>c)     The server removes the servlet through destroy() method.        </p>
<h4>84)          Who is loading the init() method of servlet?</h4>
<p>Ans: Web server</p>
<h4>85)          What are the different servers available for developing and deploying Servlets?</h4>
<p>Ans:  a)    Java Web Server</p>
<p>      b)      JRun</p>
<p>g)     Apache Server</p>
<p>h)     Netscape Information Server</p>
<p>i)      Web Logic       </p>
<h4>86)          How many ways can we track client  and what are they?</h4>
<p>Ans: The servlet API provides two ways to track client state and they are:</p>
<h5>a)             Using Session tracking and b) Using Cookies.</h5>
<h4>87)          What is session tracking and how do you track a user session in servlets?</h4>
<p>Ans: Session tracking is a mechanism that servlets use to maintain state about a series requests from the same user across some period of time. The methods used for session tracking are:</p>
<p>a)     User Authentication &#8211; occurs when a web server restricts access to some of its resources to only those clients that log in using a recognized username and password</p>
<p>b)    Hidden form fields &#8211; fields are added to an HTML form that are not displayed in the client’s browser. When the form containing the fields is submitted, the fields are sent back to the server</p>
<p>c)     URL rewriting &#8211; every URL that the user clicks on is dynamically modified or rewritten to include extra information. The extra information can be in the form of extra path information, added parameters or some custom, server-specific URL change.</p>
<p>d)    Cookies &#8211; a bit of information that is sent by a web server to a browser and which can later be read back from that browser.</p>
<p>e)     HttpSession- places a limit on the number of sessions that can exist in memory. This limit is set in the session.maxresidents property</p>
<h4>88)          What is Server-Side Includes (SSI)?</h4>
<p>Ans: Server-Side Includes allows embedding servlets within HTML pages using a special servlet tag. In many servlets that support servlets, a page can be processed by the server to include output from servlets at certain points inside the HTML page. This is accomplished using a special internal SSINCLUDE, which processes the servlet tags. SSINCLUDE servlet will be invoked whenever a file with an. shtml extension is requested. So HTML files that include  server-side includes must be stored with an .shtml extension.</p>
<h4>89)          What are cookies and how will you use them?</h4>
<p>Ans: Cookies are a mechanism that a servlet uses to have a client hold a small amount of state-information associated with the user.</p>
<p>a)             Create a cookie with the Cookie constructor:</p>
<p>public Cookie(String name, String value)</p>
<p>b)    A servlet can send a cookie to the client by passing a Cookie object to the addCookie() method of HttpServletResponse:public void HttpServletResponse.addCookie(Cookie cookie)</p>
<p>c)     A servlet retrieves cookies by calling the getCookies() method of HttpServletRequest:</p>
<p>public Cookie[ ] HttpServletRequest.getCookie( ).</p>
<h4>90)          Is it possible to communicate from an applet to servlet and how many ways and how?</h4>
<p>Ans: Yes, there are three ways to communicate from an applet to servlet and they are:</p>
<p>a)     HTTP Communication(Text-based and object-based)</p>
<p>b)    Socket Communication</p>
<p>c)     RMI Communication</p>
<p>(You can say, by using URL object open the connection to server and get the InputStream from</p>
<p>URLConnection  object).</p>
<p>Steps involved for applet-servlet communication:</p>
<h5>1)             Get the server URL.</h5>
<p>URL url = new URL();</p>
<h5>2)            Connect to the host</h5>
<p>URLConnection Con = url.openConnection();</p>
<h5>3)            Initialize the connection</h5>
<p>Con.setUseCatches(false):</p>
<p>Con.setDoOutput(true);</p>
<p>Con.setDoInput(true);</p>
<h5>4)            Data will be written to a byte array buffer so that we can tell the server the length of the data.</h5>
<p>ByteArrayOutputStream byteout  = new ByteArrayOutputStream();</p>
<h5>5)            Create the OutputStream to be used to write the data to the buffer.</h5>
<p>DataOutputStream out = new DataOutputStream(byteout);</p>
<h4>91)           What is connection pooling?</h4>
<p>Ans: With servlets, opening a database connection is a major bottleneck because we are creating and tearing down a new connection for every page request and the time taken to create connection will be more. Creating a connection pool is an ideal approach for a complicated servlet. With a connection pool, we can duplicate only the resources we need to duplicate rather than the entire servlet. A connection pool can also  intelligently manage the size of the pool and make sure each connection remains valid. A number of connection pool packages are currently available. Some like DbConnectionBroker are freely available from Java Exchange Works by creating an object that dispenses connections and connection Ids on request.The ConnectionPool class maintains a Hastable, using Connection objects as keys and Boolean values as stored values. The Boolean value indicates whether a connection is in use or not. A program calls getConnection( ) method of the ConnectionPool for getting  Connection object it can use; it calls returnConnection( ) to give the connection back to the pool.</p>
<h4>92)          Why should we go for interservlet communication?</h4>
<p>Ans: Servlets running together in the same server communicate with each other in several ways.The three major reasons to use interservlet communication are:</p>
<p>a)     Direct servlet manipulation &#8211; allows to gain access to the other currently loaded servlets and perform certain tasks (through the ServletContext object)</p>
<p>b)    Servlet reuse &#8211; allows the servlet to reuse the public methods of another servlet.</p>
<p>c)     Servlet collaboration &#8211; requires to communicate with each other by sharing specific information (through method invocation)</p>
<h4>93)          Is it possible to call servlet with parameters in the URL?</h4>
<p>Ans: Yes. You can call a servlet with parameters in the syntax as (?Param1 = xxx || m2 = yyy).</p>
<h4>94)          What is Servlet chaining?</h4>
<p>Ans: Servlet chaining is a technique in which two or more servlets can cooperate in servicing a single request.In servlet chaining, one servlet’s output is piped to the next servlet’s input. This process continues until the last servlet is reached. Its output is then sent back to the client.</p>
<h4>95)          How do servlets handle multiple simultaneous requests?</h4>
<p>Ans: The server has multiple threads that are available to handle requests. When a request comes in, it is assigned to a thread, which calls a service method (for example: doGet(), doPost( ) and service( ) ) of the servlet. For this reason, a single servlet object can have its service methods called by many threads at once.</p>
<h4>96)          What is the difference between TCP/IP and UDP?</h4>
<p>Ans: TCP/IP is a two-way communication between the client and the server and it is a reliable and there is a confirmation regarding reaching the message to the destination. It is like a phone call. UDP is a one-way communication only between the client and the server and it is not a reliable and there is no confirmation regarding reaching the message to the destination. It is like a postal mail.</p>
<h4>97)          What is Inet address?</h4>
<p>Ans: Every computer connected to a network has an IP address. An IP address is a number that uniquely identifies each computer on the Net. An IP address is a 32-bit number.</p>
Posted in Java_FAQ&#039;s  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/meherchilakalapudi.wordpress.com/678/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/meherchilakalapudi.wordpress.com/678/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/meherchilakalapudi.wordpress.com/678/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/meherchilakalapudi.wordpress.com/678/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/meherchilakalapudi.wordpress.com/678/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/meherchilakalapudi.wordpress.com/678/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/meherchilakalapudi.wordpress.com/678/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/meherchilakalapudi.wordpress.com/678/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/meherchilakalapudi.wordpress.com/678/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/meherchilakalapudi.wordpress.com/678/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meherchilakalapudi.wordpress.com&blog=5609619&post=678&subd=meherchilakalapudi&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://meherchilakalapudi.wordpress.com/2009/07/16/java-faqs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/08b95690d7ec04fc15567da451c993a3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">meherchilakalapudi</media:title>
		</media:content>
	</item>
		<item>
		<title>Save Earth!!! Save Trees!!! Save Water!!!</title>
		<link>http://meherchilakalapudi.wordpress.com/2009/06/04/save-earth-save-trees-save-water/</link>
		<comments>http://meherchilakalapudi.wordpress.com/2009/06/04/save-earth-save-trees-save-water/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 13:52:08 +0000</pubDate>
		<dc:creator>meherchilakalapudi</dc:creator>
				<category><![CDATA[Save Environment]]></category>

		<guid isPermaLink="false">http://meherchilakalapudi.wordpress.com/?p=676</guid>
		<description><![CDATA[The Hydrosphere
The hydrosphere is often called the &#8220;water sphere&#8221; as it includes all the earth&#8217;s water found in streams, lakes, the soil, groundwater, and in the air. The hydrosphere interacts with, and is influenced by, all the other earth spheres. The water of the hydrosphere is distributed among several different stores found in the other [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meherchilakalapudi.wordpress.com&blog=5609619&post=676&subd=meherchilakalapudi&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h1>The Hydrosphere</h1>
<p>The <em><strong>hydrosphere</strong></em> is often called the &#8220;water sphere&#8221; as it includes all the earth&#8217;s water found in streams, lakes, the soil, groundwater, and in the air. The hydrosphere interacts with, and is influenced by, all the other earth spheres. The water of the hydrosphere is distributed among several different stores found in the other spheres. Water is held in oceans, lakes and streams at the surface of the earth. Water is found in vapor, liquid and solid states in the atmosphere. The biosphere serves as an interface between the spheres enabling  water to move between the hydrosphere, lithosphere and atmosphere as is accomplished by plant transpiration. The <a href="http://www.uwsp.edu/geo/faculty/ritter/glossary/h_k/hydrologic_cycle.html" target="_blank"><strong><em>hydrologic cycle</em></strong></a> traces the movement of water and energy between these various stores and spheres.</p>
<p> </p>
<p style="text-align:center;"><a href="http://sailjamehra.files.wordpress.com/2009/06/1.png"><img title="1" src="http://sailjamehra.files.wordpress.com/2009/06/1.png?w=306&#038;h=342" alt="1" width="306" height="342" /></a></p>
<p align="center"> </p>
<p>                        <strong>Figure TH.1 Earth Spheres/Systems</strong></p>
<h2>Distribution of water</h2>
<p>The world&#8217;s oceans contain 97% of the water in the hydrosphere, most of which is salt water. Ice caps, like that found covering Antarctica, and glaciers that occupy high alpine locations, compose a little less than 2% of all water found on earth. Seemingly a small amount, the water stored as ice in glaciers would have a great impact on the environment if it were to melt into a liquid. One fear is that global warming will cause the melting and collapse of large ice sheets resulting in sea level rise. Rising sea levels could devastate coastal cities, displace millions of people, and wreak havoc on freshwater systems and habitats.</p>
<p style="text-align:center;"><a href="http://sailjamehra.files.wordpress.com/2009/06/2.png"><img title="2" src="http://sailjamehra.files.wordpress.com/2009/06/2.png?w=228&#038;h=174" alt="2" width="228" height="174" /></a></p>
<p> </p>
<p align="center"> </p>
<p align="center"><strong>Figure TH.2 The largest store of water is the ocean which delivers water through evaporation each day</strong></p>
<p> </p>
<p>Water beneath the surface comprises the next largest store of water. Groundwater and soil water together make up about .5% of all water (by volume). There is a difference between ground water and soil water. <em><strong>Soil water</strong></em> is the water held in pore spaces between soil particles. Soil pore spaces usually are partially void of water most of the time but fill with water after a rain storm. <em><strong>Groundwater</strong></em>, on the other hand, is found where earth materials are saturated throughout the year. That is, the pore spaces are always occupied with water. Both soil and groundwater are very important sources of water. Soil water is available for plants to extract and use. Groundwater is an important source of water for irrigation and drinking water supplies.</p>
<p>Above the surface water is found stored in streams, rivers and lakes. One might expect that given the large rivers that flow across the earth and the huge numbers of lakes that this store would be rather large. Instead, streams, rivers and lakes only comprise .02% of all water in the earth system. In the atmosphere, only about .0001 % of the water in the hydrosphere is found.</p>
<p> </p>
<p align="center">The Hydrologic Cycle</p>
<p align="center"><a href="http://sailjamehra.files.wordpress.com/2009/06/3.png"><img title="3" src="http://sailjamehra.files.wordpress.com/2009/06/3.png?w=588&#038;h=582" alt="3" width="588" height="582" /></a></p>
<p> </p>
<p> </p>
<p> </p>
<p>        The Hydrologic Cycle involves the continuous circulation of water in the Earth-atmosphere system. Of the many processes involved in the hydrologic cycle, the most important are</p>
<ul>
<li>evaporation,</li>
<li>transpiration,</li>
<li>condensation,</li>
<li>precipitation, and</li>
<li>runoff</li>
</ul>
<p>The hydrologic cycle consists of inflows, outflows, and storage. Inflows add water to the different parts of the hydrologic system, while outflows remove water. Storage is the retention of water by parts of the system. Because water movement is cyclical, an inflow for one part of the system is an outflow for another. Looking at an aquifer as an example, percolation of water into the ground is an inflow to the aquifer. Discharge of ground water from the aquifer to a stream is an outflow (also an inflow for the stream). Over time, if inflows to the aquifer are greater than its outflows, the amount of water stored in the aquifer will increase. Conversely, if the inflows to the aquifer are less than the outflows, the amount of water stored decreases. Inflows and outflows can occur naturally or result from human activity.</p>
<p align="center"> </p>
<p><strong>Evaporation</strong><br />
Evaporation is the change of state in a substance from a liquid to a gas. In meteorology, the substance we are concerned about the most is water. For evaporation to take place, energy is required. The energy can come from any source; the sun, the atmosphere, the earth, or objects on the earth such as humans.</p>
<p>Everyone has experienced evaporation personally. When the body heats up due to the air temperature or through exercise, the body sweats, secreting water onto the skin. The purpose is to cause the body to use its heat to evaporate the liquid, thereby removing heat and cooling the body. It is the same effect that can be seen when you step out of a shower or swimming pool. The coolness you feel is from the removing of bodily heat to evaporate the water on your skin.</p>
<p><strong>Transpiration</strong><br />
Transpiration is the evaporation of water from plants through stomata. Stomata are small openings found on the underside of leaves that are connected to vascular plant tissues. In most plants, transpiration is a passive process largely controlled by the humidity of the atmosphere and the moisture content of the soil. <em>Of the transpired water passing through a plant only 1% is used in the growth process of the plant. The remaining 99% is passed into the atmosphere.</em></p>
<p><strong>Condensation</strong><br />
Condensation is the process whereby water vapor in the atmosphere is returned to its original liquid state. In the atmosphere, condensation may appear as clouds, fog, mist, dew or frost, depending upon the physical conditions of the atmosphere. Condensation is not a matter of one particular temperature but of a difference between two temperatures; the air temperature and the dewpoint temperature.</p>
<p> </p>
<p><strong>Precipitation</strong><br />
Precipitation is the result when the tiny condensation particles grow too large, through collision and coalesce, for the rising air to support, and thus fall to the earth.<br />
<strong>Runoff</strong><br />
Runoff occurs when there is excessive precipitation and the ground is saturated (cannot absorb anymore water). This runoff flows into streams and rivers and eventually back into the sea.</p>
<p>Evaporation of this runoff into the atmosphere begins the hydrologic cycle over again. Some of the water perculates into the soil and into the ground water only to be drawn into plants again for transpiration to take place.</p>
<p align="center"> </p>
<p> </p>
<p> </p>
<p>                                             <strong>lithosphere</strong></p>
<p><strong> </strong></p>
<p>The <strong>lithosphere</strong><sup><a href="http://simple.wikipedia.org/wiki/#cite_note-0">[1]</a></sup> is the solid <a title="wiktionary:shell" href="http://en.wiktionary.org/wiki/shell">shell</a> of a rocky <a title="Planet" href="http://simple.wikipedia.org/wiki/Planet">planet</a> called earth. That means the <a title="Crust (geology)" href="http://simple.wikipedia.org/wiki/Crust_(geology)">crust</a> and the upper part of the <a title="Mantle (geology)" href="http://simple.wikipedia.org/wiki/Mantle_(geology)">mantle</a> which is joined to the crust (see picture on the right).</p>
<p>Under the lithosphere there is the <a title="Asthenosphere" href="http://simple.wikipedia.org/wiki/Asthenosphere">asthenosphere</a>, the weaker, hotter, and deeper part of the upper mantle.</p>
<p> </p>
<p><a title="Enlarge" href="http://simple.wikipedia.org/wiki/Image:Plates_tect2_en.svg"></a></p>
<p> </p>
<p>The lithosphere is the surface layer of the fluid parts of the Earth&#8217;s <a title="Convection" href="http://simple.wikipedia.org/wiki/Convection">convection</a> system, therefore it thickens over time. It is broken up into pieces called <a title="Plate" href="http://simple.wikipedia.org/wiki/Plate">plates</a> (shown in picture on the left), which move independently relative to one another. This movement of lithospheric plates is described as <a title="Plate tectonics" href="http://simple.wikipedia.org/wiki/Plate_tectonics">plate tectonics</a>.</p>
<p>The division of Earth&#8217;s outer layers into lithosphere and asthenosphere should not be confused with the chemical subdivision of the outer Earth into <a title="Mantle (geology)" href="http://simple.wikipedia.org/wiki/Mantle_(geology)">mantle</a>, and <a title="Crust (geology)" href="http://simple.wikipedia.org/wiki/Crust_(geology)">crust</a>. All crust is in the lithosphere, but lithosphere generally contains more mantle than crust.<br />
There are two types of lithosphere:</p>
<ul>
<li>Oceanic lithosphere, which is associated with <a title="Oceanic crust" href="http://simple.wikipedia.org/wiki/Oceanic_crust">Oceanic crust</a></li>
<li>Continental lithosphere, which is associated with <a title="Continental crust" href="http://simple.wikipedia.org/wiki/Continental_crust">Continental crust</a></li>
</ul>
<p>Oceanic lithosphere is typically about 50-100 km thick (but beneath the <a title="Mid-ocean ridge (not yet written)" href="http://simple.wikipedia.org/w/index.php?title=Mid-ocean_ridge&amp;action=edit&amp;redlink=1">mid-ocean ridges</a> is no thicker than the crust). Continental lithosphere is thicker (about 150 km). It consists of about 50 km of crust and 100 km or more of uppermost mantle.</p>
<p>Oceanic lithosphere consists mainly of <a title="Mafic" href="http://simple.wikipedia.org/wiki/Mafic">mafic</a> crust and <a title="Ultramafic (not yet written)" href="http://simple.wikipedia.org/w/index.php?title=Ultramafic&amp;action=edit&amp;redlink=1">ultramafic</a> mantle and is denser than continental lithosphere, for which the mantle is associated with crust made of <a title="Felsic (not yet written)" href="http://simple.wikipedia.org/w/index.php?title=Felsic&amp;action=edit&amp;redlink=1">felsic</a> rocks. The crust is distinguished from the upper mantle by the change in chemical composition that takes place at the <a title="Moho discontinuity (not yet written)" href="http://simple.wikipedia.org/w/index.php?title=Moho_discontinuity&amp;action=edit&amp;redlink=1">Moho discontinuity</a>. Oceanic lithosphere thickens as it ages and moves away from the mid-ocean ridge. This thickening occurs by conductive cooling, which converts hot asthenosphere into lithospheric mantle, and causes the oceanic lithosphere to become increasingly dense with age. Oceanic lithosphere is less dense than asthenosphere for a few tens of millions of years, but after this becomes increasingly denser than asthenosphere. The gravitational instability of mature oceanic lithosphere has the effect that at <a title="Subduction zone (not yet written)" href="http://simple.wikipedia.org/w/index.php?title=Subduction_zone&amp;action=edit&amp;redlink=1">subduction zones</a> the oceanic lithosphere invariably sinks underneath the overriding lithosphere, which can be oceanic or continental. New oceanic lithosphere is constantly being produced at mid-ocean ridges and is recycled back to the mantle at subduction zones. As a result, oceanic lithosphere is much younger than continental lithosphere: the oldest oceanic lithosphere is about 170 million years old, while parts of the continental lithosphere are billions of years old.</p>
<p>Another distinguishing characteristic of the lithosphere is its flow properties. Under the influence of the low-intensity, long-term stresses that drive <a title="Plate tectonic (not yet written)" href="http://simple.wikipedia.org/w/index.php?title=Plate_tectonic&amp;action=edit&amp;redlink=1">plate tectonic</a> motions, the lithosphere responds essentially as a rigid shell and thus deforms primarily through brittle failure, whereas the <a title="Asthenosphere" href="http://simple.wikipedia.org/wiki/Asthenosphere">asthenosphere</a> (the layer of the mantle below the lithosphere) is heat-softened and accommodates <a title="Strain" href="http://simple.wikipedia.org/wiki/Strain">strain</a> through <a title="Plasticity (physics) (not yet written)" href="http://simple.wikipedia.org/w/index.php?title=Plasticity_(physics)&amp;action=edit&amp;redlink=1">plastic</a> deformation</p>
<p> </p>
<p style="text-align:center;"><a href="http://sailjamehra.files.wordpress.com/2009/06/4.png"><img title="4" src="http://sailjamehra.files.wordpress.com/2009/06/4.png?w=324&#038;h=486" alt="4" width="324" height="486" /></a></p>
<p style="text-align:center;"><strong> </strong></p>
Posted in Save Environment  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/meherchilakalapudi.wordpress.com/676/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/meherchilakalapudi.wordpress.com/676/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/meherchilakalapudi.wordpress.com/676/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/meherchilakalapudi.wordpress.com/676/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/meherchilakalapudi.wordpress.com/676/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/meherchilakalapudi.wordpress.com/676/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/meherchilakalapudi.wordpress.com/676/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/meherchilakalapudi.wordpress.com/676/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/meherchilakalapudi.wordpress.com/676/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/meherchilakalapudi.wordpress.com/676/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meherchilakalapudi.wordpress.com&blog=5609619&post=676&subd=meherchilakalapudi&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://meherchilakalapudi.wordpress.com/2009/06/04/save-earth-save-trees-save-water/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/08b95690d7ec04fc15567da451c993a3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">meherchilakalapudi</media:title>
		</media:content>

		<media:content url="http://sailjamehra.files.wordpress.com/2009/06/1.png" medium="image">
			<media:title type="html">1</media:title>
		</media:content>

		<media:content url="http://sailjamehra.files.wordpress.com/2009/06/2.png" medium="image">
			<media:title type="html">2</media:title>
		</media:content>

		<media:content url="http://sailjamehra.files.wordpress.com/2009/06/3.png" medium="image">
			<media:title type="html">3</media:title>
		</media:content>

		<media:content url="http://sailjamehra.files.wordpress.com/2009/06/4.png" medium="image">
			<media:title type="html">4</media:title>
		</media:content>
	</item>
		<item>
		<title>KNOW ABOUT &#8211; BIOMETRIC AUTHENTICATION SYSTEM</title>
		<link>http://meherchilakalapudi.wordpress.com/2009/05/30/know-about-biometric-authentication-system/</link>
		<comments>http://meherchilakalapudi.wordpress.com/2009/05/30/know-about-biometric-authentication-system/#comments</comments>
		<pubDate>Sat, 30 May 2009 14:06:28 +0000</pubDate>
		<dc:creator>meherchilakalapudi</dc:creator>
				<category><![CDATA[KNOW ABOUT - BIOMETRIC AUTHENTICATION SYSTEM]]></category>

		<guid isPermaLink="false">http://meherchilakalapudi.wordpress.com/?p=674</guid>
		<description><![CDATA[    
BIOMETRIC AUTHENTICATION SYSTEM
 
           
 
 
 
BIOMETRIC AUTHENTICATION SYSTEM
 
 
      A biometric authentication system provides automatic identification of an individual based on a unique feature or characteristic possessed by the individual. Iris recognition is regarded as the most reliable and accurate biometric authentication system available. Most commercial iris recognition systems use patented algorithms developed by Daugman, and these [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meherchilakalapudi.wordpress.com&blog=5609619&post=674&subd=meherchilakalapudi&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><strong>    </strong></p>
<h4 style="text-align:center;">BIOMETRIC AUTHENTICATION SYSTEM</h4>
<p><span style="text-decoration:underline;"><strong> </strong></span></p>
<p style="text-align:center;"><span style="text-decoration:underline;"><strong>           </strong></span></p>
<p style="text-align:center;"><span style="text-decoration:underline;"><strong> </strong><a href="http://sailjamehra.files.wordpress.com/2009/05/12.png"><strong><img title="1" src="http://sailjamehra.files.wordpress.com/2009/05/12.png?w=445&#038;h=240" alt="1" width="445" height="240" /></strong></a></span></p>
<p><span style="text-decoration:underline;"><strong> </strong></span></p>
<p><span style="text-decoration:underline;"><strong> </strong></span></p>
<p style="text-align:center;"><span style="text-decoration:underline;"><strong>BIOMETRIC AUTHENTICATION SYSTEM</strong></span></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong>      A biometric authentication system provides automatic identification of an individual based on a unique feature or characteristic possessed by the individual. Iris recognition is regarded as the most reliable and accurate biometric authentication system available. Most commercial iris recognition systems use patented algorithms developed by Daugman, and these algorithms are able to produce perfect recognition rates. However, published results have usually been produced under favorable conditions, and there have been no independent trials of the technology. The paper involves the development of an iris recognition system in order to verify both the uniqueness of the human iris and also its performance as a biometric even at abnormal conditions. For determining the recognition performance of the system two digitized grayscale eye images are used. The iris recognition system consists of an automatic segmentation system that is based on the Hough transform, and is able to localize the circular iris and pupil region, occluding eyelids and eyelashes, and reflections. The extracted iris region is then normalized into a rectangular block with constant dimensions to account for imaging inconsistencies. Finally, the phase data from 1D Log-Gabor filters is extracted and quantised to encode the unique pattern of the iris into a bitwise biometric template. The Hamming distance is employed for classification of iris templates, and two templates are found to match if a test of statistical independence is failed. Based on Hamming distance the threshold is set between false acceptance and false rejection. Therefore, iris recognition is shown to be a reliable and accurate biometric technology.</strong></p>
<p><strong>INTRODUCTION</strong></p>
<p><strong>BIOMETRIC TECHNOLOGY:</strong></p>
<p><strong>    A biometric system provides automatic recognition of an individual based on some sort of unique feature or characteristic possessed by the individual. Biometric systems have been developed based on fingerprints, facial features, voice, hand geometry, handwriting, the retina, and the one presented in this thesis, the iris. Biometric systems work by first capturing a sample of the feature, such as recording a digital sound signal for voice recognition, or taking a digital colour image for face recognition. The sample is then transformed using some sort of mathematical function into a biometric template. The biometric template will provide a normalized, efficient and highly discriminating representation of the feature, which can then be objectively compared with other templates in order to determine identity. Most biometric systems allow two modes of operation. An enrolment mode for adding templates to a database, and an identification mode, where a template is created for an Individual and then a match is searched for in the database of pre-enrolled templates. A good biometric is characterized by use of a feature that is highly unique, therefore the chance of any two people having the same characteristic will be minimal stable, so that the feature does not change over time, and be easily captured in order to provide convenience to the user, and prevent misrepresentation of the feature.</strong></p>
<p><strong>THE HUMAN IRIS</strong></p>
<p><strong>The iris is a thin circular diaphragm, which lies between the cornea and the lens of the human eye. A front-on view of the iris is shown in Fig.1. The iris is perforated close to its centre by a circular aperture known as the pupil. The function of the iris is to control the amount of light entering through the pupil, and this is done by the sphincter and the dilator muscles, which adjust the size of the pupil. The average diameter of the iris is 12 mm, and the pupil size can vary from 10% to 80% of the iris diameter. The iris consists of a number of layers, the lowest being the epithelium layer, which contains dense pigmentation cells. The stromal layer lies above the epithelium layer, and contains blood vessels, pigment cells and the two iris muscles. The density of stromal pigmentation determines the colour of the iris. The externally visible surface of the multilayered iris contains two zones, which often differ in colour.</strong></p>
<p><strong>     They are an outer ciliary zone and an inner pupillary zone, and these two zones are divided by the collarette, which appears as a zigzag pattern. Formation of the iris begins during the third month of embryonic life. The unique pattern on the surface of the iris is formed during the first year of life, and pigmentation of the stroma takes place for the first few years. Formation of the unique patterns of the iris is random and not related to any genetic factors. The only characteristic that is dependent on genetics is the pigmentation of the iris, which determines its color. Due to the epigenetic nature of iris patterns, the two eyes of an individual contain completely independent iris patterns, and identical twins possess uncorrelated iris patterns, and identical twins possess uncorrelated iris patterns.</strong></p>
<p><strong> </strong></p>
<p><strong>IRIS RECOGNITION</strong></p>
<p style="text-align:center;"><strong> </strong><a href="http://sailjamehra.files.wordpress.com/2009/05/2.png"><strong><img title="2" src="http://sailjamehra.files.wordpress.com/2009/05/2.png?w=446&#038;h=270" alt="2" width="446" height="270" /></strong></a><strong>              </strong></p>
<p><strong> </strong></p>
<p><strong>The iris is an externally visible, yet protected organ whose unique epigenetic pattern remains stable throughout adult life. This characteristic makes it very attractive for use as a biometric for identifying individuals. Image processing                                                                 </strong></p>
<p><strong>Fig.1: A Front-on-view on the human eye     techniques can be employed to extract the unique iris pattern from a digitized image of the eye, and encode it into  a  biometric template, which can be stored in a database. This biometric template contains an objective mathematical representation of the unique information stored in the iris, and allows comparisons to be made between templates. When a person is to be identified by an iris recognition system, his eye is first photographed, and then a template created forthe iris region. This template is then compared with the other templates stored in a data base until a matching template is found and the person is identified. If no match is found the person remains unidentified. The Daugman system has been tested under numerous studies, all reporting a zero failure rate. The Daugman system is claimed to be able to perfectly identify an individual, given millions of possibilities.   Fig: 2: Block Diagram of Iris Recognition System.</strong></p>
<p style="text-align:center;"><a href="http://sailjamehra.files.wordpress.com/2009/05/3.png"><strong><img title="3" src="http://sailjamehra.files.wordpress.com/2009/05/3.png?w=552&#038;h=387" alt="3" width="552" height="387" /></strong></a></p>
<p><strong>  However, there have been no independent trials of the technology, and source code for systems is not available. Also, there is a lack of publicly available datasets for testing and research, and the test results published have usually been produced using carefully imaged irises under favorable conditions.</strong></p>
<p><strong> SEGMENTATION</strong></p>
<p><strong>     The first stage of iris recognition is to isolate the actual iris region in a digital eye image. Two circles can approximate the iris region, shown in Fig.3, one for the iris/sclera boundary and another, for the iris/pupil boundary (interior to the first). The eyelids and eyelashes normally occlude the upper and lower parts of the iris region. Also, specular reflections can occur within the iris region corrupting the iris pattern. A technique is required to isolate and exclude these artifacts as well as to locate the circular iris region. The success of segmentation depends on the imaging quality of eye images. Also, persons with darkly pigmented irises will present very low contrast between the pupil and iris region if imaged under natural light, making segmentation more difficult. The segmentation stage is critical to the success of an iris recognition system, since data that is falsely represented, as iris pattern data will corrupt the biometric templates generated, resulting in poor recognition rates. </strong></p>
<p><strong>IMPLEMENTATION</strong></p>
<p><strong>Here the circular Hough transform was used for detecting the iris and pupil boundaries. This involves employing Canny edge detection to generate an edge map. Gradients were biased in the vertical direction for the outer iris/sclera boundary. Vertical and horizontal gradients were weighted equally for the inner iris/pupil boundary. The range of radius values to search for was set manually, values of the iris radius range from 90 to 150 pixels, while the pupil radius ranges from 28 to 75 pixels. In order to make the circle detection process more efficient and accurate, the Hough transform for the iris/sclera boundary was performed first, then the Hough transform for the iris/pupil boundary was performed within the iris region, instead of the whole eye region, since the pupil is always within the iris region. After this process was complete, six parameters are stored, the radius, and x &amp; y center coordinates for both circles. A line is fit to the upper and lower eyelid using the linear Hough transform isolated eyelids. Then a second horizontal line is drawn, which intersects with the first line at the iris edge that is closest to the pupil. This process is illustrated in Fig.3 and is done for both the top and bottom eyelids. The second horizontal line allows maximum isolation of eyelid regions. Canny edge detection is used to create an edge map, and only horizontal gradient information is taken. The linear Hough transform is implemented using the MATLAB Radon transform, which is a form of the Hough transform. If the maximum in the Hough space is lower than a set threshold, then no line is fitted, since this corresponds to non-occluding eyelids.</strong></p>
<p><strong>Also, the lines are restricted to lie exterior to the pupil region, and interior to the iris region. A linear Hough transform has the advantage over its parabolic version, because it has lesser parameters to deduce, making the process lesser computationally demanding.</strong></p>
<p style="text-align:center;"><strong> </strong><a href="http://sailjamehra.files.wordpress.com/2009/05/4.png"><strong><img title="4" src="http://sailjamehra.files.wordpress.com/2009/05/4.png?w=558&#038;h=306" alt="4" width="558" height="306" /></strong></a></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong>                           Fig.3 Stages of segmentation with eye image.</strong></p>
<p><strong>For isolating eyelashes, a simple thresholding technique was used, since analysis reveals that eyelashes are quite dark when compared to the rest of the eye image. Although, the eyelashes are quite dark compared with the surrounding eyelid region, areas of the iris region are equally dark due to the imaging conditions. Therefore thresholding to isolate eyelashes would also remove important iris region features, making this technique infeasible.</strong></p>
<p><strong>NORMALISATION</strong></p>
<p><strong>     Once the iris region is successfully segmented from an eye image, the next step is to transform the iris region so that it has fixed dimensions in order to allow comparisons. The dimensional inconsistencies between eye images are mainly due to the stretching of the iris caused by pupil dilation from varying levels of illumination. Other sources of inconsistency include, varying imaging distance, rotation of the camera, head tilt, and rotation of the eye within the eye socket. The normalisation process will produce iris regions, which have the same constant dimensions, so that two photographs of the same iris under different conditions will have characteristic features at the same spatial location.</strong></p>
<p><strong>IMPLEMENTATION</strong></p>
<p><strong>For normalisation of iris regions, a technique based on Daugman’s rubber sheet model is employed. The centre of the pupil is considered as the reference point, and radial vectors pass through the iris region, as shown in Fig. 4. A number of data points are selected along each radial line and this is defined as the radial resolution. The number of radial lines going around the iris region is defined as the angular resolution. Since the pupil can be non-concentric to the iris, a remapping formula is needed to rescale points depending on the angle around the circle.</strong></p>
<p><strong>This is given by</strong></p>
<p><strong> </strong></p>
<p><strong>With </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong>where displacement of center of the pupil relative to center of the iris is given by Q<sub>x</sub>, Q<sub>y</sub> and r<sup>2</sup> the distance between the edge of the pupil and edge of the iris at an angle, q around the region, and r<sub>1</sub> is the radius of the iris. The remapping formula gives the radius of the iris region as a function of the angle q same number of points are chosen along each radial line, irrespective of how narrow or wide the radius is at a particular angle. Backtracking to find the Cartesian coordinates of data points from the radial and angular position in the normalized pattern creates the normalized pattern. From the iris region, normalization produces a 2D array with horizontal dimensions of angular resolution and vertical dimensions of radial resolution.</strong></p>
<p><strong>Another 2D array is created for marking reflections, eyelashes, and eyelids detected in the segmentation stage. In order to prevent non-iris region data from corrupting the normalized representation, data points that occur along the pupil border or the iris border are discarded. As in Daugman’s rubber sheet model, removing rotational inconsistencies is performed at the matching stage.</strong></p>
<p style="text-align:center;"><strong> </strong><a href="http://sailjamehra.files.wordpress.com/2009/05/5.png"><strong><img title="5" src="http://sailjamehra.files.wordpress.com/2009/05/5.png?w=504&#038;h=246" alt="5" width="504" height="246" /></strong></a></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p align="center"><strong>Fig.4. Outline of the Normalisation process.</strong></p>
<p><strong>FEATURE ENCODING AND MATCHING</strong></p>
<p><strong>    In order to provide accurate recognition of individuals, the most discriminating information present in an iris pattern must be extracted. Only the significant features of the iris must be encoded so that comparisons between templates can be made. The template that is generated in the feature encoding process will also need a corresponding matching metric, which gives a measure of similarity between two iris templates. This metric should give one range of values known as intra-class comparisons, when comparing templates generated from the same eye, and another range of values, known as inter-class comparisons when comparing templates created from different irises. These two cases should give distinct and separate values, so that a decision can be made with high confidence as to whether two templates are from the same iris, or from two different irises.</strong></p>
<p><strong>IMPLEMENTATION</strong></p>
<p><strong>  FEATURE ENCODING</strong></p>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<h3>Fig.5. Phase Quantisation</h3>
<p style="text-align:center;"><a href="http://sailjamehra.files.wordpress.com/2009/05/6.png"><strong><img title="6" src="http://sailjamehra.files.wordpress.com/2009/05/6.png?w=445&#038;h=213" alt="6" width="445" height="213" /></strong></a></p>
</td>
</tr>
</tbody>
</table>
<p><strong>   Feature encoding is implemented by convolving the normalized iris pattern with 1D Log-Gabor wavelets. The 2D normalised pattern is broken up into a number of 1D signals, and then these 1D signals are convolved with 1D Gabor wavelets. The rows of the 2D normalized pattern are taken as the 1D signal in which each row corresponds to a circular ring on the iris region. Since maximum independence occurs in the angular direction, the angular direction is taken rather than the radial one, which corresponds to columns of the normalised pattern. The intensity values at known noise areas in the normalised pattern are set to the average intensity of surrounding pixels to prevent influence of noise in the output of the filtering. The output of filtering is then phase quantised to four levels using the Daugman method , with each filter producing two bits of data for each phasor. The output of phase quantisation is chosen to be a gray code, so that while going from one quadrant to another, only 1 bit changes. This will minimize the number of bits disagreeing, if say two intra-class patterns are slightly misaligned and thus will provide more accurate recognition. The feature encoding process is illustrated in Fig. 6. The encoding process produces a bitwise template containing a number of bits of information, and a corresponding noise mask, which corresponds to corrupt areas within the iris pattern, and marks bits in the template as corrupt. Since the phase information will be meaningless at regions where the amplitude is zero, these regions are also marked in the noise mask. The total number of bits in the template will be product of the angular resolution, the radial resolution, and the number of filters used. The number of filters, their center frequencies and parameters of the modulating Gaussian function in order to achieve the best recognition rate.</strong></p>
<p><strong>   Sample Iris Template:</strong></p>
<p style="text-align:center;"><strong> </strong><a href="http://sailjamehra.files.wordpress.com/2009/05/7.png"><strong><img title="7" src="http://sailjamehra.files.wordpress.com/2009/05/7.png?w=558&#038;h=78" alt="7" width="558" height="78" /></strong></a></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p align="center"><strong>Fig.6: An illustration of the feature encoding process.</strong></p>
<p><strong>MATCHING</strong></p>
<p><strong>  For matching, the Hamming distance is chosen as a metric for recognition, since bit-wise comparisons are necessary. The Hamming distance algorithm employed also incorporates noise masking, so that only significant bits are used in calculating the Hamming distance between two iris templates. Now when taking the Hamming distance, only those bits in the iris pattern that correspond to ‘0’ bits in noise masks of both iris patterns will be used in the calculation. The Hamming distance will be calculated using only the bits generated from the true iris region, and this modified Hamming distance formula is given as :</strong></p>
<p style="text-align:center;"><strong> </strong><a href="http://sailjamehra.files.wordpress.com/2009/05/8.png"><strong><img title="8" src="http://sailjamehra.files.wordpress.com/2009/05/8.png?w=576&#038;h=192" alt="8" width="576" height="192" /></strong></a></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong>Where Xj and Yj are the two bit-wise templates to be compared, Xnj and Ynj are the corresponding noise masks for Xj and Yj, and N is the number of bits represented by each template. Although, in theory, two iris templates generated from the same iris will have a Hamming distance of 0, in practice this will not occur. Normalisation is not perfect, and also there will be some noise that goes undetected, so that some variation will be present when comparing two intra-class iris templates. In order to account for rotational inconsistencies, when the Hamming distance of two templates is calculated, one template is shifted left and right bitwise and a number of Hamming distance values are calculated from successive shifts. This bit-wise shifting in the horizontal direction corresponds to rotation of the original iris region by an angle given by the angular resolution used. If an angular resolution of 180 degrees is used, each shift will correspond to a rotation of 2 degrees in the iris region. This method is suggested by Daugman, and corrects misalignments in the normalised iris pattern caused by rotational differences during imaging. From the calculated Hamming distance values, only the lowest is taken, since this corresponds to the best match between two templates. The number of bits moved during each shift is given by two times the number of filters used, since each filter will generate two bits of information from one pixel of the normalised region. The actual number of shifts required to normalize rotational inconsistencies will be determined by the maximum angular difference between two images of the same eye. One shift is defined as one shift to the left, followed by one shift to the right. In this example one filter is used to encode the templates, so only two bits are moved during a shift. The lowest Hamming distance, in this case zero, is then used since this corresponds to the best match between the two templates.</strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong>CONCLUSION</strong></p>
<p><strong> In this paper an iris recognition system has been presented using grayscale eye images in order to check the desired performance of iris recognition technology. Firstly, an automatic segmentation algorithm was presented, which would localise the iris region from an eye image and isolate eyelid, eyelash and reflection areas. Automatic segmentation was achieved through the use of the circular Hough transform for localizing the iris and pupil regions and the linear Hough transform for localising occluding eyelids.</strong></p>
<p><strong>Thresholding was also employed for isolating eyelashes and reflections. Next, the segmented iris region was normalised to eliminate dimensional inconsistencies between iris regions. This was achieved by implementing a version of Daugman’s rubber sheet model, where the iris is modelled as a flexible rubber sheet, which is unwrapped into a rectangular block with constant polar dimensions. Finally, features of the iris were encoded by convolving the normalised iris region with 1D Log-Gabor filters and phase quantising the output in order to produce a bitwise biometric template. The Hamming distance was chosen as a matching metric, which gave a measure of bits disagreed between two templates. A failure of statistical independence between two templates would result in a match i.e., the two templates were deemed to have been generated from the same iris if the Hamming distance produced was lower than a set Hamming distance.</strong></p>
Posted in KNOW ABOUT - BIOMETRIC AUTHENTICATION SYSTEM  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/meherchilakalapudi.wordpress.com/674/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/meherchilakalapudi.wordpress.com/674/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/meherchilakalapudi.wordpress.com/674/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/meherchilakalapudi.wordpress.com/674/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/meherchilakalapudi.wordpress.com/674/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/meherchilakalapudi.wordpress.com/674/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/meherchilakalapudi.wordpress.com/674/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/meherchilakalapudi.wordpress.com/674/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/meherchilakalapudi.wordpress.com/674/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/meherchilakalapudi.wordpress.com/674/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meherchilakalapudi.wordpress.com&blog=5609619&post=674&subd=meherchilakalapudi&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://meherchilakalapudi.wordpress.com/2009/05/30/know-about-biometric-authentication-system/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/08b95690d7ec04fc15567da451c993a3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">meherchilakalapudi</media:title>
		</media:content>

		<media:content url="http://sailjamehra.files.wordpress.com/2009/05/12.png" medium="image">
			<media:title type="html">1</media:title>
		</media:content>

		<media:content url="http://sailjamehra.files.wordpress.com/2009/05/2.png" medium="image">
			<media:title type="html">2</media:title>
		</media:content>

		<media:content url="http://sailjamehra.files.wordpress.com/2009/05/3.png" medium="image">
			<media:title type="html">3</media:title>
		</media:content>

		<media:content url="http://sailjamehra.files.wordpress.com/2009/05/4.png" medium="image">
			<media:title type="html">4</media:title>
		</media:content>

		<media:content url="http://sailjamehra.files.wordpress.com/2009/05/5.png" medium="image">
			<media:title type="html">5</media:title>
		</media:content>

		<media:content url="http://sailjamehra.files.wordpress.com/2009/05/6.png" medium="image">
			<media:title type="html">6</media:title>
		</media:content>

		<media:content url="http://sailjamehra.files.wordpress.com/2009/05/7.png" medium="image">
			<media:title type="html">7</media:title>
		</media:content>

		<media:content url="http://sailjamehra.files.wordpress.com/2009/05/8.png" medium="image">
			<media:title type="html">8</media:title>
		</media:content>
	</item>
		<item>
		<title>Pointer and Files</title>
		<link>http://meherchilakalapudi.wordpress.com/2009/05/26/pointer-and-files/</link>
		<comments>http://meherchilakalapudi.wordpress.com/2009/05/26/pointer-and-files/#comments</comments>
		<pubDate>Tue, 26 May 2009 13:37:28 +0000</pubDate>
		<dc:creator>meherchilakalapudi</dc:creator>
				<category><![CDATA[Pointers_&_Files]]></category>

		<guid isPermaLink="false">http://meherchilakalapudi.wordpress.com/?p=652</guid>
		<description><![CDATA[FUNCTIONS
&#8212;&#8212;&#8212;
  Function:
            It is a self contained block of statements and is used several
  times in a program but defined only once.
  library function:-
            The functions which are inbuilt with the compiler is called as
            library function.
         ex:- printf(),scanf(),etc&#8230;..
  User defined functions:-
            User can defined functions to do a task relavent [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meherchilakalapudi.wordpress.com&blog=5609619&post=652&subd=meherchilakalapudi&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p align="center"><strong>FUNCTIONS</strong></p>
<p align="center"><strong>&#8212;&#8212;&#8212;</strong></p>
<p><strong>  Function:</strong></p>
<p><strong>            It is a self contained block of statements and is used several</strong></p>
<p><strong>  times in a program but defined only once.</strong></p>
<p><strong>  library function:-</strong></p>
<p><strong>            The functions which are inbuilt with the compiler is called as</strong></p>
<p><strong>            library function.</strong></p>
<p><strong>         ex:- printf(),scanf(),etc&#8230;..</strong></p>
<p><strong>  User defined functions:-</strong></p>
<p><strong>            User can defined functions to do a task relavent to their program.</strong></p>
<p><strong>   such functions are called user defined functions.</strong></p>
<p><strong> </strong></p>
<p><strong>       Any function has 3 things.</strong></p>
<p><strong>       1) Function declaration</strong></p>
<p><strong>       2) Function definition</strong></p>
<p><strong>       3) Function calling.</strong></p>
<p><strong> </strong></p>
<p><strong>       In case of library functions the function declaration is in headers,</strong></p>
<p><strong>       definition is in C libraries and calling is in your source program.</strong></p>
<p><strong> </strong></p>
<p><strong>       In case of user defined functions all the three things are in your</strong></p>
<p><strong>       source program.</strong></p>
<p><strong>Function declaration:-</strong></p>
<p><strong>              syn:-  returntype  func_name([arg_list]);</strong></p>
<p><strong>              ex:-  void sum(int,int);</strong></p>
<p><strong>Function definition:-</strong></p>
<p><strong>               syn:-  returntype func_name([arg_list])</strong></p>
<p><strong>                      {</strong></p>
<p><strong>                        body;</strong></p>
<p><strong>                      }</strong></p>
<p><strong>Function calling:-</strong></p>
<p><strong>               syn:-  func_name([arg_list]);</strong></p>
<p><strong>               ex:-  sum(a,b);</strong></p>
<p><strong>NOTE:-  The arguments which are given at the time of function declaration and</strong></p>
<p><strong>          function definion are called arguments (or) formal arguments.</strong></p>
<p><strong>        The arguments which are given at the time of function calling</strong></p>
<p><strong>          are known as parameters (or) actual arguments.</strong></p>
<p><strong>Void :-  Empty data type.</strong></p>
<p><strong> </strong></p>
<p align="center"><strong>RULES FOR CREATING AND ACCESSING USER DEFINED FUNCTIONS:</strong></p>
<p align="center"><strong>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</strong></p>
<p><strong>1.A function can be called by any no of times.</strong></p>
<p><strong>2.A function may or may not return a value.</strong></p>
<p><strong>3.A function may or may not receive arguments</strong></p>
<p><strong>4.If a function does not return any value,the function will be specified as</strong></p>
<p><strong>   void.</strong></p>
<p><strong>5.If a function returns a value only one value can be returned.</strong></p>
<p><strong>6.If a function returns a value the returning value must be returned with a</strong></p>
<p><strong>  statement &#8220;return&#8221;.</strong></p>
<p><strong>7.If a function returns a value the execution of return statement should be</strong></p>
<p><strong>  last.</strong></p>
<p><strong>8.A function returns an integer value by default;</strong></p>
<p><strong>9.If a function returns a value , the returning value should be match with</strong></p>
<p><strong>  the function return data type.</strong></p>
<p><strong>10.A function is defined after (or) before main function.</strong></p>
<p><strong>11.Before calling a function , the function declaration (or) definition</strong></p>
<p><strong>    is must and should.</strong></p>
<p><strong>12.If a function declaration is specified before the function call,The function</strong></p>
<p><strong>    definition can be specified any where in the program.</strong></p>
<p><strong>13.If a function definition is specified before the function call,then the</strong></p>
<p><strong>   function declaration is not necessary.</strong></p>
<p><strong>14.A function is executed when the function is call by its name.</strong></p>
<p><strong>15.The function defintion should not be terminated with semicolon.</strong></p>
<p><strong> </strong></p>
<p><strong>RETURN:-</strong></p>
<p><strong>         Exits immediately from the corresponding executing function to the</strong></p>
<p><strong>         calling. return  Optionlly returning a value.</strong></p>
<p><strong>         syntax:-  return [&lt;expression&gt;];</strong></p>
<p><strong> </strong></p>
<p align="center"><strong>Function prototypes (or) Function categories:-</strong></p>
<p align="center"><strong>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</strong></p>
<p><strong>   A Function depending on whether arguments are present (or) not and whether</strong></p>
<p><strong>a value is returning or not .They belong to one of the following categories.</strong></p>
<p><strong>  1)Function with no arguments and no return value</strong></p>
<p><strong>  2)Function with arguments and no return value.</strong></p>
<p><strong>  3)Function with arguments and return value.</strong></p>
<p><strong>  4)Function with no orguements and return value.</strong></p>
<p><strong> </strong></p>
<p><strong>Recursive function:-</strong></p>
<p><strong>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</strong></p>
<p><strong>   Calling a function with in same function is known as recursion (or)</strong></p>
<p><strong>Recursive function.If we want to work with recursive function we must follow</strong></p>
<p><strong>the two conditions.</strong></p>
<p><strong>  1. Termination condition.</strong></p>
<p><strong>  2. Calling by itself.</strong></p>
<p><strong> </strong></p>
<p align="center"><strong>POINTERS</strong></p>
<p align="center"><strong>&#8212;&#8212;&#8211;</strong></p>
<p><strong>C provides the important feature of data manipulations with the address of</strong></p>
<p><strong>the variables,hence the execution time is very much reduced such concept is</strong></p>
<p><strong>possible with the special data type called Pointers.</strong></p>
<p><strong>Pointer:-</strong></p>
<p><strong>   A pointer is a variable which stores the address of anther variable.</strong></p>
<p><strong>Declaration:-</strong></p>
<p><strong>   Pointer declaration is similar to normal variable declaration but</strong></p>
<p><strong>   preceded by a *</strong></p>
<p><strong>   syn:- data type  *identifier;</strong></p>
<p><strong>   ex:- int *p;</strong></p>
<p><strong>Initialization:-</strong></p>
<p><strong>   datatype *identifier=address;</strong></p>
<p><strong>   ex:- int n;</strong></p>
<p><strong>        int *p=&amp;n;</strong></p>
<p><strong>NOTE:-  Any type of pointer it allocates 2 bytes momory.Because it stores</strong></p>
<p><strong>address of memory location.</strong></p>
<p><strong> </strong></p>
<p><strong>Void *:-</strong></p>
<p><strong>       It is a generic pointer.It refers the address of any type of variable</strong></p>
<p><strong> and also it will assigned to any type of pointer.</strong></p>
<p><strong> ex:-   int n=100;</strong></p>
<p><strong>        int *p=&amp;n;</strong></p>
<p><strong> </strong></p>
<p align="center"><strong>FUNCTIONS AND POINTERS</strong></p>
<p align="center"><strong>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</strong></p>
<p><strong> Call by value:-</strong></p>
<p><strong> &#8212;&#8212;&#8212;&#8212;&#8211;</strong></p>
<p><strong>       The process of passing a value to the function call is known as</strong></p>
<p><strong>       call by value.</strong></p>
<p><strong>Call by Reference:-</strong></p>
<p><strong>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</strong></p>
<p><strong>       The process of calling a function using pointers to pass the address</strong></p>
<p><strong>       of the variables is known as call by reference.</strong></p>
<p><strong> </strong></p>
<p><strong>                 </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p align="center"><strong>POINTERS  AND  ARRAYS</strong></p>
<p align="center"><strong>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</strong></p>
<p><strong>        When an array is declared,the compiler allocates a BASE address and</strong></p>
<p><strong>     sufficient amount of storage and contain all the elements of array</strong></p>
<p><strong>     in continuous memory allocation.</strong></p>
<p><strong>      The base address is the location of the first element (index 0 of</strong></p>
<p><strong>      the array).</strong></p>
<p><strong>      The compiler also defines the array name as a constant pointer pointed</strong></p>
<p><strong>      to the first element.</strong></p>
<p><strong>      suppose we declare an array &#8216;a&#8217; as follows.</strong></p>
<p><strong>         int a[5]={1,2,3,4,5};</strong></p>
<p><strong>         suppose the base address of a is 1000 and assuming that each integer</strong></p>
<p><strong>         requires 2 bytes.Then the 5 elements will be stored as follows.</strong></p>
<p><strong>    Elements&#8212;&#8212;&gt;  a[0] a[1] a[2] a[3] a[4]</strong></p>
<p><strong>                     &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</strong></p>
<p><strong>    values&#8212;&#8212;-&gt;     1    2    3    4    5</strong></p>
<p><strong>                     &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</strong></p>
<p><strong>    address&#8212;&#8212;&gt;   1000 1002 1004 1006 1008</strong></p>
<p><strong>                      |</strong></p>
<p><strong>                 base address</strong></p>
<p><strong> </strong></p>
<p><strong>        Here &#8216;a&#8217; is a pointer that points to the first element. so the value</strong></p>
<p><strong>        of a is 1000</strong></p>
<p><strong>           there fore  a=&amp;a[0]=1000</strong></p>
<p><strong>        If we declare &#8216;p&#8217; is an integer pointer,then we can make the pointer</strong></p>
<p><strong>        p to point to the array &#8216;a&#8217; by the following assignment.</strong></p>
<p><strong> </strong></p>
<p><strong>                int *p;</strong></p>
<p><strong>                p=&amp;a[0]=a;</strong></p>
<p><strong>        Now we can access every value of a using p++(or)p+1 to move one</strong></p>
<p><strong>        element to another.</strong></p>
<p><strong> </strong></p>
<p><strong>        The relationship between p and a is shown below.</strong></p>
<p><strong>        p+0 = &amp;a[0]=1000</strong></p>
<p><strong>        p+1 = &amp;a[1]=1002</strong></p>
<p><strong>        p+2 = &amp;a[2]=1004</strong></p>
<p><strong>        p+3 = &amp;a[3]=1006</strong></p>
<p><strong>        p+4 = &amp;a[4]=1008</strong></p>
<p><strong> </strong></p>
<p><strong>        When handling arrays instead of using array indexing,we can use pointer</strong></p>
<p><strong>        to access array elements.</strong></p>
<p><strong>        Note that *(p+k) gives the value of a[k].</strong></p>
<p><strong>        Pointer accessing method is much faster than array indexing.</strong></p>
<p><strong> </strong></p>
<p align="center"><strong>DYNAMIC MEMORY ALLOCATION</strong></p>
<p align="center"><strong>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</strong></p>
<p><strong>       C language requires the no of elements in an array to  be specified</strong></p>
<p><strong>       at compile time.But we may not be able to do so always our initial</strong></p>
<p><strong>       judgement of size,if it is wrong,it make cause failure of the program</strong></p>
<p><strong>       (or) wastage of the memory space.In this situation we use Dynamic</strong></p>
<p><strong>       Memory allocation.</strong></p>
<p><strong>       Definition:-</strong></p>
<p><strong>                   The process of allocating memory at run time is known as</strong></p>
<p><strong>                   Dynamic memory allocation.</strong></p>
<p><strong>       Malloc():-              &lt;alloc.h&gt;</strong></p>
<p><strong>                It is a function which is used to allocating memory at</strong></p>
<p><strong>                 run time.</strong></p>
<p><strong>         syn:-    void *malloc(size_t size);</strong></p>
<p><strong>           size_t:-  unsigned integer. this is used for memory object sizes.</strong></p>
<p><strong> </strong></p>
<p><strong>       Pointer variable=(type casting)malloc(memory size);</strong></p>
<p><strong>     Ex:-    int *p;</strong></p>
<p><strong>              p=(int *)malloc(sizeof(int));  //for 1 location</strong></p>
<p><strong>              p=(int *)malloc(n*sizeof(int));  //for n locations</strong></p>
<p><strong> </strong></p>
<p><strong>       Calloc():-            &lt;alloc.h&gt;</strong></p>
<p><strong>                This is also used for allocating memory at run time.</strong></p>
<p><strong>        syn:-   void *calloc(size_t nitems, size_t size);</strong></p>
<p><strong> </strong></p>
<p><strong>      NOTE:- Calloc allocates a block (n times*size)bytes and clears into 0.</strong></p>
<p><strong>      Realloc():- It reallocates Main memory.</strong></p>
<p><strong>         syn:- void *realloc(void *block, size_t size);</strong></p>
<p><strong> </strong></p>
<p><strong>      free():- It deallocates the allocated memory.</strong></p>
<p><strong>        syn:-   void free(void *block);</strong></p>
<p><strong> </strong></p>
<p><strong>                                    FILES</strong></p>
<p><strong>                                    &#8212;&#8211;</strong></p>
<p><strong>   C language permits the usage of limited input and output functions to read</strong></p>
<p><strong>and write data.</strong></p>
<p><strong>    These functions are used for only smaller volumes of data and it becomes</strong></p>
<p><strong>difficult to handle longer data volumes.Also the entire data is lost when the</strong></p>
<p><strong>program is over.</strong></p>
<p><strong>    To overcome these difficulties a flexible method is devoloped by employing</strong></p>
<p><strong>the concept of FILES to store,to read (0r) write data and to written them even</strong></p>
<p><strong>when program is over.</strong></p>
<p><strong>Definition:-</strong></p>
<p><strong>           A FILE is one which enable the user to read , write and store a</strong></p>
<p><strong>     group of a related data .  (or)</strong></p>
<p><strong>           A FILE is a collection of related data stored in a perticular area</strong></p>
<p><strong>     On the disk.</strong></p>
<p><strong>  C supports a no of functions to have the ability to perform the basic file</strong></p>
<p><strong>  operations which indicates.</strong></p>
<p><strong>           1) Naming a file</strong></p>
<p><strong>           2) Opening a file</strong></p>
<p><strong>           3) Reading data from a file</strong></p>
<p><strong>           4) Writing data into file</strong></p>
<p><strong>           5) Closing a file</strong></p>
<p><strong> </strong></p>
<p><strong>FILE:-  It is a predefined structure and is used to declare a file pointer.</strong></p>
<p><strong>        Using this pointer we can perform all file operations.</strong></p>
<p><strong> Declaration of file pointer:-</strong></p>
<p><strong>              FILE  *identifier;</strong></p>
<p><strong>              ex:-  FILE *fp;</strong></p>
<p><strong>TEXT FILES:-</strong></p>
<p><strong>            For reading and writing data in continuous blocks.</strong></p>
<p><strong>BINARY FILES:-</strong></p>
<p><strong>            For reading and writing data in orbitrary structured files.</strong></p>
<p><strong>fopen():-</strong></p>
<p><strong>         It opens a file stream.</strong></p>
<p><strong>         syn:- FILE *fopen(const char *filename,const char *mode);</strong></p>
<p><strong>file name:-  File that the functions open.</strong></p>
<p><strong>MODE:-</strong></p>
<p><strong>String ³ Description</strong></p>
<p><strong>***********************************************************************************</strong></p>
<p><strong>   r   ³ Open for reading only</strong></p>
<p><strong>   w   ³ Create for writing</strong></p>
<p><strong>       ³ If a file by that name already exists, it will be overwritten.</strong></p>
<p><strong>   a   ³ Append; open for writing at end of file, or create for</strong></p>
<p><strong>       ³ writing if the file does not exist.</strong></p>
<p><strong>   r+  ³ Open an existing file for update (reading and writing)</strong></p>
<p><strong>   w+  ³ Create a new file for update (reading and writing).</strong></p>
<p><strong>       ³ If a file by that name already exists, it will be overwritten.</strong></p>
<p><strong>   a+  ³ Open for append; open for update at the end of the file, or</strong></p>
<p><strong>       ³ create if the file does not exist.</strong></p>
<p><strong> </strong></p>
<p><strong> þ To specify that a given file is being opened or created in text mode,</strong></p>
<p><strong>   append &#8220;t&#8221; to the string (rt, w+t, etc.).</strong></p>
<p><strong> þ To specify binary mode, append &#8220;b&#8221; to the string (wb, a+b, etc.).</strong></p>
<p><strong> </strong></p>
<p><strong> fclose():-    It closes a file stream</strong></p>
<p><strong>         syn:- int fclose(FILE *stream);</strong></p>
<p><strong> fcloseall():- It closes all opened file streams.</strong></p>
<p><strong>         syn:- int fcloseall(void);</strong></p>
<p><strong> </strong></p>
<p align="center"><strong>TEXT FILES</strong></p>
<p align="center"><strong>&#8212;&#8212;&#8212;-</strong></p>
<p><strong>Macros:-</strong></p>
<p><strong>&#8212;&#8212;&#8211;</strong></p>
<p><strong>1)getc():- It is a macro that gets one character from a stream.</strong></p>
<p><strong>           syn:- int getc(FILE *stream);</strong></p>
<p><strong>2)putc():- It is a macro that out puts a character to a file stream.</strong></p>
<p><strong>           syn:- int putc(int c,FILE *stream);</strong></p>
<p><strong>Functions:-</strong></p>
<p><strong>&#8212;&#8212;&#8212;-</strong></p>
<p><strong>1)fgetc():- It is a function.It gets a character from a file stream.</strong></p>
<p><strong>           syn:- int fgetc(FILE *stream);</strong></p>
<p><strong>2)fputc():- It is a function.It outputs a character to a file stream.</strong></p>
<p><strong>           syn:- int fputc(int c,FILE *stream);</strong></p>
<p><strong>EOF:-  A constant indicating that end of file has been reached on a file.</strong></p>
<p><strong>      To get this character from keyboard press ctrl+z.</strong></p>
<p><strong> </strong></p>
<p><strong>fgets():-</strong></p>
<p><strong>         It gets string from file stream.</strong></p>
<p><strong>         syn:-  char *fgets(char *s,int n,FILE *stream);</strong></p>
<p><strong>remarks:-</strong></p>
<p><strong>&#8212;&#8212;&#8212;</strong></p>
<p><strong>fgets reads characters from stream into the string s. It stops when it reads</strong></p>
<p><strong>either n &#8211; 1 characters or a newline character, whichever comes first.</strong></p>
<p><strong> </strong></p>
<p><strong>fputs():-</strong></p>
<p><strong>          It outputs a string to file stream.</strong></p>
<p><strong>          syn:- int fputs(const char *s,FILE *stream);</strong></p>
<p><strong>Remarks:-</strong></p>
<p><strong>&#8212;&#8212;&#8212;-</strong></p>
<p><strong>fputs copies the null-terminated string s to the given output stream. It</strong></p>
<p><strong>does not append a newline character, and the terminating null character is</strong></p>
<p><strong>not copied.</strong></p>
<p><strong> </strong></p>
<p><strong>feof():-</strong></p>
<p><strong>        It is a Macro that tests if end of file has been reached on a stream.</strong></p>
<p><strong>        syn:-  int  feof(FILE *stream);</strong></p>
<p><strong>returned value:-</strong></p>
<p><strong>&#8212;&#8212;&#8212;&#8212;&#8212;</strong></p>
<p><strong> -&gt;  Returns non-zero if an end-of-file</strong></p>
<p><strong>     indicator was detected on the last input</strong></p>
<p><strong>     operation on the named stream.</strong></p>
<p><strong> -&gt;  Returns 0 if end-of-file has not been</strong></p>
<p><strong>     reached.</strong></p>
<p><strong> </strong></p>
<p><strong>Rewind():-</strong></p>
<p><strong>            It  Repositions file pointer to stream&#8217;s beginning.</strong></p>
<p><strong>            syn:- void rewind(FILE *stream);</strong></p>
<p><strong>fscanf():-</strong></p>
<p><strong>          It scans and formats input from a file stream.</strong></p>
<p><strong>syn:- int fscanf (FILE *stream,const char *format [, address, ...]);</strong></p>
<p><strong> </strong></p>
<p><strong>fprintf():-</strong></p>
<p><strong>           It sends formatted output to the file stream.</strong></p>
<p><strong>syn:-  int fprintf (FILE *stream, const char *format [, argument, ...]);</strong></p>
<p><strong> </strong></p>
<p><strong>NOTE:-In fprintf (or) fscanf, between formats atleast one space is neccessary.</strong></p>
<p><strong> </strong></p>
<p><strong>ftell():- It Returns the current file pointer.</strong></p>
<p><strong>   syntax:-  long ftell(FILE *stream);</strong></p>
<p><strong> </strong></p>
<p><strong>fseek():- It repositions the file pointer of a stream.</strong></p>
<p><strong>  syntax:-  int fseek(FILE *stream,long offset,int whence);</strong></p>
<p><strong>Argument³ What It Is/Does</strong></p>
<p><strong>**************************************************************************</strong></p>
<p><strong> stream ³ Stream whose file pointer fseek sets</strong></p>
<p><strong> offset ³ Difference in bytes between whence (a file pointer position)</strong></p>
<p><strong>        ³ and new position. For text mode streams, offset should be 0 or</strong></p>
<p><strong>        ³ a value returned by ftell.</strong></p>
<p><strong> whence ³ One of three they are</strong></p>
<p><strong> </strong></p>
<p><strong> Constant ³Value³ File location</strong></p>
<p><strong>*******************************************************************************</strong></p>
<p><strong> SEEK_SET ³  0  ³ Seeks from beginning of file</strong></p>
<p><strong> SEEK_CUR ³  1  ³ Seeks from current position</strong></p>
<p><strong> SEEK_END ³  2  ³ Seeks from end of file</strong></p>
<p><strong> </strong></p>
<p align="center"><strong>BINARY FILES</strong></p>
<p align="center"><strong>&#8212;&#8212;&#8212;&#8212;</strong></p>
<p><strong>putw():-</strong></p>
<p><strong>&#8212;&#8212;-</strong></p>
<p><strong>        It outputs an integer on a stream.</strong></p>
<p><strong>        syn:-  int putw(int w,FILE *stream);</strong></p>
<p><strong>getw():-</strong></p>
<p><strong>        It gets an integer from stream.</strong></p>
<p><strong>        syn:- int getw(FILE *stream);</strong></p>
<p><strong> </strong></p>
<p><strong>fwrite():-</strong></p>
<p><strong>        It appends specified no of equal sized data items to an output file.</strong></p>
<p><strong>    syn:-size_t fwrite(const void *ptr, size_t size, size_t n, FILE*stream);</strong></p>
<p><strong> </strong></p>
<p><strong>fread():-</strong></p>
<p><strong>       It reads a specified no of equal sized data items from an input stream</strong></p>
<p><strong>       into a block.</strong></p>
<p><strong>  syn:- size_t fread(void *ptr, size_t size, size_t n, FILE *stream);</strong></p>
<p><strong> </strong></p>
<p><strong>Argument³ What It Is/Does</strong></p>
<p><strong>***********************************************************************************</strong></p>
<p><strong>ptr    ³ Points to a block into which data is read/write</strong></p>
<p><strong> size   ³ Length of each item read/write, in bytes</strong></p>
<p><strong> n      ³ Number of items read/write</strong></p>
<p><strong> stream ³ Points to input/output stream</strong></p>
<p><strong> </strong></p>
<p><strong> remove():-</strong></p>
<p><strong>           It is a Macro that removes a file</strong></p>
<p><strong>           Declaration:  int remove(const char *filename);</strong></p>
<p><strong> rename():-</strong></p>
<p><strong>           It Renames a file</strong></p>
<p><strong>           Declaration:  int rename(const char *oldname, const char *newname);</strong></p>
<p><strong> </strong></p>
<p align="center"><strong>STORAGE CLASSES</strong></p>
<p align="center"><strong>&#8212;&#8212;&#8212;&#8212;&#8212;</strong></p>
<p><strong>        By the declaration statement the memory is allocated temporarly for</strong></p>
<p><strong>all the variables.The size of momory varies with respect to the type of the</strong></p>
<p><strong>variable.The availability of variables for access depends on it&#8217;s declaration</strong></p>
<p><strong>time.the storage class specifiers are used to specify the life, scope of</strong></p>
<p><strong>variables with in the block , functions and the entire program.There are</strong></p>
<p><strong>four types of storage class specifiers.</strong></p>
<p><strong>        They are</strong></p>
<p><strong>        1. Automatic variables</strong></p>
<p><strong>        2. Static variables</strong></p>
<p><strong>        3. external variables (or) global variables</strong></p>
<p><strong>        4. Register variables.</strong></p>
<p><strong> </strong></p>
<p><strong>1. Automatic variables :-</strong></p>
<p><strong>        These variables are declared inside the function block.</strong></p>
<p><strong> </strong></p>
<p><strong>        storage : Main memory</strong></p>
<p><strong>        Default value : garbage value</strong></p>
<p><strong>        Scope  : Local to the block in which it is defined</strong></p>
<p><strong>        Life   : Till the control remaines with in the block in which it</strong></p>
<p><strong>                 it is defined</strong></p>
<p><strong>        Keyword : auto</strong></p>
<p><strong>NOTE:-</strong></p>
<p><strong>        If there is no storage class specifier before the declaration of any</strong></p>
<p><strong>variable inside a function block by default it takes auto storage class.</strong></p>
<p><strong>#include&lt;stdio.h&gt;</strong></p>
<p><strong>#include&lt;conio.h&gt;</strong></p>
<p><strong>void main()</strong></p>
<p><strong>{</strong></p>
<p><strong>        auto int a,b;</strong></p>
<p><strong>        clrscr();</strong></p>
<p><strong>        printf(&#8220;\na=%d&#8221;,a);</strong></p>
<p><strong>        printf(&#8220;\nb=%d&#8221;,b);</strong></p>
<p><strong>        getch();</strong></p>
<p><strong>}</strong></p>
<p><strong>o/p : a = garbage value</strong></p>
<p><strong>      b = garbage value</strong></p>
<p><strong> </strong></p>
<p><strong>2. Static variables:-</strong></p>
<p><strong>        The memory of static variable remaines unchanged until the end of the</strong></p>
<p><strong>        program.</strong></p>
<p><strong>        storage : Main memory</strong></p>
<p><strong>        default value : zero</strong></p>
<p><strong>        scope   : Local to the block in which it is defined</strong></p>
<p><strong>        life    : The value of static variable persists between the different</strong></p>
<p><strong>                    function calls.i.e.., It can not re-initialize between</strong></p>
<p><strong>                    the different function calls.</strong></p>
<p><strong>        keyword  : static</strong></p>
<p><strong> </strong></p>
<p><strong>#include&lt;stdio.h&gt;</strong></p>
<p><strong>#include&lt;conio.h&gt;</strong></p>
<p><strong>void main()</strong></p>
<p><strong>{</strong></p>
<p><strong>        static int a,b;</strong></p>
<p><strong>        clrscr();</strong></p>
<p><strong>        printf(&#8220;\na=%d&#8221;,a);</strong></p>
<p><strong>        printf(&#8220;\nb=%d&#8221;,b);</strong></p>
<p><strong>        getch();</strong></p>
<p><strong>}</strong></p>
<p><strong>o/p :   a = 0</strong></p>
<p><strong>        b = 0</strong></p>
<p><strong> </strong></p>
<p><strong>#include&lt;stdio.h&gt;</strong></p>
<p><strong>#include&lt;conio.h&gt;</strong></p>
<p><strong>void main()</strong></p>
<p><strong>{</strong></p>
<p><strong>        void disp();</strong></p>
<p><strong>        int i;</strong></p>
<p><strong>        clrscr();</strong></p>
<p><strong>        for(i=1;i&lt;=10;i++)</strong></p>
<p><strong>        {</strong></p>
<p><strong>                disp();</strong></p>
<p><strong>        }</strong></p>
<p><strong>        getch();</strong></p>
<p><strong>}</strong></p>
<p><strong>void disp()</strong></p>
<p><strong>{</strong></p>
<p><strong>        static int n=1;</strong></p>
<p><strong>        printf(&#8220;%5d&#8221;,n);</strong></p>
<p><strong>        n++;</strong></p>
<p><strong>}</strong></p>
<p><strong> </strong></p>
<p><strong>o/p :-   1 2 3 4 5 6 7 8 9 10</strong></p>
<p><strong> </strong></p>
<p><strong>NOTE :-</strong></p>
<p><strong>        If we can not use static storage class specifier in the above program</strong></p>
<p><strong>        o/p:  1 1 1 1 1 1 1 1 1 1</strong></p>
<p><strong> </strong></p>
<p><strong>3. External variables (or) Global variables:-</strong></p>
<p><strong>        The variables that are both alive and active through out the entire</strong></p>
<p><strong>        program are known as external variables.</strong></p>
<p><strong> </strong></p>
<p><strong>        storage : Main memory</strong></p>
<p><strong>        default value : zero</strong></p>
<p><strong>        scope   : global</strong></p>
<p><strong>        life    :as long as the execution of the program does not comes to end</strong></p>
<p><strong>        keyword : extern</strong></p>
<p><strong> </strong></p>
<p><strong>#include&lt;stdio.h&gt;</strong></p>
<p><strong>#include&lt;conio.h&gt;</strong></p>
<p><strong>int a,b;</strong></p>
<p><strong>void main()</strong></p>
<p><strong>{</strong></p>
<p><strong>        clrscr();</strong></p>
<p><strong>        printf(&#8220;\na=%d&#8221;,a);</strong></p>
<p><strong>        printf(&#8220;\nb=%d&#8221;,b);</strong></p>
<p><strong>        getch();</strong></p>
<p><strong>}</strong></p>
<p><strong>o/p : a = 0</strong></p>
<p><strong>      b = 0</strong></p>
<p><strong> </strong></p>
<p><strong>#include&lt;stdio.h&gt;</strong></p>
<p><strong>#include&lt;conio.h&gt;</strong></p>
<p><strong>void main()</strong></p>
<p><strong>{</strong></p>
<p><strong>        extern int n=100;</strong></p>
<p><strong>        clrscr();</strong></p>
<p><strong>        printf(&#8220;\n n=%d&#8221;,n);</strong></p>
<p><strong>        getch();</strong></p>
<p><strong>}</strong></p>
<p><strong>o/p : n = 100</strong></p>
<p><strong> </strong></p>
<p><strong>4. Register variables: -</strong></p>
<p><strong>        We can use the register variables for frequently used variables to</strong></p>
<p><strong>        Improve faster execution of the program.</strong></p>
<p><strong> </strong></p>
<p><strong>        storage :   CPU register</strong></p>
<p><strong>        default value : garbage value</strong></p>
<p><strong>        scope : Local to the block in which it is defined</strong></p>
<p><strong>        life  : Till the control remaines with in the block in which it is defined.</strong></p>
<p><strong>        keyword: register</strong></p>
<p><strong> </strong></p>
<p><strong>Note : These variables are also declared inside the function block.</strong></p>
<p><strong> </strong></p>
<p><strong>#include&lt;stdio.h&gt;</strong></p>
<p><strong>#include&lt;conio.h&gt;</strong></p>
<p><strong>void main()</strong></p>
<p><strong>{</strong></p>
<p><strong>        register int a,b;</strong></p>
<p><strong>        clrscr();</strong></p>
<p><strong>        printf(&#8220;\na =%d&#8221;,a);</strong></p>
<p><strong>        printf(&#8220;\nb =%d&#8221;,b);</strong></p>
<p><strong>        getch();</strong></p>
<p><strong>}</strong></p>
<p><strong>o/p : a = garbage value</strong></p>
<p><strong>      b = garbage value</strong></p>
<p><strong> </strong></p>
<p><strong>Note :</strong></p>
<p><strong> The register variables supports only integral data type ( int (or) char)</strong></p>
Posted in Pointers_&amp;_Files  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/meherchilakalapudi.wordpress.com/652/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/meherchilakalapudi.wordpress.com/652/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/meherchilakalapudi.wordpress.com/652/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/meherchilakalapudi.wordpress.com/652/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/meherchilakalapudi.wordpress.com/652/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/meherchilakalapudi.wordpress.com/652/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/meherchilakalapudi.wordpress.com/652/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/meherchilakalapudi.wordpress.com/652/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/meherchilakalapudi.wordpress.com/652/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/meherchilakalapudi.wordpress.com/652/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meherchilakalapudi.wordpress.com&blog=5609619&post=652&subd=meherchilakalapudi&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://meherchilakalapudi.wordpress.com/2009/05/26/pointer-and-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/08b95690d7ec04fc15567da451c993a3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">meherchilakalapudi</media:title>
		</media:content>
	</item>
		<item>
		<title>knee Care(while doing work at computers)</title>
		<link>http://meherchilakalapudi.wordpress.com/2009/05/22/knee-carewhile-doing-work-at-computers/</link>
		<comments>http://meherchilakalapudi.wordpress.com/2009/05/22/knee-carewhile-doing-work-at-computers/#comments</comments>
		<pubDate>Fri, 22 May 2009 06:41:32 +0000</pubDate>
		<dc:creator>meherchilakalapudi</dc:creator>
				<category><![CDATA[knee Care(while doing work at computers)]]></category>

		<guid isPermaLink="false">http://meherchilakalapudi.wordpress.com/?p=628</guid>
		<description><![CDATA[1. INTRODUCTION
 
INTRODUCTION TO OSTEOARTHRITIS OF THE KNEE
There are many types of arthritis (rheumatoid, degenerative, post-traumatic, auto-immune induced, etc). The most common form is osteoarthritis. It is also known as degenerative joint disease (DJD). While the exact cause is unknown, there are known to be several possible causes including: injury, age, congenital predisposition and obesity.
                        
                           
‘Osteoarthritis is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meherchilakalapudi.wordpress.com&blog=5609619&post=628&subd=meherchilakalapudi&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h1><strong>1. </strong><strong>INTRODUCTION</strong></h1>
<p> </p>
<h1><strong>INTRODUCTION TO OSTEOARTHRITIS OF THE KNEE</strong></h1>
<p>There are many types of arthritis (rheumatoid, degenerative, post-traumatic, auto-immune induced, etc). The most common form is osteoarthritis. It is also known as degenerative joint disease (DJD). While the exact cause is unknown, there are known to be several possible causes including: injury, age, congenital predisposition and obesity.</p>
<p>                        <a rel="attachment wp-att-629" href="http://meherchilakalapudi.wordpress.com/2009/05/22/knee-carewhile-doing-work-at-computers/attachment/1/"><img class="size-full wp-image-629 alignnone" title="1" src="http://meherchilakalapudi.files.wordpress.com/2009/05/110.png?w=382&#038;h=178" alt="1" width="382" height="178" /></a></p>
<p>                           </p>
<p>‘Osteoarthritis is idiopathic, slowly progressive disease of the synovial joints occurring in life and characterized pathologically by focal degeneration of articular cartilage, bone thickening, osteophytes formation and joint deformity.’</p>
<p>The word arthritis means inflammation (swelling) of a joint. Osteoarthritis, also known as “wear and tear” arthritis is the most common type of arthritis. It is estimated that osteoarthritis affects nearly 55% of Indians over the age of 65.Danielsson in 1964 first reported a 40%incidence of osteoarthritis above the age of 50. ‘Osteo’ means ‘bone’ and ‘arthritis’ means ‘joint damage’. The joint affected are spine, hip ,hands, knee. Osteoarthritis places a tremendous physical and economic burden on the society because it is the most prevalent musculoskeletal disorder.  The knee is one of the most common joints affected by this disease.</p>
<p>A joint is where two bones meet. Most of our joints are designed to allow the bones to move in certain directions. The knee is the largest joint in the body, and also one of the most complicated because it has many important jobs to do. It must be strong enough to take our weight and must lock into position so we can stand upright. But it has to act as a hinge, too, so we can walk. It must also withstand extreme stresses, twists and turns, such as when we run or play sports. Osteoarthritis of the knee affects different people in different ways. Some people have a problem with only one knee, others with both knees As a result, it is not very helpful to compare the experience of one person with another, and we cannot predict the eventual outcome for any one individual with osteoarthritis.</p>
<p>Most types of treatment for osteoarthritis of the knee work best when started early, before there is a lot of “wear and tear” in the knee. Doctors and physiotherapists who deal with people who have osteoarthritis can help outline a treatment protocol. Every osteoarthritic knee is different, and there should be a team approach to treatment. Physiotherapy is an important part of the treatment program for people with osteoarthritis of the knee. A knowledgeable physiotherapist will be able to provide more information about each of these types of treatments. While it may not be necessary to attend regular physiotherapy sessions, it may be helpful to consult a physiotherapist for an appropriate strengthening program that can be done at home. Additionally, a physiotherapist can help educate about proper exercise techniques and appropriate activities of the knee joint.</p>
<p> </p>
<p> </p>
<p> </p>
<h1><strong>ARTHROLOGY</strong></h1>
<p>It is a hinge type of a synovial joint. It is not a typical hinge variety because the transverse axis around which the movement takes place is not static but moves forward during extension and backwards during flexion. In additional to these movements there are some rotational movements also possible in the joint.</p>
<h1><strong>EPIDEMEOLOGY</strong></h1>
<p>There is a steady rise in the overall prevalence from the age of 30 such that by 65,80% of people show radiographic evidence of OA though only 25-30% have associated symptoms. Knee OA is prevalent in all racial groups but hip, hand and generalized OA are only prevalent in Caucasians. OA is more prevalent and more commonly associated with symptoms in women ,except at the hip where both genders are equally affected. Recognised occupational risks for OA include farming (OA hip),mining(OA knee) and professional football(OA knee).</p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p align="center"><strong><span style="text-decoration:underline;">2. KNEE ANATOMY</p>
<p></span></strong></p>
<p>                                        <a rel="attachment wp-att-630" href="http://meherchilakalapudi.wordpress.com/2009/05/22/knee-carewhile-doing-work-at-computers/2knee/"><img class="size-full wp-image-630 alignnone" title="2(knee)" src="http://meherchilakalapudi.files.wordpress.com/2009/05/2knee.png?w=205&#038;h=174" alt="2(knee)" width="205" height="174" /></a></p>
<p>The knee joint is the largest joint in the body and the most complex .</p>
<p><strong><span style="text-decoration:underline;">BONES</span></strong>: The knee joint is made up of four bones, which are connected by muscles, ligaments, and tendons. The femur is the large bone in the thigh. The tibia is the large shin bone. The fibula is the smaller shin bone, located next to the tibia. The patella, otherwise known as the knee cap, is the small bone in the front of the knee. It slides up and down in a groove in the femur (the femoral groove) as the knee bends and straightens. The knee joint is formed by the condyles of the tibia and the condyles of the femur and the patella.</p>
<p><strong><span style="text-decoration:underline;">SYNOVIUM</span></strong><strong>:</strong></p>
<p>The joint is surrounded by a membrane which produces small amount of thick fluid called synovial fluid which helps to nourish the cartilage and keeps it slippery.</p>
<p><strong><span style="text-decoration:underline;">CAPSULE</span></strong><strong>: </strong></p>
<p>The fibrous capsule is very thin, fibrous sac covering the joint It is deficient anteriorly where it is replaced by the quadriceps femoris patella and ligamentum patella. Attachments: <span style="text-decoration:underline;">proximally</span>: to the upper margins of the medial and lateral femoral condyles.<span style="text-decoration:underline;">Distally</span>: to the margins of the tibial condyles. <span style="text-decoration:underline;">Medially</span>: to the medial margins of the medial condylar surfaces<span style="text-decoration:underline;">. Laterally</span>: to the lateral margins of the alcondyles.    </p>
<p style="text-align:center;"><img class="size-full wp-image-631  aligncenter" title="3" src="http://meherchilakalapudi.files.wordpress.com/2009/05/32.png?w=198&#038;h=184" alt="3" width="198" height="184" /></p>
<p style="text-align:center;">                           <img class="aligncenter size-full wp-image-632" title="4(articular)" src="http://meherchilakalapudi.files.wordpress.com/2009/05/4articular.png?w=244&#038;h=226" alt="4(articular)" width="244" height="226" />                       </p>
<p>                                        <a href="http://catalog.nucleusinc.com/enlargeexhibit.php?ID=4195" target="_blank"></a></p>
<p><strong><span style="text-decoration:underline;">ARTICULAR CARTILAGE:</span></strong></p>
<p>There is another type of cartilage in the knee called articular cartilage. This cartilage is a smooth shiny material that covers the bones in the knee joint. There is articular cartilage anywhere that two bony surfaces come into contact with each other. In the knee, articular cartilage covers the ends of the femur, the femoral groove, the top of the tibia and the underside of the patella. Articular cartilage allows the knee bones to move easily as the knee bends and straightens.</p>
<p><strong><span style="text-decoration:underline;">LIGAMENTS:</span></strong></p>
<p>Ligaments are like strong ropes that help connect bones and provide stability to joints. In the knee, there are four main ligaments.</p>
<p>On the inner (medial) aspect of the knee is the <span style="text-decoration:underline;">medial collateral ligament (MCL</span>)<span style="text-decoration:underline;">Attachements:</span> to the above to medial condyles of the femur and below to the medial margins and adjoining medal surfaces of tibia.</p>
<p>The outer (lateral) aspect of the knee is the <span style="text-decoration:underline;">latera</span>l <span style="text-decoration:underline;">collateral ligament (LCL</span>).<span style="text-decoration:underline;">Attachements</span>: It is a rounded cord like structure.  attached above to the lateral condyle of the femur and below to the head of the fibula.</p>
<p><span style="text-decoration:underline;">Transverse Ligament</span>: it connects the anterior end of the medial and lateral semilunar cartilage.</p>
<p><span style="text-decoration:underline;">Coronary Ligament</span>: it consists of short ligamentous fibres which serve to connect the peripheral convex margins of the semilunar cartilages with the peripheral margins of the tibia.</p>
<p><span style="text-decoration:underline;">Arcuate ligament</span>: it is attached to the posterior aspect of the femur above the lateral femoral condyle and fibres are directed downwards and medially to merge with the capsule.</p>
<p> </p>
<p>The other two main ligaments are found in the center of the knee. These paired ligaments are called <span style="text-decoration:underline;">the anterior cruciate ligament</span> <span style="text-decoration:underline;">(ACL) and the posterior cruciate ligament (PCL</span>). They are very strong bands which maintain the stability of the knee joint. Anterior cruciate ligament:it is attached above to the anterior part of the intercondylar area of tibia between the anterior ends of the semilunar cartilages and above to the posterior part of the medial surface of the lateral femoral condyles. Posterior Cruciate ligament:it is attached below  to the posterior part of the intercondylar area of tibia and above to the anterior part of the lateral surface of the medial condyles.</p>
<p align="center"><a rel="attachment wp-att-631" href="http://meherchilakalapudi.wordpress.com/2009/05/22/knee-carewhile-doing-work-at-computers/attachment/3/"></a><a rel="attachment wp-att-633" href="http://meherchilakalapudi.wordpress.com/2009/05/22/knee-carewhile-doing-work-at-computers/5legments/"><img class="aligncenter size-full wp-image-633" title="5(legments)" src="http://meherchilakalapudi.files.wordpress.com/2009/05/5legments.png?w=206&#038;h=415" alt="5(legments)" width="206" height="415" /></a> </p>
<p align="center"><strong></strong> </p>
<p><strong><span style="text-decoration:underline;">MENISCI:</span></strong></p>
<p> Two semilunar cartilages called menisci sit between the femur and the tibia. They are fibrocartilagenous discs which are shaped in the form of cresents. These structures act as “cushions” or “shock absorbers”. They also help provide stability to the knee. There is a medial meniscus and a lateral meniscus. When either meniscus is damaged it is often referred to as a “torn cartilage”.</p>
<p><span style="text-decoration:underline;">Medial menisci: </span>it is Cshaped and has 2 ends.the anterior horn is attached to the intercondylar area of the tibia and the posterior horn is attached to the posterior part of the intercondylar part of the tibia between the attachment of the lateral semilunar cartilage and posterior cruciate ligament.</p>
<p><span style="text-decoration:underline;">Lateral menisci</span>:it has 2 horns and the anterior horn is attached to the intercondylar area of tibia between the ACL and interondylar tubercle and the posterior horn is attached to the intercondylar area of the tibia. The strong quadriceps muscles on the front of the thigh attach to the top of the patella via the quadriceps tendon. This tendon covers the patella and continues down to form the “rope-like” patellar tendon. The patellar tendon in turn, attaches to the front of the tibia. The hamstring muscles on the back of the thigh attach to the tibia at the back of the knee. The quadriceps muscles are the main muscles that straighten the knee. The hamstring muscles are the main muscles that bend the knee.             <strong></strong></p>
<p><strong><span style="text-decoration:underline;">TENDONS:</span></strong> They connect the muscles to the bones. The strong quadriceps muscles on the front of the thigh attach to the top of the patella via the quadriceps tendon. Theis tendon covers the patella and continues down to form the rope like patellar tendon. this tendon in turn attaches to the tibia.</p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;">BURSA:</span></strong><strong> </strong></p>
<p>Finally, a bursa is a small fluid filled sac that decreases the friction between two tissues. Bursae also protect bony structures. There are many different bursae around the knee but the one that is most commonly injured is the bursa in front of the patella, the prepatellar bursa. Normally, a bursa has very little fluid in it but if it becomes irritated it can fill with fluid and become very large.</p>
<p><span style="text-decoration:underline;">Anteriorly:</span> suprapatellar bursa, prepatellar bursa, superficial  infrapatellar bursa ,deep infra patellar bursa.</p>
<p><span style="text-decoration:underline;"> Laterally:</span>  bursa between the lateral cruciate ligament and the popliteal tendon, popliteal bursa between the popliteal and the lateral condyle of the femur.</p>
<p><span style="text-decoration:underline;">Medially:</span> tibial intercondylar bursa, between the MCL and semimembraneous tendon</p>
<p><span style="text-decoration:underline;"> Posterior:</span> between lateral head of the gastroneimius and capsule., semimembraneous.                             </p>
<p style="text-align:center;"> <a rel="attachment wp-att-634" href="http://meherchilakalapudi.wordpress.com/2009/05/22/knee-carewhile-doing-work-at-computers/6barusa/"><img class="aligncenter size-full wp-image-634" title="6(barusa)" src="http://meherchilakalapudi.files.wordpress.com/2009/05/6barusa.png?w=213&#038;h=141" alt="6(barusa)" width="213" height="141" /></a></p>
<p><strong><strong>MUSCLES</strong>AROUND THE  KNEE JOINT</strong></p>
<h2>FLEXORS:</h2>
<p>Seven muscles flex the knee.  The knee flexors are the semi membranosus, semi-tendinosus, biceps femoris, sartorius, gracilis, popliteus, and gastrocnemius muscles.</p>
<p>All of the knee flexors except for the short head of the biceps femoris and the popliteus are two – joint muscles.  As two joint muscles their ability to produce effective force can be influenced by the relative positions of the two joints over which they pass.  Four of the flexors (the popliteus, gracilis, semi-membranosus and semi tendinosus muscles) are considered to medially rotate the tibia on the fixed femur where as the biceps femoris is considered to be a lateral rotator of the tibia.</p>
<p>The semi-tendinosus, semi membranous and the bicep femoris muscles are known collectively as the hamstrings.  These muscles all orginate on the ischial tuberosity of the pelvis.  The semi membranosus, semi-tendinosus insert on the posteromedial and antero medial aspects of the tibia respectively.  The semi membranosus muscle has fibres that attach to the medial meniscus.  This attachment assist in knee flexion by facilitating  posterior motion of the medial meniscus during active knee flexion.</p>
<p> </p>
<p>The <strong><span style="text-decoration:underline;">Biceps femoris</span> </strong>muscle has two heads, both of which insert on the lateral condyle of the tibia and the head of the fibula.  The biceps femoris tendon may be attached to iliotibial band and retinacular fibres of the lateral joint capsule.  The biceps femoris has a stabilizing role at the posterolateral aspect of the joint.  The short head of the biceps femoris does not cross the hip joint and therefore has a unique action at the knee joint.</p>
<p>The <strong><span style="text-decoration:underline;">Gastrocnemius</span></strong> muscle arises from the posterior aspects of the medial and lateral condyles of the femur by two heads. It inserts into the calcaneus.  It is the only muscle at the knee that crosses the ankle and the knee.  The gastrocnemius becomes actively insufficient quite easily.  Rather than working to produce knee flexion the gastrocnemius appears to be effective in presenting knee joint hyper extension, it appears to be less a mobility muscle at the knee joint than a dynamic stabilizer.</p>
<p> </p>
<p>The <span style="text-decoration:underline;">S<strong>artorius</strong></span><strong> </strong>muscle arises anteriorly from the anterior, superior, spine of the ilium and crosses the femur to insert into the anteromedial surface of the tibial shaft posterior to the tibial tuberosity although a flexor and medial rotator of the tibia, activity in the sartorius is more common with hip motion than the knee motion.</p>
<p>The <strong><span style="text-decoration:underline;">Gracilis</span></strong> muscle arises from the inferior half of the symphysis pubis arch and inserts on the medial tibia by way of a common tendon with the sartorius and the semitendinosus muscle.  It is not only a hip joint  flexor and adductor but it can also flex the knee joint and produce slight medial rotation of the tibia.</p>
<p> </p>
<p> </p>
<p>The <strong><span style="text-decoration:underline;">Popliteus</span></strong> is one joint knee flexor besides the short head of the biceps femoris is the relatively small popliteus muscle.  This muscle originates on the posterior aspect of the lateral femoral condyle and attaches on the medial aspect of the tibia. The popliteus muscle is a medial rotator of the tibia on the femur in an open kinematics chain, this muscle plays a very important role in initiating un-locking of the knee because it reverses the direction of the automatic rotation.</p>
<p> </p>
<h3> </h3>
<h3>EXTENSORS:</h3>
<p> </p>
<p>The four extensors of the knee are collectively known as the quadriceps femoris muscle. </p>
<p> </p>
<ol>
<li>Together the muscles of the quadriceps femoris extend the knee.</li>
</ol>
<p> </p>
<p> </p>
<h3> </h3>
<h3> </h3>
<h3>THE PATELLO FEMORAL JOINT</h3>
<p>The ability of the patella to perform its function without restricting knee motion depends on its mobility.  The patella has the ability to slide on the femoral condyles while remaining seated between them.  In full knee extension, the patella sits on the anterior surface on the distal femur.  With knee flexion, the patella slides distally on the femoral condoles, seating itself between the femoral condyles .  In full flexion the patella sinks into the inter condylar notch.  The sagittal plane motion of the patella is called patellar flexion.  Knee extension reverses the sliding of the patella and brings it back to the patella surface of the femur.  This motion of the patella is refered to as patellar extension.</p>
<p> </p>
<h3>PATELLO FEMORAL ARTICULAR SURFACES</h3>
<p>The triangularly shaped patella is distinguished by being the largest sesamoid bone in the body.  The articular surface of the patella is smaller than the femoral trochlear surface.  The posterior surface of the patella is covered by articular cartilage and divided by vertical ridge.  The patellar articulating surface of the femur is the inter condylar groove or femoral sulcus on the anterior aspects of the distal femur.</p>
<h4><span style="text-decoration:underline;">MUSCLE INVOLVEMENT IN KNEE FLEXION</span></h4>
<p> </p>
<p>1) <span style="text-decoration:underline;">Semi tendinosus.</span></p>
<p> </p>
<p>Root value         :   L4, L5,S1,S2</p>
<p>Action         :   Flexes and medially rotates the knee joint.</p>
<p> </p>
<p> </p>
<p> </p>
<p>2) <span style="text-decoration:underline;">Semi membranous</span></p>
<p> </p>
<p>Root value :   L4, L5,S1,S2</p>
<p>Action       :   Flexes and medially rotates the knee joint.</p>
<p> </p>
<p> </p>
<p>3) <span style="text-decoration:underline;">Biceps femoris</span></p>
<p> </p>
<h5>Root value  :  Long head – L5,S1, S2, S3</h5>
<p>Short head – L5,S1,S2</p>
<p>Action  :    The long and short head of the bicep femoris flex and laterally rotate the knee joint.</p>
<p> </p>
<p>4) <span style="text-decoration:underline;">Popliteus</span></p>
<p> </p>
<p>Root value   : L5, S1</p>
<p>Action  : Flexion and internal rotation of the knee joint.</p>
<p> </p>
<p> </p>
<h6><span style="text-decoration:underline;">MUSCLE INVOLVEMENT IN KNEE EXTENSION</span></h6>
<p>1) <span style="text-decoration:underline;">Rectus Femoris</span></p>
<p> </p>
<p>Root Value : L2, L3, L4.</p>
<p>Action     :   The quadriceps extends the knee joints.</p>
<p> </p>
<p> </p>
<p>2) <span style="text-decoration:underline;">Vastus Lateralis</span></p>
<p> </p>
<h5>Root Value  : L2, L4</h5>
<p> </p>
<p>3) <span style="text-decoration:underline;">Vastus medialis &amp; Vastus intermedius</span></p>
<p> </p>
<p>Root Value   :  L2,L4</p>
<p>Action     :   It draws articular capsule proximally.</p>
<p> </p>
<h1><strong>3. BIOMECHANICS</strong></h1>
<p> </p>
<p><strong><span style="text-decoration:underline;">KINEMATICS</span></strong><strong>:</strong>  Is the area of bio &#8211; mechanics that includes description of motion without regard for the forces reducing the motion. </p>
<p><strong><span style="text-decoration:underline;">KINETICS:</span></strong><strong>  </strong></p>
<p>It is the area of bio &#8211; mechanics concerned with the forced producing motion or maintaining equilibrium.</p>
<ol>
<li>Consequently the femoral and tibial longitudinal axis normally forms and angle medially at the knee joint of 185<sup>0</sup>  &#8211; 190<sup>0</sup> hence creating a physiological valgus angle at the knee. Although this might appear weight the lateral condyle more than the medial but this is not so.</li>
</ol>
<p> The mechanical axis of the lower extremity is the weight – bearing line from the center of the head of the femur to the center of the superior surface of the head of the talus.  This line normally passes through the center of the knee joint between the intercondylar tubercles.  Because the weight bearing lines follows the mechanical rather than anatomical axis, the weight bearing stresses on the knee joint in bilateral static stance are equally distributed between the medial  and lateral condoles.  If the medial tibiofemoral angle is greater than 195<sup>0</sup> and abnormal condition called genu valgum ( knock knees ) exists.  This condition will increase the compressive forces on the lateral condyle while increasing the tensile stresses on the medial structures.  If the medial tibiofemoral angle is 180<sup>0</sup> or less then resulting abnormality is called genuvarum ( bow leg ).  In this condition the compressor stresses on  the medial tibial condyle are increased whereas the tensile stresses are increased laterally.  In both cases constant overloading on the lateral or medial articular cartilage may result in damage to the cartilage.</p>
<h3>KINEMATICS</h3>
<h4><span style="text-decoration:underline;">SURFACE JOINT MOTION</span></h4>
<p>The motion between the articular surfaces of a joint occurs between the tibial and femoral condyles and between the femoral condyles and the patella.</p>
<p><span style="text-decoration:underline;">Tibiofemoral joint </span>surface motion takes place in all three planes, simultaneously minimal in the transverse planes.</p>
<p><span style="text-decoration:underline;">Patellofemoral joint</span> takes place in two planes, simultaneously the frontal and transverse but is greater in the frontal plane.</p>
<h3>TIBIO FEMORAL JOINT</h3>
<p>In a normal knee this line is tangential to the surface of the tibia for each interval of motion from full extension to full flexion demonstrating that the femur is gliding  on the tibial condyles.  The surface motion of the joint is from 90 of flexion  to full extension in normal knees.</p>
<h3>PATELLOFEMORAL JOINT</h3>
<p>The surface motion of the patellofemoral joint in the frontal plane may also be described by means of instant center technique.  This joint also has a gliding motion.  From full extension to full flexion of the patella glides approximately 7 cm on the femoral condyles.  Both, the medial and lateral facets of the femur articulate with the patella from full extension to 90 of flexion.  Beyond 90 of flexion the patella rotates externally and only the medial femoral facets articulates with the patella, at full flexion the patella sinks into the inter condylar groove.</p>
<h3>KINETICS</h3>
<p>Static and dynamic analysis of the forces and moments acting on a joint.  Static is the study of the forces and moments acting on a body in equilibrium (body at rest or moving at a constant speed ).  Dynamic is the study of the moments and forces acting on a body in motion.  Kinetic analysis allows one to determine the magnitude of the moments and forces on a joint produced by body weight, muscle action, soft tissue resistance and externally applied load in any situation that produces excessive high moments and forces.</p>
<h3>STATICS OF THE TIBIOFEMORAL JOINTS</h3>
<p>The three main coplanar forces acting on a free body and to the main moments acting about the joint under consideration to the minimum magnitude of the forces and moments.  Those forces are designated as vectors, if four characteristics are known that are magnitude, direction, sense line of  applicability and point of application.  The tibio femoral joint of the ground reaction force equal to body weight.  The tensile force through the patella tendon exerted by the  quadraceps muscles and the joint reactions force on the tibial  plateau .</p>
<h3>DYNAMICS OF THE TIBIOFEMORAL JOINTS</h3>
<p>The main forces considered in dynamic analysis are those produced by body weights muscles, soft tissues and externally applied loads.  Friction forces which are negligible in a normal joint are not considered.  In dynamic analysis two factors in addition to those in static analysis must be taken in to account.  That is the acceleration and the mass moment of inertia of the body part.  The steps for calculating the minimum magnitudes of the forces acting on a joint at a particular instance in time during a dynamic activity are as follows :</p>
<p> </p>
<p> </p>
<p>1)         The anatomic structures involved in the production of forces are identified.</p>
<p>2)         The angular acceleration of the moving body parts is determined.</p>
<p>3)         The mass moment of inertia of the moving body part is determined</p>
<p>4)         The torque (moment) acting about the joint is calculated.</p>
<p>5)         The magnitude of the main muscle forced accelerating the body part is calculated</p>
<p>6)         The magnitude of the joint reaction force at a particular instance of time is calculated by static analysis.</p>
<p> </p>
<h3>STATIC AND DYNAMICS OF PATELLO FEMORAL JOINTS</h3>
<p>The magnitude of the muscle forces acting on a joint directly affects the magnitude of the joint reaction force.  In general the greater the muscle force is, the greater is the joint reaction force. When the knee bends 90 this force is reached 2.5 – 3 times body weight with the knee flexed to the 90, during stair climbing and desent at a point when knee flexion reaches a maximum of 60 the peak value equals 3.3 of the body weight.</p>
<h3>ARTHROKINEMATICS</h3>
<h3>FLEXION/EXTENSION</h3>
<p>The large articular surfaces of the femur and the relatively small tibial condyle create problem as the femur begins to flex on the tibia . If the femoral condyles were permitted to roll posteriorly on the tibial condyles before much flexion had occurred. This would result in limitation of flexion or the femur would roll off the tibia.For the femoral condyles to continue to  roll with the increased flexion of the femur the condyles must simultaneously glide anteriorly on the tibial condyle to prevent them from rolling posteriorly off the tibial condyle.The first part of flexion of the femur from full extension(0-25) is primarily rolling of the femoral condyles on the tibia, bringing the contact of the femoral condyle posteriorly on the tibial condyle.as flexion continues the rolling is accompanied by a simultaneous glide just sufficient to create a nearly pure spin of the femur. Extension of the knee from flexion occurs initially as a rolling of the femoral condyles on the tibial condyles ,displacing the femoral condyles anteriorly back to the neutral position after the initial forward rolling, the femoral condyles glide posteriorly just enough to continue extension of the femur as an almost pure spin. The motion of the menisci with flexion and extension are an important components of the motions. The menisci reduce friction and absorb forces of the large femoral condyles on the small tibial condyle,The menisci must remain beneath the femoral condyles to continue their function. failure of the menisci result in limitation of joint motion.</p>
<p style="text-align:center;"> <a rel="attachment wp-att-635" href="http://meherchilakalapudi.wordpress.com/2009/05/22/knee-carewhile-doing-work-at-computers/7flexion/"><img class="aligncenter size-full wp-image-635" title="7(flexion)" src="http://meherchilakalapudi.files.wordpress.com/2009/05/7flexion.png?w=322&#038;h=172" alt="7(flexion)" width="322" height="172" /></a></p>
<p align="center"><strong></strong> </p>
<h4><span style="text-decoration:underline;">LOCKING AND UNLOCKING</span></h4>
<p>The incongurence of the femoral condyles result in a rolling and gliding of the condylar surfaces. The medial rotation of the femur that accompanies the final stages of knee extension is not voluntarily or produced by muscular forces it is reffered to as automatic or terminal rotation of the knee joint. It is also known as the locking mechanism or screw home mechanism of the knee. To initiate flexion the knee must first be unlocked that is medially rotated femur cannot flex in the saggital plane but must laterally rotate before flexion can proceed. A flexion force will automatically result in lateral rotation of the femur because the longer medial side will move before the shorter lateral side of the joint.</p>
<h5><span style="text-decoration:underline;">Q- angle: </span></h5>
<p>The pull of the quadriceps and the patellar ligament is commonly assessed by the Q angle of the knee. The Q angle is the angle formed between the line connecting the anterior superior iliac spine to the midpoint of the patella and a line connecting the tibial tubercle and the midpoint of the patella. An angle of 10-15 measured with the knee either in full extension or slightly flexed is normal .The increased angle among women has attributed to the fact that they have a wider pelvis , increased femoral anteversion and knee valgus. The Q angle is  measured with the knee in extension because excessive lateral forces may be a problem. A Q angle of 20<sup>0</sup> or more is considered to be abnormal.<strong><span style="text-decoration:underline;"> </span></strong></p>
<p style="text-align:center;"><strong><span style="text-decoration:underline;"><a rel="attachment wp-att-636" href="http://meherchilakalapudi.wordpress.com/2009/05/22/knee-carewhile-doing-work-at-computers/8flexion/"><img class="aligncenter size-full wp-image-636" title="8flexion" src="http://meherchilakalapudi.files.wordpress.com/2009/05/8flexion.png?w=143&#038;h=175" alt="8flexion" width="143" height="175" /></a> </span></strong></p>
<p align="center"> 4. PATHOLOGY:</p>
<p>A variety of mechanical , metabolic, genetic or constitutional insults may damage a synovial joint and trigger the need for repair. All the joints tissues (cartilage, bone, synovium , ligament, muscle)depend on each other for health and function. The OA process is metabolically active, involving new tissues production and remodeling of joint shape</p>
<p><strong><span style="text-decoration:underline;">ARTICULAR CARTILAGE</span></strong><strong></strong></p>
<p>It is a firm rubbery protein material covering the end of a bone. It acts as a cushion or shock absorber between the bones. When articular cartilage breaks down, this cushion is lost, and the bones will grind together. This causes the development of symptoms such as pain, swelling, bone spur formation and decreased motion. Osteoarthritis commonly affects weight bearing joints such as the knee, but it may affect any joint. Cartilage changes in OA are highly characteristics.</p>
<p>There is enzymatic degradation of the major structural components aggrecan and collagen principally by aggrecanase collagenase and stromelysin. The chondrocytes increase their production of matrix components and divide to produce clones or nests of metabolically active chondrocytes. Although the turnover of aggrecan components is increased  the concentration of aggrecan eventually falls. The decrease in size of the hydrophilic aggrecan molecules increases the water concentration and swelling pressure in cartilage, further disrupting the retaining scaffolding of type 2 collagen and making the cartilage vulnerable to load bearing injury. There is eventual fissuring of the cartilage surface( fibrillation), development of deep vertical clefts, localized chondrocyte death and decrease in cartilage thickness.</p>
<p>Cartilage loss is focal rather than widespread and usually restricted to the maximum load bearing part of the joint. The changes in OA cartilage encourage deposition of calcium pyrophosphate and apatite crystals. Damage to the &#8216;articular&#8217; or hyaline cartilage can be as a result of sporting trauma, but is more commonly due to repetitive stresses over a long period of time. This is usually associated with habitual overuse of a joint for occupational or sporting purposes. In most individuals, the signs and symptoms do not appear until middle age, but the disease process starts much earlier. The hyaline cartilage is normally ultra smooth to allow friction free movement, but early damage to the superficial layers produces a frayed appearance.</p>
<p>The joint cartilage layer in osteoarthritis becomes thinner and eventually disappears entirely so that only raw  bone ends come in contact with each other. The grating of raw bone ends in direct contact causes pain and stiffness in the knee joint. The contours of the bone ends enlarge and bone spurs forms at the periphery of the knee joint. Joint capsule enlarges and becomes thicker. The joint cartilage damage may be restricted only to one knee joint compartment, as in this picture, or the whole knee joint may be affected by osteoarthritic changes. The complete loss of articular cartilage exposes the bone to stresses. It then undergoes intense osteogenic response. This phenomenon spreads towards the joint margins where unimpeded by compressive forces and perhaps encouraged by traction of capsular attachements it produces osseous outgrowth that extend into the capsule.</p>
<p><strong><span style="text-decoration:underline;">BONE</span></strong>:</p>
<p>The bone immediately below the compromised cartilage increases its trabecular thickness. In some cases it reflects healed trabecular microfractures. Holes often develop possibly the result of small areas of osteonecrosis caused by the increased pressure in bones as the cartilage fails in its load transmitting function. At the margins of the joint there is fibrocartilage that then undergoes endochondral ossification to form osteophytes. Despite central and marginal new bone formation , with severe cartilage loss there may be attrition of bone as the two unprotected bone ends wear on each other. Such wear may ablate the trabeculae and lead to a smooth ,shiny, surface (eburnation) often with deep linear grooves. Bone remodeling and cartilage thinning slowly alters the shape or the OA joint increasing its surface. The bone surfaces become hard and polished as there is loss of protection from cartilage. In time, with more stress, this frayed surface evolves into cracks or &#8216;fissures&#8217; in the articular cartilage, which can leave the underlying bone exposed. With continued wear the underlying bone takes on a polished appearance and eventually micro-fractures appear. The body&#8217;s natural reaction is to attempt to heal the micro-fractures by laying down more bone.</p>
<p><strong><span style="text-decoration:underline;">SYNOVIUM</span></strong><strong> </strong></p>
<p>The joint cartilage layer in osteoarthritis becomes thinner and eventually disappears entirely so that only raw bone ends come in contact with each other. The grating of raw bone ends in direct contact causes pain and stiffness in the knee joint. The contours of the bone ends enlarge and bone spurs forms at the periphery of the knee joint. Joint capsule enlarges and becomes thicker. The quantity of joint fluid increases (effusion). Distended joint capsule makes the movements in the knee joint painful. Small fragments of damaged joint cartilage float in the joint fluid and cause secondary inflammation of the synovium (the inner lining of the joint space) with swelling and pain in the knee joint. The joint cartilage damage may be restricted only to one knee joint compartment, as in this picture, or the whole knee joint may be affected by osteoarthritic changes. In knee joints with changes restricted to only one knee joint compartment, it is most often the inner (medial) compartment that is damaged by OA, whereas other compartments may have still healthy cartilage. The presence of knee effusion, increased warmth CPPD crystals deposition and obesity are suggested risk factors. The capsule undergoes fibrous degeneration and low grade chronic inflammatory changes. The outer (lateral) compartment has healthy joint cartilage (light blue) and meniscus (dark blue).</p>
<p> </p>
<p style="text-align:center;"> <a rel="attachment wp-att-637" href="http://meherchilakalapudi.wordpress.com/2009/05/22/knee-carewhile-doing-work-at-computers/9synoviyam/"><img class="aligncenter size-full wp-image-637" title="9synoviyam" src="http://meherchilakalapudi.files.wordpress.com/2009/05/9synoviyam.png?w=228&#038;h=170" alt="9synoviyam" width="228" height="170" /></a></p>
<p> </p>
<p>It is at the knee that the predisposition to calcium pyrophosphate dehydrate (CPPD) crystals deposition by OA is most evident. Such coexistent crystals deposition may result in a more overt inflammatory component (stiffness, effusions)and supergradedacute of synovitis.</p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;">CAPSULE</span></strong></p>
<p>The capsule undergoes fibrous degeneration and low grade chronic inflammatory changes. The presence of knee effusion ,increased warmth CPPD crystals deposition deposition and obesity are suggested risk factors. The capsule undergoes fibrous degeneration and low grade chronic inflammatory changes. The bone surfaces become hard and polished as there is loss of protection from cartilage.  Articular cartilage: the complete loss of articular cartilage exposes the bone to stresses. It then undergoes intense osteogenic response. This phenomenon spreads towards the joint margins where unimpeded by compressive forces and perhaps encouraged by traction of capsular attachements it produces osseous outgrowth that extend into the capsule</p>
<p>Damage to the &#8216;articular&#8217; or hyaline cartilage can be as a result of sporting trauma, but is more commonly due to repetitive stresses over a long period of time. This is usually associated with habitual overuse of a joint for occupational or sporting purposes. In most individuals, the signs and symptoms do not appear until middle age, but the disease process starts much earlier. The hyaline cartilage is normally ultra smooth to allow friction free movement, but early damage to the superficial layers produces a frayed appearance. In time, with more stress, this frayed surface evolves into cracks or &#8216;fissures&#8217; in the articular cartilage, which can leave the underlying bone exposed.</p>
<p>With continued wear the underlying bone takes on a polished appearance and eventually micro-fractures appear. The body&#8217;s natural reaction is to attempt to heal the micro-fractures by laying down more bone. This leads to a thickening of the underlying layer of bone, which can be seen on x-ray. Another common feature on x-ray is the formation of bone cysts in the layer of bone beneath the joint surface, together with a flattening of the articular surfaces as the body attempts to spread the surface area over which forces are transmitted.</p>
<p>As well as changes in the articular cartilage and underlying bone, osteoarthritis produces soft tissue changes. As the joint becomes deformed by bony changes the ligaments which support the joint become stretched. There is also thickening and distension of the capsule that surrounds the joint and both of these features get progressively worse.</p>
<h4><span style="text-decoration:underline;">LIGAMENTS</span></h4>
<h4>The ligaments undergo laxity</h4>
<h4> </h4>
<p><strong><span style="text-decoration:underline;">MUSCLES</span></strong></p>
<p style="text-align:center;"> <a rel="attachment wp-att-638" href="http://meherchilakalapudi.wordpress.com/2009/05/22/knee-carewhile-doing-work-at-computers/10muscils/"><img class="aligncenter size-full wp-image-638" title="10muscils" src="http://meherchilakalapudi.files.wordpress.com/2009/05/10muscils.png?w=321&#038;h=213" alt="10muscils" width="321" height="213" /></a></p>
<p>They undergo atrophy and wasting.</p>
<p> <span style="text-decoration:underline;"> ETIOLOGY</span></p>
<h4><span style="text-decoration:underline;"> </span></h4>
<h4><span style="text-decoration:underline;">THERE ARE TWO FORMS  OF OSTEOARTHRITIS</span></h4>
<p><strong><span style="text-decoration:underline;">THE IDIOPATHIC OA</span></strong> &#8211; as the name idiopathic says, no one knows for sure what causes the destruction of the joint cartilage</p>
<p><strong><span style="text-decoration:underline;">THE SECONDARY OA</span></strong><strong><span style="text-decoration:underline;"> </span></strong> - the damage to the cartilage in the knee joint has a known cause such as previous fracture through the joint surfaces or damage to the meniscus or ligaments of the knee joint.</p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<h3>ETIOLOGY:</h3>
<p>No one knows for sure the exact cause of osteoarthritis. There are two types of osteoarthritis : primary and secondary</p>
<h4> </h4>
<p><strong>1)</strong><strong><span style="text-decoration:underline;">Age:</span></strong></p>
<p>The process  starts in the second decade of life but the degenerative diseases     are not apparent until middle age by 55-65yrs.</p>
<p><strong>2)</strong><strong><span style="text-decoration:underline;">Sex:</span></strong><strong></strong></p>
<p>It is twice as common in women as in men over the age of 50.In men the order of the joint involved is hip, spine, ankle, shoulders, fingers. In women the order is knee, fingers, spine, hip, ankle, shoulders.</p>
<p> </p>
<p><strong>3)</strong><strong><span style="text-decoration:underline;">Hereditary: </span></strong><strong></strong></p>
<p> </p>
<p>Herberden’s nodes may be inherited  as a single autosomal gene, sex influnced to be dominant in females Genetic factors are very important. Genes may affect <em>collagen</em>, one of the main building blocks of cartilage, or the way the bone reacts and repairs itself, or even the inflammatory process.</p>
<p><span style="text-decoration:underline;"> </span></p>
<p><span style="text-decoration:underline;"> </span></p>
<p><strong>4)</strong><strong><span style="text-decoration:underline;">Obesity:</span></strong><strong></strong></p>
<p>            Due to lack of exercise it is very common in obese people. Due to defective lubricating mechanism and uneven nutrition of the articular cartilge.</p>
<p> </p>
<p><strong>5)</strong><strong><span style="text-decoration:underline;">Poor Posture</span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<h4><span style="text-decoration:underline;">The Secondary causes are</span></h4>
<p> </p>
<p><strong>1)</strong><span style="text-decoration:underline;">Trauma:</span>it is more important risk factor in men and may result in unilateral OA. Most knee OA particularly in women is bilateral and symmertrical.</p>
<p> <span style="text-decoration:underline;"> </span></p>
<p><strong>2)  </strong>Dislocations</p>
<p><strong>3)  </strong>Infections like TB of knee joint</p>
<h3> </h3>
<p> </p>
<h3>6.CLINICAL FEATURES</h3>
<p>OA knee targets the patella-femoral and the medial tibiofemoral compartments of the knee. The pain is localized to the anterior aspect or medial aspect of the knee and upper tibia. Patello-femoral pain is usually worse going up and down stairs or inclines. Posterior knee pain suggests a complicating popliteal cyst. Commonly functional difficulties are prolonged walking, rising, from the chair, getting in or out the car or bending to put on shoes and socks. As the disease progresses  patient finds it difficult  to walk, to do her or his daily activities of daily living, climb stairs, unable to kneel or squatt.</p>
<p> </p>
<p><strong>1)</strong><strong><span style="text-decoration:underline;">pain:</span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong> </strong>Due to inflammation in response to joint irritation caused by interference by loose bodies, cartilagenous debris engulfed by synovium. This appears with exercise but can be ‘walked off’.  Pain becomes severe as varus deformity increases.The types of pain are <span style="text-decoration:underline;">aching</span>: fleeting and constant, <span style="text-decoration:underline;">refered</span> :passing down the limb,sharp <span style="text-decoration:underline;">stabbing</span>: loose bodies, <span style="text-decoration:underline;">throbbing: </span>mainly at night. Pain with changes in weather are possible. Pain may be related to OA directly through increased pressure in subchondral bone, trabecular fractures , capsular distension and low grade synovitis or bursitis.</p>
<p>Pain in OA may be attributed to incongruent articulations of joint surfaces, periosteal elevation secondary to bone proliferation at the joint margin, abnormal pressures on sub-chondral bone, trabecular micro fractures and distention of the joint capsules.  Many patients will also experience a secondary synovitis especially when the knee is involved.</p>
<p><strong>2)</strong><strong><span style="text-decoration:underline;">crepitus:</span></strong></p>
<p> the joint is often felt to creak and grate and swell from time to time especially  after any activity involving flexed position. Eg: kneeling and squatting As the disease progresses  patient finds it difficult  to walk, to do her or his daily activities of daily living, climb stairs, unable to kneel or squatt.</p>
<p> </p>
<p><strong>3)</strong>there will be swelling and inflammation of synovium the patient feels stiffness, tightness and reddness around the joint. Stiffness loosens quickly with rest</p>
<p> </p>
<p> </p>
<p><strong>4)</strong><strong><span style="text-decoration:underline;">tenderness</span></strong> is elicited all round the joint</p>
<p> </p>
<p><strong>5)</strong>muscle spasm is present.</p>
<p> </p>
<p> </p>
<p><strong>6)</strong>A jerky asymmetric antalgic gait is present where there is less time weight bearing on the painful side.</p>
<p> </p>
<p><strong>7)</strong>Weakness and wasting of the quadriceps muscle</p>
<p> </p>
<p><strong> <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> </strong>Joint line or periarticular tenderness.</p>
<p> </p>
<p> </p>
<p><strong>9)</strong><strong><span style="text-decoration:underline;">Young onset OA</span></strong><strong>: </strong></p>
<p> </p>
<p>Some patients present with typical symptoms and clinical signs of OA but are younger than the expected(&lt;45years).In most of the cases they have OA at a single joint such as the knee and the explanation of previous trauma is apparent in their history.</p>
<p> </p>
<p><strong> </strong></p>
<p><strong>10)</strong> Changes in the weather may make a difference in some people. All joints have nerve endings which are sensitive to pressure. The nerve endings may respond to the drop in atmospheric pressure which occurs before it rains.</p>
<p> </p>
<h3>CLINICAL SIGNS</h3>
<p>1.  Restricted movements.</p>
<p>2.  Palpable sometimes audible, coarse crepitus.</p>
<p>3.  Boney swellings around the joint margins. Deformity usually without instability</p>
<p>4.  Joint line or periarticular tenderness.</p>
<p>5.  Muscle wasting or weakness.</p>
<p>6.  No or only mild synovitis (effusion, increased warmth)</p>
<p>OA does not have a bilateral, symmetrical presentation. A single joint or any combination of joints on one individual may be affected. OA is not a systemic disease and it is therefore not associated with systemic complaints such as generalized morning stiffness, fever or loss of appetite.  Individual with OA may experience some stiffness in particular joints upon awakening that is similar to the stiffness felt when mobilizing the same joints after inactivity during the day but this stiffness does not last long.</p>
<p>CREPITUS is a common clinical finding.</p>
<h4><span style="text-decoration:underline;">List of symptoms of Osteoarthritis</span></h4>
<p>The list of symptoms mentioned in various sources for Osteoarthritis includes:</p>
<ul>
<li><a href="http://www.wrongdiagnosis.com/sym/joint_symptoms.htm">Joint symptoms</a>
<ul>
<li><a href="http://www.wrongdiagnosis.com/sym/joint_pain.htm">Joint pain</a></li>
<li><a href="http://www.wrongdiagnosis.com/sym/stiff_joints.htm">Joint stiffness</a></li>
<li><a href="http://www.wrongdiagnosis.com/sym/joint_pain.htm">Joint tenderness</a></li>
<li><a href="http://www.wrongdiagnosis.com/sym/joint_pain.htm">Tenderness around joint</a></li>
<li>Joint pain worse with exercise</li>
<li><a href="http://www.wrongdiagnosis.com/sym/stiffness.htm">Stiffness</a></li>
<li><a href="http://www.wrongdiagnosis.com/sym/movement_symptoms.htm">Difficulty moving</a></li>
<li><a href="http://www.wrongdiagnosis.com/sym/movement_symptoms.htm">Slow movement</a></li>
<li><a href="http://www.wrongdiagnosis.com/sym/stiffness.htm">Morning stiffness</a></li>
<li>Joint stiffness improves with movement</li>
<li><a href="http://www.wrongdiagnosis.com/sym/pain.htm">No pain</a> &#8211; not everyone gets pain in the joints.</li>
<li>Crunching feeling</li>
<li>Crunching sound</li>
<li>Joint grating sensations</li>
<li><a href="http://www.wrongdiagnosis.com/sym/joint_swelling.htm">Swollen joints</a></li>
<li><a href="http://www.wrongdiagnosis.com/sym/stiff_joints.htm">Joint locking</a></li>
<li>Joint symptoms worse in cold or damp weather</li>
<li>Joint symptoms worse after periods of inactivity</li>
<li>Other common symptoms:
<ul>
<li><a href="http://www.wrongdiagnosis.com/sym/fever.htm">Fever</a></li>
<li><a href="http://www.wrongdiagnosis.com/sym/weight_loss.htm">Weight loss</a></li>
<li><a href="http://www.wrongdiagnosis.com/sym/fatigue.htm">Fatigue</a></li>
<li><a href="http://www.wrongdiagnosis.com/sym/breathing_difficulties.htm">Trouble breathing</a></li>
<li><a href="http://www.wrongdiagnosis.com/sym/rash.htm">Rash</a></li>
<li><a href="http://www.wrongdiagnosis.com/sym/itching_skin.htm">Itch</a></li>
<li>Symptom area depends on the affected joint</li>
<li><a href="http://www.wrongdiagnosis.com/sym/knee_symptoms.htm">Knee symptoms</a>
<ul>
<li><a href="http://www.wrongdiagnosis.com/sym/knee_pain.htm">Knee joint pain</a></li>
<li><a href="http://www.wrongdiagnosis.com/sym/stiff_knee.htm">Knee stiffness</a></li>
<li><a href="http://www.wrongdiagnosis.com/sym/knee_symptoms.htm">Knee warmth</a></li>
<li>Knee instability</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<h4> </h4>
<p> </p>
<h4><span style="text-decoration:underline;">7. INVESTIGATIONS</span></h4>
<p>The diagnosis and assessment of common OA are purely clinical.</p>
<h5>1)<span style="text-decoration:underline;">X-RAY</span></h5>
<p>A plain radiograph is the only useful but  non  essential  investigation. This may show one or more of the typical features of OA namely focal narrowing of joint space, marginal osteophyte, subchondral sclerosis, cysts, osteochondral loose bodies and deformity. Chondrocalcinosis may be an additional feature at the knee. The main use of a radiograph is to assess severity of structural changes, an issue if surgery is being considered. Standing AP radiograph are needed to assess tibio-femoral cartilage loss and a flexed skyline view is the best show the space between the bones narrowing as the cartilage thins, and changes in the bone such as spurs.</p>
<p style="text-align:center;"> <img class="aligncenter size-full wp-image-639" title="11xray" src="http://meherchilakalapudi.files.wordpress.com/2009/05/11xray.png?w=246&#038;h=150" alt="11xray" width="246" height="150" /> </p>
<h5><span style="text-decoration:underline;">2) SYNOVIAL FLUID ASPIRATION</span></h5>
<p>From the OA knees shows variable characteristics but is predominantly viscous with low turbidity. CPPD crystals may be identified up to 50%of knee OA fluids.</p>
<p> </p>
<p> </p>
<p><strong><span style="text-decoration:underline;">3) RADIO ISOTOPE BONE SCANS</span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p>It is performed for other reasons often show an incidental finding, discrete increased uptake in OA joints due to bone remodelling.</p>
<p> </p>
<p><strong><span style="text-decoration:underline;">4) MAGNETIC RESONANCE IMAGING</span></strong></p>
<p>Is also performed.<em> MRI</em> scan of the knee can be helpful. This shows the soft tissues (e.g. cartilage, tendons, muscles)  which cannot be seen on an x-ray. Sometimes, a MRI scan may be ordered to look at the knee more closely. A MRI scan is a special radiological test where magnetic waves are used to create pictures that look like slices of the knee. The MRI scan shows more than the bones of the knee. It can show the ligaments, articular cartilage, and menisci as well. The MRI scan is painless, and requires no needles or dye to be injected</p>
<p> </p>
<p>         </p>
<p>                                     </p>
<p>        </p>
<h6><span style="text-decoration:underline;">8. MEDICAL MANAGEMENT</span></h6>
<p>It consists of conservative means and surgical means.</p>
<h4><span style="text-decoration:underline;">CONSERVATIVE MEANS</span></h4>
<p>Includes reduced activity, weight loss, supports and braces, and physiotherapy are important initial steps in managing patients with osteo-arthritis of the knee. If these approaches fail, more aggressive treatment is warranted.</p>
<p><strong><span style="text-decoration:underline;">1) REDUCED ACTIVITY</span></strong></p>
<p> Decreased physical activity can diminish symptoms or reduce the incidence of osteoarthritic flares in some patients. However, these advantages must be balanced against the negative aspects of decreased activity, including weight gain, diminished general fitness, and increased quadriceps weakness. It is preferable, and for many patients nonnegotiable, that they maintain or increase their level of physical activity. A pragmatic approach is to eliminate or decrease involvement in activities that are more likely to ignite an osteoarthritic flare, such as high-impact and pivoting sports. Safe exercise programs should be designed to be encouraged.Many people with osteoarthritis of the knee are overweight. Studies have shown that people who lose weight have fewer knee problems in the future improve function and conditioning levels.</p>
<p><strong>2)</strong><strong><span style="text-decoration:underline;">WEIGHT LOSS</span></strong><strong></strong></p>
<p> </p>
<p>Weight loss can ameliorate the severity of symptoms, but it is less certain whether weight loss can slow the rate of disease progression. Unfortunately, weight loss can be difficult to achieve and maintain. Many patients have pain and functional disability that limit or preclude exercise. For these patients weight loss is exceptionally difficult to achieve. They may require intervention to improve their knee pain before they can exercise at adequate levels to achieve weight loss. Low-impact alternatives, such as cycling or water-based exercises, should than those who do not. Being overweight is also bad for your general health and increases the risk of heart disease, strokes and diabetes. So you should eat a balanced, healthy diet and keep your weight as close as possible to the ideal for your height and age. Swimming can be a very good way of exercising and keeping fit as it causes little pain. Water supports the body’s weight so that little force goes through the joints as you exercise. Also, warm water relaxes muscles and joints and is very soothing, allowing joints to move more freely. Prescribed exercises in a hydrotherapy pool can help get muscles and joints working better, without undue pain. Supervised swimming in natural spa waters is an ancient treatment.  It is the exercise that helps rather than any healing properties of the water itself!</p>
<p style="text-align:center;"><a rel="attachment wp-att-640" href="http://meherchilakalapudi.wordpress.com/2009/05/22/knee-carewhile-doing-work-at-computers/12waitloss/"><img class="aligncenter size-full wp-image-640" title="12waitloss" src="http://meherchilakalapudi.files.wordpress.com/2009/05/12waitloss.png?w=222&#038;h=372" alt="12waitloss" width="222" height="372" /></a>                                                </p>
<p>                                            </p>
<p> </p>
<p><strong>4)</strong><strong><span style="text-decoration:underline;">SUPPORTS AND BRACES</span></strong><strong></strong></p>
<p> </p>
<p>Some patients experience subjective improvement with the use of an elastic support around the knee. This approach must be used cautiously, if at all, in patients with a history of deep venous thrombosis or lower limb edema. Although the efficacy of this approach is not well established, it may enhance stabilizing inputs from articular and periarticular sensory receptors onto the motoneurons of muscles acting across the knee. Bracing can be effective in patients who have relatively focused unicompartmental medial or lateral osteoarthritis. Thus, valgus bracing is often used for varus knees in which osteoarthritis primarily involves the medial compartment. Less frequently, varus bracing may be indicated for lateral-compartment osteoarthritis associated with a valgus deformity. Unfortunately, even when a brace is prescribed and found to be efficacious, many patients stop using it after a short time.</p>
<p> </p>
<p><strong>5)</strong><strong><span style="text-decoration:underline;">DRUGS</span></strong><strong></strong></p>
<p> </p>
<p>Painkillers (such as paracetamol) and anti-inflammatory and paprika (capsaicin) creams to rub into the knee can help pain and stiffness. Some people find them more helpful than others. Anti-inflammatory drugs (NSAIDs) help some people more than paracetamol but they can cause stomach ulcers. Some new types of NSAIDs called COX-2s have the same beneficial effects but with less risk of stomach problems. However, all NSAIDs may cause other side-effects such as rashes, headaches and wheeziness. Sometimes an injection of steroids may help, either into a tender spot around the knee or even into the joint itself. The effect can last for several months. Injections of hyaluronan (Synvisc, Hyalgan, Durolane) may also help by supplementing the joint’s natural synovial fluid, although not everyone benefits from this type of treatment.<strong> </strong></p>
<p>     </p>
<h4>·        <span style="text-decoration:underline;">NSAIDS ( NON-STEROIDAL ANTI- INFLMATORY DRUGS)</span></h4>
<p>NSAIDS are a basic element in long term treatment having both analgesic and anti-inflamatory actions.  At lower doses the NSAID effect is analgesic, through the peripheral  inhibition of pro-inflamatory prostaglandin synthesis.  At higher doses, the effect is anti-inflamatory probably through both prostaglandin inhibition and alteration in macrophage and neutrophil function although NSAIDS provide symptomatic relief, they do not alter the underlined disease process.  The major serious and the most common side effects are gastro intestinal complaints ranging from nausea to gastro intestinal bleeding in ulcers.  Other possible side effects include dizziness headache doziness or tinnitus, kidney dysfunction and elevation of liver enzymes.</p>
<h4>·        <span style="text-decoration:underline;">CAPSAICIN</span></h4>
<p>In the past few years, topical capsaicin cream has been introduced as a symptomatic therapy for osteoarthritis. The neuropeptide substance P is present in A delta- and C-fiber sensory nerves. It has been implicated as a transmitter in articular pain pathways and is a putative contributor to joint inflammation. Application of capsaicin to A delta- and C-fibers depletes them of substance P, blocking subsequent intraneuronal axonal transport and synthesis of substance P.16 Thus, capsaicin can potentially ameliorate symptoms of osteoarthritis through 2 mechanisms: by attenuating transmission in articular pain pathways and by diminishing joint inflammation. Studies support the safety and efficacy of capsaicin for treating osteoarthritis of the knee.Unfortunately, people in warm climates may have difficulty using capsaicin. They may experience an extreme burning sensation when using capsaicin and exercising strenuously in a hot environment.</p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<p><strong><span style="text-decoration:underline;"> </span></strong></p>
<ul>
<li><strong><span style="text-decoration:underline;">ANTIDEPRESSANTS</span></strong><strong></strong></li>
</ul>
<p>                      The role, if any, of antidepressants such as amitriptyline in managing symptomatic osteoarthritis of the knee is unclear. Some patients with osteoarthritis of the knee exhibit chronic pain behavior.</p>
<h6>·        <span style="text-decoration:underline;">CHONDROITIN AND GLUCOSAMINE</span></h6>
<p>                          Chondroitin and glucosamine have become popular as putative symptomatic and disease-modifying therapies for osteoarthritis of the knee. It is hypothesized that these important constituent components of cartilage matrix can be ingested, enter plasma, cross the blood-synovial fluid barrier, and enter synovial fluid in sufficient concentrations to provide analgesia and promote cartilage healing. The mechanisms, if any, underlying these alleged therapeutic effects remain undiscovered. Some clinical trials data are starting to become available. Given their widespread use by patients, it is fortunate that these products appear to be safe, and they can be a useful adjunct, especially in patients who cannot tolerate NSAIDs</p>
<ul>
<li><strong><span style="text-decoration:underline;">VISCOSUPPLEMENTATION</span></strong></li>
</ul>
<p>                          Hyaluronan (HA) in both articular tissues and synovial fluid plays an important role in contributing to joint homeostasis and maintaining normal joint function. In osteoarthritis, the molecular weight and concentration of HA is diminished. This has led to the concept of visco supplementation, in which pathologic synovial fluid is aspirated and HA-based products are injected into the articular space in order to restore HA concentration and molecular weight to relatively normal levels. Therapeutic benefit is believed to occur by restoring the viselastic, antinociceptive, anti-inflammatory, and autoregulatory functions of HA in synovial fluid.</p>
<p>Clinical studies of HA-based products have demonstrated clinical benefit for about 70% of patients in a heterogenous osteoarthritis population. Duration of improvement ranges from months to years.