summaryrefslogtreecommitdiff
path: root/macos/source/getenv.c
blob: 586815dd9f7119ef635bca8c6ee29969cdad9409 (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
  Copyright (c) 1990-2005 Info-ZIP.  All rights reserved.

  See the accompanying file LICENSE, version 2004-May-22 or later
  (the contents of which are also included in zip.h) for terms of use.
  If, for some reason, both of these files are missing, the Info-ZIP license
  also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html
*/
/*

This file implements the getenv() function.

#  Background:
#  Under Unix: Each Process (= running Program) has a set of
#  associated variables. The variables are called enviroment
#  variables and, together, constitute the process environment.
#  These variables include the search path, the terminal type,
#  and the user's login name.

#  Unfortunatelly the MacOS has no equivalent. So we need
#  a file to define the environment variables.
#  Name of this file is "MacZip.Env". It can be placed
#  in the current folder of MacZip or in the
#  preference folder of the system disk.
#  If MacZip founds the "MacZip.Env" file in the current
#  the folder of MacZip the "MacZip.Env" file in the
#  preference folder will be ignored.

#  An environment variable has a name and a value:
#  Name=Value
#  Note: Spaces are significant:
#  ZIPOPT=-r  and
#  ZIPOPT = -r are different !!!


 */

/*****************************************************************************/
/*  Includes                                                                 */
/*****************************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unix.h>
#include <Files.h>
#include <Folders.h>

#include "pathname.h"
#include "helpers.h"

/*****************************************************************************/
/*  Module level Vars                                                        */
/*****************************************************************************/

static char ListAllKeyValues = 0;
static unsigned LineNumber   = 0;
static char CompletePath[NAME_MAX];
Boolean IgnoreEnvironment    = false;  /* used by dialog.c and initfunc.c
                                          of the Mainapp */

/*****************************************************************************/
/*  Macros, typedefs                                                         */
/*****************************************************************************/

typedef struct _EnviromentPair {
    char *key;
    char *value;
} EnviromentPair;


#define MAX_COMMAND 1024


/*****************************************************************************/
/*  Prototypes                                                               */
/*****************************************************************************/


int get_char(FILE *file);
void unget_char(int ch,FILE *file);
int get_string(char *string,int size, FILE *file, char *terms);
void skip_comments(FILE *file);
char *load_entry(FILE *file);
char *getenv(const char *name);
EnviromentPair *ParseLine(char *line);
OSErr FSpFindFolder_Name(short vRefNum, OSType folderType,
                         Boolean createFolder,FSSpec *spec, unsigned char *name);
FILE * FSp_fopen(ConstFSSpecPtr spec, const char * open_mode);
void ShowAllKeyValues(void);
void Set_LineNum(unsigned ln);

/*****************************************************************************/
/*  Functions                                                                */
/*****************************************************************************/


/* get_string(str, max, file, termstr) : like fgets() but
 *      (1) has terminator string which should include \n
 *      (2) will always leave room for the null
 *      (3) uses get_char() so LineNumber will be accurate
 *      (4) returns EOF or terminating character, whichever
 */
int get_string(char *string, int size, FILE *file, char *terms)
{
    int ch;

    while (EOF != (ch = get_char(file)) && !strchr(terms, ch)) {
        if (size > 1) {
            *string++ = (char) ch;
            size--;
        }
    }

    if (size > 0)
        {
        *string = '\0';
        }

    return ch;
}




void Set_LineNum(unsigned ln)
{
 LineNumber = ln;
}



/* get_char(file) : like getc() but increment LineNumber on newlines
 */
int get_char(FILE *file)
{
    int ch;

    ch = getc(file);
    if (ch == '\n')
        {
        Set_LineNum(LineNumber + 1);
        }

    return ch;
}




/* skip_comments(file) : read past comment (if any)
 */
void skip_comments(FILE *file)
{
    int ch;

    while (EOF != (ch = get_char(file)))
        {
        /* ch is now the first character of a line.
         */

        while (ch == ' ' || ch == '\t')
            {
            ch = get_char(file);
            }

        if (ch == EOF)
            {
            break;
            }

        /* ch is now the first non-blank character of a line.
         */

        if (ch != '\n' && ch != '#')
            {
            break;
            }

        /* ch must be a newline or comment as first non-blank
         * character on a line.
         */

        while (ch != '\n' && ch != EOF)
            {
            ch = get_char(file);
            }

        /* ch is now the newline of a line which we're going to
         * ignore.
         */
    }

    if (ch != EOF)
        {
        unget_char(ch, file);
        }
}




/* unget_char(ch, file) : like ungetc but do LineNumber processing
 */
void unget_char(int ch, FILE *file)
{
    ungetc(ch, file);
    if (ch == '\n')
        {
        Set_LineNum(LineNumber - 1);
        }
}


/* this function reads one file entry -- the next -- from a file.
*  it skips any leading blank lines, ignores comments, and returns
*  NULL if for any reason the entry can't be read and parsed.
*/

char *load_entry(FILE *file)
{
    int ch;
    static char cmd[MAX_COMMAND];

    skip_comments(file);

    ch = get_string(cmd, MAX_COMMAND, file, "\n");

    if (ch == EOF)
        {
        return NULL;
        }

    return cmd;
}





EnviromentPair *ParseLine(char *line)
{
char *tmpPtr;
static EnviromentPair *Env;
unsigned short length = strlen(line);

Env->key   = "";
Env->value = "";

for (tmpPtr = line; *tmpPtr; tmpPtr++)
    {
    if (*tmpPtr == '=')
        {
        *tmpPtr = 0;
        Env->key = line;
        if (strlen(Env->key) < length)
            {
            Env->value = ++tmpPtr;
            }
        return Env;
        }
    }
return Env;
}





char *getenv(const char *name)
{
FILE *fp;
char *LineStr = NULL;
EnviromentPair *Env1;
FSSpec spec;
OSErr err;

if (IgnoreEnvironment)
    return NULL;  /* user wants to ignore the environment vars */

if (name == NULL)
    return NULL;

GetCompletePath(CompletePath,"MacZip.Env",&spec,&err);

/* try open the file in the current folder */
fp = FSp_fopen(&spec,"r");
if (fp == NULL)
    { /* Okey, lets try open the file in the preference folder */
    FSpFindFolder_Name(
                   kOnSystemDisk,
                   kPreferencesFolderType,
                   kDontCreateFolder,
                   &spec,
                   "\pMacZip.Env");
    fp = FSp_fopen(&spec,"r");
    if (fp == NULL)
        {
        return NULL; /* there is no enviroment-file */
        }
    }

LineStr = load_entry(fp);
while (LineStr != NULL)
    {   /* parse the file line by line */
    Env1 = ParseLine(LineStr);
    if (strlen(Env1->value) > 0)
        {       /* we found a key/value pair */
        if (ListAllKeyValues)
            printf("\n   Line:%3d   [%s] = [%s]",LineNumber,Env1->key,Env1->value);
        if (stricmp(name,Env1->key) == 0)
            {   /* we found the value of a given key */
            return Env1->value;
            }
        }
    LineStr = load_entry(fp);  /* read next line */
    }
fclose(fp);

return NULL;
}





OSErr FSpFindFolder_Name(
    short vRefNum,          /* Volume reference number. */
    OSType folderType,      /* Folder type taken by FindFolder. */
    Boolean createFolder,   /* Should we create it if non-existant. */
    FSSpec *spec,           /* Pointer to resulting directory. */
    unsigned char *name)    /* Name of the file in the folder */
{
    short foundVRefNum;
    long foundDirID;
    OSErr err;

    err = FindFolder(vRefNum, folderType, createFolder,
                     &foundVRefNum, &foundDirID);
    if (err != noErr)
        {
        return err;
        }

    err = FSMakeFSSpec(foundVRefNum, foundDirID, name, spec);
    return err;
}




void ShowAllKeyValues(void)
{
OSErr err;
FSSpec spec;
Boolean tmpIgnoreEnvironment = IgnoreEnvironment;

ListAllKeyValues = 1;
IgnoreEnvironment = false;

GetCompletePath(CompletePath,"MacZip.Env",&spec,&err);
if (err != 0)
    { /* Okey, lets try open the file in the preference folder */
    FSpFindFolder_Name(
                   kOnSystemDisk,
                   kPreferencesFolderType,
                   kDontCreateFolder,
                   &spec,
                   "\pMacZip.Env");
    GetFullPathFromSpec(CompletePath,&spec, &err);
    if (err != 0)
        {
        return; /* there is no enviroment-file */
        }
    }

printf("\nLocation of the current \"MacZip.Env\" file:\n [%s]",CompletePath);

printf("\n\nList of all environment variables\n");
getenv(" ");
printf("\n\nEnd\n\n");

/* restore used variables */
ListAllKeyValues = 0;
LineNumber = 0;
IgnoreEnvironment = tmpIgnoreEnvironment;
}