1;
2; Ullrich von Bassewitz, 2005-04-21
3;
4; Search the environment for a string.
5;
6
7        .export searchenv, copyenvptr
8        .import __environ, __envcount
9        .import ptr1:zp, ptr2:zp, ptr3:zp
10
11.code
12
13;----------------------------------------------------------------------------
14; searchenv:
15;
16; ptr1 must contain the string to search for. On exit, the N flag will tell
17; if the entry was found, and X will contain the index of the environment
18; string in the environment (a negative value if the entry was not found).
19; On success, ptr3 will contain the entry and Y the offset of the '=' within
20; the string.
21
22.proc   searchenv
23
24; Copy the pointer to the environment to the zero page
25
26        jsr     copyenvptr
27
28; Loop over all environment entries trying to find the requested one.
29
30        ldx     __envcount
31@L0:    dex
32        bmi     @L9                     ; Out of entries
33
34; Since the maximum number of entries is 64, the index can only be 63, so
35; the following shift cannot overflow and the carry is clear.
36
37        txa
38        asl     a                       ; Mul by two for word access
39        tay
40        lda     (ptr2),y
41        sta     ptr3
42        iny
43        lda     (ptr2),y
44        sta     ptr3+1
45
46; ptr1 points to name, ptr3 points to the next environment entry. Compare the
47; two. The following loop limits the length of name to 255 bytes.
48
49        ldy     #$00
50@L1:    lda     (ptr1),y
51        beq     @L2                     ; Jump on end of name
52        cmp     (ptr3),y
53        bne     @L0                     ; Next environment entry
54        iny
55        bne     @L1
56
57; End of name reached, check if the environment entry contains a '=' char
58
59@L2:    lda     (ptr3),y
60        cmp     #'='
61        bne     @L0                     ; Next environment entry
62
63; Done. The function result is in X and the N flag is set correctly.
64
65@L9:    rts
66
67.endproc
68
69
70;----------------------------------------------------------------------------
71; copyenvptr: Copy _environ to ptr2
72;
73
74.proc   copyenvptr
75
76        lda     __environ
77        sta     ptr2
78        lda     __environ+1
79        sta     ptr2+1
80        rts
81
82.endproc
83
84
85