<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Unix on Kiril Vladimirov</title>
    <link>https://vld.bg/tags/unix/</link>
    <description>Recent content in Unix on Kiril Vladimirov</description>
    <generator>Hugo -- 0.140.2</generator>
    <language>en</language>
    <lastBuildDate>Tue, 14 Nov 2023 16:09:17 +0200</lastBuildDate>
    <atom:link href="https://vld.bg/tags/unix/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Why does Vim really use hjkl?</title>
      <link>https://vld.bg/articles/hjkl/</link>
      <pubDate>Tue, 14 Nov 2023 16:09:17 +0200</pubDate>
      <guid>https://vld.bg/articles/hjkl/</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This is part of the talk &amp;ldquo;UNIX archeology&amp;rdquo; I gave at OpenFest 2023.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If you, dear reader, are a vim user, you&amp;rsquo;ve probably seen this keyboard:&lt;/p&gt;
&lt;p&gt;&lt;img alt=&#34;ADM-3A keyboard&#34; loading=&#34;lazy&#34; src=&#34;https://vld.bg/img/posts/ADM-3A.jpg&#34;&gt;&lt;/p&gt;
&lt;p&gt;It explains a lot about our beloved text-editor. CTRL is just where it has to
be in the first place, the Esc is so near and the &lt;code&gt;:&lt;/code&gt; symbol we use for
commands is not hidden behind a modifier like nowadays. But why the arrows?&lt;/p&gt;</description>
      <content:encoded><![CDATA[<blockquote>
<p>This is part of the talk &ldquo;UNIX archeology&rdquo; I gave at OpenFest 2023.</p>
</blockquote>
<p>If you, dear reader, are a vim user, you&rsquo;ve probably seen this keyboard:</p>
<p><img alt="ADM-3A keyboard" loading="lazy" src="/img/posts/ADM-3A.jpg"></p>
<p>It explains a lot about our beloved text-editor. CTRL is just where it has to
be in the first place, the Esc is so near and the <code>:</code> symbol we use for
commands is not hidden behind a modifier like nowadays. But why the arrows?</p>
<p>So, one might say: &ldquo;Well, because <a href="https://en.wikipedia.org/wiki/Bill_Joy" target="_blank">Bill
Joy</a>
 created
<a href="https://en.wikipedia.org/wiki/Vi_%28text_editor%29" target="_blank">vi</a>
 using this
<a href="https://en.wikipedia.org/wiki/ADM-3A" target="_blank">ADM-3A</a>
 terminal where the arrows just
happened to be located there&rdquo;. And that person would be absolutely right. But
why were they located there?</p>
<p>To be fair, if you look at other terminals from these days, where to put the
arrows was not really standardized. Not even the triangular shape, we&rsquo;re so
used with today. Most machines would put them in one line, but in different
order. For instance, some would go with <code>[↓] [↑] [←] [→]</code>. So far, the answer that they
just had to put them somewhere still holds.</p>
<h2 id="ascii">ASCII</h2>
<p>It was not random, though. You see, in
<a href="https://en.wikipedia.org/wiki/ASCII" target="_blank">ASCII</a>
 characters are not just put at
some point on random. There are fairly good decisions taken there that go
mostly unnoticed today as people no longer work directly with ones and zeroes
really. If you want to get the code for an upper-case letter you should add 64
to the position of that letter in the alphabet. If you want it to be
lower-case, add 96 instead. That&rsquo;s pretty clever, because it all boils down to
flipping a bit:</p>
<table>
  <thead>
      <tr>
          <th>ASCII Char</th>
          <th>H [←]</th>
          <th>J [↓]</th>
          <th>K [↑]</th>
          <th>L [→]</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Decimal</td>
          <td>72</td>
          <td>74</td>
          <td>75</td>
          <td>76</td>
      </tr>
      <tr>
          <td>Binary</td>
          <td>0<strong>1</strong>001000</td>
          <td>0<strong>1</strong>001010</td>
          <td>0<strong>1</strong>001011</td>
          <td>0<strong>1</strong>001100</td>
      </tr>
  </tbody>
</table>
<table>
  <thead>
      <tr>
          <th>ASCII Char</th>
          <th>h [←]</th>
          <th>j [↓]</th>
          <th>k [↑]</th>
          <th>l [→]</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Decimal</td>
          <td>104</td>
          <td>106</td>
          <td>107</td>
          <td>108</td>
      </tr>
      <tr>
          <td>Binary</td>
          <td>0<strong>11</strong>01000</td>
          <td>0<strong>11</strong>01010</td>
          <td>0<strong>11</strong>01011</td>
          <td>0<strong>11</strong>01100</td>
      </tr>
  </tbody>
</table>
<p>This first bit is always 0, as ASCII is a 7-bit character set. If the second
bit is up, we are dealing with a letter. If the third is up, then this letter
is a lower-case. It&rsquo;s that simple. But look what happens if we set both of
those 2nd and 3rd bits to zero:</p>
<table>
  <thead>
      <tr>
          <th>ASCII Char</th>
          <th>Backspace</th>
          <th>Linefeed</th>
          <th>Vertical Tab</th>
          <th>Formfeed</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Decimal</td>
          <td>8</td>
          <td>10</td>
          <td>11</td>
          <td>12</td>
      </tr>
      <tr>
          <td>Binary</td>
          <td>0<strong>00</strong>01000</td>
          <td>0<strong>00</strong>01010</td>
          <td>0<strong>00</strong>01011</td>
          <td>0<strong>00</strong>01100</td>
      </tr>
  </tbody>
</table>
<p>When pressing any of those four keys, the terminal has to take two decisions:</p>
<ul>
<li>If [Shift] is pressed, set the 3rd bit to 0</li>
<li>If [CTRL] is pressed, set the 2nd and 3rd bits to 0</li>
</ul>
<h2 id="but-wait-theres-more">But wait, there&rsquo;s more</h2>
<p>Legacy we still observe from this terminal doesn&rsquo;t end here, though.</p>
<h3 id="heading">~/</h3>
<p>Take a look at the top right button. Yep. That&rsquo;s why we are shortening the path
to user&rsquo;s home folder as <code>~/</code>.</p>
<h3 id="regular-expressions">Regular expressions</h3>
<p>If you ever wondered why <code>^</code> is the beginning of line or &ldquo;home&rdquo; position in
regular expression dialects, well&hellip; now you know :)</p>
]]></content:encoded>
    </item>
    <item>
      <title>Why do we have /usr?</title>
      <link>https://vld.bg/articles/usr/</link>
      <pubDate>Sun, 12 Nov 2023 15:13:10 +0200</pubDate>
      <guid>https://vld.bg/articles/usr/</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This is part of the talk &amp;ldquo;UNIX archeology&amp;rdquo; I gave at OpenFest 2023.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In modern contexts, /usr is essential for housing the majority of user
