Skip to content

Quick : move code for wingetopt into opensea-common.#33

Open
sharma-nidhi wants to merge 5 commits into
developfrom
feature/remove_wingetopt
Open

Quick : move code for wingetopt into opensea-common.#33
sharma-nidhi wants to merge 5 commits into
developfrom
feature/remove_wingetopt

Conversation

@sharma-nidhi

Copy link
Copy Markdown
Contributor

Move getopt implementation from wingetopt into opensea-common; remove wingetopt dependency

Summary

This PR consolidates the getopt/getopt_long/getopt_long_only implementation from an external wingetopt dependency directly into opensea-common. The migration eliminates external dependencies while hardening the codebase with comprehensive compiler attributes (nullability, access patterns, printf-style checking) and security-focused I/O functions.

Motivation

Previously, command-line parsing relied on a separate wingetopt library. By bringing the implementation in-tree:

  • Reduces build complexity and external dependency management
  • Enables consistent application of opensea-common's security and code quality standards
  • Standardizes error handling and reporting across the library
  • Improves platform portability with conditional use of C11 Annex K and MSVC secure functions

Changes

Command-line Utilities

include/cmdline_utils.h | src/cmdline_utils.c

Full implementation of POSIX getopt API (derived from OpenBSD/NetBSD sources):

  • Public interfaces (getopt, getopt_long, getopt_long_only) fully annotated with:

    • Return-value checking (M_NODISCARD)
    • Null-termination requirements (M_NULL_TERM_STRING)
    • Parameter access patterns (M_PARAM_RO, M_PARAM_RW, M_PARAM_WO)
  • Parameter semantics correctly distinguished:

    • nargv read-only in getopt (no permutation)
    • nargv read-write in getopt_long/getopt_long_only (may permute argv)
  • Internal helper functions (getopt_internal, parse_long_options, permute_args):

    • Marked for compiler optimization (M_CONST_FUNC, M_PURE_FUNC)
    • Parameter lists annotated for internal-use optimization (M_NONNULL_PARAM_LIST)
  • Platform-aware environment variable access:

    • Uses getenv_s on MSVC/Windows
    • Falls back to secure_getenv or __secure_getenv on Unix-like systems
    • Standard getenv on unsupported platforms

Warning and Error Handling

include/warning_utils.h (new file)

Portable command-line error reporting:

  • Enum-based message selection (eCMDLineErrorMessage) avoids compiler warnings about non-const format strings
  • Replaces platform-specific warnx/vwarnx from <err.h> with portable alternatives
  • Dual I/O paths:
    • MSFT/C11 Annex K secure functions (fprintf_s, vfprintf_s) when available
    • Standard POSIX I/O functions as fallback

Security Improvements

  • Consistent use of secure I/O functions (fprintf_s/vfprintf_s) on MSVC and modern C11-compliant systems
  • Platform-aware secure environment variable access (secure_getenv / __secure_getenv) where supported
  • Full parameter annotation coverage: null-termination checks, mutability declarations, non-null conditions
  • Return-value checking enforcement via M_NODISCARD on all public API entry points
  • Comprehensive attribute consistency between function declarations and implementations

Testing

  • All getopt functionality previously validated in wingetopt remains unchanged
  • Attribute decorators enable enhanced static analysis and compiler warnings
  • Platform-specific code paths covered by existing test suite

@sharma-nidhi
sharma-nidhi requested a review from vonericsen July 8, 2026 10:26
Comment thread include/cmdline_utils.h Outdated
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef __GETOPT_H__

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should change this header ifdef block to #pragma once that we use in other files

Comment thread include/cmdline_utils.h Outdated
* warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

#define __GETOPT_H__ // NOLINT

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

After changing to #pragma once we can delete this line

Comment thread include/cmdline_utils.h Outdated
}
#endif

#endif /* !defined(__UNISTD_H_SOURCED__) && !defined(__GETOPT_LONG_H__) */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

After changing to #pragma once I think we can delete this line

Signed-off-by: Nidhi Sharma <nidhi.sharma@seagate.com>
Comment thread include/cmdline_utils.h Outdated
@@ -0,0 +1,124 @@
/* SPDX-License-Identifier: BSD-2-Clause */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

put and MPL-2.0 on this line

Comment thread include/program_name_utils.h Outdated
@@ -0,0 +1,141 @@
// SPDX-License-Identifier: MPL-2.0

//! \file wingetopt_utils.h

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

change to program_name_utils.h

Comment thread include/warning_utils.h Outdated
@@ -0,0 +1,153 @@
// SPDX-License-Identifier: MPL-2.0

//! \file winCMDLINE_utils.h

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

change to warning_utils.h

Comment thread src/getopt.c
@@ -0,0 +1,673 @@
/* SPDX-License-Identifier: 0BSD AND BSD-2-Clause */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

add AND MPL-2.0 to this line

… banner.

Signed-off-by: Nidhi Sharma <nidhi.sharma@seagate.com>
Signed-off-by: Nidhi Sharma <nidhi.sharma@seagate.com>
… is not available on windows by-default.

Signed-off-by: Nidhi Sharma <nidhi.sharma@seagate.com>
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.

2 participants