summaryrefslogtreecommitdiffstats
path: root/ldap/systools/pio.c
blob: 4d6593c099b5f3501e1f2856f60fe66b447f5bcd (plain)
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
/** BEGIN COPYRIGHT BLOCK
 * Copyright 2001 Sun Microsystems, Inc.
 * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

#include "pio.h"
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

int iii_pio_procparse (
	const char *cmd,
	int count,
	struct iii_pio_parsetab *tb
)
{
  FILE *fp;
  char buf[8192];
  int rc = 0;

  fp = popen(cmd,"r");

  if (fp == NULL) {
    return -1;
  }
  
  while (fgets(buf,8192,fp) != NULL && rc >= 0) {
    char *rp;
    int i;

    rp = strchr(buf,'\n');

    if (rp) {
      *rp = '\0';
    }

    rp = strchr(buf,':');

#if defined(__osf__)
    if (rp == NULL) {
      rp = strchr(buf,'=');
    }
#endif

    if (rp == NULL) continue;
    
    *rp = '\0';
    rp++;
    while(isspace(*rp)) rp++;

    for (i = 0; i < count; i++) {
      if (strcmp(tb[i].token,buf) == 0) {
	rc = (tb[i].fn)(buf,rp);
	break;
      }
    }
  }
  
  pclose(fp);
  
  return rc;
}

int iii_pio_getnum (
	const char *cmd,
	long *valPtr
)
{
  FILE *fp;
  char buf[8192];
  int rc = 0;
  
  fp = popen(cmd,"r");
  
  if (fp == NULL) {
    return -1;
  }
  
  if (fgets(buf,8192,fp) == NULL) {
    pclose(fp);
    return -1;
  }

  pclose(fp);

  if (!(isdigit(*buf))) {
    return -1;
  }
  
  *valPtr = atol(buf);
  
  return 0;
}