     1                                  ; ****************************************************************************
     2                                  ; cp386.s (cp0.s) - by Erdogan Tan - 20/04/2022
     3                                  ; ----------------------------------------------------------------------------
     4                                  ; Retro UNIX 386 v1 - copy -- cp oldfile newfile
     5                                  ;
     6                                  ; [ Last Modification: 22/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 cp0.s -l cp0.txt -o cp0 -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                                  
    21                                  ; 12/01/2022 (Retro UNIX 386 v1.2)
    22                                  ; 13/10/2015
    23                                  
    24                                  ; UNIX v1 system calls
    25                                  _rele 	equ 0
    26                                  _exit 	equ 1
    27                                  _fork 	equ 2
    28                                  _read 	equ 3
    29                                  _write	equ 4
    30                                  _open	equ 5
    31                                  _close 	equ 6
    32                                  _wait 	equ 7
    33                                  _creat 	equ 8
    34                                  _link 	equ 9
    35                                  _unlink	equ 10
    36                                  _exec	equ 11
    37                                  _chdir	equ 12
    38                                  _time 	equ 13
    39                                  _mkdir 	equ 14
    40                                  _chmod	equ 15
    41                                  _chown	equ 16
    42                                  _break	equ 17
    43                                  _stat	equ 18
    44                                  _seek	equ 19
    45                                  _tell 	equ 20
    46                                  _mount	equ 21
    47                                  _umount	equ 22
    48                                  _setuid	equ 23
    49                                  _getuid	equ 24
    50                                  _stime	equ 25
    51                                  _quit	equ 26	
    52                                  _intr	equ 27
    53                                  _fstat	equ 28
    54                                  _emt 	equ 29
    55                                  _mdate 	equ 30
    56                                  _stty 	equ 31
    57                                  _gtty	equ 32
    58                                  _ilgins	equ 33
    59                                  _sleep	equ 34 ; Retro UNIX 8086 v1 feature only !
    60                                  _msg    equ 35 ; Retro UNIX 386 v1 feature only !
    61                                  _sleep	equ 34 ; Retro UNIX 8086 v1 feature only !
    62                                  _msg	equ 35 ; Retro UNIX 386 v1 feature only !
    63                                  _geterr	equ 36 ; Retro UNIX 386 v1 feature only !
    64                                  ; 12/01/2022 - Retro UNIX 386 v1.2
    65                                  ; Retro UNIX 386 v2 system calls
    66                                  _setgid	equ 37
    67                                  _getgid	equ 38
    68                                  _sysver	equ 39 ; (get) Retro Unix 386 version
    69                                  
    70                                  ;;;
    71                                  ESCKey equ 1Bh
    72                                  EnterKey equ 0Dh
    73                                  
    74                                  %macro sys 1-4
    75                                      ; 03/09/2015	
    76                                      ; 13/04/2015
    77                                      ; Retro UNIX 386 v1 system call.		
    78                                      %if %0 >= 2   
    79                                          mov ebx, %2
    80                                          %if %0 >= 3    
    81                                              mov ecx, %3
    82                                              ;%if %0 = 4
    83                                              %if	%0 >= 4 ; 11/03/2022
    84                                  		mov edx, %4   
    85                                              %endif
    86                                          %endif
    87                                      %endif
    88                                      mov eax, %1
    89                                      int 30h	   
    90                                  %endmacro
    91                                  
    92                                  ; Retro UNIX 386 v1 system call format:
    93                                  ; sys systemcall (eax) <arg1 (ebx)>, <arg2 (ecx)>, <arg3 (edx)>
    94                                  
    95                                  ; 11/03/2022
    96                                  ; Note: Above 'sys' macro has limitation about register positions;
    97                                  ;	ebx, ecx, edx registers must not be used after their
    98                                  ;	positions in sys macro.
    99                                  ; for example:
   100                                  ;	'sys _write, 1, msg, ecx' is defective, because
   101                                  ;	 ecx will be used/assigned before edx in 'sys' macro.
   102                                  ; correct order may be:
   103                                  ;	'sys _write, 1, msg, eax ; (eax = byte count)
   104                                  
   105                                  struc stat
   106                                  	; Note: This is for Retro UNIX v1.1 'sysstat' output !!!
   107                                  	; (34 bytes)
   108 00000000 ????                    	.inode:  resw 1	
   109 00000002 ????                    	.mode:	 resw 1
   110 00000004 ??                      	.nlinks: resb 1
   111 00000005 ??                      	.uid:	 resb 1
   112 00000006 ????                    	.size:	 resw 1
   113 00000008 <res 10h>               	.dskptr: resw 8
   114 00000018 ????????                	.ctime:	 resd 1
   115 0000001C ????????                	.mtime:	 resd 1
   116 00000020 ????                    	.rsvd:   resw 1
   117                                  	.strucsize:
   118                                  endstruc   
   119                                  
   120                                  ;struc stat
   121                                  ;	; Note: This is for Retro UNIX v1.2 'sysstat' output !!!
   122                                  ;	; (66 bytes)
   123                                  ;	.inode:  resw 1	
   124                                  ;	.mode:	 resw 1
   125                                  ;	.nlinks: resw 1 
   126                                  ;	.uid:	 resw 1
   127                                  ;	.gid:	 resb 1
   128                                  ;	.size_h: resb 1
   129                                  ;	.size:	 resd 1
   130                                  ;	.dskptr: resd 10
   131                                  ;	.atime:	 resd 1
   132                                  ;	.mtime:	 resd 1
   133                                  ;	.ctime:  resd 1
   134                                  ;	.strucsize:
   135                                  ;endstruc   
   136                                  
   137                                  ;S_IFMT   equ 0F000h ; /* type of file */
   138                                  ;S_IFDIR  equ 04000h ; /* directory */
   139                                  ;S_IFCHR  equ 02000h ; /* character special */
   140                                  ;S_IFBLK  equ 06000h ; /* block special */
   141                                  ;S_IFREG  equ 08000h ; /* regular */
   142                                  ;S_ISUID  equ 00800h ; /* set user id on execution */
   143                                  ;S_ISGID  equ 00400h ; /* set group id on execution */
   144                                  ;S_IREAD  equ 00100h ; /* read permission, owner */
   145                                  ;S_IWRITE equ 00080h ; /* write permission, owner */
   146                                  ;S_IEXEC  equ 00040h ; /* execute/search permission, owner */
   147                                  
   148                                  ; 21/04/2022 - UNIX v1 inode
   149                                  ; byte 1
   150                                  S_ALLOC  equ 080h ; Allocated flag
   151                                  S_IFDIR  equ 040h ; Directory flag
   152                                  S_IFCHR  equ 020h ; File modified flag (always on)
   153                                  S_IFBLK  equ 010h ; Large File flag
   154                                  ; byte 0
   155                                  S_ISUID  equ 020h ; Set User ID On Execution flag
   156                                  S_IEXEC  equ 010h ; Executable File flag
   157                                  S_IREAD  equ 008h ; Owner's Read Permission flag
   158                                  S_IWRITE equ 004h ; Owner's Write Permission flag
   159                                  
   160                                  BSIZE equ 512
   161                                  
   162                                  ;-----------------------------------------------------------------
   163                                  ;  text - code
   164                                  ;-----------------------------------------------------------------
   165                                  
   166                                  [BITS 32] ; 32-bit intructions (for 80386 protected mode)
   167                                  
   168                                  [ORG 0] 
   169                                  
   170                                  START_CODE:
   171                                  	; 20/04/2022
   172                                  	; main(argc, argv)
   173                                  
   174 00000000 89E6                    	mov	esi, esp		
   175 00000002 89F7                    	mov	edi, esi
   176 00000004 AD                      	lodsd		; number of arguments
   177                                  	;mov	edi, esi			
   178                                  	;mov	[argc], eax
   179 00000005 A2[58020000]            	mov	[argc], al
   180                                  
   181                                  	;if (argc < 3) 
   182                                  	;   goto usage;
   183                                  
   184                                  	;cmp	eax, 3
   185 0000000A 3C03                    	cmp	al, 3
   186 0000000C 7337                    	jnb	short cp_0  ; if (argc > 3) {
   187 0000000E FEC8                    	dec	al	; 21/04/2022
   188 00000010 7516                    	jnz	short cp_usage
   189                                  	sys	_msg, program_msg, 255, 0Fh
    75                              <1> 
    76                              <1> 
    77                              <1> 
    78                              <1>  %if %0 >= 2
    79 00000012 BB[59020000]        <1>  mov ebx, %2
    80                              <1>  %if %0 >= 3
    81 00000017 B9FF000000          <1>  mov ecx, %3
    82                              <1> 
    83                              <1>  %if %0 >= 4
    84 0000001C BA0F000000          <1>  mov edx, %4
    85                              <1>  %endif
    86                              <1>  %endif
    87                              <1>  %endif
    88 00000021 B823000000          <1>  mov eax, %1
    89 00000026 CD30                <1>  int 30h
   190                                  cp_usage:
   191                                     ; fprintf(stderr, "Usage: cp: f1 f2; or cp f1 ... fn d2\n");
   192                                  	sys	_msg, usage_msg, 255, 07h
    75                              <1> 
    76                              <1> 
    77                              <1> 
    78                              <1>  %if %0 >= 2
    79 00000028 BB[90020000]        <1>  mov ebx, %2
    80                              <1>  %if %0 >= 3
    81 0000002D B9FF000000          <1>  mov ecx, %3
    82                              <1> 
    83                              <1>  %if %0 >= 4
    84 00000032 BA07000000          <1>  mov edx, %4
    85                              <1>  %endif
    86                              <1>  %endif
    87                              <1>  %endif
    88 00000037 B823000000          <1>  mov eax, %1
    89 0000003C CD30                <1>  int 30h
   193                                  cp_exit:
   194                                  	sys	_exit	; sys exit
    75                              <1> 
    76                              <1> 
    77                              <1> 
    78                              <1>  %if %0 >= 2
    79                              <1>  mov ebx, %2
    80                              <1>  %if %0 >= 3
    81                              <1>  mov ecx, %3
    82                              <1> 
    83                              <1>  %if %0 >= 4
    84                              <1>  mov edx, %4
    85                              <1>  %endif
    86                              <1>  %endif
    87                              <1>  %endif
    88 0000003E B801000000          <1>  mov eax, %1
    89 00000043 CD30                <1>  int 30h
   195                                  ;hlt:
   196                                  ;	nop
   197                                  ;	nop
   198                                  ;	jmp	short hlt
   199                                  
   200                                  cp_0:
   201 00000045 89C2                    	mov	edx, eax ; [argc]
   202                                  	; 21/04/2022
   203                                  	;;dec	edx  ; argc-1
   204                                  	;dec	dl
   205 00000047 C0E202                  	shl	dl, 2 ; * 4 
   206 0000004A 01D7                    	add	edi, edx
   207                                  
   208                                  	; 21/04/2022
   209 0000004C 3C03                    	cmp	al,3
   210 0000004E 7619                    	jna	short cp_1
   211                                  
   212                                  	sys	_stat, [edi], stbuf2
    75                              <1> 
    76                              <1> 
    77                              <1> 
    78                              <1>  %if %0 >= 2
    79 00000050 8B1F                <1>  mov ebx, %2
    80                              <1>  %if %0 >= 3
    81 00000052 B9[40030000]        <1>  mov ecx, %3
    82                              <1> 
    83                              <1>  %if %0 >= 4
    84                              <1>  mov edx, %4
    85                              <1>  %endif
    86                              <1>  %endif
    87                              <1>  %endif
    88 00000057 B812000000          <1>  mov eax, %1
    89 0000005C CD30                <1>  int 30h
   213 0000005E 72C8                    	jc	short cp_usage
   214                                  
   215                                  	;; check retro unix v2 inode flags
   216                                  	;;	(a bit different than unix v7 inode flags)
   217                                  	;; if it is a directory..
   218                                  	;;	regular file flag and dir flag must be 1
   219                                  	;mov	al, [stbuf2+stat.mode+1]
   220                                  	;and	al, S_IFDIR|S_IFREG
   221                                  	;cmp	al, S_IFDIR|S_IFREG ; directory ?
   222                                  	;jne	short cp_usage ; no
   223                                  	
   224                                  	;; check if it is a device file
   225                                  	;;test	al, S_IFREG ; regular file ?
   226                                  	;;jz	short cp_usage ; no
   227                                  	;;and	al, S_IFDIR ; directory ?
   228                                  	;;jz	short cp_usage ; no
   229                                  
   230                                  	; 21/04/2022
   231                                  	; check (unix v1 inode) directory flag
   232 00000060 F605[43030000]40        	test	byte [stbuf2+stat.mode+1], S_IFDIR ; directory ?
   233 00000067 74BF                    	jz	short cp_usage ; no
   234                                   
   235                                  cp_1:	
   236                                  	; esi = esp+4 = argv[0] ; executable file name (cp)
   237 00000069 AD                      	lodsd	; 21/04/2022
   238                                  cp_loop: ; for(i=1; i<argc-1;i++)
   239                                  	;lodsd
   240                                  	; ((esi = esp+8 = argv[1] ; (old) file 1))
   241                                  	; esi = argv[i] 
   242                                   	; edi = argv[argc-1] ; *
   243 0000006A E833000000              	call	copy
   244 0000006F 7306                    	jnc	short cp_2
   245                                  
   246 00000071 FE05[36030000]          	inc	byte [errors]
   247                                  cp_2:
   248 00000077 AD                      	lodsd	; 21/04/2022
   249 00000078 39FE                    	cmp	esi, edi
   250 0000007A 72EE                    	jb	short cp_loop
   251                                  
   252                                  	; bypass 'OK.' message if there was an error
   253 0000007C 803D[36030000]00        	cmp	byte [errors], 0
   254 00000083 77B9                    	ja	short cp_exit
   255                                  cp_ok:
   256                                  	sys	_msg, ok_msg, 255, 07h
    75                              <1> 
    76                              <1> 
    77                              <1> 
    78                              <1>  %if %0 >= 2
    79 00000085 BB[2E030000]        <1>  mov ebx, %2
    80                              <1>  %if %0 >= 3
    81 0000008A B9FF000000          <1>  mov ecx, %3
    82                              <1> 
    83                              <1>  %if %0 >= 4
    84 0000008F BA07000000          <1>  mov edx, %4
    85                              <1>  %endif
    86                              <1>  %endif
    87                              <1>  %endif
    88 00000094 B823000000          <1>  mov eax, %1
    89 00000099 CD30                <1>  int 30h
   257                                  ;cp_exit:
   258                                  	sys	_exit	; sys exit
    75                              <1> 
    76                              <1> 
    77                              <1> 
    78                              <1>  %if %0 >= 2
    79                              <1>  mov ebx, %2
    80                              <1>  %if %0 >= 3
    81                              <1>  mov ecx, %3
    82                              <1> 
    83                              <1>  %if %0 >= 4
    84                              <1>  mov edx, %4
    85                              <1>  %endif
    86                              <1>  %endif
    87                              <1>  %endif
    88 0000009B B801000000          <1>  mov eax, %1
    89 000000A0 CD30                <1>  int 30h
   259                                  
   260                                  ;_halt:
   261                                  ;	nop
   262                                  ;	jmp	short _halt
   263                                  
   264                                  copy:	; copy(from, to)
   265                                  	;
   266                                  	; 21/04/2022
   267                                  	; 20/04/2022
   268                                  	; INPUT:
   269                                  	;	esi = pointer to file name to be copied
   270                                  	;  	edi = ptr to new file name or destination dir
   271                                  	; OUTPUT:
   272                                  	;	cf = 0 -> OK
   273                                  	;	cf = 1 -> Error !
   274                                  	;
   275                                  	; Modified registers: eax, ebx, ecx, edx, ebp
   276                                  	;
   277                                  	
   278                                  	; open (old) file for read
   279                                  	sys	_open, [esi], 0 
    75                              <1> 
    76                              <1> 
    77                              <1> 
    78                              <1>  %if %0 >= 2
    79 000000A2 8B1E                <1>  mov ebx, %2
    80                              <1>  %if %0 >= 3
    81 000000A4 B900000000          <1>  mov ecx, %3
    82                              <1> 
    83                              <1>  %if %0 >= 4
    84                              <1>  mov edx, %4
    85                              <1>  %endif
    86                              <1>  %endif
    87                              <1>  %endif
    88 000000A9 B805000000          <1>  mov eax, %1
    89 000000AE CD30                <1>  int 30h
   280 000000B0 7341                    	jnc	short cp_3
   281                                  	
   282                                  	; esi = file name (from)
   283                                  
   284                                  	;fprintf(stderr, "cp: cannot open %s\n", from);
   285                                  	;	return(1);
   286                                  
   287                                  	sys	_msg, cno_err_msg, 255, 07h
    75                              <1> 
    76                              <1> 
    77                              <1> 
    78                              <1>  %if %0 >= 2
    79 000000B2 BB[B9020000]        <1>  mov ebx, %2
    80                              <1>  %if %0 >= 3
    81 000000B7 B9FF000000          <1>  mov ecx, %3
    82                              <1> 
    83                              <1>  %if %0 >= 4
    84 000000BC BA07000000          <1>  mov edx, %4
    85                              <1>  %endif
    86                              <1>  %endif
    87                              <1>  %endif
    88 000000C1 B823000000          <1>  mov eax, %1
    89 000000C6 CD30                <1>  int 30h
   288                                  	; file name (from) 
   289                                  	sys	_msg, [esi], 255, 07h
    75                              <1> 
    76                              <1> 
    77                              <1> 
    78                              <1>  %if %0 >= 2
    79 000000C8 8B1E                <1>  mov ebx, %2
    80                              <1>  %if %0 >= 3
    81 000000CA B9FF000000          <1>  mov ecx, %3
    82                              <1> 
    83                              <1>  %if %0 >= 4
    84 000000CF BA07000000          <1>  mov edx, %4
    85                              <1>  %endif
    86                              <1>  %endif
    87                              <1>  %endif
    88 000000D4 B823000000          <1>  mov eax, %1
    89 000000D9 CD30                <1>  int 30h
   290                                  write_nl:
   291                                  	; new/next line
   292                                  	sys	_msg, nextline, 255, 07h
    75                              <1> 
    76                              <1> 
    77                              <1> 
    78                              <1>  %if %0 >= 2
    79 000000DB BB[B6020000]        <1>  mov ebx, %2
    80                              <1>  %if %0 >= 3
    81 000000E0 B9FF000000          <1>  mov ecx, %3
    82                              <1> 
    83                              <1>  %if %0 >= 4
    84 000000E5 BA07000000          <1>  mov edx, %4
    85                              <1>  %endif
    86                              <1>  %endif
    87                              <1>  %endif
    88 000000EA B823000000          <1>  mov eax, %1
    89 000000EF CD30                <1>  int 30h
   293 000000F1 F9                      	stc	; return with error (cf=1)
   294 000000F2 C3                      	retn
   295                                  
   296                                  cp_3:
   297 000000F3 A3[38030000]            	mov	[fold], eax ; file (descriptor) number
   298                                  
   299                                  	; (from)
   300                                  	sys	_fstat, [fold], stbuf1
    75                              <1> 
    76                              <1> 
    77                              <1> 
    78                              <1>  %if %0 >= 2
    79 000000F8 8B1D[38030000]      <1>  mov ebx, %2
    80                              <1>  %if %0 >= 3
    81 000000FE B9[62030000]        <1>  mov ecx, %3
    82                              <1> 
    83                              <1>  %if %0 >= 4
    84                              <1>  mov edx, %4
    85                              <1>  %endif
    86                              <1>  %endif
    87                              <1>  %endif
    88 00000103 B81C000000          <1>  mov eax, %1
    89 00000108 CD30                <1>  int 30h
   301                                  
   302                                  	; save mode
   303                                  	;mov	ax, [stbuf1.mode]
   304                                  	;mov	[mode], ax
   305                                  
   306                                  	; (to)
   307                                  	;sys	_stat, [edi], stbuf2  ; stat(to, &stbuf2)
   308                                  	;jnc	short cp_4
   309                                  	;jmp	cp_9
   310                                  	
   311                                  	; 22/04/2022
   312 0000010A 8B2F                    	mov	ebp, [edi] ; ! (cp_8) !
   313                                  	sys	_stat, ebp, stbuf2  ; stat(to, &stbuf2)
    75                              <1> 
    76                              <1> 
    77                              <1> 
    78                              <1>  %if %0 >= 2
    79 0000010C 89EB                <1>  mov ebx, %2
    80                              <1>  %if %0 >= 3
    81 0000010E B9[40030000]        <1>  mov ecx, %3
    82                              <1> 
    83                              <1>  %if %0 >= 4
    84                              <1>  mov edx, %4
    85                              <1>  %endif
    86                              <1>  %endif
    87                              <1>  %endif
    88 00000113 B812000000          <1>  mov eax, %1
    89 00000118 CD30                <1>  int 30h
   314 0000011A 7305                    	jnc	short cp_4
   315 0000011C E980000000              	jmp	cp_9
   316                                  
   317                                  cp_4:
   318                                  	; /* is target a directory? */
   319                                  
   320                                  	;; check retro unix v2 inode flags
   321                                  	;;	(a bit different than unix v7 inode flags)
   322                                  	;; regular file flag and dir flag must be 1 for a dir
   323                                  	;mov	al, [stbuf2+stat.mode+1]
   324                                  	;and	al, S_IFDIR|S_IFREG
   325                                  	;cmp	al, S_IFDIR|S_IFREG ; directory ?
   326                                  	;jne	short cp_8 ; no, overwrite (create file)
   327                                  			   ; (if the new file is not same file)
   328                                  
   329                                  	;; check if it is a device file
   330                                  	;;test	al, S_IFREG ; regular file ?
   331                                  	;;jz	short cp_error ; no
   332                                  	;;and	al, S_IFDIR ; directory ?
   333                                  	;;jz	short cp_error ; no
   334                                  
   335                                  	; 21/04/2022
   336                                  	; check (unix v1 inode) directory flag
   337 00000121 F605[43030000]40        	test	byte [stbuf2+stat.mode+1], S_IFDIR ; directory ?
   338 00000128 7441                    	jz	short cp_8 ; no, overwrite (create file)
   339                                  			   ; (if the new file is not same file)
   340                                  
   341                                  	; add (old) file name to (destination ) path
   342                                  	; (directory name +'/'+ file name)  
   343                                  	
   344 0000012A 89F5                    	mov	ebp, esi   ; save esi
   345 0000012C 8B37                    	mov	esi, [edi] ; directory name address
   346 0000012E 89FB                    	mov	ebx, edi   ; save edi			
   347 00000130 BF[84030000]            	mov	edi, iobuf ; (new) path name buffer addr	
   348                                  
   349                                  	; p1 = from;
   350                                  	; p2 = to;
   351                                  	; bp = iobuf;
   352                                  cp_5:	; while(*bp++ = *p2++)
   353 00000135 AC                      	lodsb	
   354 00000136 AA                      	stosb
   355 00000137 20C0                    	and	al, al
   356 00000139 75FA                    	jnz	short cp_5
   357 0000013B C647FF2F                	mov	byte [edi-1], '/' ; bp[-1] = '/';
   358                                  	; p2 = bp
   359 0000013F 89FA                    	mov	edx, edi
   360 00000141 8B7500                  	mov	esi, [ebp] ; *p1 ; from
   361                                  cp_6:	; while(*bp = *p1++)
   362 00000144 AC                      	lodsb
   363 00000145 AA                      	stosb
   364 00000146 08C0                    	or	al, al
   365 00000148 7408                    	jz	short cp_7
   366 0000014A 3C2F                    	cmp	al, '/' ; if (*bp++ == '/')
   367 0000014C 75F6                    	jne	short cp_6
   368                                  	; bp = p2
   369                                  	; 21/04/2022
   370 0000014E 89D7                    	mov	edi, edx ; (discard path before file name)
   371 00000150 EBF2                    	jmp	short cp_6
   372                                  cp_7:	
   373 00000152 89EE                    	mov	esi, ebp ; restore esi
   374 00000154 89DF                    	mov	edi, ebx ; restore edi
   375                                  	; to = iobuf
   376 00000156 BD[84030000]            	mov	ebp, iobuf
   377                                  		
   378                                  	sys	_stat, ebp, stbuf2  ; stat(to, &stbuf2) >= 0
    75                              <1> 
    76                              <1> 
    77                              <1> 
    78                              <1>  %if %0 >= 2
    79 0000015B 89EB                <1>  mov ebx, %2
    80                              <1>  %if %0 >= 3
    81 0000015D B9[40030000]        <1>  mov ecx, %3
    82                              <1> 
    83                              <1>  %if %0 >= 4
    84                              <1>  mov edx, %4
    85                              <1>  %endif
    86                              <1>  %endif
    87                              <1>  %endif
    88 00000162 B812000000          <1>  mov eax, %1
    89 00000167 CD30                <1>  int 30h
   379 00000169 7236                    	jc	short cp_10 ; create new file
   380                                  
   381                                  cp_8:
   382                                  	;if (stbuf1.st_dev == stbuf2.st_dev &&
   383                                  	;   stbuf1.st_ino == stbuf2.st_ino) {
   384                                  	;	fprintf(stderr, "cp: cannot copy file to itself.\n");
   385                                  	; 	return(1);
   386                                  
   387 0000016B 66A1[62030000]          	mov	ax, [stbuf1+stat.inode]
   388 00000171 663B05[40030000]        	cmp	ax, [stbuf2+stat.inode]
   389 00000178 7527                    	jne	short cp_10
   390                                  
   391                                  	; same file ! error...
   392 0000017A BD[CC020000]            	mov	ebp, cncis_err_msg ; error message
   393                                  	; esi = file name (from)
   394                                  write_err_msg:
   395                                  	sys	_msg, ebp, 255, 07h
    75                              <1> 
    76                              <1> 
    77                              <1> 
    78                              <1>  %if %0 >= 2
    79 0000017F 89EB                <1>  mov ebx, %2
    80                              <1>  %if %0 >= 3
    81 00000181 B9FF000000          <1>  mov ecx, %3
    82                              <1> 
    83                              <1>  %if %0 >= 4
    84 00000186 BA07000000          <1>  mov edx, %4
    85                              <1>  %endif
    86                              <1>  %endif
    87                              <1>  %endif
    88 0000018B B823000000          <1>  mov eax, %1
    89 00000190 CD30                <1>  int 30h
   396                                  	; close (old) file
   397                                  	sys	_close, [fold]
    75                              <1> 
    76                              <1> 
    77                              <1> 
    78                              <1>  %if %0 >= 2
    79 00000192 8B1D[38030000]      <1>  mov ebx, %2
    80                              <1>  %if %0 >= 3
    81                              <1>  mov ecx, %3
    82                              <1> 
    83                              <1>  %if %0 >= 4
    84                              <1>  mov edx, %4
    85                              <1>  %endif
    86                              <1>  %endif
    87                              <1>  %endif
    88 00000198 B806000000          <1>  mov eax, %1
    89 0000019D CD30                <1>  int 30h
   398 0000019F F9                      	stc	; return with error (cf=1)
   399 000001A0 C3                      	retn
   400                                  
   401                                  cp_9:
   402                                  	; 22/04/2022
   403                                  	; ebp = [edi]
   404                                  	; 21/04/2022
   405                                  	; new file (asciiz name address)
   406                                  	;mov	ebp, [edi]
   407                                  
   408                                  	; create new file (truncate if it exists) 
   409                                  cp_10:
   410                                  	; fnew = creat(to, mode)
   411 000001A1 0FB70D[64030000]        	movzx 	ecx, word [stbuf1+stat.mode]
   412                                  	; ecx = mode 
   413                                  	sys	_creat, ebp 
    75                              <1> 
    76                              <1> 
    77                              <1> 
    78                              <1>  %if %0 >= 2
    79 000001A8 89EB                <1>  mov ebx, %2
    80                              <1>  %if %0 >= 3
    81                              <1>  mov ecx, %3
    82                              <1> 
    83                              <1>  %if %0 >= 4
    84                              <1>  mov edx, %4
    85                              <1>  %endif
    86                              <1>  %endif
    87                              <1>  %endif
    88 000001AA B808000000          <1>  mov eax, %1
    89 000001AF CD30                <1>  int 30h
   414 000001B1 732E                    	jnc	short cp_11
   415                                  
   416                                  	;if ((fnew = creat(to, mode)) < 0) {
   417                                  	;	fprintf(stderr, "cp: cannot create %s\n", to);
   418                                  	;	close(fold);
   419                                  	;	return(1);
   420                                  
   421                                  	; error message
   422                                  	sys	_msg, ccf_err_msg, 255, 07h
    75                              <1> 
    76                              <1> 
    77                              <1> 
    78                              <1>  %if %0 >= 2
    79 000001B3 BB[F0020000]        <1>  mov ebx, %2
    80                              <1>  %if %0 >= 3
    81 000001B8 B9FF000000          <1>  mov ecx, %3
    82                              <1> 
    83                              <1>  %if %0 >= 4
    84 000001BD BA07000000          <1>  mov edx, %4
    85                              <1>  %endif
    86                              <1>  %endif
    87                              <1>  %endif
    88 000001C2 B823000000          <1>  mov eax, %1
    89 000001C7 CD30                <1>  int 30h
   423                                  
   424                                  	; and file name (to) -at the end of error message-
   425                                  	sys	_msg, ebp, 255, 07h 
    75                              <1> 
    76                              <1> 
    77                              <1> 
    78                              <1>  %if %0 >= 2
    79 000001C9 89EB                <1>  mov ebx, %2
    80                              <1>  %if %0 >= 3
    81 000001CB B9FF000000          <1>  mov ecx, %3
    82                              <1> 
    83                              <1>  %if %0 >= 4
    84 000001D0 BA07000000          <1>  mov edx, %4
    85                              <1>  %endif
    86                              <1>  %endif
    87                              <1>  %endif
    88 000001D5 B823000000          <1>  mov eax, %1
    89 000001DA CD30                <1>  int 30h
   426                                  	; write next line (move cursor to next line)
   427                                  	; and return (from this/copy subroutine)
   428 000001DC E9FAFEFFFF              	jmp	write_nl
   429                                  
   430                                  cp_11:	
   431                                  	; 21/04/2022 
   432 000001E1 A3[3C030000]            	mov	[fnew], eax
   433                                  cp_rw_next:
   434                                  	; while(n = read(fold, iobuf, BSIZE))
   435                                  	sys	_read, [fold], iobuf, BSIZE
    75                              <1> 
    76                              <1> 
    77                              <1> 
    78                              <1>  %if %0 >= 2
    79 000001E6 8B1D[38030000]      <1>  mov ebx, %2
    80                              <1>  %if %0 >= 3
    81 000001EC B9[84030000]        <1>  mov ecx, %3
    82                              <1> 
    83                              <1>  %if %0 >= 4
    84 000001F1 BA00020000          <1>  mov edx, %4
    85                              <1>  %endif
    86                              <1>  %endif
    87                              <1>  %endif
    88 000001F6 B803000000          <1>  mov eax, %1
    89 000001FB CD30                <1>  int 30h
   436 000001FD 7317                    	jnc	short cp_12
   437                                  
   438                                  	;if (n < 0) {
   439                                  	;   fprintf(stderr, "cp: read error\n");
   440                                  
   441                                  	; write read error message
   442 000001FF BD[05030000]            	mov	ebp, crd_err_msg
   443                                  
   444                                  cp_rw_err:
   445                                  	; 21/04/2022
   446                                  	; cf = 1
   447                                  	sys	_close, [fnew]	
    75                              <1> 
    76                              <1> 
    77                              <1> 
    78                              <1>  %if %0 >= 2
    79 00000204 8B1D[3C030000]      <1>  mov ebx, %2
    80                              <1>  %if %0 >= 3
    81                              <1>  mov ecx, %3
    82                              <1> 
    83                              <1>  %if %0 >= 4
    84                              <1>  mov edx, %4
    85                              <1>  %endif
    86                              <1>  %endif
    87                              <1>  %endif
    88 0000020A B806000000          <1>  mov eax, %1
    89 0000020F CD30                <1>  int 30h
   448 00000211 E969FFFFFF              	jmp	write_err_msg
   449                                  
   450                                  cp_12:
   451                                  	; eax = read count
   452                                  	; eax = 0 -> eof
   453 00000216 09C0                    	or	eax, eax
   454 00000218 7423                    	jz	short cp_14 ; eof
   455                                  
   456 0000021A 89C5                    	mov	ebp, eax  ; n	
   457                                  	; write(fnew, iobuf, n)
   458                                  	sys	_write, [fnew], iobuf, ebp
    75                              <1> 
    76                              <1> 
    77                              <1> 
    78                              <1>  %if %0 >= 2
    79 0000021C 8B1D[3C030000]      <1>  mov ebx, %2
    80                              <1>  %if %0 >= 3
    81 00000222 B9[84030000]        <1>  mov ecx, %3
    82                              <1> 
    83                              <1>  %if %0 >= 4
    84 00000227 89EA                <1>  mov edx, %4
    85                              <1>  %endif
    86                              <1>  %endif
    87                              <1>  %endif
    88 00000229 B804000000          <1>  mov eax, %1
    89 0000022E CD30                <1>  int 30h
   459 00000230 7204                    	jc	short cp_13
   460                                  
   461                                  	;if (write(fnew, iobuf, n) != n)
   462                                  	;   fprintf(stderr, "cp: write error.\n");
   463                                  	;   close(fold);
   464                                  	;   close(fnew);
   465                                  	;   return(1);
   466                                  
   467                                  	; eax = written bytes
   468 00000232 39E8                    	cmp	eax, ebp
   469                                  	;je	short cp_11 ; read next (block)
   470                                  	; 21/04/2022
   471 00000234 74B0                    	je	short cp_rw_next
   472                                  	; error !
   473                                  	; eax < ebp --> cf = 1
   474                                  cp_13:
   475                                  	; close new file
   476                                  	; and then write error mesage
   477                                  	; and then close old file
   478                                  	; and then write (move cursor to) next line
   479                                  	; and then return (from subroutine) 
   480                                  	;sys	_close, [fnew]
   481                                  
   482                                  	; write error message
   483 00000236 BD[19030000]            	mov	ebp, cwr_err_msg
   484                                  	;jmp	short write_err_msg
   485                                  	; 21/04/2022
   486 0000023B EBC7                    	jmp	short cp_rw_err
   487                                  
   488                                  	; eof
   489                                  cp_14:
   490                                  	;close(fold);
   491                                  	;close(fnew);
   492                                  	;return(0);
   493                                  	
   494                                  	sys	_close, [fold]
    75                              <1> 
    76                              <1> 
    77                              <1> 
    78                              <1>  %if %0 >= 2
    79 0000023D 8B1D[38030000]      <1>  mov ebx, %2
    80                              <1>  %if %0 >= 3
    81                              <1>  mov ecx, %3
    82                              <1> 
    83                              <1>  %if %0 >= 4
    84                              <1>  mov edx, %4
    85                              <1>  %endif
    86                              <1>  %endif
    87                              <1>  %endif
    88 00000243 B806000000          <1>  mov eax, %1
    89 00000248 CD30                <1>  int 30h
   495                                  	sys	_close, [fnew]
    75                              <1> 
    76                              <1> 
    77                              <1> 
    78                              <1>  %if %0 >= 2
    79 0000024A 8B1D[3C030000]      <1>  mov ebx, %2
    80                              <1>  %if %0 >= 3
    81                              <1>  mov ecx, %3
    82                              <1> 
    83                              <1>  %if %0 >= 4
    84                              <1>  mov edx, %4
    85                              <1>  %endif
    86                              <1>  %endif
    87                              <1>  %endif
    88 00000250 B806000000          <1>  mov eax, %1
    89 00000255 CD30                <1>  int 30h
   496                                  	
   497                                  	;clc
   498 00000257 C3                      	retn
   499                                  	
   500                                  ;-----------------------------------------------------------------
   501                                  ;  data - initialized data
   502                                  ;-----------------------------------------------------------------
   503                                  
   504                                  ;argc:	dd 0
   505 00000258 00                      argc:	db 0
   506                                  
   507                                  ; ----------------------------------------------------------------
   508                                  
   509                                  program_msg:
   510 00000259 0D0A                    	db  0Dh, 0Ah
   511 0000025B 526574726F20554E49-     	db  'Retro UNIX 386 v1 COPY by Erdogan TAN - 22/04/2022'
   511 00000264 582033383620763120-
   511 0000026D 434F50592062792045-
   511 00000276 72646F67616E205441-
   511 0000027F 4E202D2032322F3034-
   511 00000288 2F32303232         
   512 0000028D 0D0A00                  	db  0Dh, 0Ah, 0
   513                                  
   514                                  usage_msg:
   515 00000290 0D0A                    	db  0Dh, 0Ah
   516 00000292 55736167653A206370-     	db  'Usage: cp: f1 f2; or cp f1 ... fn d2'
   516 0000029B 3A2066312066323B20-
   516 000002A4 6F7220637020663120-
   516 000002AD 2E2E2E20666E206432 
   517                                  nextline:
   518 000002B6 0D0A00                  	db  0Dh, 0Ah, 0
   519                                  
   520                                  cno_err_msg:
   521 000002B9 0D0A                    	db 0Dh, 0Ah
   522 000002BB 63703A2063616E6E6F-     	db 'cp: cannot open '
   522 000002C4 74206F70656E20     
   523 000002CB 00                      	db 0
   524                                  cncis_err_msg:
   525 000002CC 0D0A                    	db 0Dh, 0Ah
   526 000002CE 63703A2063616E6E6F-     	db 'cp: cannot copy file to itself.'
   526 000002D7 7420636F7079206669-
   526 000002E0 6C6520746F20697473-
   526 000002E9 656C662E           
   527 000002ED 0D0A00                  	db 0Dh, 0Ah, 0
   528                                  
   529                                  ccf_err_msg:
   530 000002F0 0D0A                    	db 0Dh, 0Ah
   531 000002F2 63703A2063616E6E6F-     	db 'cp: cannot create '
   531 000002FB 742063726561746520 
   532 00000304 00                      	db 0
   533                                  
   534                                  crd_err_msg:
   535 00000305 0D0A                    	db 0Dh, 0Ah
   536 00000307 63703A207265616420-     	db 'cp: read error.'
   536 00000310 6572726F722E       
   537 00000316 0D0A00                  	db 0Dh, 0Ah, 0
   538                                  
   539                                  cwr_err_msg:
   540 00000319 0D0A                    	db 0Dh, 0Ah
   541 0000031B 63703A207772697465-     	db 'cp: write error.'
   541 00000324 206572726F722E     
   542 0000032B 0D0A00                  	db 0Dh, 0Ah, 0
   543                                  
   544                                  ok_msg:
   545 0000032E 0D0A                    	db  0Dh, 0Ah
   546 00000330 4F4B2E                  	db  'OK.'
   547 00000333 0D0A00                  	db  0Dh, 0Ah, 0
   548                                  
   549 00000336 00                      errors:	db 0
   550                                  
   551                                  ;-----------------------------------------------------------------
   552                                  ;  bss - uninitialized data
   553                                  ;-----------------------------------------------------------------
   554                                  
   555 00000337 90                      align 2
   556                                  
   557                                  bss_start:
   558                                  
   559                                  ABSOLUTE bss_start
   560                                  
   561                                  ; 20/04/2022
   562 00000338 ????????                fold:	resd 1
   563 0000033C ????????                fnew:	resd 1
   564                                  
   565                                  ;stbuf2: resb 66 ; for Retro UNIX 386 v1.2 (66 byte sysstat data)
   566                                  ;stbuf1: resb 66 ; for Retro UNIX 386 v1.2 (66 byte sysstat data)
   567                                  ; 21/04/2022
   568 00000340 <res 22h>               stbuf2: resb 34 ; for Retro UNIX 386 v1.1 (34 byte sysstat data)
   569 00000362 <res 22h>               stbuf1: resb 34 ; for Retro UNIX 386 v1.1 (34 byte sysstat data)
   570                                  
   571 00000384 <res 200h>              iobuf:	resb BSIZE ; resb 512 ; path name buffer
   572                                  
   573                                  ; 20/04/2022
   574                                  ;-----------------------------------------------------------------
   575                                  ; Original UNIX v7 - cp (utility) c source code (cp.c)
   576                                  ;-----------------------------------------------------------------
   577                                  ;/* UNIX V7 source code: see www.tuhs.org for details. */;
   578                                  ;
   579                                  ;/*
   580                                  ; * cp oldfile newfile
   581                                  ; */
   582                                  ;
   583                                  ;#define BSIZE	512
   584                                  ;#include <stdio.h>
   585                                  ;#include <sys/types.h>
   586                                  ;#include <sys/stat.h>
   587                                  ;struct	stat stbuf1, stbuf2;
   588                                  ;char iobuf[BSIZE];
   589                                  ;
   590                                  ;main(argc, argv)
   591                                  ;char *argv[];
   592                                  ;{
   593                                  ;	register i, r;
   594                                  ;
   595                                  ;	if (argc < 3) 
   596                                  ;		goto usage;
   597                                  ;	if (argc > 3) {
   598                                  ;		if (stat(argv[argc-1], &stbuf2) < 0)
   599                                  ;			goto usage;
   600                                  ;		if ((stbuf2.st_mode&S_IFMT) != S_IFDIR) 
   601                                  ;			goto usage;
   602                                  ;	}
   603                                  ;	r = 0;
   604                                  ;	for(i=1; i<argc-1;i++)
   605                                  ;		r |= copy(argv[i], argv[argc-1]);
   606                                  ;	exit(r);
   607                                  ;usage:
   608                                  ;	fprintf(stderr, "Usage: cp: f1 f2; or cp f1 ... fn d2\n");
   609                                  ;	exit(1);
   610                                  ;}
   611                                  ;
   612                                  ;copy(from, to)
   613                                  ;char *from, *to;
   614                                  ;{
   615                                  ;	int fold, fnew, n;
   616                                  ;	register char *p1, *p2, *bp;
   617                                  ;	int mode;
   618                                  ;	if ((fold = open(from, 0)) < 0) {
   619                                  ;		fprintf(stderr, "cp: cannot open %s\n", from);
   620                                  ;		return(1);
   621                                  ;	}
   622                                  ;	fstat(fold, &stbuf1);
   623                                  ;	mode = stbuf1.st_mode;
   624                                  ;	/* is target a directory? */
   625                                  ;	if (stat(to, &stbuf2) >=0 &&
   626                                  ;	   (stbuf2.st_mode&S_IFMT) == S_IFDIR) {
   627                                  ;		p1 = from;
   628                                  ;		p2 = to;
   629                                  ;		bp = iobuf;
   630                                  ;		while(*bp++ = *p2++)
   631                                  ;			;
   632                                  ;		bp[-1] = '/';
   633                                  ;		p2 = bp;
   634                                  ;		while(*bp = *p1++)
   635                                  ;			if (*bp++ == '/')
   636                                  ;				bp = p2;
   637                                  ;		to = iobuf;
   638                                  ;	}
   639                                  ;	if (stat(to, &stbuf2) >= 0) {
   640                                  ;		if (stbuf1.st_dev == stbuf2.st_dev &&
   641                                  ;		   stbuf1.st_ino == stbuf2.st_ino) {
   642                                  ;			fprintf(stderr, "cp: cannot copy file to itself.\n");
   643                                  ;			return(1);
   644                                  ;		}
   645                                  ;	}
   646                                  ;	if ((fnew = creat(to, mode)) < 0) {
   647                                  ;		fprintf(stderr, "cp: cannot create %s\n", to);
   648                                  ;		close(fold);
   649                                  ;		return(1);
   650                                  ;	}
   651                                  ;	while(n = read(fold,  iobuf,  BSIZE)) {
   652                                  ;		if (n < 0) {
   653                                  ;			fprintf(stderr, "cp: read error\n");
   654                                  ;			close(fold);
   655                                  ;			close(fnew);
   656                                  ;			return(1);
   657                                  ;		} else
   658                                  ;			if (write(fnew, iobuf, n) != n) {
   659                                  ;				fprintf(stderr, "cp: write error.\n");
   660                                  ;				close(fold);
   661                                  ;				close(fnew);
   662                                  ;				return(1);
   663                                  ;			}
   664                                  ;	}
   665                                  ;	close(fold);
   666                                  ;	close(fnew);
   667                                  ;	return(0);
   668                                  ;}
