{"id":108792,"date":"2023-09-19T07:00:00","date_gmt":"2023-09-19T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=108792"},"modified":"2023-09-19T07:01:14","modified_gmt":"2023-09-19T14:01:14","slug":"20230919-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20230919-00\/?p=108792","title":{"rendered":"Why did the 16-bit _lopen and _lcreat function return -1 on failure instead of 0?"},"content":{"rendered":"<p>Some time ago, I discussed <a title=\"Why are HANDLE return values so inconsistent?\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20040302-00\/?p=40443\"> why <code>HANDLE<\/code> return values are so inconsistent<\/a>, and I traced it all the way back to the 16-bit <code>_lopen<\/code> and <code>_lcreat<\/code> functions, which returned <code>-1<\/code> on failure.<\/p>\n<p>But why do those functions return <code>-1<\/code> on failure instead of zero?<\/p>\n<p>The <code>_lopen<\/code> and <code>_lcreat<\/code> functions were Windows versions of the C runtime <code>_open<\/code> and <code>_creat<\/code> functions. The C runtime functions came in four different versions depending on which <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200728-00\/?p=104012\"> MS-DOS memory model you were using<\/a>, and the convention was that when Windows adopted a C runtime function, it used the &#8220;large&#8221; version with the <code>L<\/code> prefix, since that is the most general version.<\/p>\n<p>Okay, so why did <code>_open<\/code> and <code>_creat<\/code> return <code>-1<\/code> on failure?<\/p>\n<p>Because they were MS-DOS-compatible versions of the Unix functions <a href=\"https:\/\/www.man7.org\/linux\/man-pages\/man2\/open.2.html\"> <code>open<\/code> and <code>creat<\/code><\/a>. They even preserve the dropped silent &#8220;e&#8221; at the end of <code>creat<\/code>.<\/p>\n<p>Okay, so why do those functions return <code>-1<\/code> on failure?<\/p>\n<p>On Unix, the return value is an integer that represents a file descriptor, valid file descriptors are integers starting with zero. Every process comes with three predefined file descriptors:<\/p>\n<table class=\"cp3\" style=\"border-collapse: collapse;\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<th>Descriptor<\/th>\n<th>Meaning<\/th>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">0<\/td>\n<td>stdin (standard input)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">1<\/td>\n<td>stdout (standard output)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">2<\/td>\n<td>stderr (standard error)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Files opened by the program begin with file descriptor 3.<\/p>\n<p>The value <code>-1<\/code> is used to represent failure because 0 was already taken.<\/p>\n<p>And that value of <code>-1<\/code> carried forward, through a chain of backward compatibility, to Win32 as the numeric value of <code>INVALID_<wbr \/>HANDLE_<wbr \/>VALUE<\/code>. We saw a little while ago <a title=\"I accidentally performed an operation on INVALID_HANDLE_VALUE, and it worked: What just happened?\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20230914-00\/?p=108766\"> one of the consequences<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Trying to look like somebody else.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2],"class_list":["post-108792","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-history"],"acf":[],"blog_post_summary":"<p>Trying to look like somebody else.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/108792","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=108792"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/108792\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=108792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=108792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=108792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}