utilities and applications. This includes system-wide software and files like
standard programs, libraries, documentation, and much more. Unlike the root
filesystem, which contains the minimum necessary for booting and repairing the
system, /usr provides the bulk of the operating system&amp;rsquo;s functionality.&lt;/p&gt;
&lt;p&gt;However, have you ever thought why do we have seemingly duplicated with /
directories in there? For instance, there is /bin and /usr/bin, /lib and
/usr/lib, and lately we&amp;rsquo;ve started symlinking one to another. Surely, we
could&amp;rsquo;ve just avoided the need to have them both in the first place. But why
are they there?&lt;/p&gt;</description>
      <content:encoded><![CDATA[<blockquote>
<p>This is part of the talk &ldquo;UNIX archeology&rdquo; I gave at OpenFest 2023.</p>
</blockquote>
<p>In modern contexts, /usr is essential for housing the majority of user
utilities and applications. This includes system-wide software and files like
standard programs, libraries, documentation, and much more. Unlike the root
filesystem, which contains the minimum necessary for booting and repairing the
system, /usr provides the bulk of the operating system&rsquo;s functionality.</p>
<p>However, have you ever thought why do we have seemingly duplicated with /
directories in there? For instance, there is /bin and /usr/bin, /lib and
/usr/lib, and lately we&rsquo;ve started symlinking one to another. Surely, we
could&rsquo;ve just avoided the need to have them both in the first place. But why
are they there?</p>
<p>The <a href="https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard" target="_blank">Filesystem Hierarchy Standard
(FHS)</a>
 is a reference describing the
conventions used for the layout of Unix-like systems. Latest release defines its
<a href="https://refspecs.linuxfoundation.org/FHS_3.0/fhs-3.0.html#purpose18" target="_blank">purpose</a>
 as:</p>
<blockquote>
<p>/usr is the second major section of the filesystem. /usr is shareable,
read-only data. That means that /usr should be shareable between various
FHS-compliant hosts and must not be written to. Any information that is
host-specific or varies with time is stored elsewhere.</p>
<p>Large software packages must not use a direct subdirectory under the /usr
hierarchy.</p>
</blockquote>
<p>Actually, we only found out that we could mount /usr in read-only mode and even
re-use it between a bunch of machines, decades after /usr was part of Unix.</p>
<h2 id="no-space-left-on-device">no space left on device</h2>
<p>Unix was created on a <a href="https://en.wikipedia.org/wiki/PDP-7" target="_blank">PDP-7</a>
 machine in
1969 and later migrated (which included rewriting a bunch of things, but that&rsquo;s
a story for another time) to the much more powerful
<a href="https://en.wikipedia.org/wiki/PDP-11" target="_blank">PDP-11</a>
, which had one RK05 disk pack
(1.5MB):</p>
<p><img alt="RK05 disk pack" loading="lazy" src="/img/posts/rk05.jpg"></p>
<blockquote>
<p><a href="https://commons.wikimedia.org/wiki/User:ArnoldReinhold" target="_blank">Arnold Reinhold</a>
 | CC-BY-SA</p>
</blockquote>
<p>That was all. The entire / of Unix was on one of those drives, less than 1.5MB.
Eventually it grew, with its userbase, and they needed another one. It was
mounted into /usr in order to contain user home directories. There were
/usr/ken, /usr/dmr, /usr/bwk, etc. However, multiple users were writing useful
tools which they wanted to share and thus /usr/bin has been created. Eventually
man pages started getting into shape, fonts started being interesting, people
experimented with graphics and they all meant to be shared. The law was that
nothing on that drive should be crucial for the machine to boot. For instance,
you could not place the <code>mount</code> command in there, because something had to
mount /usr in the first place. This limitation no longer exists thanks to
initrd and initramfs.</p>
<p>Eventually, this drive grew bigger as well and they needed a 3rd one. It was
mounted into /home and all of the home directories have been moved there,
leaving /usr only for shareable files. That meant whole 3MB solely for the
operating system. Hell, yeah!</p>
<p>And then we just kinda gone with it. Once Unix was made (not quite) generally
available and the idea to split the OS into multiple drives was still legit.
This was no longer the case only decades later. Why going through a revolution for
that? Instead, people just made up rules and conventions on how to deal with /usr.</p>
]]></content:encoded>
    </item>
    <item>
      <title>Why do we need hidden files?</title>
      <link>https://vld.bg/articles/hidden-files/</link>
      <pubDate>Fri, 10 Nov 2023 00:58:47 +0200</pubDate>
      <guid>https://vld.bg/articles/hidden-files/</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This is part of the talk &amp;ldquo;UNIX archeology&amp;rdquo; I gave at OpenFest 2023.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Hidden files are fairly popular these days, especially in our home folders. We
