ICS54
                                =========

                       Solutions to Assignment #1

There are several ways to do each question.  I have listed several solutions
for each.

1) What is your home directory?  How did you find out?

    There are several ways to find out.
    - "echo $HOME"
    - login, and before typing any "cd" command, type "pwd".
    - type "cd" by itself, and type "pwd"
    - Read the first few lines of "man 4 passwd", then grep in /etc/passwd:
        $ grep Hayes /etc/passwd
        wayne:x:666:114:Wayne Hayes:/u/wayne:/var/shell/tcsh
                                    ^^^^^^^^ this is my home directory.

2) What is your UID?  How did you find out?

    Your UID is not the same as your user name; the former is an integer,
    the latter is a word.

    - use type "id", and read the output.
    - read the first few lines of "man 4 passwd" and grep /etc/passwd...
    - "/bin/ls -n" on any file you own, and look at the uid.

3) What is your group name(s)?  How did you find out?

    The group name is not the same as the group id; the former is a word, the
    latter is an integer.  The solutions are the same as #2, except look at
    different fields.

4) What is your "kill" character used for?

    The "kill" character erases the current line that you are typing.  It
    is usually '^U'.  It is NOT the same as the kill command (type "man 1 kill"
    to read about that), nor is it the character that you press to interrupt
    a program.  That's called the "interrupt" character, and is usually '^C'.
    It's mentioned on page 6 of K&P.  Type "stty -a" to see all the "magic"
    characters that are currently set on your terminal.


5) If you remove execute permission for yourself from a directory, are
   you still allowed to create files in that directory?  Can you cd into
   it?  Can you "ls" it?

    This question was pure experimentation.  You could answer it in 2 minutes
    if you simply tried it.  Here's my output and comment:

    $ mkdir foo
    $ chmod u-x foo
    $ echo hi >foo/bar
    ksh: foo/bar: cannot create # that answers the first question...
    $ cd foo
    ksh: foo: permission denied # that answers the second...
    $ ls foo
    $ ls -l foo
    total 0                     # hmm, but there are no files.  Let's try...
    $ chmod u+x foo             # make it executable again...
    $ echo hi >foo/bar          # put a file there
    $ ls foo
    bar
    $ ls -l foo                 # OK, it's there.
    total 0
    -rw-r--r--   1 wayne           3 Sep 26 18:05 bar
    $ chmod u-x foo             # take away execute permission again...
    $ ls foo
    bar                         # works...
    $ ls -l foo
    ls: foo/bar: Permission denied  # AHA!
    total 0

    The last line tells us that we can read the directory file (which makes
    sense, because it's just a file) and so we can see what file names are in
    there.  But "ls -l" needs to get information that's stored in the i-nodes
    of the files in the directory, and THAT is not allowed.  That is called
    "searching" a directory, and that's not allowed without execute permission.

6) If you make a hard link to a file, and then remove the original file,
   are the contents of the file actually removed?  Why?  How about if you
   make a symbolic link?  Why?

    No, and yes, respectively.  A hard link is just as strong a connection
    to the inode (and thus the data) as the original filename, so the inode
    and the file data stay around if you remove the original file.  However,
    a soft link is only a pathname to the original filename, not the original
    i-node.  So if you remove the original file (and assuming it was the only
    hard link to the file), then the data is erased and the soft link points
    nowhere.

7) Are you allowed to "mv" a directory?  Are you allowed to "cp" a
   directory?  If so, how?

    Since a directory is just a file, there is no problem moving it to
    somewhere else on the *same* mounted file system.  The directory still
    has the same i-node, and the filenames in the directory still have the
    same inodes; it is really just a name change.  You cannot move a directory
    across file systems, though, because the inodes and data reside on a
    specific mounted file system.  You can copy a directory with the "-r"
    option.  (You may need to use "-R" option if named pipes are present;
    and you may want to use "-p" to preserve date-time stamps.)  You can
    also use tar(1):

    $ (cd src-dir; tar cf - .) | (cd dest-dir; tar vxf -)

8) How many mounted file systems are there on eddie.cdf?

    First you have to get onto eddie; to do that, use "rlogin eddie" or
    "rsh eddie", or "telnet eddie".
    Typing "mount | wc" gives 16 lines; "df | wc" gives 17, but that includes
    a header, so you need to subtract 1.

9) Using only "ls", can you find out the name of your login terminal?  How?

    This was alot harder than I intended.  Here's how I did it:

    $ cd /dev
    $ ls -l | grep wayne    # no output... Hmmmm
    $ who am i              # cheating...
    wayne      /dev/pts/82  Sep 26 18:47
    $ ls -l /dev/pts/82     # the symbolic link isn't any help
    lrwxrwxrwx   1 root           29 Jun 20 15:34 /dev/pts/82 -> (long name)
    $ ls -lL /dev/pts/82    # ah, much better
    crw--w----   1 wayne     24,  82 Sep 26 18:48 /dev/pts/82
    $ cd                    # back to the home directory with knew knowledge...
    $ ls -lL /dev/pts | grep wayne  # I'm logged in 4 times.
    crw-rw-rw-   1 wayne     24,   2 Sep 26 15:19 2
    crw--w----   1 wayne     24,  46 Sep 26 18:49 46
    crw--w--w-   1 wayne     24,  52 Sep 26 18:49 52
    crw--w----   1 wayne     24,  82 Sep 26 18:50 82
    $ date          # it must be /dev/pts/82, since that's the most recent one,
    Thu Sep 26 18:50:19 EDT 1996
    $               # and that's the one I'm typing on right now.


10) In slide 6 from lecture #1, assume binary 00000000 represents
    register D0.  Then the address translation from "FFFFDC0x" to
    binary is incorrect.  Ignore the leading FFFF, then correct the
    binary representation of the DC0x addresses.  What is the machine
    language (binary) representation of the MOVE instruction? What
    about the MUL instruction?

    There are three lines of machine language, and three lines of assembly
    language, so they go one-to-one to each other.  Here is the corrected
    version:

    1100101 11011100 00000000 00000000
    MOVE    DC       00       D0

    1010001 00000010 01011101 00000000
    MUL     #2       garbage  D0

    1100101 00000000 11011100 00000100
    MOVE    D0       DC       04

The MOVE instruction is represented by 1100101, MUL by 1010001.

There are several ways to convert from hexadecimal to binary.  The easiest
way (not including using a calculator that can do it directly) is to realize
that 16 = 2^4, so that you can build the following table (note: by convention
in Unix & C, base-8 numbers have a leading 0, and base-16 have a leading 0x):

base 10    base 8      base 16     base 2
0           00          0x0         0000
1           01          0x1         0001
2           02          0x2         0010
3           03          0x3         0011
4           04          0x4         0100
5           05          0x5         0101
6           06          0x6         0110
7           07          0x7         0111
8           010         0x8         1000
9           011         0x9         1001
10          012         0xA         1010
11          013         0xB         1011
12          014         0xC         1100
13          015         0xD         1101
14          016         0xE         1110
15          017         0xF         1111
16          020         0x10       10000

So, for example, 0xDC = 1101 1100, as I have in the solution above.