C Programming in Linux 1st Edition by David Haskins 9788776814724 PDF Download
C Programming in Linux 1st Edition by David Haskins 9788776814724 PDF Download
_____ Follow the link below to get your download now _____
https://ebookball.com/product/c-programming-in-linux-1st-
edition-by-david-haskins-9788776814724-15294/
https://ebookball.com/product/programming-in-objective-c-1st-edition-
by-stephen-kochan-067248420x-9780672484209-15258/
https://ebookball.com/product/programming-in-c-4th-edition-by-stephen-
kochan-9780132781190-0132781190-15230/
https://ebookball.com/product/memory-as-a-programming-concept-in-c-
and-c-1st-edition-by-frantisek-franek-
isbn-0521520436-9780521520430-12552/
https://ebookball.com/product/memory-as-a-programming-concept-in-c-
and-c-1st-edition-by-frantisek-franek-
isbn-051183862x-9780511838620-14982/
Functional Programming in C 1st Edition by Enrico Buonanno
ISBN 9781638354048 1638354049
https://ebookball.com/product/functional-programming-in-c-1st-edition-
by-enrico-buonanno-isbn-9781638354048-1638354049-15788/
https://ebookball.com/product/object-oriented-programming-in-c-1st-
edition-by-nabajyoti-barkakati-0672228009-9780672228001-15280/
https://ebookball.com/product/linux-assembly-language-programming-1st-
edition-by-bob-neveln-isbn-0130879401-9780130879400-12096/
https://ebookball.com/product/object-oriented-programming-in-c-4th-
edition-by-robert-lafore-0672323087-9780672323089-16228/
https://ebookball.com/product/object-oriented-programming-in-c-4th-
edition-by-robert-lafore-isbn-9780132714297-0132714299-15546/
FREE ST U DY BOOKS
C PROGRAMMING
IN LINUX
DAVID HASKINS
C Programming in Linux
2
C Programming in Linux
© 2009 David Haskins & Ventus Publishing ApS
ISBN 978-87-7681-472-4
3
C Programming in Linux Contents
Contents
About the author, David Haskins 7
Introduction 8
4
C Programming in Linux Contents
www.simcorp.com
5
C Programming in Linux Contents
7. Apache C modules 73
7.1 Safer C web applications 73
7.2 Adding some functionality 76
7.3 Apache Modules Conclusion 77
Conclusion 84
www.maersk.com/mitas
6
C Programming in Linux About the author, David Haskins
I was born in 1950 in Chelsea, London, but grew up in New Zealand returning to England in 1966. I
have worked in the computer industry since 1975 after a couple of years as a professional drummer.
My first experience was five years as a mainframe hardware engineer for Sperry Univac (now Unisys)
followed by 14 years as an analyst programmer with British Telecom in London.
While engaged in a complex task of converting large quantities of geographical data (map coordinate
references) I discovered the joys of C – its speed and efficiency. That was in 1985 and I have been a
fan of C ever since.
Since 1994 I have been a senior lecturer at the Faculty of Computing, Information Systems and
Mathematics at Kingston University, London. This is a mostly technical university that evolved from
a former polytechnic college with a long tradition of aeronautical engineering.
I am engaged mainly in teaching many computer languages and internet systems design to a large and
multicultural student body.
Most of my academic research and commercial consultancy has been involved with spatial systems
design and the large data volumes and necessary processing efficiency concerns has led me to
concentrate on C and C++. My teaching web site is at www.ubiubi.org which shows some of this
material.
A keen Open Systems enthusiast, I have exclusively centred all my teaching on the Linux platform
since 2002 and Kingston University is well advanced in delivering dual boot facilities for all its
student labs.
I am a keen swimmer and in 2009 completed the annual Lorne Pier-to-Pub race in Victoria, Australia
which is the largest open-sea swimming race in the world where 4,500 people of all ages swim each
January as the shark-spotting planes fly overhead.
When not teaching I am a keen vegetable gardener and amateur musician, playing in jazz groups and in
Scottish bagpipe bands. I play the drums, the great highland bagpipe, the clarinet, the guitar and the piano.
7
C Programming in Linux Introduction
Introduction
Because the C language is like Latin - it is finite and has not changed for years. C is tight and spare,
and in the current economic climate we will need a host of young people who know C to keep existing
critical systems running.
C is built right into the core of Linux and Unix. The design idea behind Unix was to write an
operating system in C so all you needed to port it to a new architecture was a C compiler. Linux is
essentially the success story of a series of earlier attempts to make a PC version of Unix.
A knowledge of C is now and has been for years a pre-requisite for serious software professionals and
with the recent popularity and maturity of Open Systems this is even more true. The terseness and
perceived difficulty of C saw it being ousted from university teaching during the late 1990s in favour
of Java but there is a growing feeling amongst some teaching communities that Java really is not such
a good place to start beginners.
Students paradoxically arrive at colleges knowing less about computing than they did ten years ago as
programming is seen as too difficult for schools to teach. Meanwhile the body of knowledge expected
of a competent IT professional inexorably doubles every few years.
Java is commonly taught as a first language but can cause student confusion as it is in constant flux, is
very abstract and powerful, and has become too big with too many different ways to do the same
thing. It also is a bit “safe” and insulates students from scary experiences, like driving with air-bags
and listening to headphones so you take less care. The core activity of writing procedural code within
methods seems impenetrable to those who start from classes and objects.
So where do we start? A sensible place is “at the beginning” and C is as close as most of us will ever
need to go unless we are becoming hardware designers. Even for these students to start at C and go
further down into the machine is a good idea.
C is like having a very sharp knife which can be dangerous, but if you were learning to be a chef you
would need one and probably cut yourself discovering what it can do. Similarly C expects you to
know what you are doing, and if you don't it will not warn before it crashes.
A knowledge of C will give you deep knowledge of what is going on beneath the surface of higher-
level languages like Java. The syntax of C pretty-well guarantees you will easily understand other
languages that came afterwards like C++, Java, Javascript, and C#.
C gives you access to the heart of the machine and all its resources at a fine-grained bit-level.
8
C Programming in Linux Introduction
C has been described as like “driving a Porsche with no brakes” - and because it is fast as well this can
be exhilarating. C is is often the only option when speed and efficiency is crucial.
C has been called “dangerous” in that it allows low-level access to the machine but this scariness
is exactly what you need to understand as it gives you respect for the higher-level languages you
will use.
Many embedded miniaturised systems are all still written in C and the machine-to-machine world of
the invisible internet for monitoring and process control often uses C.
Hopefully this list of reasons will start you thinking that it might be a good reason to have a go at
this course.
References
MySQL C library
http://dev.mysql.com/doc/refman/5.1/en/index.html
Apache
http://httpd.apache.org/docs/2.2/developer/
A Source Code Zip File Bundle is supplied with this course which contains all the material described
and a Makefile.
9
C Programming in Linux Introduction
I began university teaching later in life after a career programming in the telecommunications industry.
My concern has been to convey the sheer fun and creativity involved in getting computers to do what
you want them to do and always try to give useful, practical, working examples of the kinds of things
students commonly tell me they want to do.
Learning a language can be a dry, boring affair unless results are immediate and visible so I tend to use
the internet as the input-output channel right from the start.
My aim is to get students confident and up to speed quickly without all the nightmare associated with
configuring complex tool chains. It is also essentially a license-free approach and runs on anything.
With this fundamental understanding about what is really going on you can progress on to use and
actually understand whatever tools you need in your career.
In order to give a sense of doing something real and useful and up to date, the focus is on developing
visible and effectively professional-quality web-server and client projects to put on-line, using:
This course has been designed for and lab-tested by first and second year Computer Science Students
at Kingston University, London UK.
10
C Programming in Linux Setting up your System
If you are using the KDE desktop you will have Konqueror or Dolphin as the File Manager and kate or
kedit for an editor
You need to be familiar with the idea of doing some things as “super user” so that you have access
permission to copy or edit certain files. This is normally done by prefacing the Linux command with
“sudo” and providing the password, as in this example:
which copies the file “hello3” to the area where the Apache server locates common gateway interface
or cgi programs.
Throughout the text you will see references to the folder cgi-bin. The location of this will vary
between Linux distributions. By default this folder used for web programs is:
OpenSuse: /srv/www/cgi-bin
Ubuntu: /usr/lib/cgi-bin
11
C Programming in Linux Setting up your System
To place programs there you need superuser rights, so it may be better to create a folder inside your
own home/*****/public_html/cgi-bin directory and change the ScriptAlias and associated Directory
references inside the Apache configuration files (OpenSuse) /etc/apache2/default-server.conf or
(Ubuntu) /etc/apache2/default-server.conf.
Where it’s
Please click the advert
12
C Programming in Linux Chapter One: Hello World
Using the File Manager (in KDE, Konqueror or in Gnome, Nautilus) create a new directory
somewhere in your home directory called something appropriate for all the examples in this book,
perhaps “Programming_In_Linux” without any spaces in the name.
Open an editor (in KDE, kate, or in Gnome, gedit) and type in (or copy from the supplied source code
zip bundle) the following:
/*****************************************************************
C Programming in Linux (c) David Haskins 2008
chapter1_1.c
*****************************************************************/
#include <stdio.h>
Save the text as chapter1_1.c in the new folder you created in your home directory.
Now type “ls -l” to list the details of all the files in this directory. You should see that chapter1_2.c is
there and a file called “hello” which is the compiled C program you have just written.
13
C Programming in Linux Chapter One: Hello World
The part inside /*** ***/ is a comment and is not compiled but just for information and
reference.
The “#include...” part tells the compiler which system libraries are needed and which header files
are being referenced by this program. In our case “printf” is used and this is defined in the
stdio.h header.
The “int main(int argc, char *argv[])” part is the start of the actual program. This is an entry-
point and most C programs have a main function.
The “int argc” is an argument to the function “main” which is an integer count of the number of
character string arguments passed in “char *argv[]” (a list of pointers to character strings) that
might be passed at the command line when we run it.
A pointer to some thing is a name given to a memory address for this kind of data type. We can
have a pointer to an integer: int *iptr, or a floating point number: float *fPtr. Any list of things is
described by [], and if we know exactly how big this list is we might declare it as [200]. In this
case we know that the second argument is a list of pointers to character strings.
Everything else in the curly brackets is the main function and in this case the entire program
expressed as lines.
We have function calls like “printf(...)” which is a call to the standard input / output library
defined in the header file stdio.h.
At the end of the program “return 0” ends the program by returning a zero to the system.
Return values are often used to indicate the success or status should the program not run
correctly.
Taking this example a stage further, examine the start of the program at the declaration of the entry
point function: int main(int argc, char *argv[])
14
C Programming in Linux Chapter One: Hello World
The function called “main”, which returns an integer, takes two arguments, an integer called “argc”
which is a count of the number of command arguments then *argv[] which is a list or array of pointers
to strings which are the actual arguments typed in when you run the program from the command line.
Some Definitions:
function: a block of program code with a return data type, a name, some arguments of varying
data types separated by commas, enclosed in brackets, then the body of the function enclosed in
curly brackets, each statement ending with a semi-colon.
integer symbol int : a counting number like 0,1,2,3,4,5.
list, array symbol []: a sequence of things of the same kind in a numbered order.
pointer symbol * : a memory address locating the start of piece of data of a certain type.
string or char * : a pointer to a sequence of characters like 'c' ,'a', 't' making up “cat”. A
character string ends with s special character NULL or '\0' ascii value 0 or hex 00
15
Other documents randomly have
different content
Fig. 71.
Fig. 71. will show the law with sufficient clearness; it represents the
perspective construction of a fillet whose profile is a semicircle, such
as f h in Fig. 60., seen above the eye. Only half the pillar with half
the fillet is drawn, to avoid confusion.
q93is the center of the shaft.
p q the thickness of the fillet, sight-magnitude at the shaft’s center.
32 This point coincides in the figure with the extremity of the horizontal diameter, but
only accidentally. Return to text
33 The engraving is a little inaccurate; the inclosing line should touch the dotted
semicircles at a and b. The student should draw it on a large scale.
Return to text
94
PROBLEM XVI.
It is often possible to shorten other perspective operations
considerably, by finding the vanishing-points of the inclined lines of
the object. Thus, in drawing the gabled roof in Fig. 43., if the gable
a y c be drawn in perspective, and the vanishing-point of a y
determined, it is not necessary to draw the two sides of the
rectangle, a′ d′ and d′ b′, in order to determine the point y′; but
merely to draw y y′ to the vanishing-point of a a′ and a′ y′ to the
vanishing-point of a y, meeting in y′, the point required.
Again, if there be a series of gables, or other figures produced by
parallel inclined lines, and retiring to the point v, as in Fig. 72.,34 it is
not necessary to draw each separately, but merely to determine their
breadths on the line a v, and draw the slopes of each to their
vanishing-points, as shown in Fig. 72. Or if the gables are equal in
height, and a line be drawn from y to v, the construction resolves
itself into a zigzag drawn alternately to p and q, between the lines
y v and a v.
The student must be very cautious, in finding the vanishing-points of
inclined lines, to notice their relations to the horizontals beneath
them, else he may easily mistake the horizontal to which they
belong.
Thus, let a b c d, Fig. 73., be a rectangular inclined plane, and let it
be required to find the vanishing-point of its diagonal b d.
Find v, the vanishing-point of a d and b c.
Draw a e to the opposite vanishing-point, so that d a e may
represent a right angle.
Let fall from b the vertical b e, cutting a e in e.
Join e d, and produce it to cut the sight-line in v′.
95
Fig. 72.
Then,
96 since the point e is vertically under the point b, the horizontal
line e d is vertically under the inclined line b d.
Fig. 73.
So that if we now let fall the vertical v′ p from v′, and produce b d to
cut v′ p in p, the point p will be the vanishing-point of b d, and of all
lines parallel to it.35
35 The student may perhaps understand this construction better by completing the
rectangle a d f e, drawing d f to the vanishing-point of a e, and e f to v. The whole
figure, b f, may then be conceived as representing half the gable roof of a house,
a f the rectangle of its base, and a c the rectangle of its sloping side.
In nearly all picturesque buildings, especially on the Continent, the slopes of gables
are much varied (frequently unequal on the two sides), and the vanishing-points of
their inclined lines become very important, if accuracy is required in the
intersections of tiling, sides of dormer windows, etc.
Obviously, also, irregular triangles and polygons in vertical planes may be more
easily constructed by finding the vanishing-points of their sides, than by the
construction given in the corollary to Problem IX.; and if such triangles or polygons
have others concentrically inscribed within them, as often in Byzantine mosaics, etc.,
the use of the vanishing-points will become essential. Return to text
97
PROBLEM XVIII.
Before examining the last three problems it is necessary that you
should understand accurately what is meant by the position of an
inclined plane.
Cut a piece of strong white pasteboard into any irregular shape, and
dip it in a sloped position into water. However you hold it, the edge
of the water, of course, will always draw a horizontal line across its
surface. The direction of this horizontal line is the direction of the
inclined plane. (In beds of rock geologists call it their “strike.”)
Fig. 74.
I.
THE SECOND COROLLARY, PROBLEM II.
In Fig. 8. omit the lines c d, c′ d′, and d s; and, as here in Fig. 75.,
from a draw a d parallel to a b, cutting b t in d; and from d draw d e
parallel to b c′.
Fig. 75.
Now as a d is parallel to a b—
a c ∶ a c ∷ b c′ ∶ d e;
but a c is equal to b c′—
∴ a c = d e.
Now
100 because the triangles a c v, b c′ v, are similar—
a c ∶ b c′ ∷ a v ∶ b v;
and because the triangles d e t, b c′ t are similar—
d e ∶ b c′ ∷ d t ∶ b t.
But a c is equal to d e—
∴ a v ∶ b v ∷ d t ∶ b t;
∴ the two triangles a b d, b t v, are similar, and their angles are
alternate;
∴ t v is parallel to a d.
But a d is parallel to a b—
∴ t v is parallel to a b.
101
II.
THE THIRD COROLLARY, PROBLEM III.
∴ a r is parallel to t v,
∴ a b r and t b v are alternate triangles,
∴ a r ∶ t v ∷ a b ∶ b v.
a r ∶ t v ∷ a b ∶ b v—
∴ a r′ ∶ m v ∷ a r ∶ t v.
But by construction, a r′ = a r—
∴ m v = t v.
102
III.
ANALYSIS OF PROBLEM XV.
Join a e.
Then, by the second Corollary of Problem II., a e is a horizontal line.
∴ v is the vanishing-point of a e.
Complete
103 the constructions of Problem II. and its second Corollary.
Then by Problem II. a b is the line a b drawn in perspective; and by
its Corollary a e is the line a e drawn in perspective.
e b ∶ e f ∷ p v ∶ v t.
Therefore p v ∶ v t ∷ e b ∶ a e.
And, by construction, angle t p v = ∠ a e b.
Therefore the triangles t v p, a e b, are similar; and t p is parallel to
a b.
Now
104 the construction in this problem is entirely general for any
inclined line a b, and a horizontal line a e in the same vertical plane
with it.
So that if we find the vanishing-point of a e in v, and from v erect a
vertical v p, and from t draw t p parallel to a b, cutting v p in p, p
will be the vanishing-point of a b, and (by the same proof as that
given at page 17) of all lines parallel to it.
Fig. 77.
Fig. 78.
But the angle a e b, Fig. 77., and the corresponding angle in all the
constructions of the earlier problems, is in reality a right angle,
though in the diagram necessarily represented as obtuse.
Therefore, if from e we draw e c, as in Fig. 79.,
at right angles to a e, make e c = e b, and join
a c, a c will be the real length of the line a b.
Now, therefore, if instead of a m in Fig. 78., we
take the real length of a b, that real length will
be to a m as a c to a b in Fig. 79.
Fig. 79.
And then, if the line drawn to the measuring-line
p q is still to cut a p in b, it is evident that the line p q must be
shortened in the same ratio that a m was shortened; and the true
dividing-point will be q′ in Fig. 80., fixed so that q′ p shall be to q p
as a m′ is to a m; a m′ representing the real length of a b.
106 a m′ is therefore to a m as a c is to a b in Fig. 79.
But
Therefore p q′ must be to p q as a c is to a b.
But p q equals p t (Fig. 78.); and p v is to v t (in Fig. 78.) as b e is to
a e (Fig. 79.).
Hence we have only to substitute p v for e c, and v t for a e, in
Fig. 79., and the resulting diagonal a c will be the required length of
p q′.
Fig. 80.
It will be seen that the construction given in the text (Fig. 46.) is the
simplest means of obtaining this magnitude, for v d in Fig. 46. (or
v m in Fig. 15.) = v t by construction in Problem IV. It should,
however, be observed, that the distance p q′ or p x, in Fig. 46., may
be laid on the sight-line of the inclined plane itself, if the measuring-
line be drawn parallel to that sight-line. And thus any form may be
drawn on an inclined plane as conveniently as on a horizontal one,
with the single exception of the radiation of the verticals, which have
a vanishing-point, as shown in Problem XX.
the end.
Transcriber’s Note
A handful of unequivocal typographical errors has been
corrected.
For increased clarity, a few diagrams have been shifted from
their original position in the text.
*** END OF THE PROJECT GUTENBERG EBOOK THE ELEMENTS OF
PERSPECTIVE ***
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside
the United States, check the laws of your country in addition to
the terms of this agreement before downloading, copying,
displaying, performing, distributing or creating derivative works
based on this work or any other Project Gutenberg™ work. The
Foundation makes no representations concerning the copyright
status of any work in any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if
you provide access to or distribute copies of a Project
Gutenberg™ work in a format other than “Plain Vanilla ASCII” or
other format used in the official version posted on the official
Project Gutenberg™ website (www.gutenberg.org), you must,
at no additional cost, fee or expense to the user, provide a copy,
a means of exporting a copy, or a means of obtaining a copy
upon request, of the work in its original “Plain Vanilla ASCII” or
other form. Any alternate format must include the full Project
Gutenberg™ License as specified in paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using
the method you already use to calculate your applicable
taxes. The fee is owed to the owner of the Project
Gutenberg™ trademark, but he has agreed to donate
royalties under this paragraph to the Project Gutenberg
Literary Archive Foundation. Royalty payments must be paid
within 60 days following each date on which you prepare (or
are legally required to prepare) your periodic tax returns.
Royalty payments should be clearly marked as such and sent
to the Project Gutenberg Literary Archive Foundation at the
address specified in Section 4, “Information about donations
to the Project Gutenberg Literary Archive Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.1. Project Gutenberg volunteers and employees expend
considerable effort to identify, do copyright research on,
transcribe and proofread works not protected by U.S. copyright
law in creating the Project Gutenberg™ collection. Despite these
efforts, Project Gutenberg™ electronic works, and the medium
on which they may be stored, may contain “Defects,” such as,
but not limited to, incomplete, inaccurate or corrupt data,
transcription errors, a copyright or other intellectual property
infringement, a defective or damaged disk or other medium, a
computer virus, or computer codes that damage or cannot be
read by your equipment.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookball.com