|Imperative Problem Solving and Data Structures
In the introduction to C lab, you edited, compiled, and ran a C program quarts.c. This part of the lab provides experience with several Linux capabilities that can simplify some parts of program development.
Move to your labs subdirectory, compile quarts.c, and run the program. Your work in a terminal window might involve the following:
cd csc161/labs gcc -o quarts quarts.c quarts
Now edit quarts.c and try to compile and run the program.
This will open emacs just fine, but now switch back to your terminal window. You will notice that it is unavailable for further use (i.e., you won't get another command prompt until emacs is closed). For example, you will not be able to compile and run quarts.c until the emacs editor is closed.
Now close emacs, and then re-open it from the terminal window, but this time add an ampersand character to the end of the command:
emacs quarts.c &
Now when you return to your terminal window, a prompt is waiting for you, making it easy to do multiple tasks at once — you can compile and run quarts.c without interruption. Adding the ampersand character to a command causes the command to be launched as a "background process," allowing you to continue working with your terminal window.
Try using "autocompletion" with quarts and quarts.c by typing
gcc -o q followed by the TAB key
You should find that when you press TAB the system completes the file name for you. In this case, both quarts and quarts.c are files in your directory, so the TAB completes q with the common prefix quarts, giving
gcc -o quarts
Type q and the TAB key again:
gcc -o quarts q followed by the TAB key
Again, autocompletion gives
gcc -o quarts quarts
Type a period and the TAB, and autocompletion gives
gcc -o quarts quarts.c
This time, start the same as in the previous step, but hit the TAB key twice typing
gcc -o q TAB TAB
Describe what you see.
Several commands are available for printing files.
a2ps --sides=duplex file
|prints file to default printer (handles many standard file
same, but double-sided
|send file to the default printer
|displays jobs in print queue on default printer
|cancels (removes) print job number 585 from default printer queue
You can accomplish printing a C file in at least two ways:
For this course, printing from emacs is not satisfactory. emacs adds a header that confuses the splitting of a file into pages. For multi-page files, printing from emacs therefore often produces awkward page breaks and partial pages.
For this course, always use lpr in a terminal window to print a C program. For example, print quarts.c with the command
Use the man command to consult the online Linux manual (Note the q key is used to exit man pages):
What do the following command options mean? Note the ability to smash flags together.
cp -p ls -ltrF mkdir -p
Why does the cat command have the name "cat", which stands for "concatenate"?
Use the man page for the C function sqrt to identify the parameters and return type for a collection of functions related to sqrt. Also, what "include" statement is needed for sqrt
The command man -k keyword lists commands that seem related to the given keyword. For example, to print a list of man pages that include the word "square" in the name or description fields, you could use "man -k square". Try this command to locate sqrt and to determine various commands related to the keyword "print".
In many applications, it is helpful to include speech synthesis within a C program. Although not perfect (actually far from perfect), the eSpeak package provides a simple way to instruct the workstation to talk, given specific text.
In order to limit interference with the work of others in the lab, you may need earphones to listen to audio from your workstation.
As an example, compile and run quarts-espeak.c.
In the Linux basics lab, you copied quarts-espeak.c to your labs subdirectory. If this did not work properly, copy the file now:
cp /home/walker/public_html/courses/161.sp14/modules/getting-started/quarts-espeak.c .
To compile quarts-espeak.c, you must tell the gcc compiler where to find the eSpeakPackage. This is done by adding a piece to the usual compile line:
gcc -leSpeakPackage -o quarts-espeak quarts-espeak.c
Run the program with the expected name of the compiled program:
Open quarts-espeak.c in an editor, and find the various eSpeak commands. Review the program with the following notes:
Modify quarts-espeak.c in several ways to explore the capabilities of this package.
Try the following commands that display all or part of this laboratory exercise:
cat /home/walker/public_html/courses/161.sp14/modules/getting-started/lab-more-intro-c.shtml more /home/walker/public_html/courses/161.sp14/modules/getting-started/lab-more-intro-c.shtml less /home/walker/public_html/courses/161.sp14/modules/getting-started/lab-more-intro-c.shtml head /home/walker/public_html/courses/161.sp14/modules/getting-started/lab-more-intro-c.shtml head -n 20 /home/walker/public_html/courses/161.sp14/modules/getting-started/lab-more-intro-c.shtml tail /home/walker/public_html/courses/161.sp14/modules/getting-started/lab-more-intro-c.shtml tail -n 20 /home/walker/public_html/courses/161.sp14/modules/getting-started/lab-more-intro-c.shtml
(Note that this is a wonderful time to use the arrow keys to edit previous commands rather than to retype the full lines each time. You can also use the tab autocomplete utility.)
For the less command, try the arrow keys to move up and down in the file.
The following steps ask you to review the permissions for your account directories and adjust them for this course.
Move to your home directory and obtain a "long" listing of the files present using the commands:
cd ls -l
(The cd command without parameters takes you to your home directory.)
Interpret the meaning of each part of the directory and file listings.
For CSC 161, it seems likely that you will want others in the class to be able to read your labs, since you are collaborating with others on that material. However, you do not want others to be able to read your supplemental problems. These steps set up this framework.
Move to your home directory.
Allow others to read (but not change) your login directory with the command:
chmod 755 .
Next allow others to read (but not change) files in your labs directory:
chmod 755 csc161 chmod 755 csc161/labs
Now use the ls -l -a command to check that others can read your home directory and the labs subdirectory, but no other directories.
Team up with another class member to check which directories of theirs you can read.
Now suppose you set your home directory with the command:
chmod 711 ~
Can others obtain a listing of your home directory?
Can others obtain a listing of your labs subdirectory?
Consider what access you want others to have to your home directory and set it accordingly. Unless you want to protect a file against your own inadventent editing, you likely will want to retain 7 at the beginning of the chmod permission list.
Development of laboratory exercises is an iterative process. Prof. Walker welcomes your feedback! Feel free to talk to him during class or stop by his office.