home *** CD-ROM | disk | FTP | other *** search
- /*
- * $Filename: ConfigFiles.c $
- * $Revision: 0.4 $
- * $Date: 1993/12/15 20:08:37 $
- *
- * Copyright (C) 1993 by Peter Simons <simons@peti.GUN.de>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- *
- * $Id: ConfigFiles.c,v 0.4 1993/12/15 20:08:37 simons Exp $
- *
- * ------------------------------- log history ----------------------------
- * $Log: ConfigFiles.c,v $
- * Revision 0.4 1993/12/15 20:08:37 simons
- * The buffer GetConfigEntry() returns now isn't static anymore!!
- *
- * Revision 0.3 1993/12/11 14:22:18 simons
- * Added file-locking.
- *
- * Revision 0.2 1993/12/11 14:16:44 simons
- * Removed the netsupport.library specific stuff and added LoadFile().
- *
- * Revision 0.1 1993/12/11 14:00:27 simons
- * Taken from the netsupport.library. Several changes will be necessary
- * to make it work with LharcUUCP.
- */
-
-
- /**************************************************************************
- * *
- * SEKTION: Labels, Macros, Switches, Structures *
- * *
- **************************************************************************/
-
- /************************************* Includes ***********/
- #include <string.h>
- #include <stdlib.h>
-
- #include <dos/dos.h>
- #include <proto/dos.h>
-
- /************************************* Defines ************/
-
- /************************************* Prototypes *********/
- char * GetConfigEntry(char * , char * , char * );
- char * LoadUUConfig(void);
- char * GetKeyword(char * , char * );
- char * GetKeywordLine(char * , char * );
- char * InterpretKeywordLine(char * );
- char * LoadFile(char * );
-
- void LockFile(char *);
- int FileIsLocked(char *);
- void UnLockFile(char *);
- void UnLockFiles(void);
-
- /************************************* global variables ***/
-
- static const char __RCSId[] = "$Id: ConfigFiles.c,v 0.4 1993/12/15 20:08:37 simons Exp $";
-
- /**************************************************************************
- * *
- * SEKTION: main functions *
- * *
- **************************************************************************/
-
- char *GetConfigEntry(char *filename, char *keyword, char *def)
- {
- char *parameter, *filebuffer;
-
- /* If no filename is given, we assume the UUCP main config
- * file.
- */
-
- if (filename) {
- LockFile(filename);
- filebuffer = LoadFile(filename);
- UnLockFile(filename);
- }
- else
- filebuffer = LoadUUConfig();
-
-
- /* If the file has been loaded successfully, find the keyword
- * and returen the parameter.
- */
-
- if (filebuffer) {
- if (parameter = GetKeywordLine(keyword, filebuffer))
- parameter = InterpretKeywordLine(parameter);
-
- free(filebuffer); /* free filebuffer */
- }
-
- return (parameter) ? parameter : def;
- }
-
-
-
- /**************************************************************************
- * *
- * SEKTION: internal functions *
- * *
- **************************************************************************/
-
- char *LoadUUConfig(void)
- {
- char *filebuffer;
-
-
- LockFile("S:UUConfig");
- filebuffer = LoadFile("S:UUConfig");
- UnLockFile("S:UUConfig");
-
- if (!filebuffer) {
- LockFile("UULIB:Config");
- filebuffer = LoadFile("UULIB:Config");
- UnLockFile("UULIB:Config");
- }
-
- return filebuffer;
- }
-
-
- char * GetKeyword(char *keyword, char *buffer)
- {
- char *parameter;
-
- if (parameter = GetKeywordLine(keyword, buffer))
- parameter = InterpretKeywordLine(parameter);
-
-
- /* If GetKeywordLine() didn't find the keyword, return a pointer
- * to the provided default. */
-
- if (!(*parameter))
- parameter = &keyword[strlen(keyword)+1];
-
- return parameter;
- }
-
-
- char * GetKeywordLine(char *keyword, char *buffer)
- {
- int keywdlen;
-
- if (!(keywdlen = strlen(keyword)))
- return NULL; /* no keyword provided */
-
- while (strnicmp(keyword, buffer, keywdlen)) {
- while(*buffer != '\0' && *buffer != '\n')
- buffer++; /* search for '\0' or '\n' */
-
- if (!(*buffer)) /* if '\0', break loop */
- break;
-
- buffer++; /* skip '\n' and compare next line */
- }
-
- return (*buffer) ? buffer : NULL;
- }
-
- char * InterpretKeywordLine(char *keywordline)
- {
- char *parameter;
- int i;
-
- while (*keywordline != ' ' && *keywordline != '\t')
- keywordline++; /* skip keyword */
-
- while (*keywordline == ' ' || *keywordline == '\t')
- keywordline++; /* skip leading blanks or tabs */
-
- for (i = 0; keywordline[i] != '\n' && keywordline[i] != '\0'; i++)
- ; /* find end of line */
-
- while (keywordline[i-1] == ' ' || keywordline[i-1] == '\t')
- i--; /* remove trailing blanks or tabs */
-
- if (parameter = malloc(i+1)) {
- parameter[i] = '\0'; /* send end of string */
- while (i--) /* copy string to keywordline */
- parameter[i] = keywordline[i];
- }
-
- return parameter;
- }
-
-
- /*
- * LoadFile() -- determines the length of a given file, allocates the
- * appropiate buffer and actually loads the file. If everything works,
- * the address of the buffer is returned and the length of the file is
- * available via IoErr().
- *
- * ATTENTION: A zero-byte is appended to the loaded file, to enable the
- * usage of standard C string routines on the contents.
- *
- * The buffer should be freed after usage using free().
- */
-
- char *LoadFile(char *filename)
- {
- BPTR fh, fh2;
- struct FileInfoBlock *fib;
- char *memblock, *filebuffer = NULL;
- BOOL isfile = FALSE;
- int readlen;
-
- if (fh = Lock(filename, ACCESS_READ)) {
- if (fib = malloc(sizeof(struct FileInfoBlock))) {
- if (Examine(fh, fib)) {
- if (fh2 = OpenFromLock(fh)) {
- isfile = TRUE;
- if (memblock = malloc(fib->fib_Size + 1)) {
- if ((readlen = Read(fh2, memblock, fib->fib_Size)) != -1L) {
- memblock[readlen] = '\0';
- filebuffer = memblock;
- SetIoErr(readlen);
- }
- else
- free(memblock);
- }
- Close(fh2);
- }
- }
- free(fib);
- }
- if (!(isfile))
- UnLock(fh);
- }
-
- return filebuffer;
- }
-
-