Skip to contentSkip to conversation

Improve strtol(3) example

PATCH In review Alejandro Colomar 2 participants 5 messages Oct 28, 2020 – Oct 29, 2020 Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore
#0 lore raw

cover Improve strtol(3) example

Hi Michael,

Here are a few improvements for the strtol(3) example.

Cheers,

Alex

Alejandro Colomar (3):
strtol.3: EXAMPLES: Delimit output string using "" strtol.3: EXAMPLES: As the default base, use special value 0 strtol.3: EXAMPLES: Simplify errno checking (no expected change in
behavior)

 man3/strtol.3 | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
Signature · 1 line
2.28.0
#1 lore raw

PATCH 1/3 strtol.3: EXAMPLES: Delimit output string using ""

---
 man3/strtol.3 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
man3/strtol.3 +2 -2
index 20b453330..0feb12dbb 100644
@@ -236,7 +236,7 @@ strtol() returned 123
 strtol() returned 123
 .RB "$" " ./a.out 123abc"
 strtol() returned 123
-Further characters after number: abc
+Further characters after number: "abc"
 .RB "$" " ./a.out 123abc 55"
 strtol: Invalid argument
 .RB "$" " ./a.out \(aq\(aq"
@@ -289,7 +289,7 @@ main(int argc, char *argv[])
     printf("strtol() returned %ld\en", val);
 
     if (*endptr != \(aq\e0\(aq)        /* Not necessarily an error... */
-        printf("Further characters after number: %s\en", endptr);
+        printf("Further characters after number: \e"%s\e"\en", endptr);
 
     exit(EXIT_SUCCESS);
 }
Signature · 1 line
2.28.0
#2 lore raw

PATCH 2/3 strtol.3: EXAMPLES: As the default base, use special value 0

In reply to Alejandro Colomar
---
 man3/strtol.3 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
man3/strtol.3 +1 -1
index 0feb12dbb..35252295e 100644
@@ -266,7 +266,7 @@ main(int argc, char *argv[])
     }
 
     str = argv[1];
-    base = (argc > 2) ? atoi(argv[2]) : 10;
+    base = (argc > 2) ? atoi(argv[2]) : 0;
 
     errno = 0;    /* To distinguish success/failure after call */
     val = strtol(str, &endptr, base);
Signature · 1 line
2.28.0
#3 lore raw
In reply to Alejandro Colomar
Quoted 3 lines · Alejandro Colomar

Hi Michael,

Here are a few improvements for the strtol(3) example.

Thanks, Alex. I've applied all of these.

Cheers,

Michael

Quoted 13 lines

Cheers,

Alex

Alejandro Colomar (3):
strtol.3: EXAMPLES: Delimit output string using "" strtol.3: EXAMPLES: As the default base, use special value 0 strtol.3: EXAMPLES: Simplify errno checking (no expected change in
behavior)

 man3/strtol.3 | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
#4 lore raw

PATCH 3/3 strtol.3: EXAMPLES: Simplify errno checking (no expected change in behavior)

In reply to Alejandro Colomar
---
 man3/strtol.3 | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
man3/strtol.3 +1 -2
index 35252295e..6b328fefe 100644
@@ -273,8 +273,7 @@ main(int argc, char *argv[])
 
     /* Check for various possible errors */
 
-    if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
-            || (errno != 0 && val == 0)) {
+    if (errno != 0) {
         perror("strtol");
         exit(EXIT_FAILURE);
     }
Signature · 1 line
2.28.0