src/tools/bsnapshot.c
bool parse_output(char *cmd, alist *ret, int *ret_nbelt, Header *hdr) {
      char *p;
      int   i=0;
      int   pos=0;
      int   nbelt=0;
      char  buf[2048];          /* Size for a single line */
      bool  header_done=false;

      if (run_program_full_output(cmd, 60, errmsg)) {
         strip_quotes(errmsg);
         Dmsg(10, "Unable to run lvs. ERR=%s\n", errmsg);
         return false;
      }

      char **current = NULL;

      for (p = errmsg; *p ; p++) {
         if (*p == ';') {        /* We have a separator, handle current value */
            buf[i]=0;
            if (!header_done) {
               nbelt++; /* Keep the number of element in the line */

               /* Find if we need this value, and where to store it */
               for (int j=0; hdr[j].name ; j++) {
                  if (strcasecmp(buf, hdr[j].name) == 0) {
                     hdr[j].pos = pos;
                     break;
                  }
               }

            } else {
               if (pos == 0) {
                  /* First item, need to allocate new array */
                  current = (char **)malloc(nbelt * sizeof(char *) + 1);
                  memset(current, 0, nbelt * sizeof(char *) + 1);
                  ret->append(current);
               }
               /* Keep the current value */
               current[pos] = bstrdup(buf);
            }
            pos++;
            i = 0;
         } else if (*p == '\n') {
            /* We deal with a new line, so the header is done (if in) */
            header_done = true;
            i = 0;
            pos = 0;

         } else if (i < (int)sizeof(buf)) {
            buf[i++] = *p;

         } else {
            Dmsg(10, "Output too big !!! %s\n", errmsg);
            break;
         }
      }
      *ret_nbelt = nbelt;
      return true;
   }
/* Function to parse LVM command output */
Function to parse LVM command output
Function to parse LVM command output
bool parse_output(char *cmd, alist *ret, int *ret_nbelt, Header *hdr) {
      char *p;
      int   i=0;
      int   pos=0;
      int   nbelt=0;
      char  buf[2048];          
      bool  header_done=false;
      if (run_program_full_output(cmd, 60, errmsg)) {
         strip_quotes(errmsg);
         Dmsg(10, "Unable to run lvs. ERR=%s\n", errmsg);
         return false;
      }
      char **current = NULL;
      for (p = errmsg; *p ; p++) {
         if (*p == ';') {        
            buf[i]=0;
            if (!header_done) {
               nbelt++; 
               for (int j=0; hdr[j].name ; j++) {
                  if (strcasecmp(buf, hdr[j].name) == 0) {
                     hdr[j].pos = pos;
                     break;
                  }
               }
            } else {
               if (pos == 0) {
                  current = (char **)malloc(nbelt * sizeof(char *) + 1);
                  memset(current, 0, nbelt * sizeof(char *) + 1);
                  ret->append(current);
               }
               current[pos] = bstrdup(buf);
            }
            pos++;
            i = 0;
         } else if (*p == '\n') {
            header_done = true;
            i = 0;
            pos = 0;
         } else if (i < (int)sizeof(buf)) {
            buf[i++] = *p;
         } else {
            Dmsg(10, "Output too big !!! %s\n", errmsg);
            break;
         }
      }
      *ret_nbelt = nbelt;
      return true;
   }
bool parse_output ( char * cmd , alist * ret , int * ret_nbelt , Header * hdr ) { char * p ; int i = 0 ; int pos = 0 ; int nbelt = 0 ; char buf [ 2048 ] ; bool header_done = false ; if ( run_program_full_output ( cmd , 60 , errmsg ) ) { strip_quotes ( errmsg ) ; Dmsg ( 10 , " \n " , errmsg ) ; return false ; } char * * current = NULL ; for ( p = errmsg ; * p ; p ++ ) { if ( * p == ' ' ) { buf [ i ] = 0 ; if ( ! header_done ) { nbelt ++ ; for ( int j = 0 ; hdr [ j ] . name ; j ++ ) { if ( strcasecmp ( buf , hdr [ j ] . name ) == 0 ) { hdr [ j ] . pos = pos ; break ; } } } else { if ( pos == 0 ) { current = ( char * * ) malloc ( nbelt * sizeof ( char * ) + 1 ) ; memset ( current , 0 , nbelt * sizeof ( char * ) + 1 ) ; ret -> append ( current ) ; } current [ pos ] = bstrdup ( buf ) ; } pos ++ ; i = 0 ; } else if ( * p == ' \n ' ) { header_done = true ; i = 0 ; pos = 0 ; } else if ( i < ( int ) sizeof ( buf ) ) { buf [ i ++ ] = * p ; } else { Dmsg ( 10 , " \n " , errmsg ) ; break ; } } * ret_nbelt = nbelt ; return true ; }
Function to parse LVM command output
Function to parse LVM command output
/* Size for a single line */ /* We have a separator, handle current value */ /* Keep the number of element in the line */ /* Find if we need this value, and where to store it */ /* First item, need to allocate new array */ /* Keep the current value */ /* We deal with a new line, so the header is done (if in) */