     1                                  ; ****************************************************************************
     2                                  ; cptest0.s (cp0.s) - by Erdogan Tan - 20/04/2022
     3                                  ; ----------------------------------------------------------------------------
     4                                  ; Retro UNIX 8086 v1 - copy -- cp oldfile newfile (test version)
     5                                  ;
     6                                  ; [ Last Modification: 24/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                                  ; cptest.s - 24/04/2022 - Retro UNIX 8086 v1 - Test version of 'cp8086.s'
    22                                  
    23                                  ; 24/04/2022
    24                                  ; Test version - Normal running version difference:
    25                                  ;	Test version write new file after reading old file at all..
    26                                  ; (what for: to understand where -which stage- is causing to failure while
    27                                  ;  creating a new file.. because i could not understand it for 'cp8086.s')	
    28                                  
    29                                  ; 12/01/2022 (Retro UNIX 386 v1.2)
    30                                  ; 13/10/2015
    31                                  
    32                                  ; UNIX v1 system calls
    33                                  _rele 	equ 0
    34                                  _exit 	equ 1
    35                                  _fork 	equ 2
    36                                  _read 	equ 3
    37                                  _write	equ 4
    38                                  _open	equ 5
    39                                  _close 	equ 6
    40                                  _wait 	equ 7
    41                                  _creat 	equ 8
    42                                  _link 	equ 9
    43                                  _unlink	equ 10
    44                                  _exec	equ 11
    45                                  _chdir	equ 12
    46                                  _time 	equ 13
    47                                  _mkdir 	equ 14
    48                                  _chmod	equ 15
    49                                  _chown	equ 16
    50                                  _break	equ 17
    51                                  _stat	equ 18
    52                                  _seek	equ 19
    53                                  _tell 	equ 20
    54                                  _mount	equ 21
    55                                  _umount	equ 22
    56                                  _setuid	equ 23
    57                                  _getuid	equ 24
    58                                  _stime	equ 25
    59                                  _quit	equ 26	
    60                                  _intr	equ 27
    61                                  _fstat	equ 28
    62                                  _emt 	equ 29
    63                                  _mdate 	equ 30
    64                                  _stty 	equ 31
    65                                  _gtty	equ 32
    66                                  _ilgins	equ 33
    67                                  _sleep	equ 34 ; Retro UNIX 8086 v1 feature only !
    68                                  _msg    equ 35 ; Retro UNIX 386 v1 feature only !
    69                                  _geterr	equ 36 ; Retro UNIX 386 v1 feature only !
    70                                  ; 12/01/2022 - Retro UNIX 386 v1.2
    71                                  ; Retro UNIX 386 v2 system calls
    72                                  _setgid	equ 37
    73                                  _getgid	equ 38
    74                                  _sysver	equ 39 ; (get) Retro Unix 386 version
    75                                  
    76                                  ;;;
    77                                  ESCKey equ 1Bh
    78                                  EnterKey equ 0Dh
    79                                  
    80                                  ;%macro sys 1-4
    81                                  ;   ; 03/09/2015	
    82                                  ;   ; 13/04/2015
    83                                  ;   ; Retro UNIX 386 v1 system call.		
    84                                  ;   %if %0 >= 2   
    85                                  ;       mov ebx, %2
    86                                  ;       %if %0 >= 3    
    87                                  ;           mov ecx, %3
    88                                  ;           ;%if %0 = 4
    89                                  ;           %if	%0 >= 4 ; 11/03/2022
    90                                  ;		mov edx, %4   
    91                                  ;           %endif
    92                                  ;       %endif
    93                                  ;   %endif
    94                                  ;   mov eax, %1
    95                                  ;   int 30h	   
    96                                  ;%endmacro
    97                                  
    98                                  %macro sys 1-4
    99                                      ; Retro UNIX 8086 v1 system call.
   100                                      %if %0 >= 2   
   101                                          mov bx, %2
   102                                          %if %0 >= 3
   103                                              mov cx, %3
   104                                              %if %0 >= 4
   105                                                 mov dx, %4
   106                                              %endif
   107                                          %endif
   108                                      %endif
   109                                      mov ax, %1
   110                                      int 20h
   111                                  %endmacro
   112                                  
   113                                  ;; Retro UNIX 386 v1 system call format:
   114                                  ;; sys systemcall (eax) <arg1 (ebx)>, <arg2 (ecx)>, <arg3 (edx)>
   115                                  
   116                                  ;; 11/03/2022
   117                                  ;; Note: Above 'sys' macro has limitation about register positions;
   118                                  ;;	ebx, ecx, edx registers must not be used after their
   119                                  ;;	positions in sys macro.
   120                                  ;; for example:
   121                                  ;;	'sys _write, 1, msg, ecx' is defective, because
   122                                  ;;	 ecx will be used/assigned before edx in 'sys' macro.
   123                                  ;; correct order may be:
   124                                  ;;	'sys _write, 1, msg, eax ; (eax = byte count)
   125                                  
   126                                  ; Retro UNIX 8086 v1 system call format:
   127                                  ; sys systemcall (ax) <arg1 (bx)>, <arg2 (cx)>, <arg3 (dx)>
   128                                  
   129                                  struc stat
   130                                  	; Note: This is for Retro UNIX v1 'sysstat' output !!!
   131                                  	; (34 bytes)
   132 00000000 ????                    	.inode:  resw 1	
   133 00000002 ????                    	.mode:	 resw 1
   134 00000004 ??                      	.nlinks: resb 1
   135 00000005 ??                      	.uid:	 resb 1
   136 00000006 ????                    	.size:	 resw 1
   137 00000008 <res 10h>               	.dskptr: resw 8
   138 00000018 ????????                	.ctime:	 resd 1
   139 0000001C ????????                	.mtime:	 resd 1
   140 00000020 ????                    	.rsvd:   resw 1
   141                                  	.strucsize:
   142                                  endstruc   
   143                                  
   144                                  ;struc stat
   145                                  ;	; Note: This is for Retro UNIX v1.2 'sysstat' output !!!
   146                                  ;	; (66 bytes)
   147                                  ;	.inode:  resw 1	
   148                                  ;	.mode:	 resw 1
   149                                  ;	.nlinks: resw 1 
   150                                  ;	.uid:	 resw 1
   151                                  ;	.gid:	 resb 1
   152                                  ;	.size_h: resb 1
   153                                  ;	.size:	 resd 1
   154                                  ;	.dskptr: resd 10
   155                                  ;	.atime:	 resd 1
   156                                  ;	.mtime:	 resd 1
   157                                  ;	.ctime:  resd 1
   158                                  ;	.strucsize:
   159                                  ;endstruc   
   160                                  
   161                                  ;S_IFMT   equ 0F000h ; /* type of file */
   162                                  ;S_IFDIR  equ 04000h ; /* directory */
   163                                  ;S_IFCHR  equ 02000h ; /* character special */
   164                                  ;S_IFBLK  equ 06000h ; /* block special */
   165                                  ;S_IFREG  equ 08000h ; /* regular */
   166                                  ;S_ISUID  equ 00800h ; /* set user id on execution */
   167                                  ;S_ISGID  equ 00400h ; /* set group id on execution */
   168                                  ;S_IREAD  equ 00100h ; /* read permission, owner */
   169                                  ;S_IWRITE equ 00080h ; /* write permission, owner */
   170                                  ;S_IEXEC  equ 00040h ; /* execute/search permission, owner */
   171                                  
   172                                  ; 21/04/2022 - UNIX v1 inode
   173                                  ; byte 1
   174                                  S_ALLOC  equ 080h ; Allocated flag
   175                                  S_IFDIR  equ 040h ; Directory flag
   176                                  S_IFCHR  equ 020h ; File modified flag (always on)
   177                                  S_IFBLK  equ 010h ; Large File flag
   178                                  ; byte 0
   179                                  S_ISUID  equ 020h ; Set User ID On Execution flag
   180                                  S_IEXEC  equ 010h ; Executable File flag
   181                                  S_IREAD  equ 008h ; Owner's Read Permission flag
   182                                  S_IWRITE equ 004h ; Owner's Write Permission flag
   183                                  
   184                                  BSIZE equ 512
   185                                  
   186                                  ; 24/04/2022 - cptest0.s
   187                                  MAXFSIZE equ 32530 - iobuf
   188                                  
   189                                  ;-----------------------------------------------------------------
   190                                  ;  text - code
   191                                  ;-----------------------------------------------------------------
   192                                  
   193                                  ;[BITS 32] ; 32-bit intructions (for 80386 protected mode)
   194                                  
   195                                  [BITS 16] ; 16-bit (x86 real mode) intructions
   196                                  
   197                                  [ORG 0] 
   198                                  
   199                                  START_CODE:
   200                                  	; 22/04/2022 - 16 bit version (Retro UNIX 8086 v1) 
   201                                  	;
   202                                  	; 20/04/2022
   203                                  	; main(argc, argv)
   204                                  
   205                                  	;mov	esi, esp		
   206                                  	;mov	edi, esi
   207                                  	;lodsd		; number of arguments
   208                                  	;;mov	edi, esi			
   209                                  	;;mov	[argc], eax
   210                                  	;mov	[argc], al
   211                                  
   212                                  	; 22/04/2022
   213 00000000 89E6                    	mov	si, sp
   214 00000002 89F7                    	mov	di, si
   215 00000004 AD                      	lodsw
   216                                  	;mov	[argc], ax
   217 00000005 A2[DC01]                	mov	[argc], al
   218                                  
   219                                  	;if (argc < 3) 
   220                                  	;   goto usage;
   221                                  
   222                                  	;;cmp	eax, 3
   223                                  	;cmp	ax, 3
   224 00000008 3C03                    	cmp	al, 3
   225 0000000A 732D                    	jnb	short cp_0  ; if (argc > 3) {
   226 0000000C FEC8                    	dec	al	; 21/04/2022
   227 0000000E 7506                    	jnz	short cp_usage
   228                                  
   229                                  	;sys	_msg, program_msg, 255, 0Fh
   230                                  	; 22/04/2022
   231 00000010 B8[DD01]                	mov	ax, program_msg ; asciiz message address
   232 00000013 E80B00                  	call	print_msg
   233                                  
   234                                  cp_usage:
   235                                     ; fprintf(stderr, "Usage: cp: f1 f2; or cp f1 ... fn d2\n");
   236                                  	;sys	_msg, usage_msg, 255, 07h
   237 00000016 B8[1802]                	mov	ax, usage_msg
   238 00000019 E80500                  	call	print_msg
   239                                  cp_exit:
   240                                  	sys	_exit	; sys exit
    99                              <1> 
   100                              <1>  %if %0 >= 2
   101                              <1>  mov bx, %2
   102                              <1>  %if %0 >= 3
   103                              <1>  mov cx, %3
   104                              <1>  %if %0 >= 4
   105                              <1>  mov dx, %4
   106                              <1>  %endif
   107                              <1>  %endif
   108                              <1>  %endif
   109 0000001C B80100              <1>  mov ax, %1
   110 0000001F CD20                <1>  int 20h
   241                                  ;hlt:
   242                                  ;	nop
   243                                  ;	nop
   244                                  ;	jmp	short hlt
   245                                  
   246                                  print_msg:
   247                                  	; 22/04/2022
   248                                  strlen:
   249                                  	; ax = asciiz string address
   250 00000021 89C3                    	mov	bx, ax
   251 00000023 4B                      	dec	bx
   252                                  nextchr:
   253 00000024 43                      	inc	bx
   254 00000025 803F00                  	cmp	byte [bx], 0
   255 00000028 77FA                    	ja	short nextchr
   256                                  	;cmp	[bx], 0Dh
   257                                  	;ja	short nextchr
   258 0000002A 29C3                    	sub	bx, ax
   259                                  	; bx = asciiz string length
   260                                  	;retn
   261                                  print_str:
   262 0000002C 89DA                    	mov	dx, bx
   263                                  	sys	_write, 1, ax
    99                              <1> 
   100                              <1>  %if %0 >= 2
   101 0000002E BB0100              <1>  mov bx, %2
   102                              <1>  %if %0 >= 3
   103 00000031 89C1                <1>  mov cx, %3
   104                              <1>  %if %0 >= 4
   105                              <1>  mov dx, %4
   106                              <1>  %endif
   107                              <1>  %endif
   108                              <1>  %endif
   109 00000033 B80400              <1>  mov ax, %1
   110 00000036 CD20                <1>  int 20h
   264                                  
   265 00000038 C3                      	retn
   266                                  
   267                                  cp_0:
   268                                  	;mov	edx, eax ; [argc]
   269                                  	;; 21/04/2022
   270                                  	;;;dec	edx  ; argc-1
   271                                  	;;dec	dl
   272                                  	;shl	dl, 2 ; * 4 
   273                                  	;add	edi, edx
   274                                  
   275                                  	; 22/04/2022
   276 00000039 89C2                    	mov	dx, ax
   277 0000003B D0E2                    	shl	dl, 1 ; * 2 ; 16 bit argument pointers
   278 0000003D 01D7                    	add	di, dx
   279                                  
   280                                  	; 21/04/2022
   281 0000003F 3C03                    	cmp	al,3
   282 00000041 7613                    	jna	short cp_1
   283                                  
   284                                  	;sys	_stat, [edi], stbuf2
   285                                  	sys	_stat, [di], stbuf2  ; 22/04/2022
    99                              <1> 
   100                              <1>  %if %0 >= 2
   101 00000043 8B1D                <1>  mov bx, %2
   102                              <1>  %if %0 >= 3
   103 00000045 B9[3003]            <1>  mov cx, %3
   104                              <1>  %if %0 >= 4
   105                              <1>  mov dx, %4
   106                              <1>  %endif
   107                              <1>  %endif
   108                              <1>  %endif
   109 00000048 B81200              <1>  mov ax, %1
   110 0000004B CD20                <1>  int 20h
   286 0000004D 72C7                    	jc	short cp_usage
   287                                  
   288                                  	;; check retro unix v2 inode flags
   289                                  	;;	(a bit different than unix v7 inode flags)
   290                                  	;; if it is a directory..
   291                                  	;;	regular file flag and dir flag must be 1
   292                                  	;mov	al, [stbuf2+stat.mode+1]
   293                                  	;and	al, S_IFDIR|S_IFREG
   294                                  	;cmp	al, S_IFDIR|S_IFREG ; directory ?
   295                                  	;jne	short cp_usage ; no
   296                                  	
   297                                  	;; check if it is a device file
   298                                  	;;test	al, S_IFREG ; regular file ?
   299                                  	;;jz	short cp_usage ; no
   300                                  	;;and	al, S_IFDIR ; directory ?
   301                                  	;;jz	short cp_usage ; no
   302                                  
   303                                  	; 21/04/2022
   304                                  	; check (unix v1 inode) directory flag
   305 0000004F F606[3303]40            	test	byte [stbuf2+stat.mode+1], S_IFDIR ; directory ?
   306 00000054 74C0                    	jz	short cp_usage ; no
   307                                   
   308                                  cp_1:	
   309                                  	; esi = esp+4 = argv[0] ; executable file name (cp)
   310                                  	;lodsd	; 21/04/2022
   311 00000056 AD                      	lodsw ; 22/04/2022 - Retro UNIX 8086 v1
   312                                  cp_loop: ; for(i=1; i<argc-1;i++)
   313                                  	;lodsd
   314                                  	; ((esi = esp+8 = argv[1] ; (old) file 1))
   315                                  
   316                                  	; esi = argv[i] 
   317                                   	; edi = argv[argc-1] ; *
   318                                  	; 22/04/2022 - Retro UNIX 8086 v1 (16 bit Retro UNIX)
   319                                  	; si = argv[i]
   320                                  	; di = argv[argc-1] ; *
   321                                   	
   322 00000057 E81D00                  	call	copy
   323 0000005A 7304                    	jnc	short cp_2
   324                                  
   325 0000005C FE06[D602]              	inc	byte [errors]
   326                                  cp_2:
   327                                  	;lodsd	; 21/04/2022
   328                                  	;cmp	esi, edi
   329                                  	;jb	short cp_loop
   330                                  
   331                                  	; 22/04/2022 - Retro UNIX 8086 v1
   332 00000060 AD                      	lodsw
   333 00000061 39FE                    	cmp	si, di
   334 00000063 72F2                    	jb	short cp_loop
   335                                  
   336                                  	; bypass 'OK.' message if there was an error
   337 00000065 803E[D602]00            	cmp	byte [errors], 0
   338 0000006A 77B0                    	ja	short cp_exit
   339                                  cp_ok:
   340                                  	;sys	_msg, ok_msg, 255, 07h
   341                                  	; 22/04/2022
   342 0000006C B8[CE02]                	mov	ax, ok_msg
   343                                  cp_err_exit:  ; cptest0.s ; 24/04/2022
   344 0000006F E8AFFF                  	call	print_msg
   345                                  ;cp_exit:
   346                                  	sys	_exit	; sys exit
    99                              <1> 
   100                              <1>  %if %0 >= 2
   101                              <1>  mov bx, %2
   102                              <1>  %if %0 >= 3
   103                              <1>  mov cx, %3
   104                              <1>  %if %0 >= 4
   105                              <1>  mov dx, %4
   106                              <1>  %endif
   107                              <1>  %endif
   108                              <1>  %endif
   109 00000072 B80100              <1>  mov ax, %1
   110 00000075 CD20                <1>  int 20h
   347                                  
   348                                  ;_halt:
   349                                  ;	nop
   350                                  ;	jmp	short _halt
   351                                  
   352                                  copy:	; copy(from, to)
   353                                  	;
   354                                  	; 22/04/2022 - 16 bit version (Retro UNIX 8086 v1) 
   355                                  	; 21/04/2022
   356                                  	; 20/04/2022
   357                                  	; INPUT:
   358                                  	;	esi = pointer to file name to be copied
   359                                  	;  	edi = ptr to new file name or destination dir
   360                                  	; OUTPUT:
   361                                  	;	cf = 0 -> OK
   362                                  	;	cf = 1 -> Error !
   363                                  	;
   364                                  	; Modified registers: eax, ebx, ecx, edx, ebp
   365                                  	;
   366                                  	
   367                                  	; open (old) file for read
   368                                  	;sys	_open, [esi], 0
   369                                  	sys	_open, [si], 0 ; 22/04/2022
    99                              <1> 
   100                              <1>  %if %0 >= 2
   101 00000077 8B1C                <1>  mov bx, %2
   102                              <1>  %if %0 >= 3
   103 00000079 B90000              <1>  mov cx, %3
   104                              <1>  %if %0 >= 4
   105                              <1>  mov dx, %4
   106                              <1>  %endif
   107                              <1>  %endif
   108                              <1>  %endif
   109 0000007C B80500              <1>  mov ax, %1
   110 0000007F CD20                <1>  int 20h
   370 00000081 7313                    	jnc	short cp_3
   371                                  	
   372                                  	; esi = file name (from)
   373                                  
   374                                  	;fprintf(stderr, "cp: cannot open %s\n", from);
   375                                  	;	return(1);
   376                                  
   377                                  	;sys	_msg, cno_err_msg, 255, 07h
   378                                  	; 22/04/2022
   379 00000083 B8[4502]                	mov	ax, cno_err_msg
   380 00000086 E898FF                  	call	print_msg
   381                                  
   382                                  	; file name (from) 
   383                                  	;sys	_msg, [esi], 255, 07h
   384                                  	; 22/04/2022
   385 00000089 8B04                    	mov	ax, [si]
   386 0000008B E893FF                  	call	print_msg
   387                                  write_nl:
   388                                  	; new/next line
   389                                  	;sys	_msg, nextline, 255, 07h
   390                                  	; 22/04/2022
   391 0000008E B8[4202]                	mov	ax, nextline
   392 00000091 E88DFF                  	call	print_msg
   393                                  	;
   394 00000094 F9                      	stc	; return with error (cf=1)
   395 00000095 C3                      	retn
   396                                  
   397                                  cp_3:
   398                                  	;mov	[fold], eax ; file (descriptor) number
   399                                  	; 22/04/2022
   400 00000096 A3[2A03]                	mov	[fold], ax ; file (descriptor) number
   401                                  
   402                                  	; (from)
   403                                  	sys	_fstat, [fold], stbuf1
    99                              <1> 
   100                              <1>  %if %0 >= 2
   101 00000099 8B1E[2A03]          <1>  mov bx, %2
   102                              <1>  %if %0 >= 3
   103 0000009D B9[5203]            <1>  mov cx, %3
   104                              <1>  %if %0 >= 4
   105                              <1>  mov dx, %4
   106                              <1>  %endif
   107                              <1>  %endif
   108                              <1>  %endif
   109 000000A0 B81C00              <1>  mov ax, %1
   110 000000A3 CD20                <1>  int 20h
   404                                  
   405                                  
   406                                  	; 24/04/2022 - cptest0.s
   407                                  
   408 000000A5 813E[5803]1E7B          	cmp	word [stbuf1+stat.size], MAXFSIZE
   409 000000AB 762A                    	jna	short cp_3_
   410                                  
   411                                  	; close file
   412                                  	sys	_close, [fold]
    99                              <1> 
   100                              <1>  %if %0 >= 2
   101 000000AD 8B1E[2A03]          <1>  mov bx, %2
   102                              <1>  %if %0 >= 3
   103                              <1>  mov cx, %3
   104                              <1>  %if %0 >= 4
   105                              <1>  mov dx, %4
   106                              <1>  %endif
   107                              <1>  %endif
   108                              <1>  %endif
   109 000000B1 B80600              <1>  mov ax, %1
   110 000000B4 CD20                <1>  int 20h
   413                                   
   414                                  write_mfs:
   415 000000B6 89E5                    	mov	bp, sp
   416 000000B8 B81E7B                  	mov	ax, MAXFSIZE
   417 000000BB B90A00                  	mov	cx, 10
   418                                  div_again:
   419 000000BE 31D2                    	xor	dx, dx	
   420 000000C0 F7F1                    	div	cx
   421 000000C2 52                      	push	dx
   422 000000C3 21C0                    	and	ax, ax
   423 000000C5 75F7                    	jnz	short div_again
   424 000000C7 BF[EC02]                	mov	di, mfsstr
   425                                  wrt_again:
   426 000000CA 58                      	pop	ax
   427 000000CB 0430                    	add	al, '0'
   428 000000CD AA                      	stosb
   429 000000CE 39E5                    	cmp	bp, sp
   430 000000D0 77F8                    	ja	short wrt_again
   431                                  
   432 000000D2 B8[D702]                	mov	ax, bigfile_err
   433 000000D5 EB98                    	jmp	cp_err_exit
   434                                  	
   435                                  cp_3_:
   436                                  	; save mode
   437                                  	;mov	ax, [stbuf1.mode]
   438                                  	;mov	[mode], ax
   439                                  	
   440                                  	; (to)
   441                                  	;;sys	_stat, [edi], stbuf2  ; stat(to, &stbuf2)
   442                                  	; 22/04/2022
   443                                  	;sys	_stat, [di], stbuf2
   444                                  	;jnc	short cp_4
   445                                  	;jmp	cp_9
   446                                  	; 22/04/2022
   447 000000D7 8B2D                    	mov	bp, [di]
   448                                  	sys	_stat, bp, stbuf2  ; stat(to, &stbuf2)
    99                              <1> 
   100                              <1>  %if %0 >= 2
   101 000000D9 89EB                <1>  mov bx, %2
   102                              <1>  %if %0 >= 3
   103 000000DB B9[3003]            <1>  mov cx, %3
   104                              <1>  %if %0 >= 4
   105                              <1>  mov dx, %4
   106                              <1>  %endif
   107                              <1>  %endif
   108                              <1>  %endif
   109 000000DE B81200              <1>  mov ax, %1
   110 000000E1 CD20                <1>  int 20h
   449                                  	;jnc	short cp_4
   450                                  	;jmp	cp_9
   451 000000E3 725C                    	jc	short cp_9
   452                                  cp_4:
   453                                  	; /* is target a directory? */
   454                                  
   455                                  	;; check retro unix v2 inode flags
   456                                  	;;	(a bit different than unix v7 inode flags)
   457                                  	;; regular file flag and dir flag must be 1 for a dir
   458                                  	;mov	al, [stbuf2+stat.mode+1]
   459                                  	;and	al, S_IFDIR|S_IFREG
   460                                  	;cmp	al, S_IFDIR|S_IFREG ; directory ?
   461                                  	;jne	short cp_8 ; no, overwrite (create file)
   462                                  			   ; (if the new file is not same file)
   463                                  
   464                                  	;; check if it is a device file
   465                                  	;;test	al, S_IFREG ; regular file ?
   466                                  	;;jz	short cp_error ; no
   467                                  	;;and	al, S_IFDIR ; directory ?
   468                                  	;;jz	short cp_error ; no
   469                                  
   470                                  	; 21/04/2022
   471                                  	; check (unix v1 inode) directory flag
   472 000000E5 F606[3303]40            	test	byte [stbuf2+stat.mode+1], S_IFDIR ; directory ?
   473 000000EA 7439                    	jz	short cp_8 ; no, overwrite (create file)
   474                                  			   ; (if the new file is not same file)
   475                                  
   476                                  	; add (old) file name to (destination ) path
   477                                  	; (directory name +'/'+ file name)  
   478                                  	
   479                                  	;mov	ebp, esi   ; save esi
   480                                  	;mov	esi, [edi] ; directory name address
   481                                  	;mov	ebx, edi   ; save edi			
   482                                  	;mov	edi, iobuf ; (new) path name buffer addr
   483                                  	; 22/04/2022 - Retro UNIX 8086 v1 (16 bit utility)
   484 000000EC 89F5                    	mov	bp, si
   485 000000EE 8B35                    	mov	si, [di]
   486 000000F0 89FB                    	mov	bx, di
   487                                  	;mov	di, iobuf
   488                                  	; 24/04/2022 - cptest0.s
   489 000000F2 BF[7403]                	mov	di, fnbuf
   490                                  
   491                                  	; p1 = from;
   492                                  	; p2 = to;
   493                                  	; bp = iobuf;
   494                                  cp_5:	; while(*bp++ = *p2++)
   495 000000F5 AC                      	lodsb	
   496 000000F6 AA                      	stosb
   497 000000F7 20C0                    	and	al, al
   498 000000F9 75FA                    	jnz	short cp_5
   499                                  	;mov	byte [edi-1], '/' ; bp[-1] = '/';
   500                                  	; 22/04/2022
   501 000000FB C645FF2F                	mov	byte [di-1], '/'
   502                                  	; p2 = bp
   503                                  	;mov	edx, edi
   504                                  	;mov	esi, [ebp] ; *p1 ; from
   505                                  	; 22/04/2022
   506 000000FF 89FA                    	mov	dx, di
   507 00000101 8B7600                  	mov	si, [bp]
   508                                  cp_6:	; while(*bp = *p1++)
   509 00000104 AC                      	lodsb
   510 00000105 AA                      	stosb
   511 00000106 08C0                    	or	al, al
   512 00000108 7408                    	jz	short cp_7
   513 0000010A 3C2F                    	cmp	al, '/' ; if (*bp++ == '/')
   514 0000010C 75F6                    	jne	short cp_6
   515                                  	; bp = p2
   516                                  	; 21/04/2022
   517                                  	;mov	edi, edx ; (discard path before file name)
   518                                  	; 22/04/2022
   519 0000010E 89D7                    	mov	di, dx
   520 00000110 EBF2                    	jmp	short cp_6
   521                                  cp_7:	
   522                                  	;mov	esi, ebp ; restore esi
   523                                  	;mov	edi, ebx ; restore edi
   524                                  	; 22/04/2022
   525 00000112 89EE                    	mov	si, bp
   526 00000114 89DF                    	mov	di, bx
   527                                  	; to = iobuf
   528                                  	;mov	ebp, iobuf
   529                                  	; 22/04/2022
   530                                  	;mov	bp, iobuf	
   531                                  	; 24/04/2022 - cptest0.s
   532 00000116 BD[7403]                	mov	bp, fnbuf	
   533                                  
   534                                  	;sys	_stat, ebp, stbuf2  ; stat(to, &stbuf2) >= 0
   535                                  	sys	_stat, bp, stbuf2 ; 22/04/2022
    99                              <1> 
   100                              <1>  %if %0 >= 2
   101 00000119 89EB                <1>  mov bx, %2
   102                              <1>  %if %0 >= 3
   103 0000011B B9[3003]            <1>  mov cx, %3
   104                              <1>  %if %0 >= 4
   105                              <1>  mov dx, %4
   106                              <1>  %endif
   107                              <1>  %endif
   108                              <1>  %endif
   109 0000011E B81200              <1>  mov ax, %1
   110 00000121 CD20                <1>  int 20h
   536 00000123 721C                    	jc	short cp_10 ; create new file
   537                                  
   538                                  cp_8:
   539                                  	;if (stbuf1.st_dev == stbuf2.st_dev &&
   540                                  	;   stbuf1.st_ino == stbuf2.st_ino) {
   541                                  	;	fprintf(stderr, "cp: cannot copy file to itself.\n");
   542                                  	; 	return(1);
   543                                  
   544 00000125 A1[5203]                	mov	ax, [stbuf1+stat.inode]
   545 00000128 3B06[3003]              	cmp	ax, [stbuf2+stat.inode]
   546 0000012C 7513                    	jne	short cp_10
   547                                  
   548                                  	; same file ! error...
   549                                  	;mov	ebp, cncis_err_msg ; error message
   550                                  	; 22/04/2022
   551 0000012E BD[5C02]                	mov	bp, cncis_err_msg ; error message 
   552                                  	; esi = file name (from)
   553                                  write_err_msg:
   554                                  	;sys	_msg, ebp, 255, 07h
   555                                  	; 22/04/2022
   556 00000131 89E8                    	mov	ax, bp
   557                                  	; ax = error message address
   558 00000133 E8EBFE                  	call	print_msg
   559                                  	;
   560                                  	; close (old) file
   561                                  	sys	_close, [fold]
    99                              <1> 
   100                              <1>  %if %0 >= 2
   101 00000136 8B1E[2A03]          <1>  mov bx, %2
   102                              <1>  %if %0 >= 3
   103                              <1>  mov cx, %3
   104                              <1>  %if %0 >= 4
   105                              <1>  mov dx, %4
   106                              <1>  %endif
   107                              <1>  %endif
   108                              <1>  %endif
   109 0000013A B80600              <1>  mov ax, %1
   110 0000013D CD20                <1>  int 20h
   562 0000013F F9                      	stc	; return with error (cf=1)
   563 00000140 C3                      	retn
   564                                  
   565                                  cp_9:	
   566                                  	; 22/04/2022
   567                                  	; bp = [di]
   568                                  	;; new file (asciiz name address)
   569                                  	;;mov	ebp, [edi] ; 21/04/2022
   570                                  	;mov	bp, [di] ; 22/04/2022
   571                                  
   572                                  	; create new file (truncate if it exists) 
   573                                  cp_10:
   574                                  	; fnew = creat(to, mode)
   575                                  	;movzx 	ecx, word [stbuf1+stat.mode]
   576                                  	; ecx = mode 
   577                                  	; 22/04/2022
   578 00000141 8B0E[5403]              	mov	cx, [stbuf1+stat.mode]
   579                                  	;sys	_creat, ebp
   580                                  	sys	_creat, bp ; 22/04/2022
    99                              <1> 
   100                              <1>  %if %0 >= 2
   101 00000145 89EB                <1>  mov bx, %2
   102                              <1>  %if %0 >= 3
   103                              <1>  mov cx, %3
   104                              <1>  %if %0 >= 4
   105                              <1>  mov dx, %4
   106                              <1>  %endif
   107                              <1>  %endif
   108                              <1>  %endif
   109 00000147 B80800              <1>  mov ax, %1
   110 0000014A CD20                <1>  int 20h
   581 0000014C 730E                    	jnc	short cp_11
   582                                  
   583                                  	;if ((fnew = creat(to, mode)) < 0) {
   584                                  	;	fprintf(stderr, "cp: cannot create %s\n", to);
   585                                  	;	close(fold);
   586                                  	;	return(1);
   587                                  
   588                                  	; error message
   589                                  	;sys	_msg, ccf_err_msg, 255, 07h
   590                                  	; 22/04/2022
   591 0000014E B8[8402]                	mov	ax, ccf_err_msg ; 'can not create' error msg
   592 00000151 E8CDFE                  	call	print_msg
   593                                  
   594                                  	; and file name (to) -at the end of error message-
   595                                  	;sys	_msg, ebp, 255, 07h
   596                                  	; 22/04/2022
   597 00000154 89E8                    	mov	ax, bp
   598 00000156 E8C8FE                  	call	print_msg
   599                                   
   600                                  	; write next line (move cursor to next line)
   601                                  	; and return (from this/copy subroutine)
   602 00000159 E932FF                  	jmp	write_nl
   603                                  
   604                                  cp_11:	
   605                                  	; 21/04/2022 
   606                                  	;mov	[fnew], eax
   607                                  	; 22/04/2022 - Retro UNIX 8086 v1
   608 0000015C A3[2C03]                	mov	[fnew], ax ; (16 bit)
   609                                  ;cp_rw_next:
   610                                  ;	; while(n = read(fold, iobuf, BSIZE))
   611                                  ;	sys	_read, [fold], iobuf, BSIZE
   612                                  ;	jnc	short cp_12
   613                                  
   614                                  	; 24/04/2022 - cptest0.s
   615 0000015F B8[FB02]                	mov	ax, reading_msg
   616 00000162 E8BCFE                  	call	print_msg
   617 00000165 8B04                    	mov	ax, [si] ; (old) file name
   618 00000167 E8B7FE                  	call	print_msg
   619                                  
   620                                  	sys	_read, [fold], iobuf, [stbuf1+stat.size]
    99                              <1> 
   100                              <1>  %if %0 >= 2
   101 0000016A 8B1E[2A03]          <1>  mov bx, %2
   102                              <1>  %if %0 >= 3
   103 0000016E B9[F403]            <1>  mov cx, %3
   104                              <1>  %if %0 >= 4
   105 00000171 8B16[5803]          <1>  mov dx, %4
   106                              <1>  %endif
   107                              <1>  %endif
   108                              <1>  %endif
   109 00000175 B80300              <1>  mov ax, %1
   110 00000178 CD20                <1>  int 20h
   621 0000017A 7314                    	jnc	short cp_12
   622                                  
   623                                  	;if (n < 0) {
   624                                  	;   fprintf(stderr, "cp: read error\n");
   625                                  
   626                                  	; 24/04/2022 - cptest0.s
   627 0000017C B8[4202]                	mov	ax, nextline
   628 0000017F E89FFE                  	call	print_msg
   629                                  
   630                                  	; write read error message
   631                                  	;mov	ebp, crd_err_msg
   632 00000182 BD[9D02]                	mov	bp, crd_err_msg ; 22/04/2022	
   633                                  
   634                                  cp_rw_err:
   635                                  	; 21/04/2022
   636                                  	; cf = 1
   637                                  	sys	_close, [fnew]	
    99                              <1> 
   100                              <1>  %if %0 >= 2
   101 00000185 8B1E[2C03]          <1>  mov bx, %2
   102                              <1>  %if %0 >= 3
   103                              <1>  mov cx, %3
   104                              <1>  %if %0 >= 4
   105                              <1>  mov dx, %4
   106                              <1>  %endif
   107                              <1>  %endif
   108                              <1>  %endif
   109 00000189 B80600              <1>  mov ax, %1
   110 0000018C CD20                <1>  int 20h
   638 0000018E EBA1                    	jmp	write_err_msg
   639                                  
   640                                  cp_12:
   641                                  	; 24/04/2022 - cptest0.s
   642 00000190 A3[2E03]                	mov	[filesize], ax
   643 00000193 B8[1D03]                	mov	ax, rw_ok_msg
   644 00000196 E888FE                  	call	print_msg
   645                                  
   646                                  	; eax = read count
   647                                  	; eax = 0 -> eof
   648                                  	;or	eax, eax
   649                                  ; 24/04/2022 - cptest0.s
   650                                  ;	or	ax, ax ; 22/04/2022 (Retro UNIX 8086 v1)
   651                                  ;	jz	short cp_14 ; eof
   652                                  
   653                                  	; 24/04/2022 - cptest0.s
   654 00000199 B8[0D03]                	mov	ax, writing_msg
   655 0000019C E882FE                  	call	print_msg
   656 0000019F 89E8                    	mov	ax, bp ; (new) file name
   657 000001A1 E87DFE                  	call	print_msg
   658                                  
   659                                  	;;mov	ebp, eax  ; n
   660                                  	;mov	dx, ax ; 22/04/2022
   661                                  	; 24/04/2022 - cptest0.s
   662                                  	; [filesize] = read/write byte count
   663 000001A4 8B16[2E03]              	mov	dx, [filesize]
   664                                  
   665                                  	; write(fnew, iobuf, n)
   666                                  	;sys	_write, [fnew], iobuf, ebp
   667                                  	; 22/04/2022
   668                                  	sys	_write, [fnew], iobuf
    99                              <1> 
   100                              <1>  %if %0 >= 2
   101 000001A8 8B1E[2C03]          <1>  mov bx, %2
   102                              <1>  %if %0 >= 3
   103 000001AC B9[F403]            <1>  mov cx, %3
   104                              <1>  %if %0 >= 4
   105                              <1>  mov dx, %4
   106                              <1>  %endif
   107                              <1>  %endif
   108                              <1>  %endif
   109 000001AF B80400              <1>  mov ax, %1
   110 000001B2 CD20                <1>  int 20h
   669 000001B4 7208                    	jc	short cp_13
   670                                  
   671                                  	;if (write(fnew, iobuf, n) != n)
   672                                  	;   fprintf(stderr, "cp: write error.\n");
   673                                  	;   close(fold);
   674                                  	;   close(fnew);
   675                                  	;   return(1);
   676                                  
   677                                  	; eax = written bytes
   678                                  	;cmp	eax, ebp
   679                                  	;;je	short cp_11 ; read next (block)
   680                                  	;; 21/04/2022
   681                                  	;je	short cp_rw_next
   682                                  
   683                                  ; 24/04/2022 - cptest0.s
   684                                  ;	; 22/04/2022
   685                                  ;	cmp	ax, dx
   686                                  ;	je	short cp_rw_next
   687                                  
   688                                  	; 24/04/2022 - cptest0.s
   689 000001B6 B8[1D03]                	mov	ax, rw_ok_msg
   690 000001B9 E865FE                  	call	print_msg
   691 000001BC EB0B                    	jmp	short cp_14		
   692                                  
   693                                  	; error !
   694                                  	;; eax < ebp --> cf = 1
   695                                  	; 22/04/2022 - Retro UNIX 8086 v1
   696                                  	; ax < dx --> cf = 1
   697                                  cp_13:
   698                                  	; close new file
   699                                  	; and then write error mesage
   700                                  	; and then close old file
   701                                  	; and then write (move cursor to) next line
   702                                  	; and then return (from subroutine) 
   703                                  	;sys	_close, [fnew]
   704                                  
   705                                  	; 24/04/2022 - cptest0.s
   706 000001BE B8[4202]                	mov	ax, nextline
   707 000001C1 E85DFE                  	call	print_msg
   708                                  
   709                                  	; write error message
   710                                  	;mov	ebp, cwr_err_msg
   711                                  	; 22/04/2022
   712 000001C4 BD[B502]                	mov	bp, cwr_err_msg
   713                                  	;
   714                                  	;jmp	short write_err_msg
   715                                  	; 21/04/2022
   716 000001C7 EBBC                    	jmp	short cp_rw_err
   717                                  
   718                                  	; eof
   719                                  cp_14:
   720                                  	;close(fold);
   721                                  	;close(fnew);
   722                                  	;return(0);
   723                                  	
   724                                  	sys	_close, [fold]
    99                              <1> 
   100                              <1>  %if %0 >= 2
   101 000001C9 8B1E[2A03]          <1>  mov bx, %2
   102                              <1>  %if %0 >= 3
   103                              <1>  mov cx, %3
   104                              <1>  %if %0 >= 4
   105                              <1>  mov dx, %4
   106                              <1>  %endif
   107                              <1>  %endif
   108                              <1>  %endif
   109 000001CD B80600              <1>  mov ax, %1
   110 000001D0 CD20                <1>  int 20h
   725                                  	sys	_close, [fnew]
    99                              <1> 
   100                              <1>  %if %0 >= 2
   101 000001D2 8B1E[2C03]          <1>  mov bx, %2
   102                              <1>  %if %0 >= 3
   103                              <1>  mov cx, %3
   104                              <1>  %if %0 >= 4
   105                              <1>  mov dx, %4
   106                              <1>  %endif
   107                              <1>  %endif
   108                              <1>  %endif
   109 000001D6 B80600              <1>  mov ax, %1
   110 000001D9 CD20                <1>  int 20h
   726                                  	
   727                                  	;clc
   728 000001DB C3                      	retn
   729                                  	
   730                                  ;-----------------------------------------------------------------
   731                                  ;  data - initialized data
   732                                  ;-----------------------------------------------------------------
   733                                  
   734                                  ;argc:	dd 0
   735 000001DC 00                      argc:	db 0
   736                                  
   737                                  ; ----------------------------------------------------------------
   738                                  
   739                                  program_msg:
   740 000001DD 0D0A                    	db  0Dh, 0Ah
   741 000001DF 526574726F20554E49-     	db  'Retro UNIX 8086 v1 COPY TEST by Erdogan TAN - 24/04/2022'
   741 000001E8 582038303836207631-
   741 000001F1 20434F505920544553-
   741 000001FA 54206279204572646F-
   741 00000203 67616E2054414E202D-
   741 0000020C 2032342F30342F3230-
   741 00000215 3232               
   742                                  	;db 0Dh, 0Ah
   743 00000217 00                      	db  0
   744                                  
   745                                  usage_msg:
   746 00000218 0D0A                    	db  0Dh, 0Ah
   747 0000021A 55736167653A206370-     	db  'Usage: cptest: f1 f2; or cp f1 ... fn d2'
   747 00000223 746573743A20663120-
   747 0000022C 66323B206F72206370-
   747 00000235 206631202E2E2E2066-
   747 0000023E 6E206432           
   748                                  nextline:
   749 00000242 0D0A00                  	db  0Dh, 0Ah, 0
   750                                  
   751                                  cno_err_msg:
   752 00000245 0D0A                    	db 0Dh, 0Ah
   753 00000247 6370746573743A2063-     	db 'cptest: cannot open '
   753 00000250 616E6E6F74206F7065-
   753 00000259 6E20               
   754 0000025B 00                      	db 0
   755                                  cncis_err_msg:
   756 0000025C 0D0A                    	db 0Dh, 0Ah
   757 0000025E 6370746573743A2063-     	db 'cptest: cannot copy file to itself.'
   757 00000267 616E6E6F7420636F70-
   757 00000270 792066696C6520746F-
   757 00000279 20697473656C662E   
   758 00000281 0D0A00                  	db 0Dh, 0Ah, 0
   759                                  
   760                                  ccf_err_msg:
   761 00000284 0D0A                    	db 0Dh, 0Ah
   762 00000286 6370746573743A2063-     	db 'cptest: cannot create '
   762 0000028F 616E6E6F7420637265-
   762 00000298 61746520           
   763 0000029C 00                      	db 0
   764                                  
   765                                  crd_err_msg:
   766 0000029D 0D0A                    	db 0Dh, 0Ah
   767 0000029F 6370746573743A2072-     	db 'cptest: read error.'
   767 000002A8 656164206572726F72-
   767 000002B1 2E                 
   768 000002B2 0D0A00                  	db 0Dh, 0Ah, 0
   769                                  
   770                                  cwr_err_msg:
   771 000002B5 0D0A                    	db 0Dh, 0Ah
   772 000002B7 6370746573743A2077-     	db 'cptest: write error.'
   772 000002C0 72697465206572726F-
   772 000002C9 722E               
   773 000002CB 0D0A00                  	db 0Dh, 0Ah, 0
   774                                  
   775                                  ok_msg:
   776 000002CE 0D0A                    	db  0Dh, 0Ah
   777 000002D0 4F4B2E                  	db  'OK.'
   778 000002D3 0D0A00                  	db  0Dh, 0Ah, 0
   779                                  
   780 000002D6 00                      errors:	db 0
   781                                  
   782                                  	; 24/04/2022 - cptest0.s
   783                                  bigfile_err:
   784 000002D7 0D0A                    	db 0Dh, 0Ah
   785 000002D9 6370746573743A2062-     	db 'cptest: big file > '
   785 000002E2 69672066696C65203E-
   785 000002EB 20                 
   786                                  mfsstr:	
   787 000002EC 333235333020627974-     	db '32530 bytes.'
   787 000002F5 65732E             
   788 000002F8 0D0A00                  	db 0Dh, 0Ah, 0
   789                                  
   790                                  reading_msg:
   791 000002FB 0D0A                    	db 0Dh, 0Ah
   792 000002FD 52656164696E672066-     	db 'Reading file : '
   792 00000306 696C65203A20       
   793 0000030C 00                      	db 0
   794                                  writing_msg:
   795                                  	;db 0Dh, 0Ah
   796 0000030D 57726974696E672066-     	db 'Writing file : '
   796 00000316 696C65203A20       
   797 0000031C 00                      	db 0
   798                                  rw_ok_msg:
   799 0000031D 206973204F4B202E2E      	db ' is OK ..'
   800 00000326 0D0A00                  	db 0Dh, 0Ah, 0
   801                                  
   802                                  ;-----------------------------------------------------------------
   803                                  ;  bss - uninitialized data
   804                                  ;-----------------------------------------------------------------
   805                                  
   806 00000329 90                      align 2
   807                                  
   808                                  bss_start:
   809                                  
   810                                  ABSOLUTE bss_start
   811                                  
   812                                  ; 20/04/2022 - Retro UNIX 386 v1 & v1.1 & v1.2
   813                                  ;fold:	resd 1
   814                                  ;fnew:	resd 1
   815                                  ; 22/04/2022 - Retro UNIX 8086 v1
   816 0000032A ????                    fold:	resw 1
   817 0000032C ????                    fnew:	resw 1
   818                                  ; 24/04/2022
   819 0000032E ????                    filesize: resw 1
   820                                  
   821                                  ;stbuf2: resb 66 ; for Retro UNIX 386 v1.2 (66 byte sysstat data)
   822                                  ;stbuf1: resb 66 ; for Retro UNIX 386 v1.2 (66 byte sysstat data)
   823                                  ; 21/04/2022
   824 00000330 <res 22h>               stbuf2: resb 34 ; for Retro UNIX 386 v1.1 (34 byte sysstat data)
   825 00000352 <res 22h>               stbuf1: resb 34 ; for Retro UNIX 386 v1.1 (34 byte sysstat data)
   826                                  
   827                                  ; 24/04/2022 - cptest1.s
   828 00000374 <res 80h>               fnbuf:	resb 128 ; file name buffer
   829                                  
   830                                  ;iobuf:	resb BSIZE ; resb 512 ; path name buffer
   831 000003F4 <res 7B1Eh>             iobuf:	resb MAXFSIZE
   832                                  
   833                                  ; 20/04/2022
   834                                  ;-----------------------------------------------------------------
   835                                  ; Original UNIX v7 - cp (utility) c source code (cp.c)
   836                                  ;-----------------------------------------------------------------
   837                                  ;/* UNIX V7 source code: see www.tuhs.org for details. */;
   838                                  ;
   839                                  ;/*
   840                                  ; * cp oldfile newfile
   841                                  ; */
   842                                  ;
   843                                  ;#define BSIZE	512
   844                                  ;#include <stdio.h>
   845                                  ;#include <sys/types.h>
   846                                  ;#include <sys/stat.h>
   847                                  ;struct	stat stbuf1, stbuf2;
   848                                  ;char iobuf[BSIZE];
   849                                  ;
   850                                  ;main(argc, argv)
   851                                  ;char *argv[];
   852                                  ;{
   853                                  ;	register i, r;
   854                                  ;
   855                                  ;	if (argc < 3) 
   856                                  ;		goto usage;
   857                                  ;	if (argc > 3) {
   858                                  ;		if (stat(argv[argc-1], &stbuf2) < 0)
   859                                  ;			goto usage;
   860                                  ;		if ((stbuf2.st_mode&S_IFMT) != S_IFDIR) 
   861                                  ;			goto usage;
   862                                  ;	}
   863                                  ;	r = 0;
   864                                  ;	for(i=1; i<argc-1;i++)
   865                                  ;		r |= copy(argv[i], argv[argc-1]);
   866                                  ;	exit(r);
   867                                  ;usage:
   868                                  ;	fprintf(stderr, "Usage: cp: f1 f2; or cp f1 ... fn d2\n");
   869                                  ;	exit(1);
   870                                  ;}
   871                                  ;
   872                                  ;copy(from, to)
   873                                  ;char *from, *to;
   874                                  ;{
   875                                  ;	int fold, fnew, n;
   876                                  ;	register char *p1, *p2, *bp;
   877                                  ;	int mode;
   878                                  ;	if ((fold = open(from, 0)) < 0) {
   879                                  ;		fprintf(stderr, "cp: cannot open %s\n", from);
   880                                  ;		return(1);
   881                                  ;	}
   882                                  ;	fstat(fold, &stbuf1);
   883                                  ;	mode = stbuf1.st_mode;
   884                                  ;	/* is target a directory? */
   885                                  ;	if (stat(to, &stbuf2) >=0 &&
   886                                  ;	   (stbuf2.st_mode&S_IFMT) == S_IFDIR) {
   887                                  ;		p1 = from;
   888                                  ;		p2 = to;
   889                                  ;		bp = iobuf;
   890                                  ;		while(*bp++ = *p2++)
   891                                  ;			;
   892                                  ;		bp[-1] = '/';
   893                                  ;		p2 = bp;
   894                                  ;		while(*bp = *p1++)
   895                                  ;			if (*bp++ == '/')
   896                                  ;				bp = p2;
   897                                  ;		to = iobuf;
   898                                  ;	}
   899                                  ;	if (stat(to, &stbuf2) >= 0) {
   900                                  ;		if (stbuf1.st_dev == stbuf2.st_dev &&
   901                                  ;		   stbuf1.st_ino == stbuf2.st_ino) {
   902                                  ;			fprintf(stderr, "cp: cannot copy file to itself.\n");
   903                                  ;			return(1);
   904                                  ;		}
   905                                  ;	}
   906                                  ;	if ((fnew = creat(to, mode)) < 0) {
   907                                  ;		fprintf(stderr, "cp: cannot create %s\n", to);
   908                                  ;		close(fold);
   909                                  ;		return(1);
   910                                  ;	}
   911                                  ;	while(n = read(fold,  iobuf,  BSIZE)) {
   912                                  ;		if (n < 0) {
   913                                  ;			fprintf(stderr, "cp: read error\n");
   914                                  ;			close(fold);
   915                                  ;			close(fnew);
   916                                  ;			return(1);
   917                                  ;		} else
   918                                  ;			if (write(fnew, iobuf, n) != n) {
   919                                  ;				fprintf(stderr, "cp: write error.\n");
   920                                  ;				close(fold);
   921                                  ;				close(fnew);
   922                                  ;				return(1);
   923                                  ;			}
   924                                  ;	}
   925                                  ;	close(fold);
   926                                  ;	close(fnew);
   927                                  ;	return(0);
   928                                  ;}
