CSCI 415/515: Fall 2023
Systems Programming
Project 2: fedit

Due: Tue, Sept 26 @7:00am


In this project, you will implement a basic file editor, which we will call fedit. Later in the semester, we will learn about ed and sed, which are two powerful command-line file editors.

Name

fedit - edit a file

Synopsis

fedit [-h] [-l NROTL] [-r NROTR] [-x NEXPAND] [-c NCONTRACT] [-v CHAR] [-s NSKIP] [-k NKEEP] [-r] PATH

Description

The fedit program performs one of the following operations on a file:
rotate left
Rotate the bytes of the file left; the bytes that "fall off" wrap around to the end of the file.
expand
Expand the file by a number of bytes.
contract
Shorten a file by a number of bytes.
keep
Keep only a range of bytes, removing the others from the file (and reducing the file's size accordingly)

The user must specify exactly one of of these operations (that is, the user cannot specify multiple operations).

Options

Mandatory options to long options are also mandatory for the corresponding short options. In this specification, the symbol FSIZE is the input file's size.

-h, --help

Print a usage statement to stdout and exit with status 0.

-l, --rotate-left NROTL

Rotate the file NROTL bytes left (with wrap around).

-x, --expand NEXPAND

Expand the file by NEXPAND. The value of these bytes is given by the --value option.

-c, --contract NCONTRACT

Contract (shrink) the file by NCONTRACT bytes. If NCONTRACT > FSIZE, shrink the file to 0 bytes.

-v, --value CHAR

The character value that is used when expanding the file.
Default: A.

-k, --keep NKEEP

Keep this many bytes (starting from the offset provided by --skip) (and remove all others). In the case of EOF, fewer than NKEEP bytes may be kept.

-s, --skip NSKIP

Optionally used with --keep. Skip NKSIP bytes before keeping.
Default: 0
It is an error to use this option if the user does not also specify --keep.

Exit Status

The exit status is 0 if the operation was successfully performed and non-zero (conventionally 1 or 2) otherwise.

Bonus 1

Implement the following feature:

-r, --rotate-right NROTR

Rotate the file NROTR bytes right (with wrap around).

Remember to include a blank file called bonus1 in your project submission so that I know to grade this feature.

Bonus 2

Implement the following feature:

-m, --repeat

Repeat skip/keep operations as many times until the end of the file is reached.
It is an error to use this option if the user does not also specify --keep.

Remember to include a blank file called bonus2 in your project submission so that I know to grade this feature.

Submitting

Submit your project as a zip file via gradescope. Your project must include a Makefile that builds an executable called fedit. Please refer to the instructions for submitting an assignment for details on how to login to gradescope and properly zip your project.

Starter Code

You can download a zip file with the starter code (fedit.c, mu.c, mu.h, Makefile).

Rubric

Input Files: alphabet

-h, --help


1.1 Print a usage statement (2 pts)


        ./fedit --help
        

Prints a usage statement to stdout. The statement must start with either Usage or usage; you decide the rest of the message. Conventionally, this option either prints the synopsis or a more verbose statement that also includes a description of the options.

1.2 Zero exit status (2 pts)


        ./fedit --help
        echo $?
        0
        

The exit status is zero.

Bad usage: More than One Operation


2.1 Print error message (2 pt)


        ./fedit -x20 -c10 alphabet
        

Print a one-line error message to stderr (you decide the contents of the message).

2.2 Nonzero exit status (2 pt)


        ./fedit -x20 -c10 alphabet
        

The exit status is nonzero.

Bad usage: Unknown option


3.1 Print error message (2 pt)


        ./fedit -k 10 -q alphabet
        

Print a one-line error message to stderr (you decide the message).

3.2 Nonzero exit status (2 pt)


        ./fedit -k 10 -q alphabet
        

The exit status is nonzero.

Bad usage: Missing option argument


4.1 Print error message (2 pt)


        ./fedit -k -s 2 alphabet
        

Print a one-line error message to stderr (you decide the message).

4.2 Nonzero exit status (2 pt)


        ./fedit -k -s 2 alphabet
        

The exit status is nonzero.

File does not exist


5.1 Print error message (2 pt)

        ./fedit -k10 nonexistent
        

Prints a message to stderr that includes the text No such file (case-insensitive).

5.2 Nonzero exit status (2 pt)

        ./fedit -k10 nonexistent
        

The exit status is nonzero.

Rotate left operation


6.1 rotl (7 pt)

        ./fedit --rotate-left 5 alphabet
        cat alphabet
        fghijklmnopqrstuvwxyzabcde$
        

The file is modified as shown above. Note that the output does not have a newline, hence the shell prompt immediately follows.

6.2 rotl multiple cycles (7 pt)

        ./fedit --rotate-left 27 alphabet
        cat alphabet
        bcdefghijklmnopqrstuvwxyza$
        

The file is modified as shown above. Note that the output does not have a newline, hence the shell prompt immediately follows.

6.3 rotl negative (5 pt)

        ./fedit --rotate-left -1 alphabet
        

Print a one-line error message to stderr (you decide the contents of the message).

Expand operation


7.1 expand (7 pt)

        ./fedit -x4 alphabet
        cat alphabet
        abcdefghijklmnopqrstuvwxyzAAAA$
        

The file is modified as shown above. Note that the output does not have a newline, hence the shell prompt immediately follows.

7.2 expand with value (7 pt)

        ./fedit --expand 5 --value B  alphabet
        cat alphabet
        abcdefghijklmnopqrstuvwxyzBBBBB$
        

The file is modified as shown above. Note that the output does not have a newline, hence the shell prompt immediately follows.

Contract operation


8.1 contract (7 pt)

        ./fedit -c8 alphabet
        cat alphabet
        abcdefghijklmnopqr$
        

The file is modified as shown above. Note that the output does not have a newline, hence the shell prompt immediately follows.

8.2 contract fsize (7 pt)

        ./fedit -c26 alphabet
        cat alphabet

        

The file has zero bytes.

8.2 contract greater than fsize (7 pt)

        ./fedit -c30 alphabet
        cat alphabet

        

The file has zero bytes.

Keep operation


9.1 keep (7 pt)

        ./fedit --keep=5 alphabet
        cat alphabet
        abcde$
        

The file is modified as shown above. Note that the output does not have a newline, hence the shell prompt immediately follows.

9.2 skip keep (7 pt)

        ./fedit --skip=3 --keep=6 alphabet
        cat alphabet
        defghi$
        

The file is modified as shown above. Note that the output does not have a newline, hence the shell prompt immediately follows.

9.3 skip keep partial (7 pt)

        ./fedit -s20 -k10  alphabet
        cat alphabet
        uvwxyz$
        

The file is modified as shown above. Note that the output does not have a newline, hence the shell prompt immediately follows.

9.4 skip keep none (7 pt)

        ./fedit -s30 -k10  alphabet
        cat alphabet

        

The file has zero bytes.

Bonus 1: Rotate right operation


100.1 rotr (5 pt)

        ./fedit -r5  alphabet
        cat alphabet
        vwxyzabcdefghijklmnopqrstu$
        

The file is modified as shown above. Note that the output does not have a newline, hence the shell prompt immediately follows.

100.2 rotr multiple cycles (5 pt)

        ./fedit --rotate-right=28  alphabet
        cat alphabet
        yzabcdefghijklmnopqrstuvwx$
        

The file is modified as shown above. Note that the output does not have a newline, hence the shell prompt immediately follows.

Bonus 2: Repeat skip+keep operations


200.1 skip keep repeat (5 pt)

        ./fedit -s2 -k3 -m alphabet
        cat alphabet
        cdehijmnorstwxy$
        

The file is modified as shown above. Note that the output does not have a newline, hence the shell prompt immediately follows.

200.2 skip keep repeat #2 (5 pt)

        ./fedit -s5 -k10 -m alphabet
        cat alphabet
        fghijklmnouvwxyz$
        

The file is modified as shown above. Note that the output does not have a newline, hence the shell prompt immediately follows.