Subversion Repositories SvarDOS

Rev

Rev 570 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 570 Rev 1731
Line 1... Line 1...
1
/* This file is part of the SvarCOM project and is published under the terms
1
/* This file is part of the SvarCOM project and is published under the terms
2
 * of the MIT license.
2
 * of the MIT license.
3
 *
3
 *
4
 * Copyright (C) 2021-2022 Mateusz Viste
4
 * Copyright (C) 2021-2024 Mateusz Viste
5
 *
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * Permission is hereby granted, free of charge, to any person obtaining a
7
 * copy of this software and associated documentation files (the "Software"),
7
 * copy of this software and associated documentation files (the "Software"),
8
 * to deal in the Software without restriction, including without limitation
8
 * to deal in the Software without restriction, including without limitation
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
Line 90... Line 90...
90
  unsigned short far *mcbsz = MK_FP(env_seg - 1, 3); /* block size is a word at offset +3 in the MCB */
90
  unsigned short far *mcbsz = MK_FP(env_seg - 1, 3); /* block size is a word at offset +3 in the MCB */
91
  return(*mcbsz * 16); /* return size in bytes, not paragraphs */
91
  return(*mcbsz * 16); /* return size in bytes, not paragraphs */
92
}
92
}
93
 
93
 
94
 
94
 
-
 
95
/* return currently used space (length, in bytes) of the environment */
-
 
96
unsigned short env_getcurlen(unsigned short env_seg) {
-
 
97
  char far *env = MK_FP(env_seg, 0);
-
 
98
  unsigned short envlen;
-
 
99
  for (envlen = 0; env[envlen] != 0; envlen++) {
-
 
100
    while (env[envlen] != 0) envlen++; /* consume a string */
-
 
101
  }
-
 
102
  return(envlen);
-
 
103
}
-
 
104
 
-
 
105
 
95
/* remove a variable from environment, if present. returns 0 on success, non-zero if variable not found */
106
/* remove a variable from environment, if present. returns 0 on success, non-zero if variable not found */
96
int env_dropvar(unsigned short env_seg, const char *varname) {
107
int env_dropvar(unsigned short env_seg, const char *varname) {
97
  unsigned short blocksz, traillen;
108
  unsigned short blocksz, traillen;
98
  unsigned short len;
109
  unsigned short len;
99
  char far *varptr = env_lookup(env_seg, varname);
110
  char far *varptr = env_lookup(env_seg, varname);
Line 123... Line 134...
123
 *   ENV_NOTENOM = not enough available space in memory block
134
 *   ENV_NOTENOM = not enough available space in memory block
124
 *   ENV_INVSYNT = invalid syntax
135
 *   ENV_INVSYNT = invalid syntax
125
 */
136
 */
126
int env_setvar(unsigned short env_seg, const char *v) {
137
int env_setvar(unsigned short env_seg, const char *v) {
127
  unsigned short envlen;
138
  unsigned short envlen;
128
  unsigned short envfree;
-
 
129
  unsigned short vlen, veqpos;
139
  unsigned short vlen, veqpos;
130
  char far *env = MK_FP(env_seg, 0);
140
  char far *env = MK_FP(env_seg, 0);
131
 
141
 
132
  /* remove variable from environment, if already set */
142
  /* remove variable from environment, if already set */
133
  env_dropvar(env_seg, v);
143
  env_dropvar(env_seg, v);
Line 142... Line 152...
142
  }
152
  }
143
 
153
 
144
  /* if variable empty, stop here */
154
  /* if variable empty, stop here */
145
  if (veqpos == vlen - 1) return(ENV_SUCCESS);
155
  if (veqpos == vlen - 1) return(ENV_SUCCESS);
146
 
156
 
147
  /* compute current size of the environment */
157
  /* get length of the current environment */
148
  for (envlen = 0; env[envlen] != 0; envlen++) {
-
 
149
    while (env[envlen] != 0) envlen++; /* consume a string */
-
 
150
  }
-
 
151
 
-
 
152
  /* compute free space available in environment */
-
 
153
  envfree = env_allocsz(env_seg);
158
  envlen = env_getcurlen(env_seg);
154
  envfree -= envlen;
-
 
155
  envfree -= 1; /* 1 byte for the environment's NULL terminator */
-
 
156
 
159
 
157
  /* do I have enough env space for the new var? */
160
  /* do I have enough env space for the new var? */
158
  if (envfree < vlen + 1) return(ENV_NOTENOM);
161
  if (envlen + vlen + 2 >= env_allocsz(env_seg)) return(ENV_NOTENOM);
159
 
162
 
160
  /* write the new variable (with its NULL terminator) to environment tail */
163
  /* write the new variable (with its NULL terminator) to environment tail */
161
  _fmemcpy(env + envlen, v, vlen + 1);
164
  _fmemcpy(env + envlen, v, vlen + 1);
162
 
165
 
163
  /* add the environment's NULL terminator */
166
  /* add the environment's NULL terminator */