Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

redirection & piping in win2k.

Status
Not open for further replies.

denc4

Programmer
Feb 17, 2005
107
0
0
NL
hi. its been a while since i last posted, i've resumed some
work i have been doing a while ago. the program i am
planning to write relies heavily on redirection (> and < in
dos) and piping (using the | character in dos). when
retrieving data from another command via the special "|"
character in dos, a program has to read from file handle
0 (that's stdin), i thought. so i wrote a test program:

Code:
cseg segment para public

  org 100h
  main proc near
    mov ax,3F00h   ;file read function.
    xor bx,bx      ;file handle 0 stdin.
    mov cx,0FFFFh  ;read in 64k. although it never will.
    mov dx,1000h   ;put data in current segment at 4096.
    int 21h
    call disp_ax   ;display bytes read in decimal on screen.
    int 20h
  main endp

; ---------------------------------------------------

  disp_ax proc near
    mov bx,10
    xor cx,cx

  lp0:
    xor dx,dx
    div bx
    add dx,30h
    push dx
    inc cx
    cmp ax,0
    jnz lp0

    mov ah,2
  lp1:
    pop dx
    int 21h
    loop lp1
    ret
  disp_ax endp

cseg ends
end main

when i enter this command line in a win95 dosbox:

c:\>type 2kb_file.txt | mytest.com

the text file is 2 kilobytes and the output from
the mytest.com program is "2048". perfect it works.
when i do the same under win2k it outputs "512".
when the file is smaller than 512 bytes it outputs the
correct amount of bytes.

is this a limitation/bug/rule(?) under win2k??
can it only handle files that are <= 512 bytes??

i hope anyone knows, thanx.
 
c:\>type 2kb_file.txt | mytest.com

The output from the type command would normally go to stdout (handle 01) which is the screen. Your program therefore needs to read from stdout and not stdin. As a guess you should probably be looking at using functions 45h/46h which I think are used to manage redirection.


Hope this helps.


[vampire][bat]
 
> mov cx,0FFFFh ;read in 64k. although it never will.
> mov dx,1000h ;put data in current segment at 4096.
So what you're saying is that if the read actually read everything you requested, you would smash through the end of the data segment?

> can it only handle files that are <= 512 bytes??
I think all you've done here is discover the default block size on your two systems.

Generally speaking, you would need a loop to read the whole file anyway, so use a loop to count how many bytes are read until the return status from the int21 signals the end of file.

--
 
@earthandfire

the pipe character "converts" stdout from the command on the left to stdin for the command on the right.
i did replace handle 0 with 1 after reading your post, but the program just waited for input from the
keyboard in both below command lines:

c:\>type 2kb_file.txt | mytest.com

c:\>mytest.com

handle 1 is obviously not meant to be used for piping.
functions 45h and 46h refer to "duplicate file handle" and "force duplicate file handle".
i don't think these are specifically designed for, or will be of use in redirection and piping.
correct me if i'm wrong.

@Salem

yes. it would exceed the current segment IF i fed it a file larger than 61439 (65535 - 4096), but i never intended to do that :)
hence my comment on the mov cx,0FFFFh line.
it is just a test program with no error checking to familiarize myself in the use of piping in my programs.

you are right, 512 bytes seems to be the largest amount of bytes that can be read at a time from
file handle 0 (in win2k. haven't tested win95 yet). the idea of multiple blocks never came up to me.
to test this i added a second

mov ax,3F00h
xor bx,bx
mov cx,0FFFFh
mov dx,1000h
int 21h
call disp_ax

instruction pair in my program and fed it a 522 bytes file.
the output was "51210" (512 and 10 without CR/LF pair).
i'll use a loop to handle this correctly in any future programs.

furthermore i searched for a function that can change the default block size,
but there doesn't seem to be one.

thanx.
 
Wouldn't this redirection work? This should redirect stdin to use your 2kb_file.txt file.

mytest.com < 2kb_file.txt

Lee
 
that works very well, even on a 54000 bytes file.
but i still have to use a loop and read it in blocks, in case i use a file >64k.
 
i've looked into all of them, and although the last link is very interesting with regard
to other subjects, it isn't interesting for handling redirection on the command line.
they describe how to redirect output from a program that is called from within another one.
i see now why you mentioned function 45h and 46h, they are used to accomplish just that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top