$Id: asg4-shell-tree.mm,v :36: $

Similar documents
5/8/2012. Creating and Changing Directories Chapter 7

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Unix background. COMP9021, Session 2, Using the Terminal application, open an x-term window. You type your commands in an x-term window.

Chapter Two. Lesson A. Objectives. Exploring the UNIX File System and File Security. Understanding Files and Directories

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

Binary Trees

Linux shell scripting Getting started *

Unix as a Platform Exercises. Course Code: OS-01-UNXPLAT

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

Trees. (Trees) Data Structures and Programming Spring / 28

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

Crash Course in Unix. For more info check out the Unix man pages -orhttp:// -or- Unix in a Nutshell (an O Reilly book).

Binary Trees, Binary Search Trees

CSCI 2132 Software Development. Lecture 4: Files and Directories

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

Garbage Collection: recycling unused memory

TREES. Trees - Introduction

Appendix A GLOSSARY. SYS-ED/ Computer Education Techniques, Inc.

: the User (owner) for this file (your cruzid, when you do it) Position: directory flag. read Group.

UNIX Tutorial One

Creating a Shell or Command Interperter Program CSCI411 Lab

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

PROGRAMMING PROJECT ONE DEVELOPING A SHELL

Introduction to Linux Spring 2014, Section 02, Lecture 3 Jason Tang

Shell Programming Overview

CE 221 Data Structures and Algorithms

A Simple Syntax-Directed Translator

Unix tutorial. Thanks to Michael Wood-Vasey (UPitt) and Beth Willman (Haverford) for providing Unix tutorials on which this is based.

CSE374 Winter 2016, Midterm Examination February 8, 2016 Please do not turn the page until the bell rings.

Programming II (CS300)

GMI-Cmd.exe Reference Manual GMI Command Utility General Management Interface Foundation

Introduction to Linux

Essential Linux Shell Commands

CS301 - Data Structures Glossary By

Operating Systems. Copyleft 2005, Binnur Kurt

Operating Systems 3. Operating Systems. Content. What is an Operating System? What is an Operating System? Resource Abstraction and Sharing

CSCI 2132 Software Development. Lecture 5: File Permissions

Examples: Directory pathname: File pathname: /home/username/ics124/assignments/ /home/username/ops224/assignments/assn1.txt

The Directory Structure

Week 2 Lecture 3. Unix

2.2 Syntax Definition

Algorithms. Deleting from Red-Black Trees B-Trees

Introduction: What is Unix?

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Scripting Languages Course 1. Diana Trandabăț

Basic Unix Command. It is used to see the manual of the various command. It helps in selecting the correct options

Filesystem and common commands

Data Structures Question Bank Multiple Choice

Operating Systems and Using Linux. Topics What is an Operating System? Linux Overview Frequently Used Linux Commands

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Useful Unix Commands Cheat Sheet

CSC 112 Lab 1: Introduction to Unix and C++ Fall 2009

CMPS 12A Introduction to Programming Lab Assignment 7

Linux & Shell Programming 2014

Unix Tutorial Haverford Astronomy 2014/2015

UNIX COMMANDS AND SHELLS. UNIX Programming 2015 Fall by Euiseong Seo

History. Terminology. Opening a Terminal. Introduction to the Unix command line GNOME

Week 5 Lesson 5 02/28/18

CS3114 (Fall 2013) PROGRAMMING ASSIGNMENT #2 Due Tuesday, October 11:00 PM for 100 points Due Monday, October 11:00 PM for 10 point bonus

Files and Directories

System Programming. Introduction to Unix

Processes. What s s a process? process? A dynamically executing instance of a program. David Morgan

Programming Assignment #1: A Simple Shell

Manual Shell Script Linux If File Exists Wildcard

Files

This is Lab Worksheet 3 - not an Assignment

Cpt S 122 Data Structures. Data Structures Trees

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

singly and doubly linked lists, one- and two-ended arrays, and circular arrays.

Utilities. September 8, 2015

22-Sep CSCI 2132 Software Development Lecture 8: Shells, Processes, and Job Control. Faculty of Computer Science, Dalhousie University

Please choose the best answer. More than one answer might be true, but choose the one that is best.

Linux Essentials. Programming and Data Structures Lab M Tech CS First Year, First Semester

Recursive Data Structures and Grammars

Basic Survival UNIX.

CSE 214 Computer Science II Introduction to Tree

Outline. Cgroup hierarchies

CMSC421: Principles of Operating Systems

Operating System Interaction via bash

Linux Command Line Primer. By: Scott Marshall

Review of Fundamentals

Lab 2: Linux/Unix shell

5/20/2007. Touring Essential Programs

Module 10: Unix Primer

COMP 250 Fall recurrences 2 Oct. 13, 2017

lsx [ls_options ] [names]

18-Sep CSCI 2132 Software Development Lecture 6: Links and Inodes. Faculty of Computer Science, Dalhousie University. Lecture 6 p.

Project 1: Implementing a Shell

