CSc 232 Assignment 2
|CSc 232 Ruby Assignments|
Due: Mar 3
/etc/services. This project involves writing a utility to read this file and look up the port number for any service. Even though this program uses a Unix configuration file, you don't need a whole Unix system to solve it; just a copy of the file. It's publicly readable, so you may download
/etc/servicesfile from Sandbox to your PC and work with the local copy. (This does, of course, require that you have the
-soption working. See below.)
Here is a portion of that file on Sandbox, showing its format:
#character denotes a comment. The comment continues to the end of a line. A comment may follow real data on the line. Data on each line is separated by white space. Blank lines are allowed, and are ignored. Each line containing data describes one service. The first field is the standard name for the service, the second describes the port number and the low-level protocol, separated by a slash. Then may appear zero or more additional fields specifying alias names for the same service. For more details, run
man serviceson Sandbox.
udp. If there is no
protocolspecified, show information for each protocol which actually occurs in the file. If
-s filedoes not appear, read from
For each service on the command line, print several lines of output. Place a blank line between the output for each service. Print the following.
If some service is specified on the command line with
a protocol as
xxx/ppp, and is not in the file,
xxx. and does not appear with any protocol, print:
NNNis the port number. The second line is printed only when the name matches an alias rather than a standard name. The
yyyis the standard name for the service. The third line shows alias names for the service. If the name on the command line was the standard name, this output line should read "Aliases" and give all the aliases. If there are no aliases, this line should not appear. If the command line name is an alias, this third output line should read "Other Aliases" and show only those aliases other than the one from the command line. If there are no additional aliases, this line should not appear. Finally, the comment line should show the comment at the end of the line where the service is described. If there is no comment, or if the comment contains no non-blank characters other than the leading
#, this line should not appear.
For instance, on Sandbox, the execution of
ARGV. If a
-soption is given, you will see
ARGVand the file name in
ARGV. Before processing service names, see if
-s. If so, off the
-sand the file name, and record the latter in some appropriate variable. Then you can continue processing from
ARGVwithout worrying about the option.
After dispensing with option, you must essentially compare each
command-line item with each line in the file. For this you will need a
double loop. You can loop through through each argument
foreach or something similar)
and scan the file for each one. Alternatively, you can scan through
the file once, at each line running through the argument list to see
if you have a match. (The latter is more efficient, but the
former produces output in the same order as the command line,
which seems more natural.) Open file objects have a
which returns to the start of the file.
After reading each line, you may want to start by removing the
comment and saving it somewhere.
The simplest way to decide if a line matches the command-line
specification is probably to break the line into fields and
examine the fields. You can break it up
split, or by matching it and using
variables. It is also possible to use the command-line item to build
a pattern which will match only lines you want, but be careful about
matching the prefix of some actual service name by accident.