lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Ranier Vilela once stated:
> Em seg., 16 de nov. de 2020 às 12:41, Andre Leiradella <andre@leiradella.com>
> escreveu:
> > Not false, since strcmp only goes as far as the first NUL is found in one
> > of the strings.
> 
> Maybe you can then explain why they implemented strncmp, if strcmp solves
> everything and is as fast and better than strncmp?

  I know Roberto said not to contribute more to this thread, but I feel that
at least this question can (and probably should) be answered.  

  "They" were Ken Thompson, Brian Kerningham and Dennis Ritchie, creators of
Unix and C (they were developed in tandem).  The early versions of Unix (and
I'm talking about 1974, 1975 or there abouts) the file system of Unix was
still quite simple, and a directory was nothing more than a special file
that allowed 14 bytes for a file name and an integer (two bytes) to point to
the inode, an on-disk structure that contains the actual file contents. 
These two fields comprised an entry, and an array of these were the
directory:

	struct dir
	{
	  char filename[14];
	  int  inode;
	};

	struct dir directory[];

Two functions were developed to help scan and update this structure,
strncmp() and strncpy().  That is why strncpy() does NOT include the NUL
byte if 'n' is reached---to avoid overwriting the inode field.  That is why
strncmp() was developed---to help maintain a simple file system.  It had a
very particular use case.  That is all.

  -spc