pio.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright 2001 Sun Microsystems, Inc.
  3. * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
  4. * All rights reserved.
  5. * END COPYRIGHT BLOCK **/
  6. #include "pio.h"
  7. #include <ctype.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. int iii_pio_procparse (
  11. const char *cmd,
  12. int count,
  13. struct iii_pio_parsetab *tb
  14. )
  15. {
  16. FILE *fp;
  17. char buf[8192];
  18. int rc = 0;
  19. fp = popen(cmd,"r");
  20. if (fp == NULL) {
  21. return -1;
  22. }
  23. while (fgets(buf,8192,fp) != NULL && rc >= 0) {
  24. char *rp;
  25. int i;
  26. rp = strchr(buf,'\n');
  27. if (rp) {
  28. *rp = '\0';
  29. }
  30. rp = strchr(buf,':');
  31. #if defined(__osf__)
  32. if (rp == NULL) {
  33. rp = strchr(buf,'=');
  34. }
  35. #endif
  36. if (rp == NULL) continue;
  37. *rp = '\0';
  38. rp++;
  39. while(isspace(*rp)) rp++;
  40. for (i = 0; i < count; i++) {
  41. if (strcmp(tb[i].token,buf) == 0) {
  42. rc = (tb[i].fn)(buf,rp);
  43. break;
  44. }
  45. }
  46. }
  47. pclose(fp);
  48. return rc;
  49. }
  50. int iii_pio_getnum (
  51. const char *cmd,
  52. long *valPtr
  53. )
  54. {
  55. FILE *fp;
  56. char buf[8192];
  57. int rc = 0;
  58. fp = popen(cmd,"r");
  59. if (fp == NULL) {
  60. return -1;
  61. }
  62. if (fgets(buf,8192,fp) == NULL) {
  63. pclose(fp);
  64. return -1;
  65. }
  66. pclose(fp);
  67. if (!(isdigit(*buf))) {
  68. return -1;
  69. }
  70. *valPtr = atol(buf);
  71. return 0;
  72. }