<!doctype
html public "-//w3c//dtd html 4.0 transitional//en">
CS 476/576
Systems Programming
Fall 2012
Midterm Exam
Time 2 & 1/2 hours
Open Book & Notes
Login:
Name:
Question 1: 20 points
What is the output of the following two shell scripts?
#! /bin/sh
echo 1 > file
tail -f file >> file &
while :
do
count=`wc -c < file`
if test $count -gt 1000
then
echo $count
pkill tail
exit
fi
done
% q1a.shell file
#! /bin/sh
echo HI1 | tee /dev/tty > $1
echo HI2 | tee /dev/tty >> $1
echo HI3 | tee /dev/tty > $1
echo HI4 | tee /dev/tty >> $1
echo 'cat $1'
cat $1
echo "cat $1"
cat $1
echo '`cat $1`'
echo "`cat $1`"
% q1b.shell file
Question 2: 20 points
The
following shell script q2.shell has the syntax:
q2.shell <letter> u1 u2 ...
will email <letter>
to users u1, u2, ...
Modify
the script to prepend a line at the top of
the letter that says:
Dear <full_name>:
Example:
Assume invitationfile contains:
I would like to invite you for dinner
this weekend at
my home.
Hussein
% q2.shell “Special Party Invitation” invitationfile wahab maly
user wahab will
get :
Dear Hussein Abdel-Wahab:
I would like to invite you for dinner
this weekend at
my home.
Hussein
since wahab’s UNIX entry is:
wahab:abc:51:13:Hussein
Abdel-Wahab:/home/wahab:/usr/local/bin/tcsh
while user maly will get:
Dear Kurt Maly:
I would like to invite you for dinner
this weekend at
my home.
Hussein
Since maly’s
UNIX entry is:
maly:xyz:79:13:Kurt
Maly:/home/maly:/usr/local/bin/tcsh
#! /bin/sh
subject="$1"
letter=$2
shift
shift
for i
do
Mail -s "$subject" $i
< $letter
done
Question 3: 20 points
Write a program called q3 with following
syntax:
q3
<command>
The
program uses curses to display the following message in the middle of the
screen (assuming /dev/tty has 20 rows and 80 columns).
Hello <user>, it costs $100, would you like to start (Y/N):
The
program executes the <command>
only if the answer is Y.
Example:
Assume user wahab
types:
% q3 draw3cbs
The program display:
Hello wahab, it costs
$100, would you like to start (Y/N):
Answering Y the program executes
draw3cbs
Question 4: 20 points
Repeat Question 3 using motif interface instead
of curses.
Question 5: 20 points
Modify the following program to draw rectangles instead of lines.
The 1st
click draws point, the 2nd click draws a line between
2nd and 1st
while the 3rd click draws two lines: one line from 3rd to 1st
and another line from 3rd to 2nd.
main (argc, argv)
int
argc;
char **argv;
{
Display *display;
Window root, window;
long fgcolor, bgcolor;
int
screen;
int
x1, y1, x2, y2;
int
count = 1;
long eventmask = ButtonPressMask | ExposureMask | KeyPressMask;
XEvent event;
XGCValues gcval;
GC draw;
Colormap cmap;
XColor color,
ignore;
char *colorname = "Navy";
display = XOpenDisplay (argv[1]);
root = RootWindow (display, screen = DefaultScreen
(display));
fgcolor
= BlackPixel (display, screen);
bgcolor
= WhitePixel (display, screen);
window=XCreateSimpleWindow(display,root,0,0,400,400,2,fgcolor,bgcolor);
cmap
= DefaultColormap (display, screen);
XAllocNamedColor
(display, cmap, colorname,
&color, &ignore);
fgcolor
= color.pixel;
gcval.foreground = fgcolor;
gcval.background = bgcolor;
gcval.line_width =
5;
draw = XCreateGC(display,window,GCForeground|GCBackground|GCLineWidth,
&gcval);
XSelectInput
(display, window, eventmask);
XMapWindow
(display, window);
for (;;)
{
XWindowEvent
(display, window, eventmask, &event);
switch (event.type)
{
case Expose:
XClearWindow
(display, window);
break;
case ButtonPress:
if
(count == 1)
{
count
= 2;
x1
= event.xbutton.x;
y1 = event.xbutton.y;
XDrawPoint
(display, window, draw, x1, y1);
}
else
if (count == 2)
{
count
= 1;
x2
= event.xbutton.x;
y2 = event.xbutton.y;
XDrawLine
(display, window, draw, x2, y2, x1, y1);
}
break;
case KeyPress:
exit
(0);
default:
fprintf (stderr,
"Unexpected event: %d\n", event.type);
}
}
}