File: util.c

package info (click to toggle)
libproplist 0.7.1-4
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 684 kB
  • ctags: 452
  • sloc: ansic: 3,684; sh: 1,856; lex: 181; yacc: 118; makefile: 106; tcl: 40
file content (339 lines) | stat: -rw-r--r-- 6,749 bytes parent folder | download | duplicates (5)
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
/* util.c:

   Miscellaneous stuff

   */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

#include <errno.h>

#include "proplist.h"

#include "plconf.h"

#include "util.h"

/*
 * handling sockets
 */

int GetServerSocket(int from, int to, int *actual)
{
  int sock;
  struct protoent *pent;
  char hostname[256];
  struct hostent *localhost;
  struct sockaddr_in saddr;
  int current;

  if(!(pent = getprotobyname("tcp")))
    return -1;
  
  if((sock = socket(AF_INET, SOCK_STREAM, pent->p_proto)) < 0)
    return -1;

  if((gethostname(hostname, 255)) < 0)
    return -1;

  if(!(localhost = gethostbyname(hostname)))
    return -1;

  *actual = 0;
  current = from;

  saddr.sin_family = AF_INET;
  bcopy((char *)localhost->h_addr, (char *)&(saddr.sin_addr),
	localhost->h_length);

  while(current <= to)
    {
      saddr.sin_port = htons(current);
  
      if((bind(sock, (struct sockaddr *)&saddr,
	       sizeof(struct sockaddr_in))) == 0)
	{
	  if((listen(sock, 5)) < 0)
	    return -1;
	  else
	    {
	      *actual = current;
	      return sock;
	    }
	}
      else /* bind() failed */
	current++;
    }
  /* gone through all allowed ports, still no bind */
  return -1;
}

int GetClientSocket(int portno)
{
  int sock;
  struct protoent *pent;
  char hostname[256];
  struct hostent *localhost;
  struct sockaddr_in saddr;

  if(!(pent = getprotobyname("tcp")))
    return -1;
  
  if((sock = socket(AF_INET, SOCK_STREAM, pent->p_proto)) < 0)
    return -1;

  if((gethostname(hostname, 255)) < 0)
    return -1;

  if(!(localhost = gethostbyname(hostname)))
    return -1;

  saddr.sin_family = AF_INET;
  saddr.sin_port = htons(portno);
  bcopy((char *)localhost->h_addr, (char *)&(saddr.sin_addr),
	localhost->h_length);

  if((connect(sock, (struct sockaddr *)&saddr,
	      sizeof(struct sockaddr_in))) < 0)
    return -1;

  return sock;
}
  
BOOL WriteString(int sock, char *str)
{
  int bytes, bytes_remaining;

  bytes_remaining = strlen(str);

  while(bytes_remaining)
    {
      bytes = write(sock, str, bytes_remaining);
      if(bytes<0)
	return NO;
      bytes_remaining -= bytes;
    }
  return YES;
}

BOOL ReadString(int sock, char *str, size_t count)
{
  int bytes;

  bytes = read(sock, str, count-1);
  if(bytes<=0)
    return NO;

  str[bytes] = '\0';
  return YES;
}

char *ReadStringAnySize(int sock)
{
  int bytes, cur_index, cur_length;
  char *str, *tmp;
  char c;

  cur_index=0;
  cur_length = 256;
  str = (char *)MyMalloc(__FILE__, __LINE__, cur_length);

  while((bytes = read(sock, &c, 1)))
    {
      if(bytes<0)
	{
	  MyFree(__FILE__, __LINE__, str);
	  return NULL;
	}

      if(c=='\n')
	{
	  str[cur_index] = '\0';
	  return str;
	}

      str[cur_index++]=c;

      if(cur_index==cur_length-1)
	{
	  cur_length+=256;
	  str[cur_index] = '\0';
	  tmp = (char *)MyMalloc(__FILE__, __LINE__, cur_length);
	  strcpy(tmp, str);
	  MyFree(__FILE__, __LINE__, str);
	  str = tmp;
	}
    }
  MyFree(__FILE__, __LINE__, str);
  return NULL;
}
      
