473,806 Members | 2,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using write() instead of puts() in a trivial program?

Greetings,

I hope my greenness isn't showing too bad by asking this, but I ran across
this trivial program today that left me flabbergasted:

#define MESSAGE "This account is currently not available.\n"

int
main(int argc, char *argv[])
{
write(STDOUT_FI LENO, MESSAGE, sizeof(MESSAGE) );
_exit(1);
}

This is an excerpt from the newly-rewritten nologin(8) in FreeBSD. I am
not willing to accept that the person who committed this code is a fool,
thus I have to conclude that I just don't understand why exactly the
following isn't good enough for such a trivial program:

puts("This account is currently not available.");

Perhaps write() would be appropriate in a program which did intensive
amounts of output, but the meat of this program is literally 1 line long.

I tried doing a Google search, but as you can imagine, the commonality of
the "write" keyword throws off any results that might be useful, so I was
wondering some kind, generous person here could clue me in on what's going
on here. I'd greatly appreciate it.

Charles Ulrich
Nov 14 '05
24 5874
Keith Thompson wrote:

(snip)
Presumably the FreeBSD nologin program doesn't need to be portable to
anything other than FreeBSD. Given that constraint, I suppose there's
not much advantage in using the portable puts() rather than the
relatively non-portable write(). Having said that, it's not how I would have written it. puts() or
printf() would make for more legible code.

In the days of dynamic link libraries, it is important that some
programs be able to run without dynamic linking. Now, it could
use the static library, but it will be a lot smaller not to.

-- glen

Nov 14 '05 #11
In <pa************ *************** *@BLOODYsecuren ym.SPAMnet> Charles Ulrich <NO***@BLOODYse curenym.SPAMnet > writes:
I hope my greenness isn't showing too bad by asking this, but I ran across
this trivial program today that left me flabbergasted:

#define MESSAGE "This account is currently not available.\n"

int
main(int argc, char *argv[])
{
write(STDOUT_FI LENO, MESSAGE, sizeof(MESSAGE) );
_exit(1);
}

This is an excerpt from the newly-rewritten nologin(8) in FreeBSD. I am
not willing to accept that the person who committed this code is a fool,
thus I have to conclude that I just don't understand why exactly the
following isn't good enough for such a trivial program:

puts("This account is currently not available.");

Perhaps write() would be appropriate in a program which did intensive
amounts of output, but the meat of this program is literally 1 line long.


write() is also appropriate for small programs that are supposed to be
statically linked, while puts() is likely to draw a lot of baggage from
the stdio library. The latter also burns more CPU cycles: in such a
program it is nothing more than a fairly expensive wrapper for a write()
call. The usage of _exit() instead of exit() also indicates a concern
for minimising the number of CPU cycles used by the program, which
explains why the application was not written as a shell script:

#!/bin/sh
echo "This account is currently not available."
exit 1

So, your answer is: the author wanted a standalone application with zero
overhead in terms of disk space and cpu cycles. It's hard to tell whether
his concerns were justified or not, considering that nologin is not even
a standard Unix utility.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #12
On 12 Jan 2004 23:09:24 GMT, Peteris Krumins
<pk************ ****@inbox.lv> wrote:
I tried doing a Google search, but as you can imagine, the commonality
of the "write" keyword throws off any results that might be useful, so
I was wondering some kind, generous person here could clue me in on
what's going on here. I'd greatly appreciate it.

Charles Ulrich


Finding an answer to such silly problem is the same as asking and trying
to find an answer to a problem like "I have to go to the shop which is
across a street, I chose to go by bike. Maybe I should have chosen to
go to the shop by my shiny car, my shiny helicopter, my elephant?".


Not exactly. In this case, it's "I notice that you took the bike to
the shop rather than the elephant. Was there a reason for that?"

It's a legitimate question, and can provide valuable insight - some of
the best advice a novice can get is "read other people's code." Asking
why a presumably experienced programmer chose one method over another
can be informative and educational. I even consider it topical -
discussing the pros and cons of using standard C rather than the
non-standard write function.

