ArgEnvC.c 2.53 KB
Newer Older
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
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
/*
	Version 1.0.1
	Ronny Wichers Schreur
	ronny@cs.kun.nl
*/
# define kVariableSize	1

# ifndef NULL
# define NULL	((void *) 0)
# endif

/* defined by the Clean run-time system */
extern int global_argc;
extern char **global_argv;

# ifndef _WINDOWS_
# include <stdlib.h>
# endif

typedef struct
{
	int		length;
	char	characters [kVariableSize];
} *CleanString;

static int
CStringLength (char *s)
{
	char	*begin;

	begin	= s;
	while (*s != '\0')
		s++;

	return (s - begin);
} /* CStringLength */


# ifdef _WINDOWS_

/* return size of environment variable's value (including null-terminator)
	zero if variable doesn't exist */
int
ArgEnvGetEnvironmentVariableSizeC (CleanString name)
{
	char	smallBuffer;

	/* Assume that name has been null-terminated in Clean */
	return (GetEnvironmentVariableA (name->characters, &smallBuffer, 0));
} /* ArgEnvGetEnvironmentVariableSizeC */

/* copy value of environment variable to Clean string
	return False if value had to be truncated */
int
ArgEnvGetEnvironmentVariableCharsC (CleanString value, CleanString name)
{
	int	size, length;

	length	= value->length;

	/* Assume that name has been null-terminated in Clean */
	size	= GetEnvironmentVariableA (name->characters, value->characters, length);

	if (size <= length)
	{
		size	=	CStringLength (value->characters);
		if (size <= length)
			value->length	= size;
	}

	return (size >= value->length);
} /* ArgEnvGetEnvironmentVariableCharsC */

# else /* ifndef _WINDOWS_ */

void
ArgEnvGetEnvironmentVariableC (CleanString name, int *sizeP, char **stringP)
{
	char	*value;

	/* Assume that name has been null-terminated in Clean */
	value	= getenv (name->characters);
	if (value == NULL)
		*sizeP	= 0;
	else
		*sizeP	= CStringLength (value);

	*stringP	= value;
} /* ArgEnvGetEnvironmentVariableCharsC */

# endif /* _WINDOWS_ */

int
ArgEnvGetCommandLineCountC (void)
{
	return (global_argc);
} /* ArgEnvGetCommandLineCountC */

void
ArgEnvGetCommandLineArgumentC (int i, int *sizeP, char **stringP)
{
	char	*arg;

	/* Assume that i is within bounds */
	arg	= global_argv [i];

	*sizeP		= CStringLength (arg);
	*stringP	= arg;
} /* ArgEnvGetCommandLineArgumentC */

/* copy C string to Clean string
	return False if string had to be truncated */
int
ArgEnvCopyCStringToCleanStringC (char *cString, CleanString cleanString)
{
	int		i, length;
	char	*to;

	length	= cleanString->length;

	to	= cleanString->characters;
	i	= 0;
	while (*cString != '\0' && i < length)
	{
		i++;
		*to++	= *cString++;
	}
	cleanString->length	= i;

	return (i <= length);
} /* ArgEnvCopyCStringToCleanStringC */