store configuration, cache, logs and whatnot in there. Here&amp;rsquo;s what&amp;rsquo;s going on
in the home folder on the machine I&amp;rsquo;m writting this right now:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;❯ find ~ -name &#39;.*&#39; -type d | wc -l
5768
❯ find ~ -name &#39;.*&#39; -type f | wc -l
30573
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But why?&lt;/p&gt;</description>
      <content:encoded><![CDATA[<blockquote>
<p>This is part of the talk &ldquo;UNIX archeology&rdquo; I gave at OpenFest 2023.</p>
</blockquote>
<p>Hidden files are fairly popular these days, especially in our home folders. We
store configuration, cache, logs and whatnot in there. Here&rsquo;s what&rsquo;s going on
in the home folder on the machine I&rsquo;m writting this right now:</p>
<pre><code>❯ find ~ -name '.*' -type d | wc -l
5768
❯ find ~ -name '.*' -type f | wc -l
30573
</code></pre>
<p>But why?</p>
<p>Suppose you&rsquo;re working on this new cool OS and receive it as a feature request:
&ldquo;We should introduce hidden files&rdquo;. How would you do it? Knowing that files
generally need attributes, why not create a new one for it? Exactly what
Microsoft has one for their <del>cool</del> OS:</p>
<p><img loading="lazy" src="/img/posts/windows_file_properties.png"></p>
<p>But that is not how we do that in Unix. Instead, soon after the beginning of
time, in Unix hidden files are those which name starts with a dot. Looks odd
when you think about it, trying to ignore the fact you&rsquo;re so used with them.
But why?</p>
<h2 id="its-not-a-bug-its-a-feature">It&rsquo;s not a bug, it&rsquo;s a feature!</h2>
<p>Rob Pike wrote an explanation on Google+. Even though Google killed
that service long ago, <a href="https://web.archive.org/web/20140803082229/https://plus.google.com/&#43;RobPikeTheHuman/posts/R58WgWwN9jp" target="_blank">The Wayback Machine has a record of
that</a>
:</p>
<blockquote>
<p>A lesson in shortcuts.</p>
<p>Long ago, as the design of the Unix file system was being worked out, the
entries . and .. appeared, to make navigation easier. I&rsquo;m not sure but I
believe .. went in during the Version 2 rewrite, when the file system became
hierarchical (it had a very different structure early on).  When one typed
ls, however, these files appeared, so either Ken or Dennis added a simple
test to the program. It was in assembler then, but the code in question was
equivalent to something like this:</p>
<p><code>if (name[0] == '.') continue;</code></p>
<p>This statement was a little shorter than what it should have been, which is</p>
<p><code>if (strcmp(name, &quot;.&quot;) == 0 || strcmp(name, &quot;..&quot;) == 0) continue;</code></p>
<p>but hey, it was easy.</p>
<p>Two things resulted.</p>
<p>First, a bad precedent was set. A lot of other lazy programmers introduced
bugs by making the same simplification. Actual files beginning with periods
are often skipped when they should be counted.</p>
<p>Second, and much worse, the idea of a &ldquo;hidden&rdquo; or &ldquo;dot&rdquo; file was created. As
a consequence, more lazy programmers started dropping files into everyone&rsquo;s
home directory. I don&rsquo;t have all that much stuff installed on the machine I&rsquo;m
using to type this, but my home directory has about a hundred dot files and I
don&rsquo;t even know what most of them are or whether they&rsquo;re still needed. Every
file name evaluation that goes through my home directory is slowed down by
this accumulated sludge.</p>
<p>I&rsquo;m pretty sure the concept of a hidden file was an unintended consequence.
It was certainly a mistake.</p>
<p>How many bugs and wasted CPU cycles and instances of human frustration (not
to mention bad design) have resulted from that one small shortcut about  40
years ago?</p>
<p>Keep that in mind next time you want to cut a corner in your code.</p>
<p>(For those who object that dot files serve a purpose, I don&rsquo;t dispute that
but counter that it&rsquo;s the files that serve the purpose, not the convention
for their names. They could just as easily be in $HOME/cfg or $HOME/lib,
which is what we did in Plan 9, which had no dot files. Lessons can be
learned.)</p>
</blockquote>
]]></content:encoded>
    </item>
    <item>
      <title>79 characters</title>
      <link>https://vld.bg/articles/79-characters/</link>
      <pubDate>Wed, 08 Nov 2023 22:35:33 +0200</pubDate>
      <guid>https://vld.bg/articles/79-characters/</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This is part of the talk &amp;ldquo;UNIX archeology&amp;rdquo; I gave at OpenFest 2023.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Have you ever thought why the 79 characters limit per line is so popular in