Overview of the UNIX File System. Navigating and Viewing Directories

A Brief Introduction to Unix

OPERATING SYSTEMS, ASSIGNMENT 4 FILE SYSTEM

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall Programming Assignment 1 (updated 9/16/2017)

Project 2: Shell with History1

EECS2301. Lab 1 Winter 2016

Processes in linux. What s s a process? process? A dynamically executing instance of a program. David Morgan. David Morgan

Lec 1 add-on: Linux Intro

Outline. Cgroup hierarchies

File-System Interface. File Structure. File Concept. File Concept Access Methods Directory Structure File-System Mounting File Sharing Protection

d. Permissions 600 on directory dir and 300 on file dir/foo. c. Permissions 700 on directory dir and 200 on file dir/foo.

CS 3410 Intro to Unix, shell commands, etc... (slides from Hussam Abu-Libdeh and David Slater)

Transcription:

cmps012b 2002q2 Assignment 4 Shell and Tree Structure page 1 $Id: asg4-shell-tree.mm,v 323.32 2002-05-08 15:36:09-07 - - $ 1. Overview A data structure that is useful in many applications is the Tree. One example of the use of a tree structure is the Unix file system, which keeps files in a hierarchical tree structure where the leaf nodes are files and the interior nodes are directories. In this assignment you will write a simulation of a shell and interpret some Unix-like commands. You will not actually interact with the file system, and will rather keep the simulated files in memory. 2. Program Specification NAME treesh.jar simple shell implemented by a tree SYNOPSIS treesh.jar [ Dx] [filename] DESCRIPTION The treesh.jar utility is a small application that simulations the execution of commands and manipulation of files. Input is read from the filename specified on the call, and from stdin if no filename is specified. All normal output is written to stdout and error messages are written to stderr. The only operand is the filename. When the shell begins, the only existing structure is the root directory, called. There are no files. When the shell exits, everything disappears because this is only a simulation and there is no real file system, so : If we šha dowß have offen ded, Think but thi ß, and all iß men ded, That you have but šlu mb red here Wh ile theše višionß did app ear. Ånd thi ß weak and idle the me, No more yield ing but a dre am, OPTIONS If the x option is given, prior to execution, each command is echoed to stdout preceded by a blank line and a prompt sign. If an input filename is given, but no x option is specified, no prompt is written at all. In this case, the output from a non-interactive shell session will look like the results of an interactive session. The D option causes debug output to be written to the stderr. Its purpose is left to the implementor and may in fact do nothing at all once recognized. EXIT STATUS An exit status of 0 is returned if no errors were detected and 1 is returned if any errors occurred. USAGE The shell repeatedly reads commands from stdin, parses the command line into a word (the command) and its operand string, then executes the command. Words are delimited by while space. The first word on the line is the command and the rest are passed as arguments to that command. In the syntax below, the notion string refers to the rest of the line and may contain spaces. The string need not be parsed, and may in fact be empty. It always begins as the first non white space character following the command. A pathname is a word from the command line that is used to refer to a file or directory. Three special directory names are recognized : slash () dot («.»), and dotdot («..»). Slash refers to the root directory and dotdot to the parent of the current directory. Because the root has no parent, the parent of the root is itself. Dot always is an alias for the current directory, but is of not much use except for output. An inode is a sequence number uniquely identifying a particular directory entry. The inode number of the root () is always 1, because it is the first inode that is generated. Each succeeding inode is generated in sequence as 2, 3, 4, etc., regardless of where they appear in the tree. In this little example program, inode numbers are never recycled. The make and mkdir commands allocate new inodes. None of the other commands do. COMMANDS The following commands are supported. An error is reported if the wrong number of operands are given to a command.

cmps012b 2002q2 Assignment 4 Shell and Tree Structure page 2 # string If the first word on a line is a hash, the line is a comment and is ignored. cat filename The line in the file is copied to stdout. It is an error if any file specified does not exist or is a directory. cd [directoryname] The current directory is set the the pathname given. If no pathname is specified, the root directory () is used. It is an error if the pathname does not exist or is a file. echo string The string, which may be empty, is echoed to stdout. ls [pathname] A description of the file or directory is printed to stdout. It is an error if any of the file or directory does not exist. If no pathname is specified, the current working directory is used. For each file specified, output consists of the inode number, then the file size, then the filename. Output must be lined up into columns and each column is separated from the next by two spaces. The numeric fields are exactly 6 characters wide and the units position in a column must be aligned. If a pathname specified is a directory, then the contents of the directory are listed. A directory listed within a directory is shown by a terminating slash. lsr [pathname] As for ls, but a recursive depth-first preorder traversal is done for subdirectories. make filename string The file specified is created and the rest of the words are put in that file. All files are only one string. If the file already exists, a new one is not created, but its content line is replaced. It is an error to specify a directory. mkdir directoryname A new directory is created. It is an error if a file or directory of the same name already exists. Two entries are added to the directory, namely dot and dotdot. Directory entries are always kept in sorted lexicographic order. prompt string Set the prompt to the words specified on the command line. Each word is separated from the next by one space and the prompt itself is terminated by an extra space. The default prompt is a single percent sign («%») pwd Prints the current working directory. rm pathname The specified file or directory is deleted (removed from its parent s list of files and subdirectories). It is an error for the pathname not to exist. If the pathname is a directory, then all files and subdirectories are disconnected as well as a side effect. 3. A Sample Run The following table shows a sample run. Each interaction with the shell is listed in a separate box with shell output in Courier Roman and user input in Courier Bold typeface. A commentary about what is happening is opposite in the right column. Initially the cwd is the root directory.