In this case, the best answer is "ask the programmer." Many
open-source programmers are happy to discuss their code. If the OP
wants guesses, my guess would be that the executable using "write" is
smaller with the proper linker options.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 14 '05 #13
Alan Balmer wrote:
It's a legitimate question, and can provide valuable insight - some of
the best advice a novice can get is "read other people's code." Asking
why a presumably experienced programmer chose one method over another
can be informative and educational. I even consider it topical -
discussing the pros and cons of using standard C rather than the
non-standard write function.

I'm on the opposite side of that fence, I don't think reading other
people's code is good for newbies. They don't have the experience base
to filter what they are reading. They are far better off sticking with
texts, standards and their own projects until such time as their skills
develop.

Reading code, to me, doesn't really improve programming skills. It
improves the skill of reading code, which is in itself a useful and
important skill if you are going to work in the industry.


Brian Rodenborn
Nov 14 '05 #14
On Tue, 13 Jan 2004 17:10:47 GMT, Default User
<fi********@boe ing.com.invalid > wrote:
Alan Balmer wrote:
It's a legitimate question, and can provide valuable insight - some of
the best advice a novice can get is "read other people's code." Asking
why a presumably experienced programmer chose one method over another
can be informative and educational. I even consider it topical -
discussing the pros and cons of using standard C rather than the
non-standard write function.

I'm on the opposite side of that fence, I don't think reading other
people's code is good for newbies. They don't have the experience base
to filter what they are reading.


Which is why they come here to ask ;-)
They are far better off sticking with
texts,
Texts are not necessarily a source of good programming practices. Many
of the popular ones misinform.
standards
I may be wrong, but I don't see the standard as being useful for new
programmers. Once "how" turns into "why" and "why not", the standard
is good, but that requires some experience.
and their own projects until such time as their skills
develop.
That's probably the best, *providing* they get proper feedback.
Otherwise, just developing their own projects can do more harm than
good.
Reading code, to me, doesn't really improve programming skills. It
improves the skill of reading code, which is in itself a useful and
important skill if you are going to work in the industry.
If I don't know how to do something, and I read someone else's code
which does it, it improves my programming skills.

I think reading *good* code is educational. Maybe we need an
"approved" body of reading material <g>. It may well be that random
bits of BSD source code don't qualify.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 14 '05 #15
In <40************ ***@boeing.com. invalid> Default User <fi********@boe ing.com.invalid > writes:
Reading code, to me, doesn't really improve programming skills. It
improves the skill of reading code, which is in itself a useful and
important skill if you are going to work in the industry.


Reading well written code does improve programming skills, because you
get to learn better ways of doing certain things.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #16
Alan Balmer wrote:
On 12 Jan 2004 23:09:24 GMT, Peteris Krumins
<pk************ ****@inbox.lv> wrote:
Finding an answer to such silly problem is the same as asking and trying
to find an answer to a problem like "I have to go to the shop which is
across a street, I chose to go by bike. Maybe I should have chosen to
go to the shop by my shiny car, my shiny helicopter, my elephant?".


Not exactly. In this case, it's "I notice that you took the bike to
the shop rather than the elephant. Was there a reason for that?"


Don't be silly. Why would anyone take a bike to an elephant?

<g,d&r>

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #17
Dan Pop wrote:

In <40************ ***@boeing.com. invalid> Default User <fi********@boe ing.com.invalid > writes:
Reading code, to me, doesn't really improve programming skills. It
improves the skill of reading code, which is in itself a useful and
important skill if you are going to work in the industry.


Reading well written code does improve programming skills, because you
get to learn better ways of doing certain things.

But that usually requires a sufficient skill level to be able to take
advantage of the new knowledge. Newbies can't tell good code from bad,
good practices from crap, nifty new techniques from hacky, unsafe
shortcuts. It also encourages cut-n-paste cargo cultism.


