From owner-freebsd-questions@freebsd.org Thu May 14 20:18:15 2020 Return-Path: Delivered-To: freebsd-questions@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 533D12DDD57 for ; Thu, 14 May 2020 20:18:15 +0000 (UTC) (envelope-from ypankov@fastmail.com) Received: from out5-smtp.messagingengine.com (out5-smtp.messagingengine.com [66.111.4.29]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 49NNCB2ddhz3Msm for ; Thu, 14 May 2020 20:18:14 +0000 (UTC) (envelope-from ypankov@fastmail.com) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 78DB85C00CB; Thu, 14 May 2020 16:18:13 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Thu, 14 May 2020 16:18:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fastmail.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=C yl6H7ntJxjePrqKqmzs6lQDNp+lUKR6ay4/LI0+8SM=; b=Kf9LnWPRuOyWWvuNa JLeFqF0fAo1SBhc1f/yNDzc2a8iKaSHz0B0dMLa3HKXuRRBH0YKc3M+vMGseb9Dt bjEQoXKOuWeWwHG2kooGnA5+7JszIHVYqr6VZRwHuW3cnU//f1tgRY45qnwb8aOj HNLwhOdojXNTow1ubKJPhvtKrWboc273d67kD6uFg8nqszzcFlxYwHlDA8XzYt6Q 1Uog0aC0gphfp3Aotp3I5tdm6TZ+cfCXRe5B/ux+aZdJVPM66EktYtYlockvuh8N MYJ7q6UT/on6Tl418QS6rwdaAuAJlF7Cs9F/NEeD2zuAtWSK4OErPFqeLgzY5LHC CjViA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=Cyl6H7ntJxjePrqKqmzs6lQDNp+lUKR6ay4/LI0+8 SM=; b=kLtH1UEPugE5cltyPl67jHuzl4KlbBVACpEnh8y9eqFGvgKpq8ACqsBES Gqbs0tefJvvy9rzV5ZI6dvAvM+suFfRBVxIOGyHr4buIeo9rxBVWWWfboBgHovnJ emlbfrD9ybqgQfpAuC5wTuJPs8Spf3+6tvSq7uOAopNWdTWHVOWIim4Y9A5Zic5k R89y/OZ810GOpf2BO+TS/7Kg1I5osfLRM+MFUfA7VsFmdPMral0zm/sCcCytuW2/ EY9cggxaxHc8e8uLVH0CypN28Dmv0ikyAjQGCOX8Zxkb/eexbkOSrdCtRLA94ovt vDkzED1ktp7S1eF/c/kMZTk/X6jpA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduhedrleeigddufeelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthejredttdefjeenucfhrhhomhepjghurhhi ucfrrghnkhhovhcuoeihphgrnhhkohhvsehfrghsthhmrghilhdrtghomheqnecuggftrf grthhtvghrnhepfffgveehgfffveeutedvteetgfetteelvefgjeefhffgvdejuedttddt jeefffetnecuffhomhgrihhnpehfrhgvvggsshgurdhorhhgnecukfhppedujeekrdefge druddtfedrudehudenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhl fhhrohhmpeihphgrnhhkohhvsehfrghsthhmrghilhdrtghomh X-ME-Proxy: Received: from [192.168.1.6] (unknown [178.34.103.151]) by mail.messagingengine.com (Postfix) with ESMTPA id 877293061203; Thu, 14 May 2020 16:18:11 -0400 (EDT) Subject: Re: Multi-line text output via printf() et al. To: Polytropon , FreeBSD Questions References: <20200514220904.7a4c1e28.freebsd@edvax.de> From: Yuri Pankov Message-ID: <0d78f2bb-4169-c1ba-48c0-3f9c6993c961@fastmail.com> Date: Thu, 14 May 2020 23:18:10 +0300 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Thunderbird/68.8.0 MIME-Version: 1.0 In-Reply-To: <20200514220904.7a4c1e28.freebsd@edvax.de> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 49NNCB2ddhz3Msm X-Spamd-Bar: --- Authentication-Results: mx1.freebsd.org; dkim=pass header.d=fastmail.com header.s=fm2 header.b=Kf9LnWPR; dkim=pass header.d=messagingengine.com header.s=fm2 header.b=kLtH1UEP; dmarc=pass (policy=none) header.from=fastmail.com; spf=pass (mx1.freebsd.org: domain of ypankov@fastmail.com designates 66.111.4.29 as permitted sender) smtp.mailfrom=ypankov@fastmail.com X-Spamd-Result: default: False [-3.10 / 15.00]; RCVD_VIA_SMTP_AUTH(0.00)[]; RWL_MAILSPIKE_GOOD(0.00)[29.4.111.66.rep.mailspike.net : 127.0.0.18]; R_SPF_ALLOW(-0.20)[+ip4:66.111.4.29]; FREEMAIL_FROM(0.00)[fastmail.com]; RCVD_COUNT_THREE(0.00)[4]; TO_DN_ALL(0.00)[]; DKIM_TRACE(0.00)[fastmail.com:+,messagingengine.com:+]; RCPT_COUNT_TWO(0.00)[2]; DMARC_POLICY_ALLOW(-0.50)[fastmail.com,none]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; IP_SCORE(0.00)[ip: (-9.85), ipnet: 66.111.4.0/24(-4.89), asn: 11403(-2.69), country: US(-0.05)]; RCVD_IN_DNSWL_LOW(-0.10)[29.4.111.66.list.dnswl.org : 127.0.5.1]; ASN(0.00)[asn:11403, ipnet:66.111.4.0/24, country:US]; MID_RHS_MATCH_FROM(0.00)[]; RECEIVED_SPAMHAUS_PBL(0.00)[151.103.34.178.khpj7ygk5idzvmvt5x4ziurxhy.zen.dq.spamhaus.net : 127.0.0.11]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000,0]; R_DKIM_ALLOW(-0.20)[fastmail.com:s=fm2,messagingengine.com:s=fm2]; FROM_HAS_DN(0.00)[]; FREEMAIL_ENVFROM(0.00)[fastmail.com]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; MIME_GOOD(-0.10)[text/plain]; RCVD_TLS_LAST(0.00)[]; IP_SCORE_FREEMAIL(0.00)[]; TO_MATCH_ENVRCPT_SOME(0.00)[] X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 14 May 2020 20:18:15 -0000 Polytropon wrote: > In the FreeBSD sources, there are several ways shown about > how to emit several lines of text to the console or to a > file. Allow me to phrase the question in C, even though > it will be a valid question in almost every other programming > language (compiled or interpreted): > > What is the _proper_ way of printing multiple lines? > > My research basically did bring up the following methods: > > a) multiple function calls: > > printf("This is the first line of text.\n"); > printf("And this is the second line.\n"); > printf("Finally a third line.\n"); > > b) one string in multiple parts in one function call: > > printf("This is the first line of text.\n" > "And this is the second line.\n" > "Finally a third line.\n"); > > c) multiple lines in one string: > > printf("This is the first line of text.\n > And this is the second line.\n > Finally a third line.\n"); > > There is a specific restriction that if a string contains > conversion specifications for variables, those have to be > in the 1st argument, so printf() does not print several > strings as individual arguments except there's a first > one containing appropriate %s entries. This applies for > any string containing %: it has to be in the > first argument passed to printf(). > > d) multiple strings with format string: > > printf("%s%s%s", > "This is the first line of text.\n", > "And this is the second line.\n", > "Finally a third line.\n"); > > In this case, the \n could be in the format string instead > of the text lines. > > So, what's the correct (or at least recommended way) of > doing this? Think about use cases like fprintf(stderr, ...) > for things like usage messages, or printing to a file for > multi-line entries. b) and d) are what I see the most in the code, b) mostly in illumos (solaris) codebase and d) in FreeBSD codebase. I just checked how some utilities print their usage messages (as that's where you'd expect multiline strings): https://svnweb.freebsd.org/base/head/bin/cp/utils.c?revision=326025&view=markup#l550 https://svnweb.freebsd.org/base/head/bin/mv/mv.c?revision=326025&view=markup#l506 https://svnweb.freebsd.org/base/head/bin/ln/ln.c?revision=326025&view=markup#l352 I've also seen a); c) looks completely weird to me.