News for hackers. Stuff that matters.
Re:Writing C software without the standard library by vxNsr on Tuesday November 29, 2016 @02:04AM (#13061399)
He claims that your code will be easy to port but then goes straight to Linux system calls.

Still I like the idea. This is something that should be covered in a CS 102 type course. I know way to many cs guys who have no idea how to debug, let alone how their is being implemented.

Re:Writing C software without the standard library by Mithaldu on Tuesday November 29, 2016 @02:55AM (#13061561)
> He claims that your code will be easy to port but then goes straight to Linux system calls.

Many linux developers believe "code is portable" means "can run on different linux distros".

Edit: To be fair, he said architectures, and i think he meant CPU architectures, i.e. AMD64 and i386.

Re:Writing C software without the standard library by pdw on Tuesday November 29, 2016 @03:37AM (#13061713)
But still, Linux syscalls are different on each architecture.
Re:Writing C software without the standard library by justincormack on Tuesday November 29, 2016 @04:18AM (#13061862)
Indeed, stupidly different. Different syscall numbers, different argument orders, different values for constants, different struct layouts, ... Some architectures, like MIPS, are particularly bad, with nods to Irix compatibility thrown in.

The BSDs are all exactly the same for every architecture, sane.

Re:Writing C software without the standard library by drewg123 on Tuesday November 29, 2016 @09:47AM (#13063294)
This dates back to how Linus did the first non-x86 port of Linux to the DEC Alpha. Rather than copying the x86 linux syscall conventions, he used the DEC OSF/1 syscall conventions to ease bootstrapping. This is a perfectly sane approach. But he probably should have reverted to a native syscall numbering rather than leaving this bootstrapping hack in place.
Re:Writing C software without the standard library by colin_mccabe on Thursday December 01, 2016 @03:08AM (#13077992)
Different platforms have different sets of registers and alignment requirements. You want to pass things in registers if possible. If you want to avoid inefficiency, you need platform differences. And this is all hidden from users and even developers anyway.
Re:Writing C software without the standard library by qb45 on Tuesday November 29, 2016 @04:50AM (#13061998)
Does that mean you can run Irix binaries on MIPS Linux?
Re:Writing C software without the standard library by startling on Tuesday November 29, 2016 @04:52AM (#13062017)
...which is why he used the SYS_ #defines.
Re:Writing C software without the standard library by kbp on Tuesday November 29, 2016 @06:12AM (#13062319)
When I was learning C, I loved Plauger's book on the standard library for just that reason; it's very informative to be guided through the implementation of the whole thing, not only to see how it works, but it's also a good way just to be introduced to everything in it without any magic. It's an excellent 2nd book on C, I think.
Re:Writing C software without the standard library by jgord on Tuesday November 29, 2016 @09:07AM (#13063077)
Plauger is one of my unsung heroes.

I discovered a little book by him after wading thru recursive descent parsers chapter by Herb Schildt... I don't want to get down on Schildt, I enjoyed that too, he showed some cool stuff..

but Plauger had me thinking that programming could be a craft, an art, a noble intellectual pursuit.

Re:Writing C software without the standard library by bogomipz on Tuesday November 29, 2016 @09:09AM (#13063090)
What is the title of the book by Plauger you are referring to?
Re:Writing C software without the standard library by kbp on Tuesday November 29, 2016 @09:16AM (#13063128)
"The Standard C Library"
Re:Writing C software without the standard library by bogomipz on Tuesday November 29, 2016 @11:56AM (#13064246)
Thank you.
Re:Writing C software without the standard library by imron on Tuesday November 29, 2016 @05:35AM (#13062196)
I've written Windows programs without the std library. The Win32 has plenty of replacement functions you can use instead though so it's much less work than what's presented here.

You get the same sorts of benefits though.

Re:Writing C software without the standard library by userbinator on Tuesday November 29, 2016 @06:23AM (#13062358)
Win32 is itself a layer on top of the syscalls, and already provides much of the functionality of the C standard library.
Re:Writing C software without the standard library by imron on Tuesday November 29, 2016 @07:33AM (#13062636)
Yes, that was partly my point.

You can get reduced executable size and a lack of dependencies on various msvc*.dlls, without giving up much of the functionality of the C standard library and without having to write it all yourself.

Re:Writing C software without the standard library by qb45 on Tuesday November 29, 2016 @08:40AM (#13062940)
Fine, but you get this thanks to already being linked with these megagodzillas like kernel32.dll. Try without them.
Re:Writing C software without the standard library by ekr on Tuesday November 29, 2016 @10:10AM (#13063454)
On windows nt, ntdll gets loaded in all processes, it's hardcoded in the kernel (http://gate.upm.ro/os/LABs/Windows_OS_Internals_Curriculum_R...). That's where the loader resides, so you can't go without it. And that's where all the Zw/Nt* functions/ i.e. where the syscalls are.

So you can't really go without it.

Re:Writing C software without the standard library by tubs on Tuesday November 29, 2016 @07:39PM (#13067506)
Except the new picoprocesses, which don't get ntdll (or anything really, I guess).
Re:Writing C software without the standard library by vertex-four on Tuesday November 29, 2016 @08:49AM (#13062982)
A program which isn't linked against the Win32 stuff is an invalid program, since syscalls in Windows are not at all stable.
Re:Writing C software without the standard library by lolisamurai on Tuesday November 29, 2016 @11:25AM (#13064009)
Suppose you want to port to some architecture not supported by libc. If you were using libc you would have to find a replacement that works and targets that arch or port libc yourself. If you wrote everything from scratch, instead, you just have to read the specification and add support to your code. That's what I meant.

Of course, if your target archs are all supported by libc, porting is much easier with libc.

Re:Writing C software without the standard library by time4tea on Wednesday November 30, 2016 @01:28PM (#13073264)
Well, not if you expect that you will have exactly the same time/space behaviour and pre/post conditions.

This is why lots of embedded, secure and defense software doesn't use standard libraries, and printf will instead be called sio04583 or something....

Also.. Don't forget that you can get gcc to get rid of unused code when compiling static executables, and use sstrip (yes, two 's' - it's a different program) to strip even more, if it's an ELF binary...

Re:Writing C software without the standard library by sqeaky on Tuesday November 29, 2016 @01:33PM (#13065088)
Depending on the scope of your project porting libc itself might be good idea. Then you simplify porting any tools you need that also use libc. Then you can port a few extra dependencies, then more tools...
Re:Writing C software without the standard library by bogomipz on Tuesday November 29, 2016 @09:39PM (#13068010)
What are some archs that arent't supported by libc? Just embedded systems?
Re:Writing C software without the standard library by mfukar on Tuesday November 29, 2016 @03:47AM (#13061742)
Portability is impossible without the standard library.
Re:Writing C software without the standard library by Sphax on Tuesday November 29, 2016 @04:13AM (#13061840)
What ? You just write your OS layer for each platform and it's portable.
Re:Writing C software without the standard library by gtufano on Tuesday November 29, 2016 @04:19AM (#13061868)
True. The OS layer you write (and port to other OSes) is called "the standard library". :-)
Re:Writing C software without the standard library by frederikvs on Tuesday November 29, 2016 @04:45AM (#13061974)
But at least now you know what's in there, what's going on behind the scenes. And on top of that, it's filled with all sorts of brand new bugs and vulnerabilities! ;-)
Re:Writing C software without the standard library by makapuf on Tuesday November 29, 2016 @04:36AM (#13061934)
A .h file with different syscalls IDs and perhaps a few inline functions is hardly a standard library though
Re:Writing C software without the standard library by vidarh on Tuesday November 29, 2016 @05:01AM (#13062055)
Not exactly syscall ids: I built network services (telnet daemon, snmp client and server, DNS) that compiled without change for win32 (both on Windows and some embedded win32 compatible abomination) and Linux with just a handful of defines and function wrappers as part of a contract once. A lot of apps can get away with very narrow interfaces to the OS.
Re:Writing C software without the standard library by masklinn on Tuesday November 29, 2016 @05:21AM (#13062135)
> A .h file with different syscalls IDs

That's not portable even across versions of the same OS, many OS don't support raw syscalls and don't make any guarantees about them. That's why you can't statically link libc on OSX for instance, libSystem is literally the system's interface and necessarily dynamically linked.

Re:Writing C software without the standard library by makapuf on Tuesday November 29, 2016 @06:52AM (#13062450)
That would be the few inline functions so to abstract a bit the syscalls.
Re:Writing C software without the standard library by masklinn on Tuesday November 29, 2016 @07:37AM (#13062651)
It doesn't abstract anything, if the syscall changes your "abstract a bit" will be broken all the same. On many if not most OS the machine code side of the syscalls can change with no notice, Windows has changed syscalls in minor revisions, Go broke several times during the Sierra beta due to syscall changes (because it handrolls gettimeofday(2) whose assembly calling convention changed).

The proper abstract interface on non-linux systems is the standard library.

Re:Writing C software without the standard library by posterboy on Tuesday November 29, 2016 @05:12AM (#13062093)
As far as PortableOSIX compliant syscalls are concerned, they are literally portable.
Re:Writing C software without the standard library by qb45 on Tuesday November 29, 2016 @08:33AM (#13062900)
The userspace/kernel interface for invoking syscalls which the OP calls with assembly code is obviously not portable. Not even between similar OSs on one architecture and absolutely not between architectures.

POSIX only specifies a set of C library functions. Once you stop using them, you are on your own.

Re:Writing C software without the standard library by qb45 on Tuesday November 29, 2016 @03:49AM (#13061748)
> He claims that your code will be easy to port but then goes straight to Linux system calls.

These are nowadays supported by Windows and few BSDs too, who needs more portability than that :)

Re:Writing C software without the standard library by LeoNatan25 on Tuesday November 29, 2016 @04:36AM (#13061936)
"Supported on Windows" is a strong claim. They are supported in the Linux subsystem, but asking your users to install that seems far fetched to me.
Re:Writing C software without the standard library by pawadu on Tuesday November 29, 2016 @03:52AM (#13061758)
> He claims that your code will be easy to port but then goes straight to Linux system calls.

Well, it is hard to see an alternative to that outside supplying your own OS with the library...