File size: Difference between revisions

Added S-BASIC example
(Jakt)
(Added S-BASIC example)
Line 1,973:
 
}</syntaxhighlight>
 
=={{header|S-BASIC}}==
The CP/M operating system -- S-BASIC's native environment --
reports file size as the number of 128-byte records. CP/M also
has no notion of a "root" directory, user area 0 on drive A (the
default on boot-up) being the closest analog. Although S-BASIC has a
built-in SIZE function, it returns the number of blocks
(allocation groups) occupied by the file -- which
varies with the disk format -- and even then
gives the wrong answer if a directory entry controls more than
one 16K logical extent.
<syntaxhighlight = "BASIC">
rem Set the logged drive ('A' to 'P')
procedure setdrive (drive = char)
var hl, de, bc, a_psw = integer
rem -- make sure drive letter is upper case!
if drive >= 'a' then drive = drive - 32
hl = 0
de = drive - 65
bc = 0EH
a_psw = 0
call (5H,hl,de,bc,a_psw)
end
 
rem Set the CP/M user area (0 to 15)
procedure setuser (user = integer)
var hl, bc, a_psw = integer
hl = 0
bc = 20H
a_psw = 0
call (5H,hl,user,bc,a_psw)
end
 
comment
Return size of named file as number of 128-byte records;
assumes file name is upper case. If the file does not
exist, the size will be reported as 0.
end
function fsize(filename = string:20) = integer
var hl, de, bc, a_psw, p = integer
based fname = string:20
based sz = integer
dim byte workfcb(36)
location array de = workfcb
base fname at de
base sz at de + 33
fname = fcb$(filename)
rem See if drive was specified and set FCB accordingly
p = instr(1,filename,":")
if p = 0 then
workfcb(0) = 0
else
workfcb(0) = asc(mid(filename,p-1,1)) - 64
bc = 23H rem BDOS filesize function
call (5,hl,de,bc,a_psw) rem result stored in sz
end = sz
 
rem Exercise the function
 
var filename = string:20
filename = "INPUT.TXT"
rem First check current drive and user
print filename;" occupies";fsize(filename)*128;" bytes"
rem Then check startup directory (A0:)
setdrive 'A'
setuser 0
print "A0:INPUT.TXT occupies";fsize(filename)*128;" bytes"
 
end
</syntaxhighlight>
{{out}}
Although both instances of INPUT.TXT consist of a single line
("The quick brown fox jumps over the lazy red dog") they will
each be reported as occupying a 128-byte record.
<pre>
INPUT.TXT occupies 128 bytes
A0:INPUT.TXT occupies 128 bytes
</pre>
 
 
=={{header|Scala}}==
211

edits