     1                                  ; ****************************************************************************
     2                                  ; cp8086.s (cp0.s) - by Erdogan Tan - 20/04/2022
     3                                  ; ----------------------------------------------------------------------------
     4                                  ; Retro UNIX 8086 v1 - copy -- cp oldfile newfile
     5                                  ;
     6                                  ; [ Last Modification: 25/04/2022 ]
     7                                  ;
     8                                  ; Derived from (original) UNIX v7 (& v7 x86) 'cp.c' source Code
     9                                  ; Ref:
    10                                  ; www.tuhs.org (https://minnie.tuhs.org)
    11                                  ; v7.tar.gz
    12                                  ; ****************************************************************************
    13                                  ; [ v7.tar - usr/src/cmd/cp.c (archive date: 10-1-1979) ]
    14                                  ;
    15                                  ; Assembler: NASM v2.15
    16                                  ; ((nasm cp8086.s -l cp8086.txt -o cp8086.bin -Z error.txt))
    17                                  ;
    18                                  ; cp1.s - 21/04/2022 - Retro UNIX 386 v1.2 (modified unix v7 inode)
    19                                  ; cp0.s - 21/04/2022 - Retro UNIX 386 v1 & v1.1
    20                                  ; cp8086.s - 22/04/2022 - Retro UNIX 8086 v1 (16 bit 'cp0.s') 
    21                                  
    22                                  ; 12/01/2022 (Retro UNIX 386 v1.2)
    23                                  ; 13/10/2015
    24                                  
    25                                  ; UNIX v1 system calls
    26                                  _rele 	equ 0
    27                                  _exit 	equ 1
    28                                  _fork 	equ 2
    29                                  _read 	equ 3
    30                                  _write	equ 4
    31                                  _open	equ 5
    32                                  _close 	equ 6
    33                                  _wait 	equ 7
    34                                  _creat 	equ 8
    35                                  _link 	equ 9
    36                                  _unlink	equ 10
    37                                  _exec	equ 11
    38                                  _chdir	equ 12
    39                                  _time 	equ 13
    40                                  _mkdir 	equ 14
    41                                  _chmod	equ 15
    42                                  _chown	equ 16
    43                                  _break	equ 17
    44                                  _stat	equ 18
    45                                  _seek	equ 19
    46                                  _tell 	equ 20
    47                                  _mount	equ 21
    48                                  _umount	equ 22
    49                                  _setuid	equ 23
    50                                  _getuid	equ 24
    51                                  _stime	equ 25
    52                                  _quit	equ 26	
    53                                  _intr	equ 27
    54                                  _fstat	equ 28
    55                                  _emt 	equ 29
    56                                  _mdate 	equ 30
    57                                  _stty 	equ 31
    58                                  _gtty	equ 32
    59                                  _ilgins	equ 33
    60                                  _sleep	equ 34 ; Retro UNIX 8086 v1 feature only !
    61                                  _msg    equ 35 ; Retro UNIX 386 v1 feature only !
    62                                  _geterr	equ 36 ; Retro UNIX 386 v1 feature only !
    63                                  ; 12/01/2022 - Retro UNIX 386 v1.2
    64                                  ; Retro UNIX 386 v2 system calls
    65                                  _setgid	equ 37
    66                                  _getgid	equ 38
    67                                  _sysver	equ 39 ; (get) Retro Unix 386 version
    68                                  
    69                                  ;;;
    70                                  ESCKey equ 1Bh
    71                                  EnterKey equ 0Dh
    72                                  
    73                                  ;%macro sys 1-4
    74                                  ;   ; 03/09/2015	
    75                                  ;   ; 13/04/2015
    76                                  ;   ; Retro UNIX 386 v1 system call.		
    77                                  ;   %if %0 >= 2   
    78                                  ;       mov ebx, %2
    79                                  ;       %if %0 >= 3    
    80                                  ;           mov ecx, %3
    81                                  ;           ;%if %0 = 4
    82                                  ;           %if	%0 >= 4 ; 11/03/2022
    83                                  ;		mov edx, %4   
    84                                  ;           %endif
    85                                  ;       %endif
    86                                  ;   %endif
    87                                  ;   mov eax, %1
    88                                  ;   int 30h	   
    89                                  ;%endmacro
    90                                  
    91                                  %macro sys 1-4
    92                                      ; Retro UNIX 8086 v1 system call.
    93                                      %if %0 >= 2   
    94                                          mov bx, %2
    95                                          %if %0 >= 3
    96                                              mov cx, %3
    97                                              %if %0 >= 4
    98                                                 mov dx, %4
    99                                              %endif
   100                                          %endif
   101                                      %endif
   102                                      mov ax, %1
   103                                      int 20h
   104                                  %endmacro
   105                                  
   106                                  ;; Retro UNIX 386 v1 system call format:
   107                                  ;; sys systemcall (eax) <arg1 (ebx)>, <arg2 (ecx)>, <arg3 (edx)>
   108                                  
   109                                  ;; 11/03/2022
   110                                  ;; Note: Above 'sys' macro has limitation about register positions;
   111                                  ;;	ebx, ecx, edx registers must not be used after their
   112                                  ;;	positions in sys macro.
   113                                  ;; for example:
   114                                  ;;	'sys _write, 1, msg, ecx' is defective, because
   115                                  ;;	 ecx will be used/assigned before edx in 'sys' macro.
   116                                  ;; correct order may be:
   117                                  ;;	'sys _write, 1, msg, eax ; (eax = byte count)
   118                                  
   119                                  ; Retro UNIX 8086 v1 system call format:
   120                                  ; sys systemcall (ax) <arg1 (bx)>, <arg2 (cx)>, <arg3 (dx)>
   121                                  
   122                                  struc stat
   123                                  	; Note: This is for Retro UNIX v1 'sysstat' output !!!
   124                                  	; (34 bytes)
   125 00000000 ????                    	.inode:  resw 1	
   126 00000002 ????                    	.mode:	 resw 1
   127 00000004 ??                      	.nlinks: resb 1
   128 00000005 ??                      	.uid:	 resb 1
   129 00000006 ????                    	.size:	 resw 1
   130 00000008 <res 10h>               	.dskptr: resw 8
   131 00000018 ????????                	.ctime:	 resd 1
   132 0000001C ????????                	.mtime:	 resd 1
   133 00000020 ????                    	.rsvd:   resw 1
   134                                  	.strucsize:
   135                                  endstruc   
   136                                  
   137                                  ;struc stat
   138                                  ;	; Note: This is for Retro UNIX v1.2 'sysstat' output !!!
   139                                  ;	; (66 bytes)
   140                                  ;	.inode:  resw 1	
   141                                  ;	.mode:	 resw 1
   142                                  ;	.nlinks: resw 1 
   143                                  ;	.uid:	 resw 1
   144                                  ;	.gid:	 resb 1
   145                                  ;	.size_h: resb 1
   146                                  ;	.size:	 resd 1
   147                                  ;	.dskptr: resd 10
   148                                  ;	.atime:	 resd 1
   149                                  ;	.mtime:	 resd 1
   150                                  ;	.ctime:  resd 1
   151                                  ;	.strucsize:
   152                                  ;endstruc   
   153                                  
   154                                  ;S_IFMT   equ 0F000h ; /* type of file */
   155                                  ;S_IFDIR  equ 04000h ; /* directory */
   156                                  ;S_IFCHR  equ 02000h ; /* character special */
   157                                  ;S_IFBLK  equ 06000h ; /* block special */
   158                                  ;S_IFREG  equ 08000h ; /* regular */
   159                                  ;S_ISUID  equ 00800h ; /* set user id on execution */
   160                                  ;S_ISGID  equ 00400h ; /* set group id on execution */
   161                                  ;S_IREAD  equ 00100h ; /* read permission, owner */
   162                                  ;S_IWRITE equ 00080h ; /* write permission, owner */
   163                                  ;S_IEXEC  equ 00040h ; /* execute/search permission, owner */
   164                                  
   165                                  ; 23/04/2022
   166                                  ; 21/04/2022 - UNIX v1 inode
   167                                  ; byte 1
   168                                  S_ALLOC  equ 080h ; Allocated flag
   169                                  S_IFDIR  equ 040h ; Directory flag
   170                                  S_IFMDF  equ 020h ; File modified flag (always on)
   171                                  S_IFLRG  equ 010h ; Large File flag
   172                                  ; byte 0
   173                                  S_ISUID  equ 020h ; Set User ID On Execution flag
   174                                  S_IEXEC  equ 010h ; Executable File flag
   175                                  S_IREAD  equ 008h ; Owner's Read Permission flag
   176                                  S_IWRITE equ 004h ; Owner's Write Permission flag
   177                                  
   178                                  BSIZE equ 512
   179                                  
   180                                  ;-----------------------------------------------------------------
   181                                  ;  text - code
   182                                  ;-----------------------------------------------------------------
   183                                  
   184                                  ;[BITS 32] ; 32-bit intructions (for 80386 protected mode)
   185                                  
   186                                  [BITS 16] ; 16-bit (x86 real mode) intructions
   187                                  
   188                                  [ORG 0] 
   189                                  
   190                                  START_CODE:
   191                                  	; 22/04/2022 - 16 bit version (Retro UNIX 8086 v1) 
   192                                  	;
   193                                  	; 20/04/2022
   194                                  	; main(argc, argv)
   195                                  
   196                                  	;mov	esi, esp		
   197                                  	;mov	edi, esi
   198                                  	;lodsd		; number of arguments
   199                                  	;;mov	edi, esi			
   200                                  	;;mov	[argc], eax
   201                                  	;mov	[argc], al
   202                                  
   203                                  	; 22/04/2022
   204 00000000 89E6                    	mov	si, sp
   205 00000002 89F7                    	mov	di, si
   206 00000004 AD                      	lodsw
   207                                  	;mov	[argc], ax
   208 00000005 A2[9201]                	mov	[argc], al
   209                                  
   210                                  	;if (argc < 3) 
   211                                  	;   goto usage;
   212                                  
   213                                  	;;cmp	eax, 3
   214                                  	;cmp	ax, 3
   215 00000008 3C03                    	cmp	al, 3
   216 0000000A 732D                    	jnb	short cp_0  ; if (argc > 3) {
   217 0000000C FEC8                    	dec	al	; 21/04/2022
   218 0000000E 7506                    	jnz	short cp_usage
   219                                  
   220                                  	;sys	_msg, program_msg, 255, 0Fh
   221                                  	; 22/04/2022
   222 00000010 B8[9301]                	mov	ax, program_msg ; asciiz message address
   223 00000013 E80B00                  	call	print_msg
   224                                  
   225                                  cp_usage:
   226                                     ; fprintf(stderr, "Usage: cp: f1 f2; or cp f1 ... fn d2\n");
   227                                  	;sys	_msg, usage_msg, 255, 07h
   228 00000016 B8[CB01]                	mov	ax, usage_msg
   229 00000019 E80500                  	call	print_msg
   230                                  cp_exit:
   231                                  	sys	_exit	; sys exit
    92                              <1> 
    93                              <1>  %if %0 >= 2
    94                              <1>  mov bx, %2
    95                              <1>  %if %0 >= 3
    96                              <1>  mov cx, %3
    97                              <1>  %if %0 >= 4
    98                              <1>  mov dx, %4
    99                              <1>  %endif
   100                              <1>  %endif
   101                              <1>  %endif
   102 0000001C B80100              <1>  mov ax, %1
   103 0000001F CD20                <1>  int 20h
   232                                  ;hlt:
   233                                  ;	nop
   234                                  ;	nop
   235                                  ;	jmp	short hlt
   236                                  
   237                                  print_msg:
   238                                  	; 22/04/2022
   239                                  	; Modified registers: ax, bx, cx, dx
   240                                  strlen:
   241                                  	; ax = asciiz string address
   242 00000021 89C3                    	mov	bx, ax
   243 00000023 4B                      	dec	bx
   244                                  nextchr:
   245 00000024 43                      	inc	bx
   246 00000025 803F00                  	cmp	byte [bx], 0
   247 00000028 77FA                    	ja	short nextchr
   248                                  	;cmp	[bx], 0Dh
   249                                  	;ja	short nextchr
   250 0000002A 29C3                    	sub	bx, ax
   251                                  	; bx = asciiz string length
   252                                  	;retn
   253                                  print_str:
   254 0000002C 89DA                    	mov	dx, bx
   255                                  	sys	_write, 1, ax
    92                              <1> 
    93                              <1>  %if %0 >= 2
    94 0000002E BB0100              <1>  mov bx, %2
    95                              <1>  %if %0 >= 3
    96 00000031 89C1                <1>  mov cx, %3
    97                              <1>  %if %0 >= 4
    98                              <1>  mov dx, %4
    99                              <1>  %endif
   100                              <1>  %endif
   101                              <1>  %endif
   102 00000033 B80400              <1>  mov ax, %1
   103 00000036 CD20                <1>  int 20h
   256                                  
   257 00000038 C3                      	retn
   258                                  
   259                                  cp_0:
   260                                  	;mov	edx, eax ; [argc]
   261                                  	;; 21/04/2022
   262                                  	;;;dec	edx  ; argc-1
   263                                  	;;dec	dl
   264                                  	;shl	dl, 2 ; * 4 
   265                                  	;add	edi, edx
   266                                  
   267                                  	; 22/04/2022
   268 00000039 89C2                    	mov	dx, ax
   269 0000003B D0E2                    	shl	dl, 1 ; * 2 ; 16 bit argument pointers
   270 0000003D 01D7                    	add	di, dx
   271                                  
   272                                  	; 21/04/2022
   273 0000003F 3C03                    	cmp	al,3
   274 00000041 7613                    	jna	short cp_1
   275                                  
   276                                  	;sys	_stat, [edi], stbuf2
   277                                  	sys	_stat, [di], stbuf2  ; 22/04/2022
    92                              <1> 
    93                              <1>  %if %0 >= 2
    94 00000043 8B1D                <1>  mov bx, %2
    95                              <1>  %if %0 >= 3
    96 00000045 B9[7602]            <1>  mov cx, %3
    97                              <1>  %if %0 >= 4
    98                              <1>  mov dx, %4
    99                              <1>  %endif
   100                              <1>  %endif
   101                              <1>  %endif
   102 00000048 B81200              <1>  mov ax, %1
   103 0000004B CD20                <1>  int 20h
   278 0000004D 72C7                    	jc	short cp_usage
   279                                  
   280                                  	;; check retro unix v2 inode flags
   281                                  	;;	(a bit different than unix v7 inode flags)
   282                                  	;; if it is a directory..
   283                                  	;;	regular file flag and dir flag must be 1
   284                                  	;mov	al, [stbuf2+stat.mode+1]
   285                                  	;and	al, S_IFDIR|S_IFREG
   286                                  	;cmp	al, S_IFDIR|S_IFREG ; directory ?
   287                                  	;jne	short cp_usage ; no
   288                                  	
   289                                  	;; check if it is a device file
   290                                  	;;test	al, S_IFREG ; regular file ?
   291                                  	;;jz	short cp_usage ; no
   292                                  	;;and	al, S_IFDIR ; directory ?
   293                                  	;;jz	short cp_usage ; no
   294                                  
   295                                  	; 21/04/2022
   296                                  	; check (unix v1 inode) directory flag
   297 0000004F F606[7902]40            	test	byte [stbuf2+stat.mode+1], S_IFDIR ; directory ?
   298 00000054 74C0                    	jz	short cp_usage ; no
   299                                   
   300                                  cp_1:	
   301                                  	; esi = esp+4 = argv[0] ; executable file name (cp)
   302                                  	;lodsd	; 21/04/2022
   303 00000056 AD                      	lodsw ; 22/04/2022 - Retro UNIX 8086 v1
   304                                  cp_loop: ; for(i=1; i<argc-1;i++)
   305                                  	;lodsd
   306                                  	; ((esi = esp+8 = argv[1] ; (old) file 1))
   307                                  
   308                                  	; esi = argv[i] 
   309                                   	; edi = argv[argc-1] ; *
   310                                  	; 22/04/2022 - Retro UNIX 8086 v1 (16 bit Retro UNIX)
   311                                  	; si = argv[i]
   312                                  	; di = argv[argc-1] ; *
   313                                   	
   314 00000057 E81D00                  	call	copy
   315 0000005A 7304                    	jnc	short cp_2
   316                                  
   317 0000005C FE06[7102]              	inc	byte [errors]
   318                                  cp_2:
   319                                  	;lodsd	; 21/04/2022
   320                                  	;cmp	esi, edi
   321                                  	;jb	short cp_loop
   322                                  
   323                                  	; 22/04/2022 - Retro UNIX 8086 v1
   324 00000060 AD                      	lodsw
   325 00000061 39FE                    	cmp	si, di
   326 00000063 72F2                    	jb	short cp_loop
   327                                  
   328                                  	; bypass 'OK.' message if there was an error
   329 00000065 803E[7102]00            	cmp	byte [errors], 0
   330 0000006A 77B0                    	ja	short cp_exit
   331                                  cp_ok:
   332                                  	;sys	_msg, ok_msg, 255, 07h
   333                                  	; 22/04/2022
   334 0000006C B8[6902]                	mov	ax, ok_msg
   335 0000006F E8AFFF                  	call	print_msg
   336                                  ;cp_exit:
   337                                  	sys	_exit	; sys exit
    92                              <1> 
    93                              <1>  %if %0 >= 2
    94                              <1>  mov bx, %2
    95                              <1>  %if %0 >= 3
    96                              <1>  mov cx, %3
    97                              <1>  %if %0 >= 4
    98                              <1>  mov dx, %4
    99                              <1>  %endif
   100                              <1>  %endif
   101                              <1>  %endif
   102 00000072 B80100              <1>  mov ax, %1
   103 00000075 CD20                <1>  int 20h
   338                                  
   339                                  ;_halt:
   340                                  ;	nop
   341                                  ;	jmp	short _halt
   342                                  
   343                                  copy:	; copy(from, to)
   344                                  	;
   345                                  	; 22/04/2022 - 16 bit version (Retro UNIX 8086 v1) 
   346                                  	; 21/04/2022
   347                                  	; 20/04/2022
   348                                  	; INPUT:
   349                                  	;	esi = pointer to file name to be copied
   350                                  	;  	edi = ptr to new file name or destination dir
   351                                  	; OUTPUT:
   352                                  	;	cf = 0 -> OK
   353                                  	;	cf = 1 -> Error !
   354                                  	;
   355                                  	; Modified registers: eax, ebx, ecx, edx, ebp
   356                                  	;
   357                                  	
   358                                  	; open (old) file for read
   359                                  	;sys	_open, [esi], 0
   360                                  	sys	_open, [si], 0 ; 22/04/2022
    92                              <1> 
    93                              <1>  %if %0 >= 2
    94 00000077 8B1C                <1>  mov bx, %2
    95                              <1>  %if %0 >= 3
    96 00000079 B90000              <1>  mov cx, %3
    97                              <1>  %if %0 >= 4
    98                              <1>  mov dx, %4
    99                              <1>  %endif
   100                              <1>  %endif
   101                              <1>  %endif
   102 0000007C B80500              <1>  mov ax, %1
   103 0000007F CD20                <1>  int 20h
   361 00000081 7313                    	jnc	short cp_3
   362                                  	
   363                                  	; esi = file name (from)
   364                                  
   365                                  	;fprintf(stderr, "cp: cannot open %s\n", from);
   366                                  	;	return(1);
   367                                  
   368                                  	;sys	_msg, cno_err_msg, 255, 07h
   369                                  	; 22/04/2022
   370 00000083 B8[F401]                	mov	ax, cno_err_msg
   371 00000086 E898FF                  	call	print_msg
   372                                  
   373                                  	; file name (from) 
   374                                  	;sys	_msg, [esi], 255, 07h
   375                                  	; 22/04/2022
   376 00000089 8B04                    	mov	ax, [si]
   377                                  write_err_nl:	; 24/04/2022
   378 0000008B E893FF                  	call	print_msg
   379                                  write_nl:
   380                                  	; new/next line
   381                                  	;sys	_msg, nextline, 255, 07h
   382                                  	; 22/04/2022
   383 0000008E B8[F101]                	mov	ax, nextline
   384 00000091 E88DFF                  	call	print_msg
   385                                  	;
   386 00000094 F9                      	stc	; return with error (cf=1)
   387 00000095 C3                      	retn
   388                                  
   389                                  cp_3:
   390                                  	;mov	[fold], eax ; file (descriptor) number
   391                                  	; 22/04/2022
   392 00000096 A3[7202]                	mov	[fold], ax ; file (descriptor) number
   393                                  
   394                                  	; (from)
   395                                  	sys	_fstat, [fold], stbuf1
    92                              <1> 
    93                              <1>  %if %0 >= 2
    94 00000099 8B1E[7202]          <1>  mov bx, %2
    95                              <1>  %if %0 >= 3
    96 0000009D B9[9802]            <1>  mov cx, %3
    97                              <1>  %if %0 >= 4
    98                              <1>  mov dx, %4
    99                              <1>  %endif
   100                              <1>  %endif
   101                              <1>  %endif
   102 000000A0 B81C00              <1>  mov ax, %1
   103 000000A3 CD20                <1>  int 20h
   396                                  
   397                                  	; save mode
   398                                  	;mov	ax, [stbuf1.mode]
   399                                  	;mov	[mode], ax
   400                                  	
   401                                  	; (to)
   402                                  	;;sys	_stat, [edi], stbuf2  ; stat(to, &stbuf2)
   403                                  	; 22/04/2022
   404                                  	;sys	_stat, [di], stbuf2
   405                                  	;jnc	short cp_4
   406                                  	;jmp	cp_9
   407                                  	; 22/04/2022
   408 000000A5 8B2D                    	mov	bp, [di]
   409                                  	sys	_stat, bp, stbuf2  ; stat(to, &stbuf2)
    92                              <1> 
    93                              <1>  %if %0 >= 2
    94 000000A7 89EB                <1>  mov bx, %2
    95                              <1>  %if %0 >= 3
    96 000000A9 B9[7602]            <1>  mov cx, %3
    97                              <1>  %if %0 >= 4
    98                              <1>  mov dx, %4
    99                              <1>  %endif
   100                              <1>  %endif
   101                              <1>  %endif
   102 000000AC B81200              <1>  mov ax, %1
   103 000000AF CD20                <1>  int 20h
   410                                  	;jnc	short cp_4
   411                                  	;jmp	cp_9
   412 000000B1 7259                    	jc	short cp_9
   413                                  cp_4:
   414                                  	; /* is target a directory? */
   415                                  
   416                                  	;; check retro unix v2 inode flags
   417                                  	;;	(a bit different than unix v7 inode flags)
   418                                  	;; regular file flag and dir flag must be 1 for a dir
   419                                  	;mov	al, [stbuf2+stat.mode+1]
   420                                  	;and	al, S_IFDIR|S_IFREG
   421                                  	;cmp	al, S_IFDIR|S_IFREG ; directory ?
   422                                  	;jne	short cp_8 ; no, overwrite (create file)
   423                                  			   ; (if the new file is not same file)
   424                                  
   425                                  	;; check if it is a device file
   426                                  	;;test	al, S_IFREG ; regular file ?
   427                                  	;;jz	short cp_error ; no
   428                                  	;;and	al, S_IFDIR ; directory ?
   429                                  	;;jz	short cp_error ; no
   430                                  
   431                                  	; 21/04/2022
   432                                  	; check (unix v1 inode) directory flag
   433 000000B3 F606[7902]40            	test	byte [stbuf2+stat.mode+1], S_IFDIR ; directory ?
   434 000000B8 7439                    	jz	short cp_8 ; no, overwrite (create file)
   435                                  			   ; (if the new file is not same file)
   436                                  
   437                                  	; add (old) file name to (destination ) path
   438                                  	; (directory name +'/'+ file name)  
   439                                  	
   440                                  	;mov	ebp, esi   ; save esi
   441                                  	;mov	esi, [edi] ; directory name address
   442                                  	;mov	ebx, edi   ; save edi			
   443                                  	;mov	edi, iobuf ; (new) path name buffer addr
   444                                  	; 22/04/2022 - Retro UNIX 8086 v1 (16 bit utility)
   445 000000BA 89F5                    	mov	bp, si
   446 000000BC 8B35                    	mov	si, [di]
   447 000000BE 89FB                    	mov	bx, di
   448 000000C0 BF[BA02]                	mov	di, iobuf
   449                                  
   450                                  	; p1 = from;
   451                                  	; p2 = to;
   452                                  	; bp = iobuf;
   453                                  cp_5:	; while(*bp++ = *p2++)
   454 000000C3 AC                      	lodsb	
   455 000000C4 AA                      	stosb
   456 000000C5 20C0                    	and	al, al
   457 000000C7 75FA                    	jnz	short cp_5
   458                                  	;mov	byte [edi-1], '/' ; bp[-1] = '/';
   459                                  	; 22/04/2022
   460 000000C9 C645FF2F                	mov	byte [di-1], '/'
   461                                  	; p2 = bp
   462                                  	;mov	edx, edi
   463                                  	;mov	esi, [ebp] ; *p1 ; from
   464                                  	; 22/04/2022
   465 000000CD 89FA                    	mov	dx, di
   466 000000CF 8B7600                  	mov	si, [bp]
   467                                  
   468                                  cp_6:	; while(*bp = *p1++)
   469 000000D2 AC                      	lodsb
   470 000000D3 AA                      	stosb
   471 000000D4 08C0                    	or	al, al
   472 000000D6 7408                    	jz	short cp_7
   473 000000D8 3C2F                    	cmp	al, '/' ; if (*bp++ == '/')
   474 000000DA 75F6                    	jne	short cp_6
   475                                  	; bp = p2
   476                                  	; 21/04/2022
   477                                  	;mov	edi, edx ; (discard path before file name)
   478                                  	; 22/04/2022
   479 000000DC 89D7                    	mov	di, dx
   480 000000DE EBF2                    	jmp	short cp_6
   481                                  cp_7:	
   482                                  	;mov	esi, ebp ; restore esi
   483                                  	;mov	edi, ebx ; restore edi
   484                                  	; 22/04/2022
   485 000000E0 89EE                    	mov	si, bp
   486 000000E2 89DF                    	mov	di, bx
   487                                  	; to = iobuf
   488                                  	;mov	ebp, iobuf
   489                                  	; 22/04/2022
   490 000000E4 BD[BA02]                	mov	bp, iobuf	
   491                                  	
   492                                  	;sys	_stat, ebp, stbuf2  ; stat(to, &stbuf2) >= 0
   493                                  	sys	_stat, bp, stbuf2 ; 22/04/2022
    92                              <1> 
    93                              <1>  %if %0 >= 2
    94 000000E7 89EB                <1>  mov bx, %2
    95                              <1>  %if %0 >= 3
    96 000000E9 B9[7602]            <1>  mov cx, %3
    97                              <1>  %if %0 >= 4
    98                              <1>  mov dx, %4
    99                              <1>  %endif
   100                              <1>  %endif
   101                              <1>  %endif
   102 000000EC B81200              <1>  mov ax, %1
   103 000000EF CD20                <1>  int 20h
   494 000000F1 7219                    	jc	short cp_10 ; create new file
   495                                  
   496                                  cp_8:
   497                                  	;if (stbuf1.st_dev == stbuf2.st_dev &&
   498                                  	;   stbuf1.st_ino == stbuf2.st_ino) {
   499                                  	;	fprintf(stderr, "cp: cannot copy file to itself.\n");
   500                                  	; 	return(1);
   501                                  
   502 000000F3 A1[9802]                	mov	ax, [stbuf1+stat.inode]
   503 000000F6 3B06[7602]              	cmp	ax, [stbuf2+stat.inode]
   504 000000FA 7510                    	jne	short cp_10
   505                                  
   506                                  	; same file ! error...
   507                                  	;mov	ebp, cncis_err_msg ; error message
   508                                  	; 22/04/2022
   509                                  	;mov	bp, cncis_err_msg ; error message 
   510                                  	; esi = file name (from)
   511                                  write_err_msg:
   512                                  	;sys	_msg, ebp, 255, 07h
   513                                  	; 22/04/2022
   514 000000FC 89E8                    	mov	ax, bp
   515                                  	; ax = error message address
   516 000000FE E820FF                  	call	print_msg
   517                                  	;
   518                                  	; close (old) file
   519                                  	sys	_close, [fold]
    92                              <1> 
    93                              <1>  %if %0 >= 2
    94 00000101 8B1E[7202]          <1>  mov bx, %2
    95                              <1>  %if %0 >= 3
    96                              <1>  mov cx, %3
    97                              <1>  %if %0 >= 4
    98                              <1>  mov dx, %4
    99                              <1>  %endif
   100                              <1>  %endif
   101                              <1>  %endif
   102 00000105 B80600              <1>  mov ax, %1
   103 00000108 CD20                <1>  int 20h
   520 0000010A F9                      	stc	; return with error (cf=1)
   521 0000010B C3                      	retn
   522                                  
   523                                  cp_9:	
   524                                  	; 22/04/2022
   525                                  	; bp = [di]
   526                                  	;; new file (asciiz name address)
   527                                  	;;mov	ebp, [edi] ; 21/04/2022
   528                                  	;mov	bp, [di] ; 22/04/2022
   529                                  
   530                                  	; create new file (truncate if it exists) 
   531                                  cp_10:
   532                                  	; fnew = creat(to, mode)
   533                                  	;movzx 	ecx, word [stbuf1+stat.mode]
   534                                  	; ecx = mode 
   535                                  	; 22/04/2022
   536 0000010C 8B0E[9A02]              	mov	cx, [stbuf1+stat.mode]
   537                                  	;sys	_creat, ebp
   538                                  	sys	_creat, bp ; 22/04/2022
    92                              <1> 
    93                              <1>  %if %0 >= 2
    94 00000110 89EB                <1>  mov bx, %2
    95                              <1>  %if %0 >= 3
    96                              <1>  mov cx, %3
    97                              <1>  %if %0 >= 4
    98                              <1>  mov dx, %4
    99                              <1>  %endif
   100                              <1>  %endif
   101                              <1>  %endif
   102 00000112 B80800              <1>  mov ax, %1
   103 00000115 CD20                <1>  int 20h
   539 00000117 7314                    	jnc	short cp_11
   540                                  
   541                                  	;if ((fnew = creat(to, mode)) < 0) {
   542                                  	;	fprintf(stderr, "cp: cannot create %s\n", to);
   543                                  	;	close(fold);
   544                                  	;	return(1);
   545                                  
   546                                  cp_10_err:
   547                                  	; 24/04/2022
   548                                  	sys	_close, [fold]
    92                              <1> 
    93                              <1>  %if %0 >= 2
    94 00000119 8B1E[7202]          <1>  mov bx, %2
    95                              <1>  %if %0 >= 3
    96                              <1>  mov cx, %3
    97                              <1>  %if %0 >= 4
    98                              <1>  mov dx, %4
    99                              <1>  %endif
   100                              <1>  %endif
   101                              <1>  %endif
   102 0000011D B80600              <1>  mov ax, %1
   103 00000120 CD20                <1>  int 20h
   549                                  
   550                                  	; error message
   551                                  	;sys	_msg, ccf_err_msg, 255, 07h
   552                                  	; 22/04/2022
   553 00000122 B8[2B02]                	mov	ax, ccf_err_msg ; 'can not create' error msg
   554 00000125 E8F9FE                  	call	print_msg
   555                                  
   556                                  	; and file name (to) -at the end of error message-
   557                                  	;sys	_msg, ebp, 255, 07h
   558                                  	; 22/04/2022
   559 00000128 89E8                    	mov	ax, bp	 ; (new) file name
   560                                  	;call	print_msg 
   561                                   
   562                                  	; write next line (move cursor to next line)
   563                                  	; and return (from this/copy subroutine)
   564                                  	;jmp	write_nl
   565 0000012A E95EFF                  	jmp	write_err_nl ; 24/04/2022
   566                                  
   567                                  cp_11:
   568                                  	; 25/04/2022
   569                                  	; temporary solution
   570                                  	; to overcome freezing/failure problem while
   571                                  	; creating a new file (which does not appear
   572                                  	; when the target file exists) 
   573                                  	;
   574                                  	; [ possible reason:
   575                                  	;        buffer flush/update problem
   576                                  	;		in retro unix 8086 v1 kernel code ]
   577                                  	; 25/04/2022
   578                                  	; close new file
   579                                  	sys	_close, ax ; flush buffers ?
    92                              <1> 
    93                              <1>  %if %0 >= 2
    94 0000012D 89C3                <1>  mov bx, %2
    95                              <1>  %if %0 >= 3
    96                              <1>  mov cx, %3
    97                              <1>  %if %0 >= 4
    98                              <1>  mov dx, %4
    99                              <1>  %endif
   100                              <1>  %endif
   101                              <1>  %endif
   102 0000012F B80600              <1>  mov ax, %1
   103 00000132 CD20                <1>  int 20h
   580                                  	; open it again 
   581                                  	sys	_open, bp, 1 ; open for write
    92                              <1> 
    93                              <1>  %if %0 >= 2
    94 00000134 89EB                <1>  mov bx, %2
    95                              <1>  %if %0 >= 3
    96 00000136 B90100              <1>  mov cx, %3
    97                              <1>  %if %0 >= 4
    98                              <1>  mov dx, %4
    99                              <1>  %endif
   100                              <1>  %endif
   101                              <1>  %endif
   102 00000139 B80500              <1>  mov ax, %1
   103 0000013C CD20                <1>  int 20h
   582 0000013E 72D9                    	jc	short cp_10_err ; nonsense !?
   583                                  	
   584                                  	; 21/04/2022 
   585                                  	;mov	[fnew], eax
   586                                  	; 22/04/2022 - Retro UNIX 8086 v1
   587 00000140 A3[7402]                	mov	[fnew], ax ; (16 bit)
   588                                  
   589                                  cp_rw_next:
   590                                  	; while(n = read(fold, iobuf, BSIZE))
   591                                  	sys	_read, [fold], iobuf, BSIZE
    92                              <1> 
    93                              <1>  %if %0 >= 2
    94 00000143 8B1E[7202]          <1>  mov bx, %2
    95                              <1>  %if %0 >= 3
    96 00000147 B9[BA02]            <1>  mov cx, %3
    97                              <1>  %if %0 >= 4
    98 0000014A BA0002              <1>  mov dx, %4
    99                              <1>  %endif
   100                              <1>  %endif
   101                              <1>  %endif
   102 0000014D B80300              <1>  mov ax, %1
   103 00000150 CD20                <1>  int 20h
   592 00000152 730E                    	jnc	short cp_12
   593                                  
   594                                  	;if (n < 0) {
   595                                  	;   fprintf(stderr, "cp: read error\n");
   596                                  
   597                                  	; write read error message
   598                                  	;mov	ebp, crd_err_msg
   599 00000154 BD[4002]                	mov	bp, crd_err_msg ; 22/04/2022	
   600                                  
   601                                  cp_rw_err:
   602                                  	; 21/04/2022
   603                                  	; cf = 1
   604                                  	sys	_close, [fnew]	
    92                              <1> 
    93                              <1>  %if %0 >= 2
    94 00000157 8B1E[7402]          <1>  mov bx, %2
    95                              <1>  %if %0 >= 3
    96                              <1>  mov cx, %3
    97                              <1>  %if %0 >= 4
    98                              <1>  mov dx, %4
    99                              <1>  %endif
   100                              <1>  %endif
   101                              <1>  %endif
   102 0000015B B80600              <1>  mov ax, %1
   103 0000015E CD20                <1>  int 20h
   605 00000160 EB9A                    	jmp	write_err_msg
   606                                  
   607                                  cp_12:
   608                                  	; eax = read count
   609                                  	; eax = 0 -> eof
   610                                  	;or	eax, eax
   611 00000162 09C0                    	or	ax, ax ; 22/04/2022 (Retro UNIX 8086 v1)
   612 00000164 7419                    	jz	short cp_14 ; eof
   613                                  
   614                                  	;mov	ebp, eax  ; n
   615 00000166 89C2                    	mov	dx, ax ; 22/04/2022
   616                                  	; write(fnew, iobuf, n)
   617                                  	;sys	_write, [fnew], iobuf, ebp
   618                                  	; 22/04/2022
   619                                  	sys	_write, [fnew], iobuf
    92                              <1> 
    93                              <1>  %if %0 >= 2
    94 00000168 8B1E[7402]          <1>  mov bx, %2
    95                              <1>  %if %0 >= 3
    96 0000016C B9[BA02]            <1>  mov cx, %3
    97                              <1>  %if %0 >= 4
    98                              <1>  mov dx, %4
    99                              <1>  %endif
   100                              <1>  %endif
   101                              <1>  %endif
   102 0000016F B80400              <1>  mov ax, %1
   103 00000172 CD20                <1>  int 20h
   620 00000174 7204                    	jc	short cp_13
   621                                  
   622                                  	;if (write(fnew, iobuf, n) != n)
   623                                  	;   fprintf(stderr, "cp: write error.\n");
   624                                  	;   close(fold);
   625                                  	;   close(fnew);
   626                                  	;   return(1);
   627                                  
   628                                  	; eax = written bytes
   629                                  	;cmp	eax, ebp
   630                                  	;;je	short cp_11 ; read next (block)
   631                                  	;; 21/04/2022
   632                                  	;je	short cp_rw_next
   633                                  	; 22/04/2022
   634 00000176 39D0                    	cmp	ax, dx
   635 00000178 74C9                    	je	short cp_rw_next		
   636                                  
   637                                  	; error !
   638                                  	;; eax < ebp --> cf = 1
   639                                  	; 22/04/2022 - Retro UNIX 8086 v1
   640                                  	; ax < dx --> cf = 1
   641                                  cp_13:
   642                                  	; close new file
   643                                  	; and then write error mesage
   644                                  	; and then close old file
   645                                  	; and then write (move cursor to) next line
   646                                  	; and then return (from subroutine) 
   647                                  	;sys	_close, [fnew]
   648                                  
   649                                  	; write error message
   650                                  	;mov	ebp, cwr_err_msg
   651                                  	; 22/04/2022
   652 0000017A BD[5402]                	mov	bp, cwr_err_msg
   653                                  	;
   654                                  	;jmp	short write_err_msg
   655                                  	; 21/04/2022
   656 0000017D EBD8                    	jmp	short cp_rw_err
   657                                  
   658                                  	; eof
   659                                  cp_14:
   660                                  	;close(fold);
   661                                  	;close(fnew);
   662                                  	;return(0);
   663                                  	
   664                                  	sys	_close, [fold]
    92                              <1> 
    93                              <1>  %if %0 >= 2
    94 0000017F 8B1E[7202]          <1>  mov bx, %2
    95                              <1>  %if %0 >= 3
    96                              <1>  mov cx, %3
    97                              <1>  %if %0 >= 4
    98                              <1>  mov dx, %4
    99                              <1>  %endif
   100                              <1>  %endif
   101                              <1>  %endif
   102 00000183 B80600              <1>  mov ax, %1
   103 00000186 CD20                <1>  int 20h
   665                                  	sys	_close, [fnew]
    92                              <1> 
    93                              <1>  %if %0 >= 2
    94 00000188 8B1E[7402]          <1>  mov bx, %2
    95                              <1>  %if %0 >= 3
    96                              <1>  mov cx, %3
    97                              <1>  %if %0 >= 4
    98                              <1>  mov dx, %4
    99                              <1>  %endif
   100                              <1>  %endif
   101                              <1>  %endif
   102 0000018C B80600              <1>  mov ax, %1
   103 0000018F CD20                <1>  int 20h
   666                                  	
   667                                  	;clc
   668 00000191 C3                      	retn
   669                                  	
   670                                  ;-----------------------------------------------------------------
   671                                  ;  data - initialized data
   672                                  ;-----------------------------------------------------------------
   673                                  
   674                                  ;argc:	dd 0
   675 00000192 00                      argc:	db 0
   676                                  
   677                                  ; ----------------------------------------------------------------
   678                                  
   679                                  program_msg:
   680 00000193 0D0A                    	db  0Dh, 0Ah
   681 00000195 526574726F20554E49-     	db  'Retro UNIX 8086 v1 COPY by Erdogan TAN - 25/04/2022'
   681 0000019E 582038303836207631-
   681 000001A7 20434F505920627920-
   681 000001B0 4572646F67616E2054-
   681 000001B9 414E202D2032352F30-
   681 000001C2 342F32303232       
   682 000001C8 0D0A00                  	db  0Dh, 0Ah, 0
   683                                  
   684                                  usage_msg:
   685 000001CB 0D0A                    	db  0Dh, 0Ah
   686 000001CD 55736167653A206370-     	db  'Usage: cp: f1 f2; or cp f1 ... fn d2'
   686 000001D6 3A2066312066323B20-
   686 000001DF 6F7220637020663120-
   686 000001E8 2E2E2E20666E206432 
   687                                  nextline:
   688 000001F1 0D0A00                  	db  0Dh, 0Ah, 0
   689                                  
   690                                  cno_err_msg:
   691 000001F4 0D0A                    	db 0Dh, 0Ah
   692 000001F6 63703A2063616E6E6F-     	db 'cp: cannot open '
   692 000001FF 74206F70656E20     
   693 00000206 00                      	db 0
   694                                  cncis_err_msg:
   695 00000207 0D0A                    	db 0Dh, 0Ah
   696 00000209 63703A2063616E6E6F-     	db 'cp: cannot copy file to itself.'
   696 00000212 7420636F7079206669-
   696 0000021B 6C6520746F20697473-
   696 00000224 656C662E           
   697 00000228 0D0A00                  	db 0Dh, 0Ah, 0
   698                                  
   699                                  ccf_err_msg:
   700 0000022B 0D0A                    	db 0Dh, 0Ah
   701 0000022D 63703A2063616E6E6F-     	db 'cp: cannot create '
   701 00000236 742063726561746520 
   702 0000023F 00                      	db 0
   703                                  
   704                                  crd_err_msg:
   705 00000240 0D0A                    	db 0Dh, 0Ah
   706 00000242 63703A207265616420-     	db 'cp: read error.'
   706 0000024B 6572726F722E       
   707 00000251 0D0A00                  	db 0Dh, 0Ah, 0
   708                                  
   709                                  cwr_err_msg:
   710 00000254 0D0A                    	db 0Dh, 0Ah
   711 00000256 63703A207772697465-     	db 'cp: write error.'
   711 0000025F 206572726F722E     
   712 00000266 0D0A00                  	db 0Dh, 0Ah, 0
   713                                  
   714                                  ok_msg:
   715 00000269 0D0A                    	db  0Dh, 0Ah
   716 0000026B 4F4B2E                  	db  'OK.'
   717 0000026E 0D0A00                  	db  0Dh, 0Ah, 0
   718                                  
   719 00000271 00                      errors:	db 0
   720                                  
   721                                  ;-----------------------------------------------------------------
   722                                  ;  bss - uninitialized data
   723                                  ;-----------------------------------------------------------------
   724                                  
   725                                  align 2
   726                                  
   727                                  bss_start:
   728                                  
   729                                  ABSOLUTE bss_start
   730                                  
   731                                  ; 20/04/2022 - Retro UNIX 386 v1 & v1.1 & v1.2
   732                                  ;fold:	resd 1
   733                                  ;fnew:	resd 1
   734                                  ; 22/04/2022 - Retro UNIX 8086 v1
   735 00000272 ????                    fold:	resw 1
   736 00000274 ????                    fnew:	resw 1 
   737                                  
   738                                  ;stbuf2: resb 66 ; for Retro UNIX 386 v1.2 (66 byte sysstat data)
   739                                  ;stbuf1: resb 66 ; for Retro UNIX 386 v1.2 (66 byte sysstat data)
   740                                  ; 21/04/2022
   741 00000276 <res 22h>               stbuf2: resb 34 ; for Retro UNIX 386 v1.1 (34 byte sysstat data)
   742 00000298 <res 22h>               stbuf1: resb 34 ; for Retro UNIX 386 v1.1 (34 byte sysstat data)
   743                                  
   744 000002BA <res 200h>              iobuf:	resb BSIZE ; resb 512 ; path name buffer
   745                                  
   746                                  ; 20/04/2022
   747                                  ;-----------------------------------------------------------------
   748                                  ; Original UNIX v7 - cp (utility) c source code (cp.c)
   749                                  ;-----------------------------------------------------------------
   750                                  ;/* UNIX V7 source code: see www.tuhs.org for details. */;
   751                                  ;
   752                                  ;/*
   753                                  ; * cp oldfile newfile
   754                                  ; */
   755                                  ;
   756                                  ;#define BSIZE	512
   757                                  ;#include <stdio.h>
   758                                  ;#include <sys/types.h>
   759                                  ;#include <sys/stat.h>
   760                                  ;struct	stat stbuf1, stbuf2;
   761                                  ;char iobuf[BSIZE];
   762                                  ;
   763                                  ;main(argc, argv)
   764                                  ;char *argv[];
   765                                  ;{
   766                                  ;	register i, r;
   767                                  ;
   768                                  ;	if (argc < 3) 
   769                                  ;		goto usage;
   770                                  ;	if (argc > 3) {
   771                                  ;		if (stat(argv[argc-1], &stbuf2) < 0)
   772                                  ;			goto usage;
   773                                  ;		if ((stbuf2.st_mode&S_IFMT) != S_IFDIR) 
   774                                  ;			goto usage;
   775                                  ;	}
   776                                  ;	r = 0;
   777                                  ;	for(i=1; i<argc-1;i++)
   778                                  ;		r |= copy(argv[i], argv[argc-1]);
   779                                  ;	exit(r);
   780                                  ;usage:
   781                                  ;	fprintf(stderr, "Usage: cp: f1 f2; or cp f1 ... fn d2\n");
   782                                  ;	exit(1);
   783                                  ;}
   784                                  ;
   785                                  ;copy(from, to)
   786                                  ;char *from, *to;
   787                                  ;{
   788                                  ;	int fold, fnew, n;
   789                                  ;	register char *p1, *p2, *bp;
   790                                  ;	int mode;
   791                                  ;	if ((fold = open(from, 0)) < 0) {
   792                                  ;		fprintf(stderr, "cp: cannot open %s\n", from);
   793                                  ;		return(1);
   794                                  ;	}
   795                                  ;	fstat(fold, &stbuf1);
   796                                  ;	mode = stbuf1.st_mode;
   797                                  ;	/* is target a directory? */
   798                                  ;	if (stat(to, &stbuf2) >=0 &&
   799                                  ;	   (stbuf2.st_mode&S_IFMT) == S_IFDIR) {
   800                                  ;		p1 = from;
   801                                  ;		p2 = to;
   802                                  ;		bp = iobuf;
   803                                  ;		while(*bp++ = *p2++)
   804                                  ;			;
   805                                  ;		bp[-1] = '/';
   806                                  ;		p2 = bp;
   807                                  ;		while(*bp = *p1++)
   808                                  ;			if (*bp++ == '/')
   809                                  ;				bp = p2;
   810                                  ;		to = iobuf;
   811                                  ;	}
   812                                  ;	if (stat(to, &stbuf2) >= 0) {
   813                                  ;		if (stbuf1.st_dev == stbuf2.st_dev &&
   814                                  ;		   stbuf1.st_ino == stbuf2.st_ino) {
   815                                  ;			fprintf(stderr, "cp: cannot copy file to itself.\n");
   816                                  ;			return(1);
   817                                  ;		}
   818                                  ;	}
   819                                  ;	if ((fnew = creat(to, mode)) < 0) {
   820                                  ;		fprintf(stderr, "cp: cannot create %s\n", to);
   821                                  ;		close(fold);
   822                                  ;		return(1);
   823                                  ;	}
   824                                  ;	while(n = read(fold,  iobuf,  BSIZE)) {
   825                                  ;		if (n < 0) {
   826                                  ;			fprintf(stderr, "cp: read error\n");
   827                                  ;			close(fold);
   828                                  ;			close(fnew);
   829                                  ;			return(1);
   830                                  ;		} else
   831                                  ;			if (write(fnew, iobuf, n) != n) {
   832                                  ;				fprintf(stderr, "cp: write error.\n");
   833                                  ;				close(fold);
   834                                  ;				close(fnew);
   835                                  ;				return(1);
   836                                  ;			}
   837                                  ;	}
   838                                  ;	close(fold);
   839                                  ;	close(fnew);
   840                                  ;	return(0);
   841                                  ;}
