<!doctype html public "-//w3c//dtd html 4.0 transitional//en">

CS 476/576
Systems Programming
Fall 2009
Midterm Exam

Time 2 & 1/2 hours
Open Book & Notes

 

 

 

 Name:

 Unix Login:

 

 

 


Question 1:  Each part is 15 points

 

A.     The  UNIX command  whoami  displays the user name.

For example:

% whoami  

wahab

Write a shell script   Q1A that creates an empty file called: /usr/tmp/Q1A_<user>:

For example:

% Q1A

creates an empty an empty  file named:

/usr/tmp/Q1A_wahab


à      Solution Q1A

 

B.     The UNIX command  who am i  displays one line of information about the user.

For example:

% who am i 

wahab    pts/30       Oct  18  15:03   (dhcp-143.cs.odu.edu)

Write a shell script  Q1B  that displays the host name.

For example::

% Q1B

dhcp-143


 

à      Solution Q1B

 

C.     Write a shell script called Q1C  that has a single argument. The argument is a file F containing login names.

The program produces a file called Q1C_F that has both the login names and the corresponding full names, separated by “ <space>:<space>” of each user in F.

For example:

% cat mylist

wahab

maly

cmo

 

%  Q1C mylist

%  cat Q1C_mylist

wahab : Hussein Abdel-Wahab

maly : Kurt Maly

cmo :  Mike Overstreet

 

Hint:  % ypcat passwd command  produces a line for each user.

For example, the line for user wahab is:

wahab:stg/i.0xxJ1zU:51:13:Hussein Abdel-Wahab:/home/wahab:/usr/local/bin/tcsh


 

à      Solution Q1C

 

Question 2: 15 points

 

Write a curses program called Q2 that displays its string argument e.g., WAHAB as a X.

Hint: You may use the curses function addch (c) instead of the familiar  addstr(s) to display a single  character.

 

Examples:

 

% Q2  MALY

 

M     M

   A A

   L L

 Y     Y

 

 

% Q2  WAHAB

 

W       W

   A   A

     H

   A   A

 B       B

 


 

à      Solution Q2


Question 3: 40 points

 

 

Modify the following motif/xlib program such that:

1.      User odd clicks  (1,3,5, ...) on the canvas draws a cross + centered at the click point. The initial arms length  is 10 pixels.

2.   User even clicks (2,4,6, ...) on the canvas draws a circle O centered at the click point. The initial diameter is 10 pixels.

3.   Add two buttons  to the interface: Double and Half. Clicking on the Double (Half) button will multiply (divide) the new drawings size by 2.

 

 

 

 

void eraseCallback ();

void quitCallback ();

 

 

void HandleBoardEvents ( Widget w, XtPointer clientData, XEvent *event, Boolean *flag );

 

 

Widget quit;

Widget draw;

Widget erase;

 

 

 

 

Widget canvas;

Display *display;

int screen;

long fgcolor, bgcolor;

XGCValues gcval;

GC drawgc;

Colormap cmap;

XColor color, ignore;

char *colorname = "red";

int pointx, pointy;

int L = 300;

 

 

 

void main ( int argc, char **argv )

{

            Widget shell,  panel, commands;

            XtAppContext app;

           

            shell = XtAppInitialize ( &app, "Q2", NULL, 0, &argc, argv, NULL, NULL, 0 );                   

            panel = XtCreateManagedWidget ( "panel", xmFormWidgetClass, shell, NULL, 0 );        

            display= XtDisplay(shell);

            screen = DefaultScreen(display);    

   canvas =

       XtVaCreateManagedWidget ( "canvas", xmDrawingAreaWidgetClass,

       panel,

       XmNtopAttachment, XmATTACH_FORM,

       XmNrightAttachment, XmATTACH_FORM,

       XmNleftAttachment, XmATTACH_FORM,

       XmNbottomAttachment,XmATTACH_NONE,

       XmNwidth,L,

       XmNheight,L,

       XmNbackground,WhitePixel(display,screen),

       NULL );

 

   commands =

       XtVaCreateManagedWidget ( "commands", xmRowColumnWidgetClass,

       panel,

       XmNnumColumns, 4,

       XmNorientation, XmHORIZONTAL,

       XmNtopAttachment, XmATTACH_WIDGET,

       XmNtopWidget, canvas,

       XmNrightAttachment, XmATTACH_FORM,

       XmNleftAttachment, XmATTACH_FORM,

       XmNbottomAttachment, XmATTACH_FORM,

       NULL );

 

erase = XtCreateManagedWidget ( "Erase", xmPushButtonWidgetClass, commands, NULL, 0 );

quit = XtCreateManagedWidget ( "Quit", xmPushButtonWidgetClass, commands, NULL, 0 );

 

 

 

 

 

 XtAddCallback ( quit, XmNactivateCallback, quitCallback, NULL );

 XtAddCallback ( erase, XmNactivateCallback, eraseCallback, NULL );

           

 

 

           

cmap = DefaultColormap (display, screen);

XAllocNamedColor(display, cmap, colorname, &color, &ignore);

gcval.foreground =  color.pixel;

drawgc = XtGetGC ( canvas, GCForeground | GCBackground , &gcval );

gcval.foreground = BlackPixel(display,screen);

 

 

 

           

            XtRealizeWidget ( shell );

            XtAppMainLoop ( app );

}

 

 

void eraseCallback ()

{

            XClearArea( XtDisplay(canvas),XtWindow(canvas),  0,0, L, L, 0);

}

void quitCallback ( )

{

            exit (0);

}

 

 

à      Solution Q3