123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- #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"
- static char ListAllKeyValues = 0;
- static unsigned LineNumber = 0;
- static char CompletePath[NAME_MAX];
- Boolean IgnoreEnvironment = false;
- typedef struct _EnviromentPair {
- char *key;
- char *value;
- } EnviromentPair;
- #define MAX_COMMAND 1024
- 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);
- 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;
- }
- int get_char(FILE *file)
- {
- int ch;
- ch = getc(file);
- if (ch == '\n')
- {
- Set_LineNum(LineNumber + 1);
- }
- return ch;
- }
- void skip_comments(FILE *file)
- {
- int ch;
- while (EOF != (ch = get_char(file)))
- {
-
- while (ch == ' ' || ch == '\t')
- {
- ch = get_char(file);
- }
- if (ch == EOF)
- {
- break;
- }
-
- if (ch != '\n' && ch != '#')
- {
- break;
- }
-
- while (ch != '\n' && ch != EOF)
- {
- ch = get_char(file);
- }
-
- }
- if (ch != EOF)
- {
- unget_char(ch, file);
- }
- }
- void unget_char(int ch, FILE *file)
- {
- ungetc(ch, file);
- if (ch == '\n')
- {
- Set_LineNum(LineNumber - 1);
- }
- }
- 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;
- if (name == NULL)
- return NULL;
- GetCompletePath(CompletePath,"MacZip.Env",&spec,&err);
- fp = FSp_fopen(&spec,"r");
- if (fp == NULL)
- {
- FSpFindFolder_Name(
- kOnSystemDisk,
- kPreferencesFolderType,
- kDontCreateFolder,
- &spec,
- "\pMacZip.Env");
- fp = FSp_fopen(&spec,"r");
- if (fp == NULL)
- {
- return NULL;
- }
- }
- LineStr = load_entry(fp);
- while (LineStr != NULL)
- {
- Env1 = ParseLine(LineStr);
- if (strlen(Env1->value) > 0)
- {
- if (ListAllKeyValues)
- printf("\n Line:%3d [%s] = [%s]",LineNumber,Env1->key,Env1->value);
- if (stricmp(name,Env1->key) == 0)
- {
- return Env1->value;
- }
- }
- LineStr = load_entry(fp);
- }
- fclose(fp);
- return NULL;
- }
- OSErr FSpFindFolder_Name(
- short vRefNum,
- OSType folderType,
- Boolean createFolder,
- FSSpec *spec,
- unsigned char *name)
- {
- 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)
- {
- FSpFindFolder_Name(
- kOnSystemDisk,
- kPreferencesFolderType,
- kDontCreateFolder,
- &spec,
- "\pMacZip.Env");
- GetFullPathFromSpec(CompletePath,&spec, &err);
- if (err != 0)
- {
- return;
- }
- }
- 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");
- ListAllKeyValues = 0;
- LineNumber = 0;
- IgnoreEnvironment = tmpIgnoreEnvironment;
- }
|