From 89c6c2bdd2eac97dfca413d55a54cc8e9baa2165 Mon Sep 17 00:00:00 2001 From: Michael Kerrisk Date: Mon, 21 Sep 2020 16:08:18 +0200 Subject: [PATCH] system_data_types.7: Minor tweaks to Alejandro Colomar's patch Signed-off-by: Michael Kerrisk --- man7/system_data_types.7 | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/man7/system_data_types.7 b/man7/system_data_types.7 index da57deffa..d050c437c 100644 --- a/man7/system_data_types.7 +++ b/man7/system_data_types.7 @@ -660,8 +660,8 @@ The program shown below scans from a string and prints a value stored in a variable of an integer type that doesn't have a length modifier. The appropriate conversions from and to .IR intmax_t , -and the appropriate range checkings, -are used as explained in the notes section above: +and the appropriate range checks, +are used as explained in the notes section above. .PP .EX #include @@ -669,7 +669,6 @@ are used as explained in the notes section above: #include #include - int main (void) { @@ -678,26 +677,32 @@ main (void) intmax_t tmp; /* Scan the number from the string into the temporary variable */ + sscanf(str, "%jd", &tmp); /* Check that the value is within the valid range of suseconds_t */ - if (tmp < -1 || tmp > 1000000) { - fprintf(stderr, "Scaned value might overflow!\en"); + + if (tmp < \-1 || tmp > 1000000) { + fprintf(stderr, "Scanned value outside valid range!\en"); exit(EXIT_FAILURE); } - /* Copy the value to the suseconds_t variable 'us' */ + /* Copy the value to the suseconds_t variable \(aqus\(aq */ + us = tmp; - /* Even though suseconds_t can hold the value -1, - it represents an error code */ + /* Even though suseconds_t can hold the value \-1, this isn\(aqt + a sensible number of microseconds */ + if (us < 0) { - fprintf(stderr, "Scanned an error code!\en"); + fprintf(stderr, "Scanned value shouldn\(aqt be negative!\en"); exit(EXIT_FAILURE); } /* Print the value */ - printf("There are %jd us in half a second.\en", (intmax_t) us); + + printf("There are %jd microseconds in half a second.\en", + (intmax_t) us); exit(EXIT_SUCCESS); }