char *ManglePath(const char *path)
{
  char *home;
  char *ret;

  if(!path) return 0;

  if(path[0] == '~')
    {
      home = getenv("HOME");
      ret = (char *)MyMalloc(__FILE__, __LINE__, strlen(home)+strlen(path)+1);
      sprintf(ret, "%s/%s", home, &(path[1]));
    }
  else
    {
      ret = (char *)MyMalloc(__FILE__, __LINE__, strlen(path)+1);
      strcpy(ret, path);
    }
  return ret;
}
      
char *MakeDefaultsFilename()
{
  char *env;
  char actual_filename[255];

  env = (char *)getenv("GNUSTEP_USER_PATH");
  if(!env)
    {
      env = (char *)getenv("HOME");
      if(!env) /* No HOME --- can this happen? */
	sprintf(actual_filename, "/GNUstep");
      else
	sprintf(actual_filename, "%s/GNUstep", env);
    }
  else
    sprintf(actual_filename, env);

  sprintf(&(actual_filename[strlen(actual_filename)]), "/");

  env = (char *)getenv("GNUSTEP_DEFAULTS_FILE");
  if(!env)
    sprintf(&(actual_filename[strlen(actual_filename)]), "Defaults");
  else
    sprintf(&(actual_filename[strlen(actual_filename)]), env);

  return ManglePath(actual_filename);
}

BOOL LockFile(char *name)
{
  char *lockfilename;

  lockfilename = MyMalloc(__FILE__, __LINE__, strlen(name)+6);
  sprintf(lockfilename, "%s.lock", name);

  if((mkdir(lockfilename, 0755)) < 0)
    {
      if(errno != EEXIST)
	{
	  MyFree(__FILE__, __LINE__, lockfilename);
	  return NO;
	}
      else
	{
	  sleep(2);
	  if((mkdir(lockfilename, 0755)) < 0)
	    {
	      MyFree(__FILE__, __LINE__, lockfilename);
	      return NO;
	    }
	}
    }
  MyFree(__FILE__, __LINE__, lockfilename);
  return YES;
}

BOOL UnlockFile(char *filename)
{
  char *lockfilename;
  
  lockfilename = MyMalloc(__FILE__, __LINE__, strlen(filename)+6);
  sprintf(lockfilename, "%s.lock", filename);
  
  if(rmdir(lockfilename) < 0)
    {
      MyFree(__FILE__, __LINE__, lockfilename);
      if(errno==ENOENT) return YES;
      else return NO;
    }

  MyFree(__FILE__, __LINE__, lockfilename);
  return YES;
}

BOOL StatDomain(char *filename, proplist_t key, struct stat *buf)
{
  char *actualFilename;

  actualFilename = MyMalloc(__FILE__, __LINE__, strlen(filename) +
			  strlen(PLGetString(key)) + 2);
  sprintf(actualFilename, "%s/%s", filename, PLGetString(key));

  if(stat(actualFilename, buf)<0)
    {
      MyFree(__FILE__, __LINE__, actualFilename);
      return NO;
    }
  MyFree(__FILE__, __LINE__, actualFilename);
  return YES;
}

BOOL DeleteDomain(char *filename, proplist_t key)
{
  char *actualFilename;

  actualFilename = MyMalloc(__FILE__, __LINE__, strlen(filename) +
			  strlen(PLGetString(key)) + 2);
  sprintf(actualFilename, "%s/%s", filename, PLGetString(key));

  if(unlink(actualFilename)<0)
    {
      MyFree(__FILE__, __LINE__, actualFilename);
      return NO;
    }
  MyFree(__FILE__, __LINE__, actualFilename);
  return YES;
}

proplist_t ReadDomain(char *filename, proplist_t key)
{
  char *actualFilename;
  proplist_t retval;
  
  actualFilename = MyMalloc(__FILE__, __LINE__, strlen(filename) +
			  strlen(PLGetString(key)) + 2);
  sprintf(actualFilename, "%s/%s", filename, PLGetString(key));

  retval = PLGetProplistWithPath(actualFilename);

  MyFree(__FILE__, __LINE__, actualFilename);
  return retval;
}

void *MyMalloc(char *file, int line, size_t size)
{
	void *retval = malloc(size);
#ifdef MEMDEBUG
	printf("Allocating %d bytes of memory at address 0x%x (%s:%d)\n", size, retval, file, line);
#endif
	return retval;
}

void MyFree(char *file, int line, void *mem)
{
#ifdef MEMDEBUG
	printf("Freeing memory at address 0x%x (%s:%d)\n", mem, file, line);
#endif
	free(mem);
}