CS010 Practice 1

Getting started with Unix and C


This set of notes assumes that you have never used Unix before. It does assume, however, that you are comfortable using computers with a graphical user interface, such as the Macintosh operating system or Microsoft Windows. You should sit down at a CS Unix machine and go through this tutorial trying things out as they are explained. Upon completion, you should be able to log in, understand the basics of entering a command line, move around in the file system and view existing files, do basic manipulations of windows, do basic editing with the Emacs text editor, know where to find  help and have written your first C program! There are specific exercises listed at the end of the handout, but you'll gain more experience if you try things out as you read.

Logging In

Since Unix is a multi-user machine, you must login to identify yourself so that Unix gives you appropriate access to your files. When you first sit down at our Unix machines you will see:
FreeBSD/i386 (pineywoods.cs.williams.edu) (ttyv0)
This identifies the operating system as FreeBSD, the machine as an Intel x86 machine. The machine name is in the first set of parentheses. The second set of parentheses is not really important, but it identfies the terminal connected to the machine. At this point, the computer is waiting for you to enter your login id. Your login id is 2 digits identifying the year you will graduate followed by your initials (usually 3 initials, sometimes 2). If that doses not result in a unique id, it will be followed by _# where # is the value of a counter to make your id unique. For example, Vladimir L. Andonov's login is 02vla. Your login id is set by the system administrator and is never changed. Simply type your login and then carriage return.

The login prompt is now replaced with a password prompt. Your password will not display as you type it for security reasons. Hit the carriage return after your password. At this point, you should be logged in. If you typed either your login or your password incorrectly, you will receive an error message "Login incorrect" and will need to re-enter both. If you already have a Unix account from last semester, your password has not changed. If this is your first acount on a CS Unix machine, I have your password.

After successfully logging in, you will see:

Last login: Wed Jan  2 17:29:34 on ttyv0
Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994
        The Regents of the University of California.  All rights reserved.
FreeBSD 4.3-RELEASE (GENERIC) #0: Sat Apr 21 10:54:49 GMT 2001
Welcome to Computer Science at Williams!
              You are running FreeBSD 4.3!
You are logged into pineywoods running FreeBSD 4.3-RELEASE
kde, Xwindows, fvwm2 or vanilla? (k|[x]|f|v)
This identifies the version of the operating system. (FreeBSD is derived from BSD which was developed at Umiversity of California at Berkeley, explaining the copyright.)

At this point, the computer is asking you to select a window manager by typing a single letter:

The only thing that is clear is that you do not want to choose v. kde, twm, and fvwm2 are simply 3 different window managers running on top of X. Of these, kde is the most menu-oriented and thus is likely to feel more comfortable if you are used to using Macintosh or Windows. I am most familiar with fvwm2, so that is what I will show you in these examples. While these window managers have somewhat different user interfaces, they are all running on top of FreeBSD and thus they all have the same capabilities. After selecting fvwm2, you will see 5 windows. They are: A shell is a Unix program that allows you to enter commands via the keyboard. (Much like a DOS window on a Windows machine and quite unfamiliar to Unix users!) Prior to the introduction of a mouse and windows, all operating system input was entered via the keyboard. Now, Mac OS and Windows allow nearly everything to be done using the mouse and menus. We will use the shell to change your password.

Your initial password is provided by the system administrator and you should change it the first time that you login. A good password should be impossible for others to guess or to find by trying every word in the dictionary. A good password has the following characteristics:

If you forget your password, the system administrator can give you a new password.

To change your password, use the yppasswd command as follows. In the body of the window there is a prompt that looks like: ->. At the prompt, type yppasswd. Here is what you will see:

-> yppasswd
Changing NIS password for your-login-id on bull.cs.williams.edu
Old password:
Here you should enter your current password. Your password will not be displayed on the screen when you type it. Then enter carriage return. Next you will see:
New password:
Type in the new password that you want. Again, it will not appear on the screen. Type carriage return. Now you will see:
Retype new password:
Type the same new password again. This ensures that you typed correctly since you could not see the characters you typed. If you type the password the same both times, you will see the message:
NIS password has been changed on bull.cs.williams.edu.
If the two attempts at typing your new password differ, you will see:
Mismatch; try again, EOF to quit.
New password:
EOF is a mystical phrase meaning End-Of-File. This still doesn't answer the question of what to type, though, does it? EOF is represented by the character Control-d. You type this by holding down the control key (much like you would the shift key) and typing d. (Unforttunately, I can't explain why Control-D means EOF, but it is the case that some Unix commands will expect you to type this magical character to quit.) If you do type Control-d, your password will not be changed, so don't type this!