programming and pretty much nowhere else? Python with their notable PEP-0008,
the Linux kernel, PostgreSQL and the GNU project, just to name a few. Sounds
like an arbitrary one. But why?&lt;/p&gt;
&lt;h2 id=&#34;tty&#34;&gt;tty&lt;/h2&gt;
&lt;p&gt;People tend to believe that this is because terminals were able to print up to
80 symbols per line and this just got carried over. The most popular one being
VT100:&lt;/p&gt;</description>
      <content:encoded><![CDATA[<blockquote>
<p>This is part of the talk &ldquo;UNIX archeology&rdquo; I gave at OpenFest 2023.</p>
</blockquote>
<p>Have you ever thought why the 79 characters limit per line is so popular in
programming and pretty much nowhere else? Python with their notable PEP-0008,
the Linux kernel, PostgreSQL and the GNU project, just to name a few. Sounds
like an arbitrary one. But why?</p>
<h2 id="tty">tty</h2>
<p>People tend to believe that this is because terminals were able to print up to
80 symbols per line and this just got carried over. The most popular one being
VT100:</p>
<p><img alt="DEC VT100" loading="lazy" src="/img/posts/DEC_VT100.jpg"></p>
<blockquote>
<p><a href="https://www.flickr.com/people/54568729@N00" target="_blank">Jason Scott</a>
 | CC BY 2.0</p>
</blockquote>
<p>To be fair, not all of them did, but VT100 is iconic enough to inspire such a
tradition. It was able to operate in two modes - 80 columns by 24 rows or 132
columns by 14 rows. So, we might as well agree with that and call it a night.
But why?</p>
<h2 id="ibm-80-column-punched-card">IBM 80-column punched card</h2>
<p>Okay, so seems like VT100 and some of its predecessor were not the first ones
sticking with 80 symbols. Here&rsquo;s the IBM 12-row/80-column punched card format
which came to dominate the industry:</p>
<p><img alt="IBM 12-row/80-column punched card" loading="lazy" src="/img/posts/ibm_punchcard.jpg"></p>
<blockquote>
<p><a href="https://www.flickr.com/people/93001633@N00" target="_blank">Pete Birkinshaw</a>
 | CC BY 2.0</p>
</blockquote>
<p>This makes sense. The EOL is an actual characters and this is how we&rsquo;ve ended
up with 79 characters limit, depending on whether you&rsquo;re counting the EOL as
one. So, this makes sense to be the initial reason for that limitation - the
most popular punched cards were limited to 80 symbols.
But why?</p>
<p>Initially, punched cards allowed far less characters per-line in order not to
tear the card with too many consecutive holes. In fact, way before that they
weren&rsquo;t used to store lines of text in the first place. The only thing that
never changed was the size of those cards.</p>
<h2 id="hollerith-tabulating-machine">Hollerith tabulating machine</h2>
<p><img alt="Hollerith tabulating machine" loading="lazy" src="/img/posts/HollerithMachine.CHM.jpg"></p>
<blockquote>
<p><a href="https://www.flickr.com/people/44124384537@N01" target="_blank">Adam Schuster</a>
 | CC BY 2.0</p>
</blockquote>
<p>The Hollerith Tabulating Machine, an ingenious invention of the late 19th
century, marked a pivotal moment in the history of data processing. Conceived
by Herman Hollerith, this revolutionary device was inspired by the challenges
of efficiently processing vast amounts of data from the 1890 U.S. Census. The
machine&rsquo;s core innovation lay in its use of punched cards, a concept borrowed
from the Jacquard loom, to store and sort data.</p>
<p>Each punched card represented a unique data point, with various patterns of
holes encoding specific information. When fed into the Hollerith machine, a
series of mechanical and electrical systems interpreted these holes, tabulating
and summarizing data with unprecedented speed and accuracy.</p>
<p>Yep, U.S. Census. Each card did NOT encode text, but the details for each
counted person. Here&rsquo;s what it looked like:
<img alt="Hollerith punched card" loading="lazy" src="/img/posts/hollerith_punched_card.png"></p>
<p>If you wonder what any of this means, early computers have a great article
about <a href="https://www.earlycomputers.com/cgi-bin/census1890_cards.cgi" target="_blank">1890 Census: The Meaning of the Hollerith Card
Codes</a>
. Some of
the entries here might not be approved by today&rsquo;s standards. So, the cards for
that machine were that big and every single machine dealing with punched cards,
kind of just went with it. But why were the cards for that machines so big?</p>
<p><img alt="US dollar bill from before 1920" loading="lazy" src="/img/posts/us1920.jpg"></p>
<p>U.S. dollar bills. See those boxes next to the machine? They were used to
transport bills which before 1920 were kind of huge. This way, you don&rsquo;t get to
manufacture new type of boxes and new type of paper cutters for those new
special cards.</p>
<p>So, the next time somebody asks you why they should abide to that 79-characters
per-line rule, you can tell them the old U.S. dollar bills are to blame. That&rsquo;s why!</p>
]]></content:encoded>
    </item>
  </channel>
</rss>
