root/source3/lib/util_file.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. file_pload
  2. file_lines_pload

   1 /*
   2  * Unix SMB/CIFS implementation.
   3  * SMB parameters and setup
   4  * Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
   5  * 
   6  * This program is free software; you can redistribute it and/or modify it under
   7  * the terms of the GNU General Public License as published by the Free
   8  * Software Foundation; either version 3 of the License, or (at your option)
   9  * any later version.
  10  * 
  11  * This program is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  14  * more details.
  15  * 
  16  * You should have received a copy of the GNU General Public License along with
  17  * this program; if not, see <http://www.gnu.org/licenses/>.
  18  */
  19 
  20 #include "includes.h"
  21 
  22 /**
  23  Load from a pipe into memory.
  24 **/
  25 
  26 static char *file_pload(const char *syscmd, size_t *size)
     /* [<][>][^][v][top][bottom][index][help] */
  27 {
  28         int fd, n;
  29         char *p;
  30         char buf[1024];
  31         size_t total;
  32 
  33         fd = sys_popen(syscmd);
  34         if (fd == -1) {
  35                 return NULL;
  36         }
  37 
  38         p = NULL;
  39         total = 0;
  40 
  41         while ((n = read(fd, buf, sizeof(buf))) > 0) {
  42                 p = talloc_realloc(NULL, p, char, total + n + 1);
  43                 if (!p) {
  44                         DEBUG(0,("file_pload: failed to expand buffer!\n"));
  45                         close(fd);
  46                         return NULL;
  47                 }
  48                 memcpy(p+total, buf, n);
  49                 total += n;
  50         }
  51 
  52         if (p) {
  53                 p[total] = 0;
  54         }
  55 
  56         /* FIXME: Perhaps ought to check that the command completed
  57          * successfully (returned 0); if not the data may be
  58          * truncated. */
  59         sys_pclose(fd);
  60 
  61         if (size) {
  62                 *size = total;
  63         }
  64 
  65         return p;
  66 }
  67 
  68 
  69 
  70 /**
  71  Load a pipe into memory and return an array of pointers to lines in the data
  72  must be freed with file_lines_free(). 
  73 **/
  74 
  75 char **file_lines_pload(const char *syscmd, int *numlines)
     /* [<][>][^][v][top][bottom][index][help] */
  76 {
  77         char *p;
  78         size_t size;
  79 
  80         p = file_pload(syscmd, &size);
  81         if (!p) {
  82                 return NULL;
  83         }
  84 
  85         return file_lines_parse(p, size, numlines, NULL);
  86 }

/* [<][>][^][v][top][bottom][index][help] */