     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: 09/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[4203]              	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[3001]            <1>  mov bx, %2
    85                              <1>  %if %0 >= 3
    86 0000000B B9[4603]            <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[2F01]            <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[4003]                	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[4003]          <1>  mov bx, %2
    85                              <1>  %if %0 >= 3
    86 00000027 B9[6803]            <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[6803]                	mov	ax, [jnum]	   
   171 0000003B 3B06[4603]              	cmp	ax, [inum]
   172 0000003F 75E2                    	jne	short loop1
   173                                  	sys	_close, [file]
    82                              <1> 
    83                              <1>  %if %0 >= 2
    84 00000041 8B1E[4003]          <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[6803]29            	cmp	word [jnum], 41 ; root dir inode number
   176 0000004F 7439                    	je	short ckroot
   177 00000051 E89500                  	call	cat
   178                                  	sys	_chdir, dotdot
    82                              <1> 
    83                              <1>  %if %0 >= 2
    84 00000054 BB[2F01]            <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[3201]            <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[4203]              	mov	si, [off]
   190                                  	;mov	byte [si+name], 0Dh ; CR
   191 00000070 C784[4001]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[4001]            <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                                  	; 09/05/2022 (new -retro unix 8286 v1- kernel)
   222                                  	; (sysstat returns with device number in ax)
   223                                  ckroot:
   224                                  	; 09/05/2022	
   225                                  	sys	_stat, yname, _x
    82                              <1> 
    83                              <1>  %if %0 >= 2
    84 0000008A BB[6A03]            <1>  mov bx, %2
    85                              <1>  %if %0 >= 3
    86 0000008D B9[4603]            <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
   226 00000095 724F                    	jc	short ckroot_err
   227 00000097 A3[4403]                	mov	[idev], ax ; device number
   228                                  	sys	_chdir, root
    82                              <1> 
    83                              <1>  %if %0 >= 2
    84 0000009A BB[3401]            <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
   229 000000A2 7242                    	jc	short ckroot_err
   230                                  	sys	_open, root, 0 ; open root dir for read
    82                              <1> 
    83                              <1>  %if %0 >= 2
    84 000000A4 BB[3401]            <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
   231 000000AF 7235                    	jc	short ckroot_err
   232                                  	;mov	[file], ax
   233 000000B1 89C2                    	mov	dx, ax	
   234                                  ckroot_loop:
   235                                  	;sys	_read, [file], _y, 10
   236                                  			; read one dir entry	
   237                                  	sys	_read, dx, _y, 10
    82                              <1> 
    83                              <1>  %if %0 >= 2
    84 000000B3 89D3                <1>  mov bx, %2
    85                              <1>  %if %0 >= 3
    86 000000B5 B9[6803]            <1>  mov cx, %3
    87                              <1>  %if %0 >= 4
    88 000000B8 BA0A00              <1>  mov dx, %4
    89                              <1>  %endif
    90                              <1>  %endif
    91                              <1>  %endif
    92 000000BB B80300              <1>  mov ax, %1
    93 000000BE CD20                <1>  int 20h
   238 000000C0 7224                    	jc	short ckroot_err	
   239 000000C2 833E[6803]00            	cmp	word [jnum], 0
   240 000000C7 76EA                    	jna	short ckroot_loop
   241                                  	sys	_stat, yname, _x
    82                              <1> 
    83                              <1>  %if %0 >= 2
    84 000000C9 BB[6A03]            <1>  mov bx, %2
    85                              <1>  %if %0 >= 3
    86 000000CC B9[4603]            <1>  mov cx, %3
    87                              <1>  %if %0 >= 4
    88                              <1>  mov dx, %4
    89                              <1>  %endif
    90                              <1>  %endif
    91                              <1>  %endif
    92 000000CF B81200              <1>  mov ax, %1
    93 000000D2 CD20                <1>  int 20h
   242 000000D4 7210                    	jc	short ckroot_err
   243 000000D6 3B06[4403]              	cmp	ax, [idev] ; same device number ?
   244 000000DA 75D7                    	jne	short ckroot_loop ; no
   245 000000DC F606[4903]40            	test	byte [ximode+1], 40h ; directory ?
   246 000000E1 75D0                    	jne	short ckroot_loop
   247                                  	;
   248 000000E3 E80300                  	call	cat	
   249                                  	;
   250                                  ckroot_err:
   251 000000E6 E975FF                  	jmp	prname
   252                                  
   253                                  	
   254                                  ;	; 06/05/2022
   255                                  ;	; Note: For Retro UNIX 386 v1 & v1.1 & v1.2,
   256                                  ;	;	mounted device is handled via 'mnti'
   257                                  ;	;	field which keeps (device 0) mounting
   258                                  ;	;	directory inode number for mounted
   259                                  ;	;	device (1). Device number (0 or 1) check
   260                                  ;	;	(via 'sysstat') is not possible
   261                                  ;	;	for current Retro UNIX kernel versions
   262                                  ;	;	because 'sysstat' output does not contain
   263                                  ;	;	device number.
   264                                  ;	;	***
   265                                  ;	;	As a temporary solution (before a next
   266                                  ;	;	kernel version with new 'sysstat'),
   267                                  ;	;	root dir entries are checked for a subdir 
   268                                  ;	;	('usr' & 'mnt' dirs for now) with root dir
   269                                  ;	;	inode number (41 or 1), if there is..
   270                                  ;	;	root directory is a real (device 0) root
   271                                  ;	;	directory; if not, device 1 root directory 
   272                                  ;	;	is mounted to a sub directory of device 0.
   273                                  ;	; 	(Also it must be verified after 'chdir /')
   274                                  ;	;
   275                                  ;	;	assumptions...
   276                                  ;	;	a sub dir with root inode number	
   277                                  ;	;	dotdot / - chdir /
   278                                  ;	;	   no    -  no ----> root
   279                                  ;	;	   yes 	 - (yes) --> root, not checked
   280                                  ;	;	   no    - yes ----> mounted
   281                                  ;	;	   yes   - (no) ---> root, not checked				  
   282                                  ;
   283                                  ;ckroot:
   284                                  ;	; 06/05/2022
   285                                  ;	; check 'usr' directory
   286                                  ;	mov	dx, usr
   287                                  ;	call	statm 
   288                                  ;	jz	short ckroot_2	; root dir
   289                                  ;	; check 'mnt' directory
   290                                  ;	mov	dx, mnt
   291                                  ;	call	statm 
   292                                  ;	jz	short ckroot_2	; root dir
   293                                  ;
   294                                  ;	sys	_chdir, root
   295                                  ;	jc	short ckroot_2	; jmp short prname
   296                                  ;
   297                                  ;	; check 'usr' directory
   298                                  ;	mov	dx, usr
   299                                  ;	call	statm 
   300                                  ;	jz	short ckroot_1	; mounted
   301                                  ;	; check 'mnt' directory
   302                                  ;	mov	dx, mnt
   303                                  ;	call	statm 
   304                                  ;	jnz	short ckroot_2	; not mounted
   305                                  ;
   306                                  ;	; mounted
   307                                  ;ckroot_1:
   308                                  ;	; move mounting directory name
   309                                  ;	mov	si, dx
   310                                  ;	mov	di, yname
   311                                  ;	movsw
   312                                  ;	movsw
   313                                  ;	; concatenate (add to head of the path)
   314                                  ;	call	cat
   315                                  ;ckroot_2:
   316                                  ;	jmp	prname
   317                                  ;
   318                                  ;statm:
   319                                  ;	; 06/05/2022
   320                                  ;	sys	_stat, dx, _x
   321                                  ;	jc	short statm_1
   322                                  ;	;cmp	word [inum], 1	; Retro UNIX 386 v1.2
   323                                  ;	cmp	word [inum], 41 ; root dir inode number
   324                                  ;statm_1:
   325                                  ;	retn
   326                                  
   327                                  ;cat() {
   328                                  ;	int i, j;
   329                                  ;
   330                                  ;	i = -1;
   331                                  ;	while(y.name[++i] != 0);
   332                                  ;	if((off+i+2) > 511) prname();
   333                                  ;	for(j=off+1; j>=0; --j) name[j+i+1] = name[j];
   334                                  ;	off=i+off+1;
   335                                  ;	name[i] = root[0];
   336                                  ;	for(--i; i>=0; --i) name[i] = y.name[i];
   337                                  ;}
   338                                  
   339                                  cat:
   340 000000E9 31DB                    	xor	bx, bx
   341 000000EB 4B                      	dec	bx ; i = -1
   342                                  cat_0:
   343 000000EC 43                      	inc	bx ; ++i
   344 000000ED 80BF[6A03]00            	cmp	byte [yname+bx], 0
   345 000000F2 77F8                    	ja	short cat_0
   346 000000F4 89DA                    	mov	dx, bx ; i
   347 000000F6 8B0E[4203]              	mov	cx, [off]
   348 000000FA 41                      	inc	cx ; j = [off]+1
   349 000000FB 01CA                    	add	dx, cx  ; dx = [off]+i+1
   350 000000FD 81FAFD01                	cmp	dx, 511-2 ; (+ CRLF + 0) 
   351                                  	;;ja	short prname
   352                                  	;ja	short ckroot_2 ; jmp prname
   353 00000101 77E3                    	ja	short ckroot_err ; 09/05/2022
   354 00000103 BE[4001]                	mov	si, name
   355 00000106 01CE                    	add	si, cx ; name[j]
   356 00000108 89F7                    	mov	di, si
   357 0000010A 01DF                    	add	di, bx ; name[j+i]
   358 0000010C 47                      	inc	di ; name[j+i+1]	
   359                                  cat_1:
   360                                  	;std
   361                                  	;rep	stosb
   362                                  	;cld
   363 0000010D 49                      	dec	cx
   364 0000010E 7808                    	js	short cat_2
   365 00000110 4E                      	dec	si
   366 00000111 8A04                    	mov	al, [si]
   367 00000113 4F                      	dec	di
   368 00000114 8805                    	mov	[di], al
   369 00000116 EBF5                    	jmp	short cat_1
   370                                  cat_2:
   371 00000118 8916[4203]              	mov	[off], dx ; [off] = i+[off]+1
   372 0000011C C687[4001]2F            	mov	byte [name+bx], '/' ; name[i] = root[0]
   373                                  cat_3:
   374 00000121 4B                      	dec	bx ; --i
   375 00000122 780A                    	js	short cat_4 ; 0 -> -1
   376                                  		 ; name[i] = yname[i]
   377 00000124 8A87[6A03]              	mov	al, [yname+bx]
   378 00000128 8887[4001]              	mov	[name+bx], al
   379 0000012C EBF3                    	jmp	short cat_3 ; i >= 0
   380                                  cat_4:
   381 0000012E C3                      	retn
   382                                  
   383                                  ;-----------------------------------------------------------------
   384                                  ;  data - initialized data
   385                                  ;-----------------------------------------------------------------
   386                                  
   387                                  ; 05/05/2022
   388                                  
   389 0000012F 2E                      dotdot:	db '.'
   390 00000130 2E                      dot:	db '.'
   391 00000131 00                      	db 0
   392                                  
   393                                  ; 06/05/2022
   394                                  nextline:
   395 00000132 0D0A                    	db 0Dh, 0Ah
   396 00000134 2F00                    root:	db '/', 0
   397                                  
   398                                  ; default mounting directories (for current retro unix version)
   399 00000136 75737200                usr:	db 'usr', 0
   400 0000013A 6D6E7400                mnt:	db 'mnt', 0
   401                                  
   402                                  ;-----------------------------------------------------------------
   403                                  ;  bss - uninitialized data
   404                                  ;-----------------------------------------------------------------	
   405                                  
   406 0000013E 90<rep 2h>              align 4
   407                                  
   408                                  bss_start:
   409                                  
   410                                  ABSOLUTE bss_start
   411                                  
   412                                  ; 06/05/2022
   413                                  
   414 00000140 <res 200h>              name:	resb 512
   415                                  
   416 00000340 ????                    file:	resw 1
   417 00000342 ????                    off:	resw 1
   418                                  
   419                                  ; 09/05/2022
   420 00000344 ????                    idev:	resw 1 ; device number (0 = root, 1 = mounted)
   421                                  
   422                                  ; 06/05/2022
   423                                  _x:	; stat(us) buffer
   424                                  inum:
   425 00000346 ????                    	resw 1
   426                                  ximode:
   427                                  	;resb 64 ; Retro UNIX 386 v1.2
   428 00000348 <res 20h>               	resb 32 ; Retro UNIX 386 v1 & v1.1
   429                                  _y:	; directory entry buffer
   430 00000368 ????                    jnum:	resw 1
   431                                  yname:	;resb 14 ; Retro UNIX 386 v1.1 & v1.2
   432 0000036A ????????????????        	resb 8 ; Retro UNIX 386 v1 (unix v1)
   433 00000372 ????                    yzero:	resw 1
   434                                  
   435                                  ; 05/05/2022
   436                                  
   437                                  ;-----------------------------------------------------------------
   438                                  ; Original UNIX v5 - /usr/bin/pwd file - c source code (pwd.c)
   439                                  ;-----------------------------------------------------------------
   440                                  ;
   441                                  ;char dot[] ".";
   442                                  ;char dotdot[] "..";
   443                                  ;char root[] "/";
   444                                  ;char name[512];
   445                                  ;int file, off -1;
   446                                  ;struct statb {int devn, inum, i[18];}x;
   447                                  ;struct entry { int jnum; char name[16];}y;
   448                                  ;
   449                                  ;main() {
   450                                  ;	int n;
   451                                  ;
   452                                  ;loop0:
   453                                  ;	stat(dot, &x);
   454                                  ;	if((file = open(dotdot,0)) < 0) prname();
   455                                  ;loop1:
   456                                  ;	if((n = read(file,&y,16)) < 16) prname();
   457                                  ;	if(y.jnum != x.inum)goto loop1;
   458                                  ;	close(file);
   459                                  ;	if(y.jnum == 1) ckroot();
   460                                  ;	cat();
   461                                  ;	chdir(dotdot);
   462                                  ;	goto loop0;
   463                                  ;}
   464                                  ;ckroot() {
   465                                  ;	int i, n;
   466                                  ;
   467                                  ;	if((n = stat(y.name,&x)) < 0) prname();
   468                                  ;	i = x.devn;
   469                                  ;	if((n = chdir(root)) < 0) prname();
   470                                  ;	if((file = open(root,0)) < 0) prname();
   471                                  ;loop:
   472                                  ;	if((n = read(file,&y,16)) < 16) prname();
   473                                  ;	if(y.jnum == 0) goto loop;
   474                                  ;	if((n = stat(y.name,&x)) < 0) prname();
   475                                  ;	if(x.devn != i) goto loop;
   476                                  ;	x.i[0] =& 060000;
   477                                  ;	if(x.i[0] != 040000) goto loop;
   478                                  ;	cat();
   479                                  ;	prname();
   480                                  ;}
   481                                  ;prname() {
   482                                  ;	name[off] = '\n';
   483                                  ;	write(1,name,off+1);
   484                                  ;	exit();
   485                                  ;}
   486                                  ;cat() {
   487                                  ;	int i, j;
   488                                  ;
   489                                  ;	i = -1;
   490                                  ;	while(y.name[++i] != 0);
   491                                  ;	if((off+i+2) > 511) prname();
   492                                  ;	for(j=off+1; j>=0; --j) name[j+i+1] = name[j];
   493                                  ;	off=i+off+1;
   494                                  ;	name[i] = root[0];
   495                                  ;	for(--i; i>=0; --i) name[i] = y.name[i];
   496                                  ;}
