CS 240 Lab 1:
Crash Course in GNU Emacs, GNU Make & C fundamentals
Introduction
UNIX
It is expected that you become familiar with the UNIX command line environment in this course.
If you are new to UNIX, a good starting point would be to review this tutorial:
Emacs
GNU Emacs
is an extremely customizable text editor with a multitude of extensions to add other capabilities to it.
If you haven't used Emacs before, we suggest you to grab a copy of the
GNU Emacs reference card.
Note: it is acceptable to use an editor other than Emacs.
However, you should become familiar with some type of UNIX programmer's text editor such as Emacs,
XEmacs or Vi/Vim, as that experience will be of great benefit to you in
the future.
Make
GNU Make is
the standard utility used to generate an executable program from a set of source files.
A Makefile defines the set of rules necessary to build the executable from the source.
Tutorials
There are dozens of tutorials available online that may be helpful for this lab. The following links are
some of the better ones (note: these are not necessary to do the lab, but may be helpful as a reference):
Newsgroup
The course newsgroup is: purdue.class.cs240.
You should get in the habit of checking the newsgroup regularly, as course updates and other important
information will be posted there.
Also, posting questions to the newsgroup is one of the best ways to get help if you are having difficulty
with an assignment.
You can check the newsgroup through Microsoft Outlook in Windows, Mozilla Thunderbird in Windows, Linux or Unix, or
pine in the terminal of lore.
GCC requirement
Make sure that your programs can be compiled using gcc 3.4.5.
If your program fails to compile using gcc 3.4.5, you will by default receive zero mark.
To check which version of gcc you are using, run the command
gcc --version
Coding Conventions
You should follow the Coding Convention specified here
when coding your programs, particularly for your projects.
Although you may not be able to follow the suggestions strictly for your lab programs because of
the time constraint, writing comments can help us understand your code more easily and give you
partial credits if your program is not working well.
Remote access
You can remote login to lore through Secure Shell secure connections, so that you can work at home for your projects and are sure that your programs work in lore.
A copy of the non-commercial Windows SSH Client program can be obtained through this link.
As you connect to lore through the SSH client, the host name would be
lore.cs.purdue.edu
Lab Exercise
One rather irritating issue that a programmer may run into when developing software is discrepencies between
character encodings for different operating systems.
One of these issues has to do with the character(s) used to indicate the beginning of a new line in ASCII
text files. For instance:
- Windows and DOS machines use the characters "\r\n" (linefeed + newline) to indicate a new line
- GNU/Linux and other UNIX-based machines use the character '\n' to indicate a new line
Editing a text file created on a Windows machine on a Unix machine will have the character ^M at the end of each line (Note: some editors do not display this character, to view the hidden characters, type: "cat -v textfile.txt"). To test this, we can use the programs "dos2unix" and "unix2dos" to create files for each specific encoding. For instance, the command
unix2dos textfile.txt > output.txt
converts a UNIX-based text file to DOS encoding, and redirects the output to the output.txt file, while
dos2unix textfile.txt > output.txt
does the opposite.
You can also obtain a sample DOS encoding file here.
To make this lab exercise more challenging, you also need to convert each tab to two space characters.
You should write a program to convert text files from Windows-style encoding to Unix style encoding and
tab to two spaces, and create a Makefile to build the program using GNU make.
You should include the header file nlconv.h in your program and Makefile, and use the symbolic
constants defined in the header file in your conversion program.
Do NOT simply copy the contents of the header file into your program; one of the requirements of this lab is that you
become familiar with using GNU make with separate source and header files.
Everything you need to write the program is described in Chapter 1 of Kernighan and Ritchie.
The file copying program on pg 16 is of particular relevance to this lab.
Also, you should use only getchar/putchar for I/O.
What you have to do is check whether the character 'c' is a LF (Line Feed, as defined in the header file), a TAB or other
characters before printing it out.
If the character is LF, do not print it. If the character is TAB, print two spaces instead.
The particular code segment looks like this:
if (c == TAB) {
putchar(' ');
putchar(' ');
}
else if(c != LF)
putchar(c);
You should use redirection to pass input to your program. For instance, if you compile the aforementioned file copying program provided in the textbook, you can redirect the input from a file to the program by running the command:
mycopy < textfile.txt
Furthermore, you can redirect the output of that command to another file, like this:
mycopy < textfile.txt > copiedfile.txt
In the example commands given above, "mycopy" is the name of the executable used in the book.
You should compile your program using the Gnu C compiler gcc, and you should have gcc create an
executable named "nlconv.out" (this information should be included in your Makefile).
Development Notes
Usually the software development process involves the iterative steps of:
- Design: Solve the problem on paper. Choose how to represent your data.
- Code: Write code using the selected language.
- Compile: Compile your code into an executable.
- Debug: Correct any compile time and link time errors.
- Execute: Run the executable to check the result.
- Validate: Look for run time errors and verify the result.
Debugging
Your program will be tested using automated scripts.
This means that the output of your program should match the output of our implementation EXACTLY.
In order to make sure your implementation matches ours, you can use the unix2dos command to create text
files (with tabs) in DOS encoding, and then use your program to convert to UNIX format.
Further edit the original UNIX encoded files to replace tabs with spaces, then compare the results of
your program to these updated original files with the command:
diff updated_original.txt your_output.txt
If the command above produces no output, the files match.
To make your life easier, the corresponding output of the sample DOS encoding file is here.
Turnin
When you are satisfied that your program works, print out a copy
of your program and turn it in to your TA. Make sure your name
and section are in the comment section according to the
CS 240 Coding Conventions.
Below is the list of the lab sections:
| Section |
Time |
TA |
| 0201 |
Thursday 15:30-17:20 |
Dan Zhang |
| 0301 |
Friday 09:30-11:20 |
Suli Xi |
| 0401 |
Friday 13:30-15:20 |
Youhan Fang |
| 0501 |
Thursday 09:30-11:20 |
J. C. Chin |
Grading Criteria