德国开元华人社区 开元周游

标题: [news] Linus' First Analysis of the Files [打印本页]

作者: cyan    时间: 23.12.2003 12:10
Linus&#39; First Analysis of the Files <br> <br>Monday, December 22 2003 @ 05:41 PM EST  <br><br>Linus has taken a quick look at the files and has given permission for us to publish his first analysis. There will be more to say later, but the bottom line is that SCO appears to have made a copyright claim to some files that Linus wrote himself. I&#39;ll let him tell you about it, by reproducing his initial email to me from earlier today.<br><br>***************************************************************<br><br>I have a very strong memory of having written the original &quot;errno.h&quot; <br>myself too, and I really think that at least the i386 version of errno.h <br>actually has different numbers from &quot;real UNIX&quot;. Some of the first ones <br>match, but not the rest. That one I explain by just having a list of error <br>codes, and just giving numbers in order, but maybe I&#39;m wrong.<br><br>I have this distinct memory of figuring out only later that I _should_<br>have made the numbers be the same, so that I could have been binary<br>compatible. After all, I do actually have the book &quot;Intel386 Family Binary<br>Compatibility Specification 2&quot; (copyright Intel corporation, btw, not <br>SCO), and it lists the error numbers right there. They are different from <br>what Linux uses on x86.<br><br>Other architectures fixed that mistake, but the point is that the history <br>of &quot;errno.h&quot; is definitely _not_ from UNIX sources.<br><br>Linus<br><br>-----<br><br>For example, SCO lists the files &quot;include/linux/ctype.h&quot; and<br>&quot;lib/ctype.h&quot;, and some trivial digging shows that those files are<br>actually there in the original 0.01 distribution of Linux (ie September of<br>1991). And I can state <br><br>- I wrote them (and looking at the original ones, I&#39;m a bit ashamed: <br>the &quot;toupper()&quot; and &quot;tolower()&quot; macros are so horribly ugly that I <br>wouldn&#39;t admit to writing them if it wasn&#39;t because somebody else <br>claimed to have done so <!--emo&;)--><img src='http://210.51.184.178/kaiyuan/modules/ipboard/html/emoticons/wink.gif' border='0' style='vertical-align:middle' alt='wink.gif' /><!--endemo--><br><br>- writing them is no more than five minutes of work (you can verify that <br>with any C programmer, so you don&#39;t have to take my word for it)<br><br>- the details in them aren&#39;t even the same as in the BSD/UNIX files (the <br>approach is the same, but if you look at actual implementation details <br>you will notice that it&#39;s not just that my original &quot;tolower/toupper&quot; <br>were embarrassingly ugly, a number of other details differ too).<br><br>In short: for the files where I personally checked the history, I can<br>definitely say that those files are trivially written by me personally,<br>with no copying from any UNIX code _ever_.<br><br>So it&#39;s definitely not a question of &quot;all derivative branches&quot;. It&#39;s a<br>question of the fact that I can show (and SCO should have been able to<br>see) that the list they show clearly shows original work, not &quot;copied&quot;.<br><br><br>Analysis of &quot;lib/ctype.c&quot; and &quot;include/linux/ctype.h&quot;.<br><br><br>First, some background: the &quot;ctype&quot; name comes &quot;character type&quot;, and the<br>whole point of &quot;ctype.h&quot; and &quot;ctype.c&quot; is to test what kind of character<br>we&#39;re dealing with. In other words, those files implement tests for doing<br>things like asking &quot;is this character a digit&quot; or &quot;is this character an<br>uppercase letter&quot; etc. So you can write thing like<br><br>if (isdigit&copy;) {<br>.. we do something with the digit ..<br><br>and the ctype files implement that logic.<br><br>Those files exist (in very similar form) in the original Linux-0.01 <br>release under the names &quot;lib/ctype.c&quot; and &quot;include/ctype.h&quot;. That kernel <br>was released in September of 1991, and contains no code except for mine <br>(and Lars Wirzenius, who co-wrote &quot;kernel/vsprintf.c&quot;).<br><br>In fact, you can look at the files today and 12 years ago, and you can see <br>clearly that they are largely the same: the modern files have been cleaned <br>up and fix a number of really ugly things (tolower/toupper works <br>properly), but they are clearly incremental improvement on the original <br>one.<br><br>And the original one does NOT look like the unix source one. It has <br>several similarities, but they are clearly due to:<br><br>- the &quot;ctype&quot; interfaces are defined by the C standard library.<br><br>- the C standard also specifies what kinds of names a system library <br>interface can use internally. In particular, the C standard specifies <br>that names that start with an underscore and a capital letter are <br>&quot;internal&quot; to the library. This is important, because it explains why<br>both the Linux implementation _and_ the UNIX implementation used a<br>particular naming scheme for the flags.<br><br>- algorithmically, there aren&#39;t that many ways to test whether a <br>character is a number or not. That&#39;s _especially_ true in<br>C, where a macro must not use it&#39;s argument more than once. So for <br>example, the &quot;obvious&quot; implementation of &quot;isdigit()&quot; (which tests for <br>whether a character is a digit or not) would be<br><br>#define isdigit(x) (<!--emo&(x)--><img src='http://210.51.184.178/kaiyuan/modules/ipboard/html/emoticons/girl_handsacrossamerica.gif' border='0' style='vertical-align:middle' alt='girl_handsacrossamerica.gif' /><!--endemo--> &gt;= &#39;0&#39; && <!--emo&(x)--><img src='http://210.51.184.178/kaiyuan/modules/ipboard/html/emoticons/girl_handsacrossamerica.gif' border='0' style='vertical-align:middle' alt='girl_handsacrossamerica.gif' /><!--endemo--> &lt;= &#39;9&#39;)<br><br>but this is not actually allowed by the C standard (because &#39;x&#39; is used <br>twice).<br><br>This explains why both Linux and traditional UNIX use the &quot;other&quot; <br>obvious implementation: having an array that describes what each of the <br>possible 256 characters are, and testing the contents of that array<br>(indexed by the character) instead. That way the macro argument is only <br>used once.<br><br>The above things basically explain the similarities. There simply aren&#39;t<br>that many ways to do a standard C &quot;ctype&quot; implementation, in other words.<br><br>Now, let&#39;s look at the _differences_ in Linux and traditional UNIX:<br><br>- both Linux and traditional unix use a naming scheme of &quot;underscore and <br>a capital letter&quot; for the flag names. There are flags for &quot;is upper <br>case&quot; (_U) and &quot;is lower case&quot; (_L), and surprise surprise, both UNIX <br>and Linux use the same name. But think about it - if you wanted to use <br>a short flag name, and you were limited by the C standard naming, what <br>names _would_ you use? Maybe you&#39;d select &quot;U&quot; for &quot;Upper case&quot; and &quot;L&quot; <br>for &quot;Lower case&quot;?<br><br>Looking at the other flags, Linux uses &quot;_D&quot; for &quot;Digit&quot;, while<br>traditional UNIX instead uses &quot;_N&quot; for &quot;Number&quot;. Both make sense, but <br>they are different. I personally think that the Linux naming makes more <br>sense (the function that tests for a digit is called &quot;isdigit()&quot;, not<br>&quot;isnumber()&quot;), but on the other hand I can certainly understand why <br>UNIX uses &quot;_N&quot; - the function that checs for whether a character is <br>&quot;alphanumeric&quot; is called &quot;isalnum()&quot;, and that checks whether the <br>character is a upper case letter, a lower-case letter _or_ a digit (aka <br>&quot;number&quot;).<br><br>In short: there aren&#39;t that many ways you can choose the names, and <br>there is lots of overlap, but it&#39;s clearly not 100%.<br><br>- The original Linux ctype.h/ctype.c file has obvious deficiencies, which <br>pretty much point to somebody new to C making mistakes (me) rather than <br>any old and respected source. For example, the &quot;toupper()/tolower()&quot; <br>macros are just totally broken, and nobody would write the &quot;isascii()&quot; <br>and &quot;toascii()&quot; the way they were written in that original Linux. And<br>you can see that they got fixed later on in Linux development, even <br>though you can also see that the files otherwise didn&#39;t change.<br><br>For example: remember how C macros must only use their argument once <br>(never mind why - you really don&#39;t care, so just take it on faith, for<br>now). So let&#39;s say that you wanted to change an upper case character <br>into a lower case one, which is what &quot;tolower()&quot; does. Normal use is <br>just a fairly obvious<br><br>newchar = tolower(oldchar);<br><br>and the original Linux code does<br><br>extern char _ctmp;<br>#define tolower&copy; (_ctmp=c,isupper(_ctmp)?_ctmp+(&#39;a&#39;+&#39;A&#39;):_ctmp)<br><br>which is not very pretty, but notice how we have a &quot;temporary <br>character&quot; _ctmp (remember that internal header names should start with<br>an underscore and an upper case character - this is already slightly <br>broken in itself). That&#39;s there so that we can use the argument &quot;c&quot; <br>only once - to assign it to the new temporary - and then later on we <br>use that temporary several times.<br><br>Now, the reason this is broken is <br><br>- it&#39;s not thread-safe (if two different threads try to do this at <br>once, they will stomp on each others temporary variable)<br><br>- the argument &copy; might be a complex expression, and as such it<br>should really be parenthesized. The above gets several valid <br>(but unusual) expressions wrong.<br><br>Basically, the above is _exactly_ the kinds of mistakes a young programmer <br>would make. It&#39;s classic.<br><br>And I bet it&#39;s _not_ what the UNIX code looked like, even in 1991. UNIX by<br>then was 20 years old, and I _think_ that it uses a simple table lookup<br>(which makes a lot more sense anyway and solves all problems). I&#39;d be very<br>susprised if it had those kinds of &quot;beginner mistakes&quot; in it, but I don&#39;t <br>actually have access to the code, so what do I know? (I can look up some <br>BSD code on the web, it definitely does _not_ do anythign like the above).<br><br>The lack of proper parenthesis exists in other places of the original <br>Linux ctype.h file too: isascii() and toascii() are similarly broken.<br><br>In other words: there are _lots_ of indications that the code was not <br>copied, but was written from scratch. Bugs and all.<br><br>Oh, another detail: try searching the web (google is your friend) for <br>&quot;_ctmp&quot;. It&#39;s unique enough that you&#39;ll notice that all the returned hits <br>are all Linux-related. No UNIX hits anywhere. Doing a google for<br><br>_ctmp -linux<br><br>shows more Linux pages (that just don&#39;t happen to have &quot;linux&quot; in them),<br>except for one which is the L4 microkernel, and that one shows that they<br>used the Linux header file (it still says &quot;_LINUX_CTYPE_H&quot; in it).<br><br>So there is definitely a lot of proof that my ctype.h is original work.  <br>




欢迎光临 德国开元华人社区 开元周游 (https://forum.kaiyuan.de/) Powered by Discuz! X3.2