HW Simulating Linux FDISK in TCL
asmt3_Al.tcl
#saad aldahlawi #CIT270(asmt3) #************************************* #Procedure: Create new partition #************************************** proc new_partition {ptable} { #Prompt table information puts -nonewline "Enter device label: " flush stdout set label [gets stdin] if { [lsearch $ptable $label*]>=0 } { puts "Label already exists" return $ptable } #Prompt other fields puts -nonewline "Enter starting cylinder: " flush stdout set start_cylinder [gets stdin] puts -nonewline "Enter ending cylinder: " flush stdout set end_cylinder [gets stdin] puts -nonewline "Enter number of blocks: " flush stdout set blocks [gets stdin] puts -nonewline "Enter id: " flush stdout set id [gets stdin] puts -nonewline "Enter file system (ex: NTFS, Linux): " flush stdout set file_system [gets stdin] set new_entry [concat $label $start_cylinder $end_cylinder $blocks $id $file_system] lappend ptable $new_entry return $ptable } ############################ #Procedure: Delete partition proc delete_partition {ptable} { puts -nonewline "Enter device label you'd like to remove: " flush stdout set label [gets stdin] set row [lsearch $ptable $label*] if { "row" == "-1" } { puts "Partition doesn't exist" return $ptable } set ptable [lreplace $ptable $row $row] return $ptable } proc print_table {ptable} { puts "****************************************************" puts "Partition Start End Blocks Id System" puts "****************************************************" foreach index $ptable { puts $index } return $ptable } proc write_table {ptable} { global file_handle global file_name close $file_handle set file_handle [open $file_name "w"] foreach index $ptable { puts $file_handle $index } close $file_handle } # Main Script puts -nonewline "Enter table name to open: " flush stdout set file_name [gets stdin] set file_handle [open $file_name "r+"] while { [gets $file_handle line] > 0 } { lappend ptable $line } set input "pass" while {$input != "w" && $input != "q"} { puts " " puts "**********************************************" puts "FDISK SIMULATOR" puts "***********************************************" puts "n = Create a new partition" puts "d = Delete a partition" puts "p = Print the partition table" puts "w = Write the new partition table and exit" puts "q = Quit without saving changes" puts "************************************************" puts -nonewline "Enter input: " flush stdout set input [gets stdin] #Based on user input, call appropriate function switch $input { "n" { set ptable [new_partition $ptable] } "d" { set ptable [delete_partition $ptable] } "p" { set ptable [print_table $ptable] } "w" { set ptable [write_table $ptable] puts "Saved." } "q" { puts "Exiting." } default { puts "Unknown input" } } }
asmt3sheet.pdf
CIT 270L Integrative Programming
Assignment #3—Simulating Linux FDISK in TCL
Due 4/7 in Lab section / Moodle
Objective:
To simulate the FDISK command for allocating partitioning on a disk.
Inputs:
Input text file consisting of disk allocation information (device, starting location, ending location, block size,
id, file system)
User choice: (‘n’-Create a new partition, ‘d’-Delete a partition, ‘p’- Print the partition table, ‘w’-Write the
partition table and exit, ‘q’-Quit without saving changes)
Output:
Display of disk allocation information in a partition table
Output text file consisting of disk allocation information (device, starting location, ending location, block
size, id, file system)
Specification:
The program maintains disk partition allocation information among multiple logical devices.
Any user input should consist of:
(1) a visible prompt (requiring the use of: "flush stdout" after: puts –nonewline "prompt string"
(2) setting the user input to a new variable (requiring the use of: "set var [gets stdin]")
Each option should be implemented as a separate function, so that its function can be called using either a
(i) switch or an (ii) if-elseif-else statement.
The disk partition allocation information will be loaded from an input file and stored in a list. The list will be
passed as a parameter from the "main" portion of the script to the appropriate function in the file and updated
according to the function. To search for a pattern within a list (for checking if a device label exists/doesn't exist),
use: lsearch $list $pattern* which returns -1 if the pattern is not found, or the first instance of the
pattern within the list if found.
Initially, the program prompts for an input text file to read from. If the file does not exist, it should be created.
The program should then loop until the user chooses to quit (either selects choice as 'w' or ‘q’). If the user enters
an illegal status, the program will prompt again for the status input. Upon quitting, the program prompts the user
to save the list to an output file with the same name as the original input text file.
What to turn in:
A single file (asmt3_yourlastname.tcl) containing the source code file, submitted via Moodle
(http://moodle.csun.edu) to the Lab section (not the lecture section) Any deviation from the format for
submission will result in an automatic -10%.
Sample Output:
% tclsh asmt3_mcilhenny.tcl
Enter file to open: table.txt
FDISK Simulator
---------------
n = Create a new partition
d = Delete a partition
p = Print the partition table
w = Write the new partition table and exit
q = Quit without saving changes
Enter choice: p
Partition Start End Blocks Id System
------------------------------------------------------
/dev/sda1 1 196 395104 83 Linux
/dev/sda2 197 262 133056 82 Swap
/dev/sda3 263 458 395136 83 Linux
FDISK Simulator
---------------
n = Create a new partition
d = Delete a partition
p = Print the partition table
w = Write the new partition table and exit
q = Quit without saving changes
Enter choice: n
Enter device label: /dev/sda1
Label already exists
FDISK Simulator
---------------
n = Create a new partition
d = Delete a partition
p = Print the partition table
w = Write the new partition table and exit
q = Quit without saving changes
Enter choice: n
Enter device label: /dev/sda4
Enter starting cylinder: 459
Enter ending cylinder: 621
Enter number of blocks: 328608
Enter id: 86
Enter file system (ex: NTFS, Linux): NTFS
FDISK Simulator
---------------
n = Create a new partition
d = Delete a partition
p = Print the partition table
w = Write the new partition table and exit
q = Quit without saving changes
Enter choice: p
Partition Start End Blocks Id System
------------------------------------------------------
/dev/sda1 1 196 395104 83 Linux
/dev/sda2 197 262 133056 82 Swap
/dev/sda3 263 458 395136 83 Linux
/dev/sda4 459 621 328608 86 NTFS
FDISK Simulator
---------------
n = Create a new partition
d = Delete a partition
p = Print the partition table
w = Write the new partition table and exit
q = Quit without saving changes
Enter choice: d
Enter partition to delete: /dev/sda5
Partition doesn't exists
FDISK Simulator
---------------
n = Create a new partition
d = Delete a partition
p = Print the partition table
w = Write the new partition table and exit
q = Quit without saving changes
Enter choice: d
Enter partition to delete: /dev/sda3
FDISK Simulator
---------------
n = Create a new partition
d = Delete a partition
p = Print the partition table
w = Write the new partition table and exit
q = Quit without saving changes
Enter choice: p
Partition Start End Blocks Id System
------------------------------------------------------
/dev/sda1 1 196 395104 83 Linux
/dev/sda2 197 262 133056 82 Swap
/dev/sda4 459 621 328608 86 NTFS
FDISK Simulator
---------------
n = Create a new partition
d = Delete a partition
p = Print the partition table
w = Write the new partition table and exit
q = Quit without saving changes
Enter choice: w
Exiting...
% cat table.txt
/dev/sda1 1 196 395104 83 Linux
/dev/sda2 197 262 133056 82 Swap
/dev/sda4 459 621 328608 86 NTFS