Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update lseek.c #51

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Update lseek.c #51

wants to merge 4 commits into from

Conversation

jkool702
Copy link
Owner

@jkool702 jkool702 commented Nov 25, 2024

add support to the lseek loadable builtin to use SEEK_SET and SEEK_END

Summary by Sourcery

Enhance the lseek loadable builtin by adding support for SEEK_SET and SEEK_END options, updating the usage documentation and error messages accordingly.

New Features:

  • Add support for SEEK_SET and SEEK_END options in the lseek loadable builtin, allowing users to specify the seek type when moving the file descriptor.

Enhancements:

  • Update the usage documentation and error messages to reflect the new optional SEEK_TYPE parameter in the lseek command.

Copy link
Contributor

sourcery-ai bot commented Nov 25, 2024

Reviewer's Guide by Sourcery

This PR extends the lseek loadable builtin functionality by adding support for SEEK_SET and SEEK_END seek types in addition to the existing SEEK_CUR behavior. The implementation adds an optional third parameter to specify the seek type, defaulting to SEEK_CUR for backward compatibility.

Sequence diagram for lseek command execution

sequenceDiagram
    actor User
    participant lseek_builtin as lseek_builtin
    participant lseek_main as lseek_main
    participant lseek as lseek

    User->>lseek_builtin: Execute lseek <FD> <REL_OFFSET> [<SEEK_TYPE>]
    lseek_builtin->>lseek_main: Parse arguments
    alt Valid arguments
        lseek_main->>lseek: Call lseek with SEEK_TYPE
        alt SEEK_TYPE is SEEK_SET
            lseek-->>lseek_main: Move FD to offset from start
        else SEEK_TYPE is SEEK_END
            lseek-->>lseek_main: Move FD to offset from end
        else SEEK_TYPE is SEEK_CUR or invalid
            lseek-->>lseek_main: Move FD to offset from current
        end
    else Invalid arguments
        lseek_main-->>User: Error message
    end
    lseek_main-->>User: Return result
Loading

File-Level Changes

Change Details Files
Added support for additional seek types through a new optional parameter
  • Updated function signature and usage documentation to include optional SEEK_TYPE parameter
  • Modified argument validation to accept either 2 or 3 arguments
  • Added conditional logic to handle different seek types (SEEK_SET, SEEK_END, SEEK_CUR)
  • Maintained backward compatibility by defaulting to SEEK_CUR when seek type is not specified or invalid
lseek_builtin/lseek.c
Enhanced documentation to explain new seek type functionality
  • Added documentation for the new SEEK_TYPE parameter
  • Listed available seek type options (SEEK_SET, SEEK_CUR, SEEK_END)
  • Updated usage string to show optional parameter
lseek_builtin/lseek.c

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @jkool702 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider extracting the repeated lseek calls and error handling into a helper function to reduce code duplication and improve maintainability
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

fprintf(stderr, "\nIncorrect number of arguments.\nUSAGE: lseek <FD> <REL_OFFSET>\n");
// check for exactly 2 or 3 args passed to lseek
if (argc != 3 && argc != 4) {
fprintf(stderr, "\nIncorrect number of arguments.\nUSAGE: lseek <FD> <REL_OFFSET> [<SEEK_TYPE>]\n");
return 1;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider consolidating the duplicate lseek error handling code

You could move the whence parameter selection before the lseek call and have a single error handling block. This would reduce code duplication and make maintenance easier.

int whence;
if (argc == 4) {
    if (strcmp(argv[3], "SEEK_SET") == 0) {
        whence = SEEK_SET;
    } else if (strcmp(argv[3], "SEEK_END") == 0) {
        whence = SEEK_END;
    }
}
if (lseek(fd, offset, whence) == (off_t) -1) {
    fprintf(stderr, "\nERROR: %s\n", strerror(errno));
    return 1;
}

@@ -65,18 +69,30 @@
return 1;
}

// get + validate (relative) offset
// get + validate (relative) offset
errno = 0;
off_t offset = atoll(argv[2]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Add validation for non-numeric offset input

atoll() returns 0 for invalid input without setting errno. Consider using strtoll() instead, which allows for better error detection.

Suggested change
off_t offset = atoll(argv[2]);
char *endptr;
off_t offset = strtoll(argv[2], &endptr, 10);
if (*endptr != '\0') {
fprintf(stderr, "Invalid offset: not a number\n");
return 1;
}

if (lseek(fd, offset, SEEK_CUR) == (off_t) -1) {
fprintf(stderr, "\nERROR: %s\n", strerror(errno));
return 1;
// get SEEK_TYPE and call lseek to move fd byte offset
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider consolidating the seek type handling and error checking into a single code path.

The error handling code is duplicated three times unnecessarily. Here's how to simplify while maintaining all functionality:

// get SEEK_TYPE and call lseek to move fd byte offset
int whence = SEEK_CUR;  // default
if (argc == 4) {
    if (strcmp(argv[3], "SEEK_SET") == 0) {
        whence = SEEK_SET;
    } else if (strcmp(argv[3], "SEEK_END") == 0) {
        whence = SEEK_END;
    }
}

if (lseek(fd, offset, whence) == (off_t) -1) {
    fprintf(stderr, "\nERROR: %s\n", strerror(errno));
    return 1;
}

This maintains identical behavior while:

  • Reducing code duplication
  • Making error handling consistent
  • Improving maintainability for future seek types

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant