pio.c 1.4 KB

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