forked from FDOS/freecom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.c
141 lines (125 loc) · 3.42 KB
/
set.c
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* $Id$
* Set environment variables
*
*/
#include "../config.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <fcntl.h>
#include "environ.h"
#include "nls_c.h"
#include "../include/cmdline.h"
#include "../include/command.h"
#include "../include/context.h"
#include "../include/misc.h"
#include "../err_fcts.h"
#include "../strings.h"
#define promptBuffer 256
/* optC determines if env variable's name (param) is set using
passed in case or uppercased, whereas upCaseValue determines
if the value the user passes in (with the /P option) is
stored as typed or uppercased. */
static int optC, promptUser, upCaseValue, optExecute;
optScanFct(opt_set)
{
(void)arg;
switch(ch) {
/* case 'A': return optScanBool(optA); arithmetic expression */
case 'C': return optScanBool(optC);
case 'U': return optScanBool(upCaseValue);
#if 1
case 'I': /* Display Information about current memory */
printf("Size of environment segment: %u bytes; unused: %u\n"
, env_resize(0, 0), env_freeCount(env_glbSeg));
return E_Other;
#endif
case 'P': return optScanBool(promptUser);
case 'E': return optScanBool(optExecute);
}
optErr();
return E_Useage;
}
int cmd_set(char *param)
{ char *value;
char *promptBuf = 0, tempcmd[255];
int ret;
optC = promptUser = upCaseValue = optExecute = 0;
if(leadOptions(¶m, opt_set, 0) != E_None)
return 1;
switch(breakVarAssign(ctxtEnvironment, param, &value)) {
case 1: /* no equal sign */
#ifdef FEATURE_CMD_SET_PRINT
if( ( value = getEnv( param ) ) != NULL ) printf( "%s\n", value );
else {
error_env_var_not_found( param );
return( 1 );
}
free(value);
return( 0 );
#else
error_syntax(0);
return 1;
#endif
case 0: /* displayed */
return 0;
#ifdef DEBUG
case 2: break;
default:
dprintf(("[SET: Invalid response from breakVarAssign()]\n"));
return 1;
#endif
}
if(promptUser) { /* -> Display the value, then read and assign */
int len;
assert(value);
outs(value);
promptBuf = malloc(promptBuffer);
if(!promptBuf) {
error_out_of_memory();
return E_NoMem;
}
len = dos_read(0, promptBuf, promptBuffer);
if(cbreak || len < 0) {
free(promptBuf);
return E_CBreak;
}
value = &promptBuf[len];
while(--value >= promptBuf && (*value == '\n' || *value == '\r'));
value[1] = '\0'; /* strip trailing newlines */
value = promptBuf;
}
if (optExecute) {
char *tempfile = tmpfn();
int fd, len;
if (!tempfile) return (1);
sprintf (tempcmd, "%s>%s", value, tempfile);
parsecommandline (tempcmd, TRUE);
fd = dos_open (tempfile, O_RDONLY);
if (fd < 0) {
unlink (tempfile);
free (tempfile);
return (1);
}
len = dos_read(fd, tempcmd, 254);
if (len >= 0) {
value = memchr(tempcmd, '\n', len);
if (value) len = value - tempcmd;
if (len > 0 && tempcmd[len-1] == '\r') len--;
tempcmd[len] = '\0';
}
value = tempcmd;
dos_close (fd);
unlink (tempfile);
free (tempfile);
}
/* If the value is just blanks, it means to delete the value;
but otherwise even leading and trailing spaces must be kept */
if(is_empty(value))
value = 0;
if (upCaseValue) StrUpr(value); /* set value as upper case, eg for if testing */
ret = chgEnvCase(optC, param, value);
free(promptBuf);
return ret;
}