<!doctype html public "-//w3c//dtd html 4.0
transitional//en">
CS 476/576
Systems Programming
Fall 2010
Midterm Exam
Time 2 & 1/2 hours
Open Book & Notes
Login:
Name:
Question 1: 45
points (each part 15 points)
A:
The
UNIX command last
displays a line for each successful login at the local host.
The format for one line of user wahab is:
wahab sshd
dhcp-154.cs.odu. Sat Aug 28 17:42 - 17:59 (00:16)
Write a shell script called lastcount that
produces the number of unique users who successfully logged to the local host.
For example:
% lastcount
942
B:
The
UNIX command hostname display the host name where the user is
logged in.
For example:
% hostname
vega
The UNIX command
who am i display one
line of user information.
For example:
% who am I
cs476 pts/15 2010-10-14 19:17 (dhcp-154.cs.odu.edu)
Write a shell script called
whoami that display the host name and user information in the following
format:
% whoami
HOST: vega
LOGIN: cs476
SINCE: 2010-10-14 19:17
FROM: dhcp-154.cs.odu.edu
C:
Write a shell script
called listmygroup to list all
members of the same group as the user executing the script.
The list should be sorted by login names.
For Example for user wahab, the group is 13,
and the program lists all users whose group is 13.
% listmygroup
....
maly:Kurt
Maly
mln:Michael
Nelson
mukka:Ravi
Mukkamala
mweigle:Michele
Weigle
olariu:Stephan
Olariu
wahab:Hussein
Abdel-Wahab
....
Note that:
% ypcat passwd
the line for user wahab is:
wahab:stg/i.0xxJ1zU:51:13:Hussein
Abdel-Wahab:/home/wahab:/usr/local/bin/tcsh
Question 2: 15 points
Write a curses program called Q2 that
randomly selects a row R and a colum C.
Then displays the string X (R, C)
at curses position (R, C). Assume
we have 20 rows and 80 columns.
Examples:
% Q2
X
(4, 20)
% Q2
X (2, 40)
Question 3: 20 points
Modify
the following xlib program to draw a line
between the click point and the nearest corner of the drawing area.
For example if the click
pint is (x,y) where x < 300 and y < 300, then the nearest corner is (0,0).
main(argc,argv)
int argc;
char **argv;
{
Display
*display;
Window root,
window;
long fgcolor, bgcolor;
int screen, pointx,
pointy ;
long eventmask = ButtonPressMask|ExposureMask|KeyPressMask;
XEvent event;
XGCValues gcval;
GC draw;
Colormap cmap;
XColor color, ignore;
char *colorname = "Red";
display = XOpenDisplay(argv[1]);
root = RootWindow(display,screen = DefaultScreen(display));
fgcolor = BlackPixel(display,screen);
bgcolor = WhitePixel(display,screen);
window = XCreateSimpleWindow(display,root,0,0,800,800,2,fgcolor,bgcolor);
cmap = DefaultColormap
(display, screen);
XAllocNamedColor(display,
cmap, colorname,
&color, &ignore);
fgcolor = color.pixel;
gcval.foreground = fgcolor;
gcval.background = bgcolor;
draw = XCreateGC(display,window,GCForeground|GCBackground,&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:
pointx = event.xbutton.x;
pointy = event.xbutton.y;
XDrawPoint (display,window,draw,
pointx, pointy);
break;
case KeyPress:
exit(0);
}
}
}

Question 4: 20 points
The
program Q4 accepts one argument and creates a
motif interface with two buttons.
For
Example:
% Q4
“ls –lt”
Will
create two buttons: QUIT and ls –lt
Clicking
on QUIT terminates the program.
Clicking
on ls –lt executes
the UNIX command: ls –lt.
Modify
the program so that it accepts any
number of arguments and creates as many buttons.
For
example:
%Q4
“who am I” “last” “ls -l” “date”
“host fast”
Creates
the following interface:

Clicking
on the QUIT button terminates the program.
Clicking
on any other button executes the corresponding UNIX command.
void quitCallback
( Widget w, XtPointer clientData,
XtPointer callData );
void cmdCallback (
Widget w, XtPointer clientData,
XtPointer callData );
void main ( int argc, char **argv )
{
Widget
shell, commands, quit, cmd;
XtAppContext app;
shell = XtAppInitialize (
&app, "Xecute", NULL, 0, &argc, argv, NULL, NULL, 0 );
quit = XtVaCreateManagedWidget (
"QUIT", xmPushButtonWidgetClass, commands,
NULL );
XtAddCallback ( quit, XmNactivateCallback, quitCallback,
NULL );
commands = XtVaCreateManagedWidget
( "commands", xmRowColumnWidgetClass,
shell, XmNnumColumns, 3,
XmNorientation,
XmHORIZONTAL, NULL );
cmd = XtVaCreateManagedWidget
( argv[1], xmPushButtonWidgetClass,
commands, NULL );
XtAddCallback ( cmd,
XmNactivateCallback, cmdCallback,
( XtPointer ) argv[1] );
XtRealizeWidget ( shell );
XtAppMainLoop ( app );
}
void cmdCallback (
Widget w, XtPointer clientData,
XtPointer callData )
{
char * cmd = (char *) clientData;
system ( cmd );
}
void quitCallback
( Widget w, XtPointer clientData,
XtPointer callData)
{
exit ( 0 );
}