Subversion Repositories SvarDOS

Rev

Rev 361 | Rev 421 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
361 mateuszvis 1
/*
2
 * routines used to manipulate the environment block
3
 */
4
 
5
#include <i86.h>
6
#include <string.h>
7
 
8
#include "env.h"
9
 
10
/* looks for varname in environment block and returns a far ptr to it if
11
 * found, NULL otherwise. varname MUST be in upper-case and MUST be terminated
12
 * by either a = sign or a NULL terminator */
13
char far *env_lookup(unsigned short env_seg, const char *varname) {
14
  char far *env = MK_FP(env_seg, 0);
15
  int i;
16
  for (;;) {
17
    /* is this it? */
18
    for (i = 0;; i++) {
19
      if ((varname[i] == '=') || (varname[i] == 0)) {
20
        if (env[i] == '=') return(env); /* FOUND! */
21
        break; /* else look for next string */
22
      }
23
      if (varname[i] != env[i]) break;
24
    }
25
    /* move env to end of current string */
26
    while (*env != 0) env++;
27
    /* if there's another trailing zero, then that is the end of environment */
28
    env++;
29
    if (*env == 0) return(NULL);
30
  }
31
}
32
 
33
 
34
/* returns the size, in bytes, of the allocated environment block */
35
unsigned short env_allocsz(unsigned short env_seg) {
36
  unsigned short far *mcbsz = MK_FP(env_seg - 1, 3); /* block size is a word at offset +3 in the MCB */
37
  return(*mcbsz * 16); /* return size in bytes, not paragraphs */
38
}
39
 
40
 
41
/* remove a variable from environment, if present. returns 0 on success, non-zero if variable not found */
42
int env_dropvar(unsigned short env_seg, const char *varname) {
43
  unsigned short blocksz, traillen;
44
  unsigned short len;
45
  char far *varptr = env_lookup(env_seg, varname);
46
 
47
  /* if variable not found in environment, quit now */
48
  if (varptr == NULL) return(-1);
49
 
50
  for (len = 0; varptr[len] != 0; len++); /* compute length of variable (without trailing null) */
51
  blocksz = env_allocsz(env_seg);           /* total environment size */
52
  traillen = blocksz - (FP_OFF(varptr) + len + 1); /* how much bytes are present after the variable */
53
  _fmemset(varptr, 0, len);               /* zero out the variable */
54
  if (traillen != 0) {
55
    _fmemmove(varptr, varptr + len + 1, traillen); /* move rest of memory */
56
  }
57
  return(0);
58
}
59
 
60
 
61
/* Writes a variable to environment block. The variable must in the form
62
 * "VARNAME=value". If variable is already present in the environment block,
63
 * then the value will be updated. If the new value is empty, then the
64
 * existing variable (if any) is removed.
65
 * VARNAME *MUST* be all-uppercase.
66
 *
67
 * This function returns:
68
 *   ENV_SUCCESS = success
69
 *   ENV_NOTENOM = not enough available space in memory block
70
 *   ENV_INVSYNT = invalid syntax
71
 */
72
int env_setvar(unsigned short env_seg, const char *v) {
73
  unsigned short envlen;
74
  unsigned short envfree;
75
  unsigned short vlen, veqpos;
76
  char far *env = MK_FP(env_seg, 0);
77
 
78
  /* remove variable from environment, if already set */
79
  env_dropvar(env_seg, v);
80
 
81
  /* compute v length and find the position of the eq sign */
82
  veqpos = 0xffff;
83
  for (vlen = 0; v[vlen] != 0; vlen++) {
84
    if (v[vlen] == '=') {
85
      if (veqpos != 0xffff) return(ENV_INVSYNT); /* equal sign is forbidden in value */
86
      veqpos = vlen;
87
    }
88
  }
89
 
90
  /* if variable empty, stop here */
91
  if (veqpos == vlen - 1) return(ENV_SUCCESS);
92
 
93
  /* compute current size of the environment */
94
  for (envlen = 0; env[envlen] != 0; envlen++) {
95
    while (env[envlen] != 0) envlen++; /* consume a string */
96
  }
97
 
98
  /* compute free space available in environment */
99
  envfree = env_allocsz(env_seg);
100
  envfree -= envlen;
101
  envfree -= 1; /* 1 byte for the environment's NULL terminator */
102
 
103
  /* do I have enough env space for the new var? */
104
  if (envfree < vlen + 1) return(ENV_NOTENOM);
105
 
106
  /* write the new variable (with its NULL terminator) to environment tail */
107
  _fmemcpy(env + envlen, v, vlen + 1);
108
 
109
  /* add the environment's NULL terminator */
110
  env[envlen + vlen + 1] = 0;
111
 
112
  return(ENV_SUCCESS);
113
}