kevinlawler / kona

Open-source implementation of the K programming language
ISC License
1.36k stars 138 forks source link

Loading and Saving a text file results in a file labeled "executable" #536

Closed tavmem closed 4 years ago

tavmem commented 5 years ago
$ ls -lrt
...
-rw-rw-r--. 1 tom tom  145332 Mar  1 16:52 out
$

$ rlwrap -n ./k
kona      \ for help. \\ to exit.

  a: 0: `out
  `out1 0: a
  \\
$ 

$ ls -lrt
...
-rw-rw-r--. 1 tom tom  145332 Mar  1 16:52 out
-rwxrwxr-t. 1 tom tom  145332 Mar  2 10:57 out1
$
tavmem commented 4 years ago

But ... if we use k2.8 to load and save an executable file ... that does not work ... Simple c program

$ cat main.c
#include <stdio.h>

#define O printf
#define R return

typedef char C;
typedef C* S;

int main()
{
  S s="0123456";
  O("s: %s\n",s);
  if(s[0]=='1')O("a\n");
  else if(s[0]=='2')O("b\n");
  else if(s[0]=='0')O("c\n");
  R 0;
}
$ 

Run it

$ ./a.out
s: 0123456
c

directory

$ ls -lrt
total 40
-rw-rw-r--. 1 tom tom     5 Apr 18  2019 tstScrpt.k
-rw-rw-r--. 1 tom tom  4674 Jan  7 16:43 ts.h
-rw-rw-r--. 1 tom tom   261 Jan  7 17:14 main.c
-rwxrwxr-x. 1 tom tom 22272 Jan  7 17:15 a.out

make a copy of the executable with k2.8

 rlwrap ~/k2.8/k
K 2.8 2000-10-10 Copyright (C) 1993-2000 Kx Systems
Evaluation. Not for commercial use. 
\ for help. \\ to exit.

  a: 0: `a.out
  `b.out 0: a
  \\

$ ls -lrt
total 44
-rw-rw-r--. 1 tom tom     5 Apr 18  2019 tstScrpt.k
-rw-rw-r--. 1 tom tom  4674 Jan  7 16:43 ts.h
-rw-rw-r--. 1 tom tom   261 Jan  7 17:14 main.c
-rwxrwxr-x. 1 tom tom 22272 Jan  7 17:15 a.out
-rw-rw-r--. 1 tom tom    12 Feb 27 21:47 b.out
$

try to execute it

$ ./b.out
bash: ./b.out: Permission denied

make a copy with kona

$ rlwrap ~/k200227/k
kona      \ for help. \\ to exit.

  a: 0: `a.out
  `c.out 0: a
  \\
$ ls -lrt
total 68
-rw-rw-r--. 1 tom tom     5 Apr 18  2019 tstScrpt.k
-rw-rw-r--. 1 tom tom  4674 Jan  7 16:43 ts.h
-rw-rw-r--. 1 tom tom   261 Jan  7 17:14 main.c
-rwxrwxr-x. 1 tom tom 22272 Jan  7 17:15 a.out
-rw-rw-r--. 1 tom tom    12 Feb 27 21:47 b.out
-rwxrwxr-t. 1 tom tom 22273 Feb 27 21:48 c.out
$ 

try to execute it

$ ./c.out
s: 0123456
c
$
tavmem commented 4 years ago

If you load and save a text file in either kona or k2.8 ... you can still compile and execute it successfully.

$ ls -lrt
total 4
-rw-rw-r--. 1 tom tom 226 Feb 27 22:32 m.c
$ 
$ cat m.c
#include <stdio.h>
#define O printf
#define R return
typedef char C;
typedef C* S;

int main()
{
  S s="0123456";
  O("s: %s\n",s);
  if(s[0]=='1')O("a\n");
  else if(s[0]=='2')O("b\n");
  else if(s[0]=='0')O("c\n");
  R 0;
}
$ 
$ rlwrap ~/kona/k
kona      \ for help. \\ to exit.

  a: 0: `m.c
  `main.c 0: a
  \\
$ 
$ ls -lrt
total 8
-rw-rw-r--. 1 tom tom 226 Feb 27 22:32 m.c
-rwxrwxr-t. 1 tom tom 226 Feb 27 22:34 main.c
$ 
$ gcc main.c
$ 
$ ls -lrt
total 32
-rw-rw-r--. 1 tom tom   226 Feb 27 22:32 m.c
-rwxrwxr-t. 1 tom tom   226 Feb 27 22:34 main.c
-rwxrwxr-x. 1 tom tom 21840 Feb 27 22:34 a.out
$ 
$ ./a.out
s: 0123456
c
$
bakul commented 4 years ago

0: should be used with text files only as it it strips off line-end (which is \n for unix and \r\n for DOS/Windows). For example:

$ sed 's/$/^M/' < README.md > xx    # add ^M at line end. ^M is carriage return
$ k-3.2
...
  a:0:"README.md"
  b:0:"xx"
  &/&/'a=b  // check if all lines are the same
1

Also, if given a binary file which may have embedded \0 in it, 0: will do the wrong thing.

  a:0:"k"
  \ls -l k
-rwxr-xr-x 1 bakul bakul 795412 2020-02-15 20:13 k
  (#a)++/#:'a
55721

Suggest using 6: to read/write arbitrary files.

tavmem commented 4 years ago

Thanks for the comment & suggestion.

I was not looking to extend kona beyond k3 ... just looking to identify differences between kona and k3, and fix any that seem to be bugs in kona.

More importantly: I was not aware of the existence of 6:, which appears to be included both in k2.8 and in kona. 6: is not documented in the K2.0 manuals.

However, so far, it looks like the only kona "bug" here is that when kona does a read/write of a text file with 0: the resulting file becomes labelled "executable".