aboutsummaryrefslogtreecommitdiff
path: root/pkg/bench/xctest/unique.x
blob: fcabfe00da997ddc7a770a936b5d46a1d54b223c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

# UNIQUE -- Pass only unique lines from the (presumably sorted) standard
# input to the standard output.  In other words, if a sequence of identical
# lines are found in the input, only one copy is passed to the output.

procedure t_unique()

int	list, fd
pointer	sp, fname, old_line, new_line, temp
bool	streq()
int	getline(), clpopni(), clgfil(), clplen(), open()

begin
	call smark (sp)
	call salloc (fname, SZ_FNAME, TY_CHAR)
	call salloc (old_line, SZ_LINE, TY_CHAR)
	call salloc (new_line, SZ_LINE, TY_CHAR)

	list = clpopni ("files")

	while (clgfil (list, Memc[fname], SZ_FNAME) != EOF) {
	    fd = open (Memc[fname], READ_ONLY, TEXT_FILE)
	    if (clplen (list) > 1) {
		call printf ("\n\n==> %s <==\n")
		    call pargstr (Memc[fname])
	    }

	    Memc[old_line] = EOS

	    while (getline (fd, Memc[new_line]) != EOF) {
		if (streq (Memc[old_line], Memc[new_line]))
		    next
		call putline (STDOUT, Memc[new_line])

		# Swap buffers.
		temp = old_line
		old_line = new_line
		new_line = temp
	    }

	    call close (fd)
	}

	call sfree (sp)
end