File: config.c

package info (click to toggle)
cpbk 2.0-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 248 kB
  • ctags: 168
  • sloc: ansic: 2,193; makefile: 114; sh: 35
file content (165 lines) | stat: -rw-r--r-- 3,768 bytes parent folder | download
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
/*****************************************************************************
 *  Backup Copy                                                              *
 *  Programmed by: Kevin Lindsay                                             *
 *  Copyright (c) 1998 NetNation Communications Inc                          *
 * ***************************************************************************/ 

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include "config.h"
#include "statmalloc.h"


/* Load file into buffer */
char *
load_file(const char *filename)
{
   int res = 1;
   int fptr;
   struct stat *fbufstat;
   char *fbuf;
   
   fbufstat = (struct stat *) malloc(sizeof(struct stat));
   
   if ((fptr = open(filename,O_RDONLY)) != -1) {
      fstat(fptr,fbufstat);
      
      fbuf = (char *) malloc(fbufstat->st_size+1);
      
      read(fptr,fbuf,fbufstat->st_size);
      
      close(fptr);
      
   } else {
      res = 0;
      fbuf = (char *) malloc(1);
      fbuf[0] = '\0';
   }
   
   return(fbuf);
} 

/* Get Config Item */

char *
get_config(char *name, char *filebuf, char gtype)
{
   char *value = NULL;
   char *ptr1;
   char *ptr2;   
   int jump = 0;
   char *tempval = NULL;
   int grow;
   
   ptr1 = filebuf;
   
   while (!jump) {
      
      if ((ptr1 = strstr(ptr1,name)) != NULL) {
         ptr2 = ptr1;
         while (ptr2[0] != '\n' && ptr2[0] != '#' && ptr2 != filebuf)
             ptr2--;
         
         if (ptr2[0] != '#') 
             jump = 1;
         else         
             ptr1++;
         
      } else {
         fprintf(stderr,"Configuration File Error! Could not find field: %s\n",name);
         return("NULL");
      }
      
   }
   
   if (gtype == 'S') {
      if ((ptr1 = strchr(ptr1,' ')) == NULL) {
         fprintf(stderr,"Configuration File Error! Could not find value for field: %s\n",name);
         return("NULL");
      }
      
      while (isblank(ptr1[0]))
          ptr1++;
      
      ptr2 = ptr1;
      
      while (!isblank(ptr2[0]) && ptr2[0] != '\n')
          ptr2++;
      
      value = (char *)statmalloc((ptr2-ptr1)+1);
      value[0] = '\0';
      strncat(value,ptr1,ptr2-ptr1);

   } else if (gtype == 'M') {
   
      if ((ptr1 = strchr(ptr1,'\n')) == NULL) {
         fprintf(stderr,"Configuratin File Error! EXCLUDE DIR Option incorrectly setup!\n");
         return("NULL");
      }
      
      ptr1++;
      
      jump = 0;
      grow = 1;
      
      value = (char *)malloc(1);
      value[0] = '\0';
      
      
      while (!jump) {
         
         ptr2 = ptr1;
         
         while (!isblank(ptr2[0]) && ptr2[0] != '\n')
             ptr2++;
         
         ptr2--;
         
         if (ptr2[0] != '/')
             ptr2++;
         
         tempval = (char *)realloc(tempval,(ptr2-ptr1)+1);
         tempval[0] = '\0';
         strncat(tempval,ptr1,ptr2-ptr1);         
         
         if (strcasecmp(tempval,"END") != 0) {
            grow += (ptr2-ptr1)+2;
            value = (char *)realloc(value,grow);
            strcat(value,"^");
            strcat(value,tempval);
            strcat(value,"^");
         } else jump = 1;
         
         if ((ptr1 = strchr(ptr1,'\n')) == NULL) {
            fprintf(stderr,"Configuratin File Error! EXCLUDE DIR Option incorrectly setup!\n");
            return("NULL");
         }
         
         ptr1++;
         
         while (isblank(ptr1[0]))
             ptr1++;
         
      }
   }
   
   return(value);
   
//   free(value);
}

#ifdef IRIX_CPBK
int isblank(int c)
{
        if (c == ' ' || c == '\n' || c =='\t' ) return 1;

        return 0;
} /* isblank */
#endif