File: getservent_r.c

package info (click to toggle)
dietlibc 0.34~cvs20160606-10
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 11,336 kB
  • sloc: ansic: 71,631; asm: 13,006; cpp: 1,860; makefile: 799; sh: 292; perl: 62
file content (91 lines) | stat: -rw-r--r-- 1,996 bytes parent folder | download | duplicates (4)
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
#include <sys/types.h>
#include <sys/mman.h>
#include <netinet/in.h>
#include <string.h>
#include <netdb.h>
#include <ctype.h>
#include "parselib.h"

static struct state __ps;

void setservent(int stayopen) {
  (void)stayopen;
  __prepare_parse(_PATH_SERVICES,&__ps);
}

void endservent(void) {
  __end_parse(&__ps);
}

#define ALIASES 16

/* "tcpmux		1/tcp		# TCP port multiplexer (RFC1078)" */
int getservent_r(struct servent *res, char *buf, size_t buflen,
		 struct servent **res_sig) {
  size_t i,j,n,g;
  unsigned long l;
  if (!__ps.buffirst) setservent(0);
  if (!__ps.buffirst) goto error;
  if (__ps.cur>=__ps.buflen) goto error;
  res->s_aliases=(char**)buf;
/*  getservent */
again:
  n=ALIASES*sizeof(char*); g=0;
  for (i=0; i<3; ++i) {
    char found;
    __ps.cur+=__parse_ws(&__ps);
    if (__ps.cur>=__ps.buflen) { if (i==2) break; else goto error; }
    j=__parse_nws(&__ps);
    if (i==2 && j==0) break;
    if (!isblank(found=__ps.buffirst[__ps.cur+j])) {
      if (found=='#' || (i>1 && found!='\n')) {
parseerror:
	while (__ps.cur+j<__ps.buflen) {
	  if (__ps.buffirst[__ps.cur+j]=='\n') {
	    __ps.cur+=j+1;
	    goto again;
	  }
	  ++j;
	}
	goto error;
      }
    }
    switch (i) {
    case 0:
      res->s_name=buf+n;
copy:
      if (!j) goto parseerror;
      if ((size_t)buflen<=n+j) goto error;
      memcpy(buf+n,__ps.buffirst+__ps.cur,j);
      buf[n+j]=0;
      n+=j+1;
      if ((found=='\n' || found=='#') && i==1) i=3;
      break;
    case 1:
      {
	int k;
	k=scan_ulong(__ps.buffirst+__ps.cur,&l);
	if (__ps.buffirst[__ps.cur+k]!='/') {
	  goto parseerror;
	}
	res->s_port=htons(l);
	res->s_proto=buf+n;
	j-=k+1; __ps.cur+=k+1;
	goto copy;
      }
    case 2:
      res->s_aliases[g]=buf+n;
      ++g;
      if (g==(ALIASES-1)) break;
      --i;	/* again */
      goto copy;
    }
    __ps.cur+=j+1;
  }
  res->s_aliases[g]=0;
  *res_sig=res;
  return 0;
error:
  *res_sig=0;/* the glibc people should be taken behind the barn and shot */
  return -1;
}