Brian Rodenborn
Nov 14 '05 #18
Richard Heathfield wrote:
Alan Balmer wrote:
<pk************ ****@inbox.lv> wrote:
Finding an answer to such silly problem is the same as asking
and trying to find an answer to a problem like "I have to go
to the shop which is across a street, I chose to go by bike.
Maybe I should have chosen to go to the shop by my shiny car,
my shiny helicopter, my elephant?".


Not exactly. In this case, it's "I notice that you took the
bike to the shop rather than the elephant. Was there a reason
for that?"


Don't be silly. Why would anyone take a bike to an elephant?


So the elephant could learn to ride it, and make than 'anyone' a
pot of money in the circus.

<d & r II>

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #19
In <40************ ***@boeing.com. invalid> Default User <fi********@boe ing.com.invalid > writes:
Dan Pop wrote:

In <40************ ***@boeing.com. invalid> Default User <fi********@boe ing.com.invalid > writes:
>Reading code, to me, doesn't really improve programming skills. It ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^ >improves the skill of reading code, which is in itself a useful and
>important skill if you are going to work in the industry.
Reading well written code does improve programming skills, because you ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^ get to learn better ways of doing certain things.


But that usually requires a sufficient skill level to be able to take
advantage of the new knowledge.


We're talking about *improving* programming skills, not about *acquiring*
them, right?
Newbies can't tell good code from bad,
good practices from crap, nifty new techniques from hacky, unsafe
shortcuts.
Newbies can ask advanced programmers for pieces of good C code, that
can be used for this purpose. Few people are learning C alone on a
desert island...
It also encourages cut-n-paste cargo cultism.


Not in my experience. The main purpose of the exercise is to get ideas
about how to code in C, rather than finding solutions to a concrete
problem. For the latter, people usually go to algorithms books with
examples in C.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
55606
by: Pontus F | last post by:
Hi I am learning C++ and I'm still trying to get a grip of pointers and other C/C++ concepts. I would appreciate if somebody could explain what's wrong with this code: ---begin code block--- #include "stdio.h" #include "string.h" void printText(char c){
8
9862
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out | ios::binary) to declare a file object with read/write modes turned on for working with binary data. I've tried this and my file is not created. The only time it is created is when I specify ifstream or ofstream but not fstream. I've tried removing the...
7
6160
by: Yandos | last post by:
Hello all, I have maybe a trivial question, but I cannot think out what is wrong :( How do i detect EOF correctly when i read from stdin? Am I doing it wrong? <pipetest.c> #include <stdio.h> int main(void) { char ch;
121
10200
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
42
9905
by: Prashanth Badabagni | last post by:
Hi, Can any body tell me how to print "hello,world" with out using semicolon Thanks in advance .. Bye Prashanth Badabagni
6
3505
by: hpy_awad | last post by:
I am writing stings ((*cust).name),((*cust).address)to a file using fgets but rabish is being wrote to that file ? Look to my source please and help me finding the reason why this rabish is being written. /* Book name : File name : E:\programs\cpp\iti01\ch10\ex09_5p1.cpp Program discription: Adding name,Address to customer_record SETUP PROGRAM
1
4811
by: noleander | last post by:
Hi. I've got a C++ program written in Visual C++ 2003. The program is trivial, created with the Program-creation wizard: used the .NET "Form" template. The program has a trivial single-pane form GUI. I've got some stdout print statements in the code ... but I cannot find where in the world the output text is appearing. For printing I tried both: printf ("Hello world\n"); and Console::Write ("Hello World\n");
27
3246
by: lovecreatesbea... | last post by:
This code snippet is an exercise on allocating two dimension array dynamically. Though this one is trivial, is it a correct one? Furthermore, when I tried to make these changes to the original example: for (i = 0; i < ROW + 2; ++i){ /*line 14*/ strcpy(array, "C! C!"); /*line 15*/ Apparently, the modified code wrote to the memory unit outside the allocated array, but the program ran well and there was no Segmentation
23
3008
by: asit dhal | last post by:
hello friends, can anyone explain me how to use read() write() function in C. and also how to read a file from disk and show it on the monitor using onlu read(), write() function ??????
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10620
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10369
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10110
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3851
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.