From patchwork Sun Sep 20 21:40:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alejandro Colomar X-Patchwork-Id: 40463 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 50C39394504F; Sun, 20 Sep 2020 21:40:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 50C39394504F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1600638049; bh=7QHw6uvDkflovYAhoDhInFEpLNAfLYvvjLltYpz/6Lw=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=QJtuQEQYF6fKg+mkDKnqz+PWStkr8J1l55iwyG70/X+8C3I33jrjybA5k8lJyeHVI qcrtg945CyMJQ1xd+xUn74QXFgAsgDMKKaf2ERLL3WxNyNMFwj4qC1l+DgyTCtcOq1 gA2utW5MtUgv6fHGTPzPZQ7QVA27fIEYY11bWvk4= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) by sourceware.org (Postfix) with ESMTPS id 5CCC73857C61 for ; Sun, 20 Sep 2020 21:40:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 5CCC73857C61 Received: by mail-wr1-x442.google.com with SMTP id e16so10760660wrm.2 for ; Sun, 20 Sep 2020 14:40:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=7QHw6uvDkflovYAhoDhInFEpLNAfLYvvjLltYpz/6Lw=; b=uWEazNHpLXRMdvWvflUYRpA7MYAcV7EpWNvZyEfc7FTJofA4K0/8S0FA1KRwAZkKRV D/+5It4VYtFL8NBsv2z+v1RyotYr1IvLRp3JpeeQUt6lSY28RUX5cdslruqTGU/vGIQ9 wYWGMe7DQypbrtmSJa7r31ETRr9Vw71qA7VXK19fcH3e9gPWk0VXsj5toKQt0xxdrxlD Rc1SrYqApWBXn2vQujRIsQaRSPB4nmBcuDw/+borQuI0+GJYeXxHrK9lMKwksab3vLcw ccyXbLEl1B/J4FM0GaLRLpd8BCbpZsTCJ2x23UUcFAstz6FN7NjjlIMKqQfCKe6R2DqE JaCA== X-Gm-Message-State: AOAM530XGEABhDrcRT+EPbIQrbHe/W5JUV7CyXs5jwP1hgO+IQlr9cin QQVNObi4qhs31R17ZYYhttY= X-Google-Smtp-Source: ABdhPJxmHXRrhiYG1Wj2el2jNvWzqSdfsJVtdkbuWilEK5ygqn0udfFoa0oPifkD1MTb/XDnC8jXQg== X-Received: by 2002:adf:f5c7:: with SMTP id k7mr50737842wrp.246.1600638046504; Sun, 20 Sep 2020 14:40:46 -0700 (PDT) Received: from localhost.localdomain ([93.115.133.118]) by smtp.googlemail.com with ESMTPSA id v9sm17767994wrv.35.2020.09.20.14.40.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Sep 2020 14:40:45 -0700 (PDT) To: mtk.manpages@gmail.com Subject: [PATCH] system_data_types.7: Add note about length modifiers and conversions to [u]intmax_t, and corresponding example Date: Sun, 20 Sep 2020 23:40:13 +0200 Message-Id: <20200920214012.454410-1-colomar.6.4.3@gmail.com> X-Mailer: git-send-email 2.28.0 MIME-Version: 1.0 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Alejandro Colomar via Libc-alpha From: Alejandro Colomar Reply-To: Alejandro Colomar Cc: fweimer@redhat.com, linux-man@vger.kernel.org, libc-alpha@sourceware.org, Alejandro Colomar Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Reported-by: Michael Kerrisk Signed-off-by: Alejandro Colomar --- Hi Michael, On 9/20/20 10:20 PM, Michael Kerrisk (man-pages) wrote: > PS It occurs to me that this manual page is a suitable place > to explain the general technique of casting integral system > data types to [u]intmax_t for the purpose of printf(). Would > you like to add this, Alex? Sure. Good idea! Hope you like the patch :) Cheers, Alex man7/system_data_types.7 | 52 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/man7/system_data_types.7 b/man7/system_data_types.7 index 5128e1f01..1fcc09dae 100644 --- a/man7/system_data_types.7 +++ b/man7/system_data_types.7 @@ -626,6 +626,58 @@ See also: .SH NOTES The structures described in this manual page shall contain, at least, the members shown in their definition, in no particular order. +.PP +Most of the types described in this page don't have a corresponding +length modifier for the +.BR printf (3) +and the +.BR scanf (3) +families of functions. +To print a value of a type that doesn't have a length modifier, +it should be converted to +.I intmax_t +or +.I uintmax_t +by an explicit cast. +To scan into a variable of a type that doesn't have a length modifier, +an intermediate temporary variable of type +.I intmax_t +or +.I uintmax_t +should be used. +The example below shows how these conversions should be done. +.SH EXAMPLES +The program shown below scans from a string and prints a value stored in +a variable of a type that doesn't have a length modifier. +The appropriate conversions from and to +.I intmax_t +are used as explained in the notes section above: +.PP +.EX +#include +#include +#include +#include + +int +main (void) +{ + static const char *const str = "There are 60 s in an hour"; + time_t secs; + intmax_t tmp; + + /* Scan the number from the string into the temporary variable */ + sscanf(str, "There are %jd", &tmp); + + /* Copy the value to the time_t variable secs */ + secs = tmp; + + /* Print the value */ + printf("There are %jd seconds in an hour!\en", (intmax_t) secs); + + exit(EXIT_SUCCESS); +} +.EE .SH SEE ALSO .BR feature_test_macros (7), .BR standards (7)