Command Lines

The most obvious difference between Unix and Macintosh/Windows is the user interface. Whereas Macintosh/Windows have primarily graphical user interfaces, Unix has a primarily textual user interface. Thus, to accomplish most things in Unix, one types commands to a "shell" program. The passwd command you used above was one example of this. A command consists of the name of a program, zero or more options preceded by '-', and zero or more arguments, which are themselves words. The command line is terminated with a carriage return.

If you make mistakes in typing on the command line, use the Delete or Backspace key to erase the previous character.

File System

The file system is organized into directories and files. A directory is analogous to a Macintosh folder. A directory may contain files and/or other directories. Here are the most useful commands related to files:
ls List the files in the current directory
cp file1 file2 Copy the file named file1 to a new file named file2
mv file1 file2 Rename the file named file1 to have the name file2. (mv is actually short for move, but it doesn't necessarily move anything!)
rm file1 Remove the file named file1
more file1 Display the contents of a file
lpr file1 Print the file named file1
One thing you will notice with Unix is that "no news is good news". Often if a Unix command is successful, it will produce no output! If it fails, you will receive an error message. Of the list of commands above, only ls and more produce output when they succeed.

The "more" command deserves some more comment because it is an interactive command. It displays your file one screenful at a time. After each screenful, it displays a prompt either consisting of the name of the file being displayed in reverse video (for the first screenful), (END) in reverse video (for the last screenful), or : (for all other screenfuls). The prompt appears at the bottom of the screen. At this prompt, you have several options, the most useful being:

space Display the next screenful
b Display the previous screenful
q Quit more, returning to a shell prompt.
You can try out "more" by typing "more /home/faculty/freund/shared/cs010/long-file".  This will show you the contents of a file in my directory that you have permission to read.

Directories are containers for files. (For some reason, Macintosh and Windows use the term "folder", but it means the same thing.) When you use ls to list your files, a directory will appear with / at the end of its name. At all times you have a current directory. Your commands are interpreted with respect to your current directory by default. So, for example, ls lists the files in your current directory if you give it no arguments. Here are some useful commands to manipulate directories:

mkdir dir1 Create a new directory named dir1.
rmdir dir1 Remove the directory named dir1. You can only do this if the directory is empty.
cd dir1 Make dir1 be the current directory
pwd Display the name of the current working directory
Filenames should consist only of letters, numbers, and '.' A filename is typically divided into a descriptive name and an extension, separated by '.'. Extensions are purely by convention but typically indicate the type of file. For example, "myfile.txt" would be a text file, while "myfile.c" would contain a C program. Extensions are generally optional although some programs, such as the C compiler, expect them to be there.

Filenames can be given as names that are relative to the current working directory or as absolute names from the special root directory. Thus far, we have assumed relative names that refer simply to files in the current directory. We can also use relative names to identify files in subdirectories. For example, "mydir/myfile" is the file named "myfile" in the directory named "mydir", where "mydir" is located in the current working directory.

Each user also has a home directory. This is the directory that is the user's current directory when the user initially logs in. No matter what the absolute pathname is to your home directory, you can always refer to it with the special relative pathname '~'. For example, no matter what your current directory is, if you type "ls ~", you will see a listing of the files in your home directory.

An absolute pathname always begins with / while a relative pathname never does. To find out the pathname that corresponds to your current directory, type pwd. If you are still in the directory that you logged into, you should see something like /home/cs-students/03abc. This indicates that your home directory is named "03abc" and that it is inside a directory called "/home/cs-students". You can see what other students have accounts on the CS Unix machines by using the ls command, telling it to show you what is in the /home/cs-students directory:

-> ls /home/cs-students
You should see a long list of directories with names that all look like user ids along with a few other random directories.

Basic Window Manipulation

To use a window, the window must have the user's focus. To get focus, you simply click the mouse in the window. All keyboard goes to the window in focus even if the mouse points outside the window, as long as it has not been clicked in another window.

To move a window, point the mouse in the title bar, hold down the left mouse button and drag the window.

To resize a window, point at any of the four corners of the window, hold down the left mouse button, and drag the corner.

To bring a window to the top so that it is completely visible, click the left button in the title bar.

To lower a window so the windows below it are more visible, click the right button in the title bar.

To enlarge a window to a standard page size, left-click on the rightmost icon in the title bar. A second left-click will return the window to its previous size.

To iconify a window, click on the second icon from the right in the title bar. The window should appear as an icon at the bottom of the display. If it does, middle-clicking or double-clicking will restore the window. If, for some reason, no icon is displayed, you can still restore the window by pointing at the desktop (an area outside any window) and selecting the window's title from the menu displayed.

The leftmost symbol in the title bar is a rounded rectangle. If you click on it, it brings up a menu containing. The most useful commands in this menu are.

(Un)Maximize Makes the window as large as the display and restores it to its previous size on a second click.
Close Close this window. If the application uses more than one window, the application will continue to run.
Destroy Quit the application running in this window. This will close all windows associated with the application.
The area that is outside all windows is called the root. The mouse buttons behave differently in the root. In this case, each button brings up a menu. We have already seen that the right button gives a menu of all windows. The middle button is the same as the menu we saw above. The left button brings up a menu to open some common applications and also to logout. The menu includes:
xterm Creates a new window running a Unix shell
xfig Creates a new window running a tool to draw figures with
xv Creates a new window allowing you to view gifs, jpegs, etc. and to convert between image formats.
Netscape You know...
emacs Creates a new window running emacs, a text editor
Exit Fvwm Use this to logout

Basic Emacs

Emacs is a widely used text editor for Unix. It relies heavily upon control-characters as a means of entering editing commands. Many commands are also available via pulldown menus, but experienced users tend to use control commands almost exclusively. To start emacs, left click in the root and select emacs from the menu. (To feel more like a Unix user, go to a shell window and type "emacs &".) This will open a new window on your display. The window will look similar to that shown below:

The top row are the titles of pulldown menus that are available. Initially, we will work with a few of these menus and a few keyboard commands. Over the month, your repertoire of commands will grow and will move away from menus and toward keyboard commands. For now, let's create a new file. To do this, point at "Files" and click the left mouse button. Then select "Open file...". At the bottom of the window, you will see "Find file: ~/" with a small black box following it. The box indicates where characters will go when you type. Type in the name of the file that you want to create and hit carriage return.

The last line of the Emacs window is where command prompts and messages appear. The reverse video line immediately preceding it is a status line. It shows you the name of the file you are editing. Before that you should see --:--. This means that the file is up-to-date. If it says, --:**, it means the buffer has been changed since it was last saved. If it says --:%%, it means that you cannot modify the file. After the filename is the "mode" that you are editing in. This should say "(Fundamental)". We will talk about modes later this month. Next is the current line number (L1 indicates the first line). Following that is an indication of how far the current line is in the file in terms of percentage. All indicates that the entire file is showing. Top indicates that you are on the first screenful. Bottom indicates that you are on the last screenful.

The area between the menu titles and the status line is the buffer that displays the contents of the file. Most of your typing goes directly into this buffer. Type some text. Notice the status line changes. You can move the cursor around using the arrow keys. Use the scrollbar to move through the file. Use the "Save Buffer" command from the Files menu to save the buffer to a file. Use "Exit Emacs" from the Files menu to exit emacs. Notice the characters that appear in parentheses at the ends of command names in the menus. These tell you the keyboard characters to type to execute the commands without using the commands. For example, saving the buffer can be executed by typing Control-x Control-s. In the menu control characters appear as C-. Type these by holding the control key down at the same time as typing the second character. Another common way to enter commands is using the Escape key. This is abbreviated M-. In this case the escape key and the following key are typed separately. Here are the keyboard sequences for the most common Emacs commands:

C-f Forward one character
C-b Backward one character
C-n Next line
C-p Previous line
C-v Next screenful
M-v Previous screenful
C-d Delete next character
Backspace Delete previous character
C-x C-s Save buffer to a file
C-x C-f Open a file
C-x C-c Exit Emacs
Using keyboard commands is generally faster than moving one's hands back and forth between the mouse and keyboard as one must do to use the menus and scrollbar. While it takes extra effort to learn, the payoff is worth it in the long run in terms of speed of using the editor.

Getting Help

One of the most important things to learn this semester is how to get help online so that you can find things yourself. There are two tools of particular interest: xman and the info package within Emacs.

xman is already started when you log in on the console of the FreeBSD systems in TCL 312.  xman provides a graphical user interface to online Unix manuals. The manual is divided into several sections. The most useful section for users is section 1 on user commands. C programmers will be interested in sections 2 and 3 which describe library routines provided by Unix that can be called from C programs. In the xman window, click on the "Manual Page" button to bring up a window.  If you click on the Sections menu, a list of sections will appear. Selection Section 1, user commands, and you will see a complete table of contents of all Unix commands. If you click on one entry in the window, you will now see the manual page for that command. This is very useful when you want to know exactly how to use a command or exactly what arguments it takes. You will be reading a lot of man pages over the month so you will become more familiar with doing this as the month progresses.

Using xman in this way is only useful if you know exactly what command you are looking for. xman also supplies a keyword-based search. To use it, pulldown the Options menu. Select Search. Type in a keyword and then click on the apropos button. You will now see a list of every entry in the manual with that keyword in its title. Remember that you will find section 1 most interesting so it is safe to ignore those entries that do not have (1) after the command name since those are not in section 1 and thus are not user commands.

xman is most useful for the simplest Unix commands. More complex tools are usually better explained with the info package within Emacs. To start info, enter Emacs and then use "C-h i" to begin info. The first screen shows a list of programs for which help is available. Move the cursor to the program you want more help on using the arrows and then use the carriage return to select the entry. This will now give you help in a hypertext style for the program. You may find underlined links that you can move the cursor to and then hit carriage return to select them. You can also navigate the hypertext selection using the following keys:

l Last item visited (like Back in Netscape)
u Up to parent
n Next sibling
p Previous sibling
Many people prefer the text-based manual system, called man. You can get text-based information about any of the commands or system calls by typing man followed by the name of the command or system call. You can then page through the manual information. To learn more about the manual, you can read the manual about the manual by entering man man at the command prompt.

Logging Out

As mentioned earlier, to logout you point the cursor at the root (outside all X windows) and click the left mouse button. Select Exit Fvwm. You will then see a submenu whose first line reads "Really Quit Fvwm?". If you intend to logout, select "Yes, Really Quit". If you don't intend to logout select "No, Don't Quit". You should spend an hour today familiarizing yourself with using the Unix command line, file system, and Emacs text editor.

Compiling and Running a C program

To compile a C program, use the following command line:

gcc -Wall -o <output-filename> <source-filename>

For example, if you place the above program in a file called face.c, you would compile it using:

gcc -Wall -o face face.c

This will create a file called face in your directory. Note that there will be a * at the end of face. This just indicates that the file is executable and is not actually part of the filename. To execute the file, just type the output-filename at a shell prompt as follows:

-> face


Here are some simple exercises to familiarize yourself with Unix and C.
  1. Login and change your password. Remember the keys to a good password:
    • Do not use words or names
    • Do not use a variation of your login
    • Do include digits, upper and lower case, and punctuation
    • Do remember your password
  2. Start Emacs.
  3. Record your findings to the suggested exercises below in Emacs and save the results to a file.
  4. What files are in your home directory?
  5. What is the absolute pathname of your home directory?
  6. Create a directory. Copy one of your files into the directory. Now remove the directory. What commands did you need to do to accomplish all of these.
  7. Launch netscape by selecting it from the menu that appears when clicking the left button outside of any window.  Navigate to the course home page.  Set a bookmark.  You will be visiting this page often.
  8. Use xman to lookup information about some of the commands you have already been using, such as ls, cd, gcc, etc. You will notice that most Unix commands have lots and lots of options. Don't worry about memorizing them- only a few are important enough that you will ever need them, and you can always look up the options when you need to.
  9. Copy face.c from /home/faculty/freund/shared/cs010/face.c to your account. Compile and execute it. Modify the program to add a new type of face. Compile and execute the modified program.
  10. Write a C program that reads characters from the keyboard. When the user types q, it should output the number of characters that the user typed and then quit. (These last two tasks will appear in the second practice assignment too- if you feel like exploring some of the Unix utilities in the handout or experimenting with you own programs, feel free to do that today instead of these).
  11. Modify the previous program so that it also counts lines and words and outputs all three counts when the user quits.
You're done. Bring comments and questions to class next time.

Return to CS 010 Home