cmps012b 2002q2 Assignment 4 Shell and Tree Structure page 3 % ls 1 2. 1 2.. The absense of an operand to ls means that dot is used as its operand, which is currently the root. Directories always contain at least two items, namely dot and dotdot. The inode number of the root is always inode #1. The parent of dotdot is itself. % make foo this is a test Make a file called foo which contains the string «this is a test», which is 14 characters. An inode is allocated, namely inode #2. % make bar test a is this Another file, similarly created, with inode #3. % ls 1 4. 1 4.. 3 14 bar % cat food cat: food: No such file or directory Same as the previous output of ls, except with two more files. Note that files are kept in lexicographic order, so bar is listed before foo. An error message is printed, causing the return code from the shell eventually to be 1 rather than 0. Note the error format : command followed by object causing the problem followed by the reason for the failure. Files can consist of only one line, namely a String. % cat foo this is a test % echo O for a muse of fire Arguments to echo are simply written to the standard output. O for a muse of fire % prompt => The prompt is changed to the characters «=>» followed by a space. Multiple words would have been permitted. => rm bar The file bar is deleted and the size of the root directory is reduced by 1. => make baz foo bar baz A new file is created with inode #4. => mkdir test Inode #5 is created as a directory called test. This directory is a child of the root and contains the two usual entries, dot and dotdot. => prompt % % ls Just checking the contents of the root. 1 5. 4 11 baz 5 2 test The cwd is now test. Yes, it is. test % cd Without arguments cd goes back to the root directory. OK. Go to a directory called test which is a subdirectory of the cwd, whose alias name is always dot. test % cd.. Dotdot is always an alias for the parent of the cwd.

cmps012b 2002q2 Assignment 4 Shell and Tree Structure page 4 % make me me me me % cat me me me me % cd.. % cat me me me me % cd % lsr 1 5. 4 11 baz 5 3 test test: 5 3. 6 8 me % mkdir foo % cd foo % mkdir bar % cd bar % mkdir baz % cd baz % ls..: 9 2. 8 3.. % cd % lsr test test: 5 4. 7 3 foo 6 8 me testfoo: 7 3. 5 4.. 8 3 bar testfoobar: 8 3. 7 3.. 9 2 baz testfoobarbaz: 9 2. 8 3.. This would have errored out if test were not a directory or did not exist. The next available inode is #6. Recursive directory listing. This is done using a preorder traversal. Withing a given lev el, lexicographic ordering is used. Recursion will go through all subdirectories at all levels. Note that foo uses inode #7, bar uses inode #8, and baz uses inode #9. At this point dot is baz and dotdot is bar. A rather large test showing inode numbers, file and directory sizes, and filenames. Note that directory names are indicated in the listing with a trailing slash. Again, the size of a file is the number of characters in it and the size of a directory is the sum of the number of files and subdirectories in it. The subdirectory count is not recursive. % ^D End of file or ControlD causes the shell to exit.

cmps012b 2002q2 Assignment 4 Shell and Tree Structure page 5 4. The Tr ee Data Structure The main point of this assignment is the construction of an n-way tree data structure. Each node in the tree should have a pointer to the parent node. The parent pointer of the root should either be null or point at itself. Each tree node also has a sibling link manipulated by the directory operations. This should be implemented as an abstract base class Inode, using proper object-oriented style. The method size is abstract. The method inode returns the inode number of the current structure. A static field in this class will record the next available inode number and this will be incremented in the constructor. Class Directory extends Inode will contain other files and directories and as such will need to have a list of children. Use a singly linked list of children as in the LinkedStack example. When inserting a new file or directory into its parent, search the list and insert it according to descending lexicographic ordering. The method size counts the number of files in the directory. Class File extends Inode Contains the data in the file. The method size returns the length of the string which contains the data. Each file is represented by a single string. 5. Implementation Strategy 1. Do not use PerlBox for this assignment. Write your own code. First, analyze the options and direct the input to the appropriate place. 2. Read lines one at a time and print the words from the line with the command first, followed by the operand. 3. Start working on the tree class. The three tree classes should not be nested, but you may place them all in one file if you like. 4. Then select the commands, one at a time, and implement them. Be careful about implementation order. 6. Handin As usual, verify your submit, and submit README, GNUmakefile, and the Java source files. Your makefile should build the jar from the sources.