473,806 Members | 2,929 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
Em Mon, 12 Jan 2004 21:10:59 +0000, Alexander Bartolich escreveu:
<snip>
write(2) is a system call.
_exit(2) is a system call.
By using nothing but system calls the total size of the resulting
binary can be minimized. See option -nostdlib of gcc.


That's it. /sbin/nologin isn't linked to any library, not even libc. The
result?

$ ls -lh /sbin/nologin
-r-xr-xr-x 1 root wheel 1,9K 4 Jun 2003 /sbin/nologin

FreeBSD 5.1 i386. A hand-tweaked assembly program could hardly be smaller.
Well, it could, but that's another topic.

--
Emacs is a great operating system---it lacks a good editor, though.

Nov 14 '05 #21
José de Paula wrote:
That's it. /sbin/nologin isn't linked to any library, not even libc.
The result?

$ ls -lh /sbin/nologin
-r-xr-xr-x 1 root wheel 1,9K 4 Jun 2003 /sbin/nologin

FreeBSD 5.1 i386. A hand-tweaked assembly program could hardly be smaller.
Well, it could, but that's another topic.


1900 bytes just to make two system calls?

It could be made much smaller :-)

e.g. http://www.muppetlabs.com/~breadbox/...ny/teensy.html

Nov 14 '05 #22
In <pa************ *************** *@ig.com.br> =?iso-8859-1?q?Jos=E9_de_P aula?= <jo***********@ ig.com.br> writes:
Em Mon, 12 Jan 2004 21:10:59 +0000, Alexander Bartolich escreveu:

<snip>
write(2) is a system call.
_exit(2) is a system call.

By using nothing but system calls the total size of the resulting
binary can be minimized. See option -nostdlib of gcc.


That's it. /sbin/nologin isn't linked to any library, not even libc.


Then, the linker would complain about the unresolved write and _exit
symbols. Even this program requires minimal library support, write() and
_exit() usually being wrappers to syscall().

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #23
In <bu**********@n ews-rocq.inria.fr> Grumble <in*****@kma.eu .org> writes:
José de Paula wrote:
That's it. /sbin/nologin isn't linked to any library, not even libc.
The result?

$ ls -lh /sbin/nologin
-r-xr-xr-x 1 root wheel 1,9K 4 Jun 2003 /sbin/nologin

FreeBSD 5.1 i386. A hand-tweaked assembly program could hardly be smaller.
Well, it could, but that's another topic.


1900 bytes just to make two system calls?

It could be made much smaller :-)


The size of the smallest C program is determined by the size of the
startup module:

fangorn:~/tmp 110> cat test.c
int main(void)
{
return 0;
}
fangorn:~/tmp 111> gcc -s test.c
fangorn:~/tmp 112> ls -l a.out
-rwxr-xr-x 1 danpop sysprog 2912 Jan 19 15:50 a.out*

If I want to get a smaller binary, that still works, I have to start
cheating:

fangorn:~/tmp 123> cat cheat.c
int main()
{
_exit(0);
}
fangorn:~/tmp 124> gcc -s -static -nostdlib cheat.c -lc
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 08048080
fangorn:~/tmp 125> ls -l a.out
-rwxr-xr-x 1 danpop sysprog 740 Jan 19 15:56 a.out*
fangorn:~/tmp 126> ./a.out
fangorn:~/tmp 127> echo $status
0

The ld warning was caused by the missing startup module, but this
minimal program doesn't really need it. The first program above, however,
does:

fangorn:~/tmp 128> gcc -s -static -nostdlib test.c -lc
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 08048080
fangorn:~/tmp 129> ./a.out
Segmentation fault
fangorn:~/tmp 130> ls -l a.out
-rwxr-xr-x 1 danpop sysprog 588 Jan 19 16:00 a.out*

The missing syscall support from the statically linked executable
made it both shorter and non-functional.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #24
Dan Pop wrote:
The size of the smallest C program is determined by the size of the
startup module:


And the size of the smallest ELF binary is determined by the size of
the ELF header ;-)

(Jose mentioned hand-tweaked assembly under FreeBSD.)

Nov 14 '05 #25

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
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...
1
10372
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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...
0
9187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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...
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
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.