     1                                  ; ****************************************************************************
     2                                  ; pwd8086.s (pwd0.s) - by Erdogan Tan - 05/05/2022
     3                                  ; ----------------------------------------------------------------------------
     4                                  ; Retro UNIX 8086 v1 - pwd - print working directory pathname
     5                                  ;
     6                                  ; [ Last Modification: 10/05/2022 ]
     7                                  ;
     8                                  ; Derived from (original) UNIX v5 'pwd.c' source Code
     9                                  ; Ref:
    10                                  ; www.tuhs.org (https://minnie.tuhs.org)
    11                                  ; v5root.tar.gz
    12                                  ; ****************************************************************************
    13                                  ; [ usr/source/s2/pwd.c (archive date: 27-11-1974) ]
    14                                  
    15                                  ; pwd0.s - Retro UNIX 8086 v1 (16 bit version of 'pwd1.s')
    16                                  ; pwd1.s - Retro UNIX 386 v1 (unix v1 inode structure)
    17                                  ; pwd2.s - Retro UNIX 386 v1.1 (16 byte directory entries)
    18                                  ; pwd3.s - Retro UNIX 386 v1.2 (& v2) (modified unix v7 inode)
    19                                  
    20                                  ; UNIX v1 system calls
    21                                  _rele 	equ 0
    22                                  _exit 	equ 1
    23                                  _fork 	equ 2
    24                                  _read 	equ 3
    25                                  _write	equ 4
    26                                  _open	equ 5
    27                                  _close 	equ 6
    28                                  _wait 	equ 7
    29                                  _creat 	equ 8
    30                                  _link 	equ 9
    31                                  _unlink	equ 10
    32                                  _exec	equ 11
    33                                  _chdir	equ 12
    34                                  _time 	equ 13
    35                                  _mkdir 	equ 14
    36                                  _chmod	equ 15
    37                                  _chown	equ 16
    38                                  _break	equ 17
    39                                  _stat	equ 18
    40                                  _seek	equ 19
    41                                  _tell 	equ 20
    42                                  _mount	equ 21
    43                                  _umount	equ 22
    44                                  _setuid	equ 23
    45                                  _getuid	equ 24
    46                                  _stime	equ 25
    47                                  _quit	equ 26	
    48                                  _intr	equ 27
    49                                  _fstat	equ 28
    50                                  _emt 	equ 29
    51                                  _mdate 	equ 30
    52                                  _stty 	equ 31
    53                                  _gtty	equ 32
    54                                  _ilgins	equ 33
    55                                  _sleep	equ 34 ; Retro UNIX 8086 v1 feature only !
    56                                  _msg    equ 35 ; Retro UNIX 386 v1 feature only !
    57                                  
    58                                  ;;;
    59                                  ESCKey equ 1Bh
    60                                  EnterKey equ 0Dh
    61                                  
    62                                  
    63                                  ;%macro sys 1-4
    64                                  ;   ; 03/09/2015
    65                                  ;   ; 13/04/2015
    66                                  ;   ; Retro UNIX 386 v1 system call.
    67                                  ;   %if %0 >= 2   
    68                                  ;       mov ebx, %2
    69                                  ;       %if %0 >= 3    
    70                                  ;           mov ecx, %3
    71                                  ;           ;%if %0 = 4
    72                                  ;           %if	%0 >= 4 ; 11/03/2022
    73                                  ;		mov edx, %4
    74                                  ;           %endif
    75                                  ;       %endif
    76                                  ;   %endif
    77                                  ;   mov eax, %1
    78                                  ;   int 30h
    79                                  ;%endmacro
    80                                  
    81                                  %macro sys 1-4
    82                                      ; Retro UNIX 8086 v1 system call.
    83                                      %if %0 >= 2   
    84                                          mov bx, %2
    85                                          %if %0 >= 3
    86                                              mov cx, %3
    87                                              %if %0 >= 4
    88                                                 mov dx, %4
    89                                              %endif
    90                                          %endif
    91                                      %endif
    92                                      mov ax, %1
    93                                      int 20h
    94                                  %endmacro
    95                                  
    96                                  ;; Retro UNIX 386 v1 system call format:
    97                                  ;; sys systemcall (eax) <arg1 (ebx)>, <arg2 (ecx)>, <arg3 (edx)>
    98                                  
    99                                  ;; 11/03/2022
   100                                  ;; Note: Above 'sys' macro has limitation about register positions;
   101                                  ;;	ebx, ecx, edx registers must not be used after their
   102                                  ;;	positions in sys macro.
   103                                  ;; for example:
   104                                  ;;	'sys _write, 1, msg, ecx' is defective, because
   105                                  ;;	 ecx will be used/assigned before edx in 'sys' macro.
   106                                  ;; correct order may be:
   107                                  ;;	'sys _write, 1, msg, eax ; (eax = byte count)
   108                                  
   109                                  ; Retro UNIX 8086 v1 system call format:
   110                                  ; sys systemcall (ax) <arg1 (bx)>, <arg2 (cx)>, <arg3 (dx)>
   111                                  
   112                                  ; ----------------------------------------------------------------------------
   113                                  
   114                                  [BITS 16] ; 16-bit intructions (for 8086/x86 real mode)
   115                                  
   116                                  [ORG 0] 
   117                                  
   118                                  START_CODE:
   119                                  	; 06/05/2022 (16 bit version) -ax,bx,cx,dx-
   120                                  	; 06/05/2022 (32 bit version) -eax,ebx,ecx,edx-
   121                                  	; 05/05/2022
   122                                  
   123                                  ;main() {
   124                                  ;	int n;
   125                                  
   126                                  	;  clear (reset) stack (not necessary)
   127 00000000 59                      	pop	cx ; cx = number of arguments
   128                                  pwd_0:
   129 00000001 58                      	pop	ax ; ax = argument 0 = executable file name
   130 00000002 E2FD                    	loop	pwd_0
   131                                  
   132                                  	;mov	byte [y.zero], 0 ; (not necessary)
   133                                  			; (because Retro UNIX kernel
   134                                  			; clears bss -memory page- while
   135                                  			; assigning it to program)
   136                                  	;mov	word [off], 0
   137                                  
   138                                  	; 06/05/2022
   139 00000004 FF0E[4A03]              	dec	word [off] 
   140                                  
   141                                  ;loop0:
   142                                  ;	stat(dot, &x);
   143                                  ;	if((file = open(dotdot,0)) < 0) prname();
   144                                  
   145                                  loop0:
   146                                  	sys	_stat, dot, _x
    82                              <1> 
    83                              <1>  %if %0 >= 2
    84 00000008 BB[3701]            <1>  mov bx, %2
    85                              <1>  %if %0 >= 3
    86 0000000B B9[4E03]            <1>  mov cx, %3
    87                              <1>  %if %0 >= 4
    88                              <1>  mov dx, %4
    89                              <1>  %endif
    90                              <1>  %endif
    91                              <1>  %endif
    92 0000000E B81200              <1>  mov ax, %1
    93 00000011 CD20                <1>  int 20h
   147                                  
   148                                  	sys	_open, dotdot, 0  ; open for read
    82                              <1> 
    83                              <1>  %if %0 >= 2
    84 00000013 BB[3601]            <1>  mov bx, %2
    85                              <1>  %if %0 >= 3
    86 00000016 B90000              <1>  mov cx, %3
    87                              <1>  %if %0 >= 4
    88                              <1>  mov dx, %4
    89                              <1>  %endif
    90                              <1>  %endif
    91                              <1>  %endif
    92 00000019 B80500              <1>  mov ax, %1
    93 0000001C CD20                <1>  int 20h
   149 0000001E 723E                    	jc	short prname
   150                                  
   151                                  ;loop1:
   152                                  ;	if((n = read(file,&y,16)) < 16) prname();
   153                                  ;	if(y.jnum != x.inum)goto loop1;
   154                                  ;	close(file);
   155                                  ;	if(y.jnum == 1) ckroot();
   156                                  ;	cat();
   157                                  ;	chdir(dotdot);
   158                                  ;	goto loop0;
   159                                  ;}
   160                                  
   161 00000020 A3[4803]                	mov	[file], ax
   162                                  loop1:
   163                                  	sys	_read, [file], _y, 10 ; Retro UNIX 386 v1
    82                              <1> 
    83                              <1>  %if %0 >= 2
    84 00000023 8B1E[4803]          <1>  mov bx, %2
    85                              <1>  %if %0 >= 3
    86 00000027 B9[7003]            <1>  mov cx, %3
    87                              <1>  %if %0 >= 4
    88 0000002A BA0A00              <1>  mov dx, %4
    89                              <1>  %endif
    90                              <1>  %endif
    91                              <1>  %endif
    92 0000002D B80300              <1>  mov ax, %1
    93 00000030 CD20                <1>  int 20h
   164                                  	;sys	_read, [file], _y, 16 ; Retro UNIX 386 v1.1 & v1.2
   165 00000032 722A                    	jc	short prname
   166                                  	;cmp	ax, 10	 ; cmp eax, 16
   167                                  	;jb	short prname	
   168 00000034 09C0                    	or	ax, ax
   169 00000036 7426                    	jz	short prname
   170 00000038 A1[7003]                	mov	ax, [jnum]	   
   171 0000003B 3B06[4E03]              	cmp	ax, [inum]
   172 0000003F 75E2                    	jne	short loop1
   173                                  	sys	_close, [file]
    82                              <1> 
    83                              <1>  %if %0 >= 2
    84 00000041 8B1E[4803]          <1>  mov bx, %2
    85                              <1>  %if %0 >= 3
    86                              <1>  mov cx, %3
    87                              <1>  %if %0 >= 4
    88                              <1>  mov dx, %4
    89                              <1>  %endif
    90                              <1>  %endif
    91                              <1>  %endif
    92 00000045 B80600              <1>  mov ax, %1
    93 00000048 CD20                <1>  int 20h
   174                                  	;cmp	word [jnum], 1	; Retro UNIX 386 v1.2
   175 0000004A 833E[7003]29            	cmp	word [jnum], 41 ; root dir inode number
   176 0000004F 7439                    	je	short ckroot
   177 00000051 E89C00                  	call	cat
   178                                  	sys	_chdir, dotdot
    82                              <1> 
    83                              <1>  %if %0 >= 2
    84 00000054 BB[3601]            <1>  mov bx, %2
    85                              <1>  %if %0 >= 3
    86                              <1>  mov cx, %3
    87                              <1>  %if %0 >= 4
    88                              <1>  mov dx, %4
    89                              <1>  %endif
    90                              <1>  %endif
    91                              <1>  %endif
    92 00000057 B80C00              <1>  mov ax, %1
    93 0000005A CD20                <1>  int 20h
   179 0000005C EBAA                    	jmp	short loop0
   180                                  
   181                                  ;prname() {
   182                                  ;	name[off] = '\n';
   183                                  ;	write(1,name,off+1);
   184                                  ;	exit();
   185                                  
   186                                  prname:
   187                                  	sys	_write, 1, nextline, 3	; 06/05/2022
    82                              <1> 
    83                              <1>  %if %0 >= 2
    84 0000005E BB0100              <1>  mov bx, %2
    85                              <1>  %if %0 >= 3
    86 00000061 B9[3901]            <1>  mov cx, %3
    87                              <1>  %if %0 >= 4
    88 00000064 BA0300              <1>  mov dx, %4
    89                              <1>  %endif
    90                              <1>  %endif
    91                              <1>  %endif
    92 00000067 B80400              <1>  mov ax, %1
    93 0000006A CD20                <1>  int 20h
   188                                  
   189 0000006C 8B36[4A03]              	mov	si, [off]
   190                                  	;mov	byte [si+name], 0Dh ; CR
   191 00000070 C784[4801]0D0A          	mov	word [si+name], 0A0Dh  ; CRLF 
   192                                  	;inc	dword [off]
   193 00000076 46                      	inc	si
   194 00000077 46                      	inc	si
   195                                  	;mov	[off], si
   196                                  	;sys	_write, 1, name, [off]
   197                                  	sys	_write, 1, name, si
    82                              <1> 
    83                              <1>  %if %0 >= 2
    84 00000078 BB0100              <1>  mov bx, %2
    85                              <1>  %if %0 >= 3
    86 0000007B B9[4801]            <1>  mov cx, %3
    87                              <1>  %if %0 >= 4
    88 0000007E 89F2                <1>  mov dx, %4
    89                              <1>  %endif
    90                              <1>  %endif
    91                              <1>  %endif
    92 00000080 B80400              <1>  mov ax, %1
    93 00000083 CD20                <1>  int 20h
   198                                  exit:
   199                                  	sys	_exit
    82                              <1> 
    83                              <1>  %if %0 >= 2
    84                              <1>  mov bx, %2
    85                              <1>  %if %0 >= 3
    86                              <1>  mov cx, %3
    87                              <1>  %if %0 >= 4
    88                              <1>  mov dx, %4
    89                              <1>  %endif
    90                              <1>  %endif
    91                              <1>  %endif
    92 00000085 B80100              <1>  mov ax, %1
    93 00000088 CD20                <1>  int 20h
   200                                  ;hang:
   201                                  ;	nop
   202                                  ;	jmp	short hang
   203                                  
   204                                  ;ckroot() {
   205                                  ;	int i, n;
   206                                  ;
   207                                  ;	if((n = stat(y.name,&x)) < 0) prname();
   208                                  ;	i = x.devn;
   209                                  ;	if((n = chdir(root)) < 0) prname();
   210                                  ;	if((file = open(root,0)) < 0) prname();
   211                                  ;loop:
   212                                  ;	if((n = read(file,&y,16)) < 16) prname();
   213                                  ;	if(y.jnum == 0) goto loop;
   214                                  ;	if((n = stat(y.name,&x)) < 0) prname();
   215                                  ;	if(x.devn != i) goto loop;
   216                                  ;	x.i[0] =& 060000;
   217                                  ;	if(x.i[0] != 040000) goto loop;
   218                                  ;	cat();
   219                                  ;	prname();
   220                                  
   221                                  	; 10/05/2022
   222                                  	; 09/05/2022 (new -retro unix 8286 v1- kernel)
   223                                  	; (sysstat returns with device number in ax)
   224                                  ckroot:
   225                                  	; 09/05/2022	
   226                                  	sys	_stat, yname, _x
    82                              <1> 
    83                              <1>  %if %0 >= 2
    84 0000008A BB[7203]            <1>  mov bx, %2
    85                              <1>  %if %0 >= 3
    86 0000008D B9[4E03]            <1>  mov cx, %3
    87                              <1>  %if %0 >= 4
    88                              <1>  mov dx, %4
    89                              <1>  %endif
    90                              <1>  %endif
    91                              <1>  %endif
    92 00000090 B81200              <1>  mov ax, %1
    93 00000093 CD20                <1>  int 20h
   227 00000095 7256                    	jc	short ckroot_err
   228 00000097 A3[4C03]                	mov	[idev], ax ; device number
   229                                  	sys	_chdir, root
    82                              <1> 
    83                              <1>  %if %0 >= 2
    84 0000009A BB[3B01]            <1>  mov bx, %2
    85                              <1>  %if %0 >= 3
    86                              <1>  mov cx, %3
    87                              <1>  %if %0 >= 4
    88                              <1>  mov dx, %4
    89                              <1>  %endif
    90                              <1>  %endif
    91                              <1>  %endif
    92 0000009D B80C00              <1>  mov ax, %1
    93 000000A0 CD20                <1>  int 20h
   230 000000A2 7249                    	jc	short ckroot_err
   231                                  	sys	_open, root, 0 ; open root dir for read
    82                              <1> 
    83                              <1>  %if %0 >= 2
    84 000000A4 BB[3B01]            <1>  mov bx, %2
    85                              <1>  %if %0 >= 3
    86 000000A7 B90000              <1>  mov cx, %3
    87                              <1>  %if %0 >= 4
    88                              <1>  mov dx, %4
    89                              <1>  %endif
    90                              <1>  %endif
    91                              <1>  %endif
    92 000000AA B80500              <1>  mov ax, %1
    93 000000AD CD20                <1>  int 20h
   232 000000AF 723C                    	jc	short ckroot_err
   233                                  	; 10/05/2022
   234 000000B1 A3[4803]                	mov	[file], ax
   235                                  ckroot_loop:
   236                                  	sys	_read, [file], _y, 10
    82                              <1> 
    83                              <1>  %if %0 >= 2
    84 000000B4 8B1E[4803]          <1>  mov bx, %2
    85                              <1>  %if %0 >= 3
    86 000000B8 B9[7003]            <1>  mov cx, %3
    87                              <1>  %if %0 >= 4
    88 000000BB BA0A00              <1>  mov dx, %4
    89                              <1>  %endif
    90                              <1>  %endif
    91                              <1>  %endif
    92 000000BE B80300              <1>  mov ax, %1
    93 000000C1 CD20                <1>  int 20h
   237                                  			; read one dir entry	
   238 000000C3 7228                    	jc	short ckroot_err
   239                                  	; 10/05/2022
   240 000000C5 09C0                    	or	ax, ax
   241 000000C7 7424                    	jz	short ckroot_err	
   242 000000C9 833E[7003]00            	cmp	word [jnum], 0
   243 000000CE 76E4                    	jna	short ckroot_loop
   244                                  	sys	_stat, yname, _x
    82                              <1> 
    83                              <1>  %if %0 >= 2
    84 000000D0 BB[7203]            <1>  mov bx, %2
    85                              <1>  %if %0 >= 3
    86 000000D3 B9[4E03]            <1>  mov cx, %3
    87                              <1>  %if %0 >= 4
    88                              <1>  mov dx, %4
    89                              <1>  %endif
    90                              <1>  %endif
    91                              <1>  %endif
    92 000000D6 B81200              <1>  mov ax, %1
    93 000000D9 CD20                <1>  int 20h
   245 000000DB 7210                    	jc	short ckroot_err
   246 000000DD 3B06[4C03]              	cmp	ax, [idev] ; same device number ?
   247 000000E1 75D1                    	jne	short ckroot_loop ; no
   248                                  	; 10/05/2022
   249 000000E3 F606[5103]40            	test	byte [ximode+1], 40h ; directory ?
   250 000000E8 74CA                    	jz	short ckroot_loop
   251                                  	;
   252 000000EA E80300                  	call	cat	
   253                                  	;
   254                                  ckroot_err:
   255 000000ED E96EFF                  	jmp	prname
   256                                  
   257                                  	
   258                                  ;	; 06/05/2022
   259                                  ;	; Note: For Retro UNIX 386 v1 & v1.1 & v1.2,
   260                                  ;	;	mounted device is handled via 'mnti'
   261                                  ;	;	field which keeps (device 0) mounting
   262                                  ;	;	directory inode number for mounted
   263                                  ;	;	device (1). Device number (0 or 1) check
   264                                  ;	;	(via 'sysstat') is not possible
   265                                  ;	;	for current Retro UNIX kernel versions
   266                                  ;	;	because 'sysstat' output does not contain
   267                                  ;	;	device number.
   268                                  ;	;	***
   269                                  ;	;	As a temporary solution (before a next
   270                                  ;	;	kernel version with new 'sysstat'),
   271                                  ;	;	root dir entries are checked for a subdir 
   272                                  ;	;	('usr' & 'mnt' dirs for now) with root dir
   273                                  ;	;	inode number (41 or 1), if there is..
   274                                  ;	;	root directory is a real (device 0) root
   275                                  ;	;	directory; if not, device 1 root directory 
   276                                  ;	;	is mounted to a sub directory of device 0.
   277                                  ;	; 	(Also it must be verified after 'chdir /')
   278                                  ;	;
   279                                  ;	;	assumptions...
   280                                  ;	;	a sub dir with root inode number	
   281                                  ;	;	dotdot / - chdir /
   282                                  ;	;	   no    -  no ----> root
   283                                  ;	;	   yes 	 - (yes) --> root, not checked
   284                                  ;	;	   no    - yes ----> mounted
   285                                  ;	;	   yes   - (no) ---> root, not checked				  
   286                                  ;
   287                                  ;ckroot:
   288                                  ;	; 06/05/2022
   289                                  ;	; check 'usr' directory
   290                                  ;	mov	dx, usr
   291                                  ;	call	statm 
   292                                  ;	jz	short ckroot_2	; root dir
   293                                  ;	; check 'mnt' directory
   294                                  ;	mov	dx, mnt
   295                                  ;	call	statm 
   296                                  ;	jz	short ckroot_2	; root dir
   297                                  ;
   298                                  ;	sys	_chdir, root
   299                                  ;	jc	short ckroot_2	; jmp short prname
   300                                  ;
   301                                  ;	; check 'usr' directory
   302                                  ;	mov	dx, usr
   303                                  ;	call	statm 
   304                                  ;	jz	short ckroot_1	; mounted
   305                                  ;	; check 'mnt' directory
   306                                  ;	mov	dx, mnt
   307                                  ;	call	statm 
   308                                  ;	jnz	short ckroot_2	; not mounted
   309                                  ;
   310                                  ;	; mounted
   311                                  ;ckroot_1:
   312                                  ;	; move mounting directory name
   313                                  ;	mov	si, dx
   314                                  ;	mov	di, yname
   315                                  ;	movsw
   316                                  ;	movsw
   317                                  ;	; concatenate (add to head of the path)
   318                                  ;	call	cat
   319                                  ;ckroot_2:
   320                                  ;	jmp	prname
   321                                  ;
   322                                  ;statm:
   323                                  ;	; 06/05/2022
   324                                  ;	sys	_stat, dx, _x
   325                                  ;	jc	short statm_1
   326                                  ;	;cmp	word [inum], 1	; Retro UNIX 386 v1.2
   327                                  ;	cmp	word [inum], 41 ; root dir inode number
   328                                  ;statm_1:
   329                                  ;	retn
   330                                  
   331                                  ;cat() {
   332                                  ;	int i, j;
   333                                  ;
   334                                  ;	i = -1;
   335                                  ;	while(y.name[++i] != 0);
   336                                  ;	if((off+i+2) > 511) prname();
   337                                  ;	for(j=off+1; j>=0; --j) name[j+i+1] = name[j];
   338                                  ;	off=i+off+1;
   339                                  ;	name[i] = root[0];
   340                                  ;	for(--i; i>=0; --i) name[i] = y.name[i];
   341                                  ;}
   342                                  
   343                                  cat:
   344 000000F0 31DB                    	xor	bx, bx
   345 000000F2 4B                      	dec	bx ; i = -1
   346                                  cat_0:
   347 000000F3 43                      	inc	bx ; ++i
   348 000000F4 80BF[7203]00            	cmp	byte [yname+bx], 0
   349 000000F9 77F8                    	ja	short cat_0
   350 000000FB 89DA                    	mov	dx, bx ; i
   351 000000FD 8B0E[4A03]              	mov	cx, [off]
   352 00000101 41                      	inc	cx ; j = [off]+1
   353 00000102 01CA                    	add	dx, cx  ; dx = [off]+i+1
   354 00000104 81FAFD01                	cmp	dx, 511-2 ; (+ CRLF + 0) 
   355                                  	;;ja	short prname
   356                                  	;ja	short ckroot_2 ; jmp prname
   357 00000108 77E3                    	ja	short ckroot_err ; 09/05/2022
   358 0000010A BE[4801]                	mov	si, name
   359 0000010D 01CE                    	add	si, cx ; name[j]
   360 0000010F 89F7                    	mov	di, si
   361 00000111 01DF                    	add	di, bx ; name[j+i]
   362 00000113 47                      	inc	di ; name[j+i+1]	
   363                                  cat_1:
   364                                  	;std
   365                                  	;rep	stosb
   366                                  	;cld
   367 00000114 49                      	dec	cx
   368 00000115 7808                    	js	short cat_2
   369 00000117 4E                      	dec	si
   370 00000118 8A04                    	mov	al, [si]
   371 0000011A 4F                      	dec	di
   372 0000011B 8805                    	mov	[di], al
   373 0000011D EBF5                    	jmp	short cat_1
   374                                  cat_2:
   375 0000011F 8916[4A03]              	mov	[off], dx ; [off] = i+[off]+1
   376 00000123 C687[4801]2F            	mov	byte [name+bx], '/' ; name[i] = root[0]
   377                                  cat_3:
   378 00000128 4B                      	dec	bx ; --i
   379 00000129 780A                    	js	short cat_4 ; 0 -> -1
   380                                  		 ; name[i] = yname[i]
   381 0000012B 8A87[7203]              	mov	al, [yname+bx]
   382 0000012F 8887[4801]              	mov	[name+bx], al
   383 00000133 EBF3                    	jmp	short cat_3 ; i >= 0
   384                                  cat_4:
   385 00000135 C3                      	retn
   386                                  
   387                                  ;-----------------------------------------------------------------
   388                                  ;  data - initialized data
   389                                  ;-----------------------------------------------------------------
   390                                  
   391                                  ; 05/05/2022
   392                                  
   393 00000136 2E                      dotdot:	db '.'
   394 00000137 2E                      dot:	db '.'
   395 00000138 00                      	db 0
   396                                  
   397                                  ; 06/05/2022
   398                                  nextline:
   399 00000139 0D0A                    	db 0Dh, 0Ah
   400 0000013B 2F00                    root:	db '/', 0
   401                                  
   402                                  ; default mounting directories (for current retro unix version)
   403 0000013D 75737200                usr:	db 'usr', 0
   404 00000141 6D6E7400                mnt:	db 'mnt', 0
   405                                  
   406                                  ;-----------------------------------------------------------------
   407                                  ;  bss - uninitialized data
   408                                  ;-----------------------------------------------------------------	
   409                                  
   410 00000145 90<rep 3h>              align 4
   411                                  
   412                                  bss_start:
   413                                  
   414                                  ABSOLUTE bss_start
   415                                  
   416                                  ; 06/05/2022
   417                                  
   418 00000148 <res 200h>              name:	resb 512
   419                                  
   420 00000348 ????                    file:	resw 1
   421 0000034A ????                    off:	resw 1
   422                                  
   423                                  ; 09/05/2022
   424 0000034C ????                    idev:	resw 1 ; device number (0 = root, 1 = mounted)
   425                                  
   426                                  ; 06/05/2022
   427                                  _x:	; stat(us) buffer
   428                                  inum:
   429 0000034E ????                    	resw 1
   430                                  ximode:
   431                                  	;resb 64 ; Retro UNIX 386 v1.2
   432 00000350 <res 20h>               	resb 32 ; Retro UNIX 386 v1 & v1.1
   433                                  _y:	; directory entry buffer
   434 00000370 ????                    jnum:	resw 1
   435                                  yname:	;resb 14 ; Retro UNIX 386 v1.1 & v1.2
   436 00000372 ????????????????        	resb 8 ; Retro UNIX 386 v1 (unix v1)
   437 0000037A ????                    yzero:	resw 1
   438                                  
   439                                  ; 05/05/2022
   440                                  
   441                                  ;-----------------------------------------------------------------
   442                                  ; Original UNIX v5 - /usr/bin/pwd file - c source code (pwd.c)
   443                                  ;-----------------------------------------------------------------
   444                                  ;
   445                                  ;char dot[] ".";
   446                                  ;char dotdot[] "..";
   447                                  ;char root[] "/";
   448                                  ;char name[512];
   449                                  ;int file, off -1;
   450                                  ;struct statb {int devn, inum, i[18];}x;
   451                                  ;struct entry { int jnum; char name[16];}y;
   452                                  ;
   453                                  ;main() {
   454                                  ;	int n;
   455                                  ;
   456                                  ;loop0:
   457                                  ;	stat(dot, &x);
   458                                  ;	if((file = open(dotdot,0)) < 0) prname();
   459                                  ;loop1:
   460                                  ;	if((n = read(file,&y,16)) < 16) prname();
   461                                  ;	if(y.jnum != x.inum)goto loop1;
   462                                  ;	close(file);
   463                                  ;	if(y.jnum == 1) ckroot();
   464                                  ;	cat();
   465                                  ;	chdir(dotdot);
   466                                  ;	goto loop0;
   467                                  ;}
   468                                  ;ckroot() {
   469                                  ;	int i, n;
   470                                  ;
   471                                  ;	if((n = stat(y.name,&x)) < 0) prname();
   472                                  ;	i = x.devn;
   473                                  ;	if((n = chdir(root)) < 0) prname();
   474                                  ;	if((file = open(root,0)) < 0) prname();
   475                                  ;loop:
   476                                  ;	if((n = read(file,&y,16)) < 16) prname();
   477                                  ;	if(y.jnum == 0) goto loop;
   478                                  ;	if((n = stat(y.name,&x)) < 0) prname();
   479                                  ;	if(x.devn != i) goto loop;
   480                                  ;	x.i[0] =& 060000;
   481                                  ;	if(x.i[0] != 040000) goto loop;
   482                                  ;	cat();
   483                                  ;	prname();
   484                                  ;}
   485                                  ;prname() {
   486                                  ;	name[off] = '\n';
   487                                  ;	write(1,name,off+1);
   488                                  ;	exit();
   489                                  ;}
   490                                  ;cat() {
   491                                  ;	int i, j;
   492                                  ;
   493                                  ;	i = -1;
   494                                  ;	while(y.name[++i] != 0);
   495                                  ;	if((off+i+2) > 511) prname();
   496                                  ;	for(j=off+1; j>=0; --j) name[j+i+1] = name[j];
   497                                  ;	off=i+off+1;
   498                                  ;	name[i] = root[0];
   499                                  ;	for(--i; i>=0; --i) name[i] = y.name[i];
   500                                  ;}
