converted a bunch more functions to use a fd instead of a FILE*
authorAndrew Tridgell <tridge@samba.org>
Sun, 16 Apr 2000 11:00:21 +0000 (11:00 +0000)
committerAndrew Tridgell <tridge@samba.org>
Sun, 16 Apr 2000 11:00:21 +0000 (11:00 +0000)
to support some of this I added the following functions in util_file.c

file_lines_pload : load lines from a pipe
file_pload : load a pipe into memory

source/include/proto.h
source/lib/system.c
source/lib/util_file.c
source/param/loadparm.c
source/param/params.c
source/printing/print_svid.c
source/smbd/dfree.c
source/smbd/ipc.c

index 94afe2174934c80f23f222bd61284cdd62009301..dc9d9c389ecf045ac9e8e21a800c683d04106f7b 100644 (file)
@@ -235,8 +235,8 @@ DIR *wsys_opendir(const smb_ucs2_t *wfname);
 smb_ucs2_t *wsys_getwd(smb_ucs2_t *s);
 int wsys_chown(const smb_ucs2_t *wfname, uid_t uid, gid_t gid);
 int wsys_chroot(const smb_ucs2_t *wfname);
-FILE *sys_popen(const char *command, const char *mode, BOOL paranoid);
-int sys_pclose( FILE *fp);
+int sys_popen(const char *command);
+int sys_pclose(int fd);
 
 /*The following definitions come from  lib/talloc.c  */
 
@@ -378,7 +378,10 @@ SMB_BIG_UINT getfilepwpos(void *vp);
 BOOL setfilepwpos(void *vp, SMB_BIG_UINT tok);
 int getfileline(void *vp, char *linebuf, int linebuf_size);
 char *fgets_slash(char *s2,int maxlen,FILE *f);
+char *file_pload(char *syscmd, size_t *size);
+char *file_load(char *fname, size_t *size);
 char **file_lines_load(char *fname, int *numlines);
+char **file_lines_pload(char *syscmd, int *numlines);
 void file_lines_free(char **lines);
 
 /*The following definitions come from  lib/util_sec.c  */
index 8ac07e26a5f4cc6d564f11425194327dd809a577..3354ee0bbc37d138eef484d044f9f7896211719d 100644 (file)
@@ -952,37 +952,29 @@ static char **extract_args(const char *command)
 /**************************************************************************
  Wrapper for popen. Safer as it doesn't search a path.
  Modified from the glibc sources.
+ modified by tridge to return a file descriptor. We must kick our FILE* habit
 ****************************************************************************/
-
 typedef struct _popen_list
 {
-       FILE *fp;
+       int fd;
        pid_t child_pid;
        struct _popen_list *next;
 } popen_list;
 
 static popen_list *popen_chain;
 
-FILE *sys_popen(const char *command, const char *mode, BOOL paranoid)
+int sys_popen(const char *command)
 {
        int parent_end, child_end;
        int pipe_fds[2];
-    popen_list *entry = NULL;
+       popen_list *entry = NULL;
        char **argl = NULL;
 
        if (pipe(pipe_fds) < 0)
-               return NULL;
+               return -1;
 
-       if (mode[0] == 'r' && mode[1] == '\0') {
-               parent_end = pipe_fds[0];
-               child_end = pipe_fds[1];
-    } else if (mode[0] == 'w' && mode[1] == '\0') {
-               parent_end = pipe_fds[1];
-               child_end = pipe_fds[0];
-    } else {
-               errno = EINVAL;
-               goto err_exit;
-    }
+       parent_end = pipe_fds[0];
+       child_end = pipe_fds[1];
 
        if (!*command) {
                errno = EINVAL;
@@ -999,69 +991,9 @@ FILE *sys_popen(const char *command, const char *mode, BOOL paranoid)
        if(!(argl = extract_args(command)))
                goto err_exit;
 
-       if(paranoid) {
-               /*
-                * Do some basic paranioa checks. Do a stat on the parent
-                * directory and ensure it's not world writable. Do a stat
-                * on the file itself and ensure it's owned by root and not
-                * world writable. Note this does *not* prevent symlink races,
-                * but is a generic "don't let the admin screw themselves"
-                * check.
-                */
-
-               SMB_STRUCT_STAT st;
-               pstring dir_name;
-               char *ptr = strrchr(argl[0], '/');
-       
-               if(sys_stat(argl[0], &st) != 0)
-                       goto err_exit;
-
-               if((st.st_uid != (uid_t)0) || (st.st_mode & S_IWOTH)) {
-                       errno = EACCES;
-                       goto err_exit;
-               }
-               
-               if(!ptr) {
-                       /*
-                        * No '/' in name - use current directory.
-                        */
-                       pstrcpy(dir_name, ".");
-               } else {
-
-                       /*
-                        * Copy into a pstring and do the checks
-                        * again (in case we were length tuncated).
-                        */
-
-                       pstrcpy(dir_name, argl[0]);
-                       ptr = strrchr(dir_name, '/');
-                       if(!ptr) {
-                               errno = EINVAL;
-                               goto err_exit;
-                       }
-                       if(strcmp(dir_name, "/") != 0)
-                               *ptr = '\0';
-                       if(!dir_name[0])
-                               pstrcpy(dir_name, ".");
-               }
-
-               if(sys_stat(argl[0], &st) != 0)
-                       goto err_exit;
-
-               if(!S_ISDIR(st.st_mode) || (st.st_mode & S_IWOTH)) {
-                       errno = EACCES;
-                       goto err_exit;
-               }
-       }
-
        entry->child_pid = fork();
 
        if (entry->child_pid == -1) {
-
-               /*
-                * Error !
-                */
-
                goto err_exit;
        }
 
@@ -1071,7 +1003,7 @@ FILE *sys_popen(const char *command, const char *mode, BOOL paranoid)
                 * Child !
                 */
 
-               int child_std_end = (mode[0] == 'r') ? STDOUT_FILENO : STDIN_FILENO;
+               int child_std_end = STDOUT_FILENO;
                popen_list *p;
 
                close(parent_end);
@@ -1087,7 +1019,7 @@ FILE *sys_popen(const char *command, const char *mode, BOOL paranoid)
                 */
 
                for (p = popen_chain; p; p = p->next)
-                       close(fileno(p->fp));
+                       close(p->fd);
 
                execv(argl[0], argl);
                _exit (127);
@@ -1100,16 +1032,11 @@ FILE *sys_popen(const char *command, const char *mode, BOOL paranoid)
        close (child_end);
        free((char *)argl);
 
-       /*
-        * Create the FILE * representing this fd.
-        */
-    entry->fp = fdopen(parent_end, mode);
-
        /* Link into popen_chain. */
        entry->next = popen_chain;
        popen_chain = entry;
 
-       return entry->fp;
+       return entry->fd;
 
 err_exit:
 
@@ -1119,14 +1046,13 @@ err_exit:
                free((char *)argl);
        close(pipe_fds[0]);
        close(pipe_fds[1]);
-       return NULL;
+       return -1;
 }
 
 /**************************************************************************
  Wrapper for pclose. Modified from the glibc sources.
 ****************************************************************************/
-
-int sys_pclose( FILE *fp)
+int sys_pclose(int fd)
 {
        int wstatus;
        popen_list **ptr = &popen_chain;
@@ -1136,7 +1062,7 @@ int sys_pclose( FILE *fp)
 
        /* Unlink from popen_chain. */
        for ( ; *ptr != NULL; ptr = &(*ptr)->next) {
-               if ((*ptr)->fp == fp) {
+               if ((*ptr)->fd == fd) {
                        entry = *ptr;
                        *ptr = (*ptr)->next;
                        status = 0;
@@ -1144,7 +1070,7 @@ int sys_pclose( FILE *fp)
                }
        }
 
-       if (status < 0 || close(fileno(entry->fp)) < 0)
+       if (status < 0 || close(entry->fd) < 0)
                return -1;
 
        /*
index d533e6428fdd77cb7fd3de4fba22cd69eaeeee2d..1ef713b105a75ff4cf9f8329a4bd6dd6fc287abc 100644 (file)
@@ -329,35 +329,83 @@ char *fgets_slash(char *s2,int maxlen,FILE *f)
 
 
 /****************************************************************************
-load a file into memory and return an array of pointers to lines in the file
-must be freed with file_lines_free()
+load from a pipe into memory
 ****************************************************************************/
-char **file_lines_load(char *fname, int *numlines)
+char *file_pload(char *syscmd, size_t *size)
 {
-       int fd, i;
+       int fd, n;
+       char *p;
+       pstring buf;
+       size_t total;
+       
+       fd = sys_popen(syscmd);
+       if (fd == -1) return NULL;
+
+       p = NULL;
+       total = 0;
+
+       while ((n = read(fd, buf, sizeof(buf))) > 0) {
+               p = Realloc(p, total + n + 1);
+               if (!p) {
+                       close(fd);
+                       return NULL;
+               }
+               memcpy(p+total, buf, n);
+               total += n;
+       }
+       p[total] = 0;
+
+       sys_pclose(fd);
+
+       if (size) *size = total;
+
+       return p;
+}
+
+
+/****************************************************************************
+load a file into memory
+****************************************************************************/
+char *file_load(char *fname, size_t *size)
+{
+       int fd;
        SMB_STRUCT_STAT sbuf;
-       char *p, *s, **ret;
-       size_t size;
+       char *p;
        
        fd = open(fname,O_RDONLY);
        if (fd == -1) return NULL;
 
        if (sys_fstat(fd, &sbuf) != 0) return NULL;
-       size = sbuf.st_size;
 
-       if (size == 0) return NULL;
+       if (sbuf.st_size == 0) return NULL;
 
-       p = (char *)malloc(size+1);
+       p = (char *)malloc(sbuf.st_size+1);
        if (!p) return NULL;
 
-       if (read(fd, p, size) != size) {
+       if (read(fd, p, sbuf.st_size) != sbuf.st_size) {
                free(p);
                return NULL;
        }
-       p[size] = 0;
+       p[sbuf.st_size] = 0;
 
        close(fd);
 
+       if (size) *size = sbuf.st_size;
+
+       return p;
+}
+
+
+/****************************************************************************
+parse a buffer into lines
+****************************************************************************/
+static char **file_lines_parse(char *p, size_t size, int *numlines)
+{
+       int i;
+       char *s, **ret;
+
+       if (!p) return NULL;
+
        for (s = p, i=0; s < p+size; s++) {
                if (s[0] == '\n') i++;
        }
@@ -383,6 +431,38 @@ char **file_lines_load(char *fname, int *numlines)
        return ret;
 }
 
+
+/****************************************************************************
+load a file into memory and return an array of pointers to lines in the file
+must be freed with file_lines_free()
+****************************************************************************/
+char **file_lines_load(char *fname, int *numlines)
+{
+       char *p;
+       size_t size;
+
+       p = file_load(fname, &size);
+       if (!p) return NULL;
+
+       return file_lines_parse(p, size, numlines);
+}
+
+
+/****************************************************************************
+load a pipe into memory and return an array of pointers to lines in the data
+must be freed with file_lines_free()
+****************************************************************************/
+char **file_lines_pload(char *syscmd, int *numlines)
+{
+       char *p;
+       size_t size;
+
+       p = file_pload(syscmd, &size);
+       if (!p) return NULL;
+
+       return file_lines_parse(p, size, numlines);
+}
+
 /****************************************************************************
 free lines loaded with file_lines_load
 ****************************************************************************/
index 6890a08fce3b65418a59f594f124dd7d5c84cf78..cc5a51c6c66a42b87948b0dfbc7371526a05db77 100644 (file)
@@ -1971,19 +1971,15 @@ static BOOL handle_netbios_name(char *pszParmValue,char **ptr)
  Do the work of sourcing in environment variable/value pairs.
 ***************************************************************************/
 
-static BOOL source_env(FILE *fenv)
+static BOOL source_env(char **lines)
 {
-       pstring line;
        char *varval;
        size_t len;
+       int i;
        char *p;
 
-       while (!feof(fenv)) {
-               if (fgets(line, sizeof(line), fenv) == NULL)
-                       break;
-
-               if(feof(fenv))
-                       break;
+       for (i=0; lines[i]; i++) {
+               char *line = lines[i];
 
                if((len = strlen(line)) == 0)
                        continue;
@@ -2028,8 +2024,8 @@ static BOOL handle_source_env(char *pszParmValue,char **ptr)
 {
        pstring fname;
        char *p = fname;
-       FILE *env;
        BOOL result;
+       char **lines;
 
        pstrcpy(fname,pszParmValue);
 
@@ -2044,47 +2040,19 @@ static BOOL handle_source_env(char *pszParmValue,char **ptr)
         */
 
        if (*p == '|') {
-
-               DEBUG(4, ("handle_source_env: source env from pipe\n"));
-               p++;
-
-               if ((env = sys_popen(p, "r", True)) == NULL) {
-                       DEBUG(0,("handle_source_env: Failed to popen %s. Error was %s\n", p, strerror(errno) ));
-                       return(False);
-               }
-
-               DEBUG(4, ("handle_source_env: calling source_env()\n"));
-               result = source_env(env);
-               sys_pclose(env);
-
+               lines = file_lines_pload(p+1, NULL);
        } else {
+               lines = file_lines_load(fname, NULL);
+       }
 
-               SMB_STRUCT_STAT st;
-
-               DEBUG(4, ("handle_source_env: source env from file %s\n", fname));
-               if ((env = sys_fopen(fname, "r")) == NULL) {
-                       DEBUG(0,("handle_source_env: Failed to open file %s, Error was %s\n", fname, strerror(errno) ));
-                       return(False);
-               }
-
-               /*
-                * Ensure this file is owned by root and not writable by world.
-                */
-               if(sys_fstat(fileno(env), &st) != 0) {
-                       DEBUG(0,("handle_source_env: Failed to stat file %s, Error was %s\n", fname, strerror(errno) ));
-                       fclose(env);
-                       return False;
-               }
+       if (!lines) {
+               DEBUG(0,("handle_source_env: Failed to open file %s, Error was %s\n", fname, strerror(errno) ));
+               return(False);
+       }
 
-               if((st.st_uid != (uid_t)0) || (st.st_mode & S_IWOTH)) {
-                       DEBUG(0,("handle_source_env: unsafe to source env file %s. Not owned by root or world writable\n", fname ));
-                       fclose(env);
-                       return False;
-               }
+       result=source_env(lines);
+       file_lines_free(lines);
 
-               result=source_env(env);
-               fclose(env);
-       }
        return(result);
 }
 
index 3ecdcdc92b3457d2dd6476d495f43a620006baf1..8e926217911fc91499ddca52c35a1d7100d24d6d 100644 (file)
@@ -103,11 +103,32 @@ extern int DEBUGLEVEL;
 static char *bufr  = NULL;
 static int   bSize = 0;
 
+/* we can't use FILE* due to the 256 fd limit - use this cheap hack
+   instead */
+typedef struct {
+       char *buf;
+       char *p;
+       size_t size;
+} myFILE;
+
+static int mygetc(myFILE *f)
+{
+       if (f->p >= f->buf+f->size) return EOF;
+       return (int)*(f->p++);
+}
+
+static void myfile_close(myFILE *f)
+{
+       if (!f) return;
+       if (f->buf) free(f->buf);
+       free(f);
+}
+
 /* -------------------------------------------------------------------------- **
  * Functions...
  */
 
-static int EatWhitespace( FILE *InFile )
+static int EatWhitespace( myFILE *InFile )
   /* ------------------------------------------------------------------------ **
    * Scan past whitespace (see ctype(3C)) and return the first non-whitespace
    * character, or newline, or EOF.
@@ -127,12 +148,12 @@ static int EatWhitespace( FILE *InFile )
   {
   int c;
 
-  for( c = getc( InFile ); isspace( c ) && ('\n' != c); c = getc( InFile ) )
+  for( c = mygetc( InFile ); isspace( c ) && ('\n' != c); c = mygetc( InFile ) )
     ;
   return( c );
   } /* EatWhitespace */
 
-static int EatComment( FILE *InFile )
+static int EatComment( myFILE *InFile )
   /* ------------------------------------------------------------------------ **
    * Scan to the end of a comment.
    *
@@ -152,7 +173,7 @@ static int EatComment( FILE *InFile )
   {
   int c;
 
-  for( c = getc( InFile ); ('\n'!=c) && (EOF!=c) && (c>0); c = getc( InFile ) )
+  for( c = mygetc( InFile ); ('\n'!=c) && (EOF!=c) && (c>0); c = mygetc( InFile ) )
     ;
   return( c );
   } /* EatComment */
@@ -195,7 +216,7 @@ static int Continuation( char *line, int pos )
 }
 
 
-static BOOL Section( FILE *InFile, BOOL (*sfunc)(char *) )
+static BOOL Section( myFILE *InFile, BOOL (*sfunc)(char *) )
   /* ------------------------------------------------------------------------ **
    * Scan a section name, and pass the name to function sfunc().
    *
@@ -264,7 +285,7 @@ static BOOL Section( FILE *InFile, BOOL (*sfunc)(char *) )
           return( False );
           }
         end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i);
-        c = getc( InFile );             /* Continue with next line.         */
+        c = mygetc( InFile );             /* Continue with next line.         */
         break;
 
       default:                        /* All else are a valid name chars.   */
@@ -278,7 +299,7 @@ static BOOL Section( FILE *InFile, BOOL (*sfunc)(char *) )
           {
           bufr[i++] = c;
           end = i;
-          c = getc( InFile );
+          c = mygetc( InFile );
           }
       }
     }
@@ -288,7 +309,7 @@ static BOOL Section( FILE *InFile, BOOL (*sfunc)(char *) )
   return( False );
   } /* Section */
 
-static BOOL Parameter( FILE *InFile, BOOL (*pfunc)(char *, char *), int c )
+static BOOL Parameter( myFILE *InFile, BOOL (*pfunc)(char *, char *), int c )
   /* ------------------------------------------------------------------------ **
    * Scan a parameter name and value, and pass these two fields to pfunc().
    *
@@ -357,7 +378,7 @@ static BOOL Parameter( FILE *InFile, BOOL (*pfunc)(char *, char *), int c )
           return( True );
           }
         end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i);
-        c = getc( InFile );       /* Read past eoln.                   */
+        c = mygetc( InFile );       /* Read past eoln.                   */
         break;
 
       case '\0':                /* Shouldn't have EOF within param name. */
@@ -377,7 +398,7 @@ static BOOL Parameter( FILE *InFile, BOOL (*pfunc)(char *, char *), int c )
           {
           bufr[i++] = c;
           end = i;
-          c = getc( InFile );
+          c = mygetc( InFile );
           }
       }
     }
@@ -401,7 +422,7 @@ static BOOL Parameter( FILE *InFile, BOOL (*pfunc)(char *, char *), int c )
     switch( c )
       {
       case '\r':              /* Explicitly remove '\r' because the older */
-        c = getc( InFile );   /* version called fgets_slash() which also  */
+        c = mygetc( InFile );   /* version called fgets_slash() which also  */
         break;                /* removes them.                            */
 
       case '\n':              /* Marks end of value unless there's a '\'. */
@@ -412,7 +433,7 @@ static BOOL Parameter( FILE *InFile, BOOL (*pfunc)(char *, char *), int c )
           {
           for( end = i; (end >= 0) && isspace(bufr[end]); end-- )
             ;
-          c = getc( InFile );
+          c = mygetc( InFile );
           }
         break;
 
@@ -420,7 +441,7 @@ static BOOL Parameter( FILE *InFile, BOOL (*pfunc)(char *, char *), int c )
         bufr[i++] = c;       /* not advance <end>.  This allows trimming  */
         if( !isspace( c ) )  /* of whitespace at the end of the line.     */
           end = i;
-        c = getc( InFile );
+        c = mygetc( InFile );
         break;
       }
     }
@@ -429,7 +450,7 @@ static BOOL Parameter( FILE *InFile, BOOL (*pfunc)(char *, char *), int c )
   return( pfunc( bufr, &bufr[vstart] ) );   /* Pass name & value to pfunc().  */
   } /* Parameter */
 
-static BOOL Parse( FILE *InFile,
+static BOOL Parse( myFILE *InFile,
                    BOOL (*sfunc)(char *),
                    BOOL (*pfunc)(char *, char *) )
   /* ------------------------------------------------------------------------ **
@@ -490,38 +511,37 @@ static BOOL Parse( FILE *InFile,
   return( True );
   } /* Parse */
 
-static FILE *OpenConfFile( char *FileName )
+static myFILE *OpenConfFile( char *FileName )
   /* ------------------------------------------------------------------------ **
    * Open a configuration file.
    *
    *  Input:  FileName  - The pathname of the config file to be opened.
    *
-   *  Output: A pointer of type (FILE *) to the opened file, or NULL if the
-   *          file could not be opened.
+   *  Output: A pointer of type (char **) to the lines of the file
    *
    * ------------------------------------------------------------------------ **
    */
   {
-  FILE *OpenedFile;
   char *func = "params.c:OpenConfFile() -";
   extern BOOL in_client;
   int lvl = in_client?1:0;
+  myFILE *ret;
 
-  if( NULL == FileName || 0 == *FileName )
-    {
-    DEBUG( lvl, ("%s No configuration filename specified.\n", func) );
-    return( NULL );
-    }
+  ret = (myFILE *)malloc(sizeof(*ret));
+  if (!ret) return NULL;
 
-  OpenedFile = sys_fopen( FileName, "r" );
-  if( NULL == OpenedFile )
+  ret->buf = file_load(FileName, &ret->size);
+  if( NULL == ret->buf )
     {
     DEBUG( lvl,
       ("%s Unable to open configuration file \"%s\":\n\t%s\n",
       func, FileName, strerror(errno)) );
+    free(ret);
+    ret = NULL;
     }
 
-  return( OpenedFile );
+  ret->p = ret->buf;
+  return( ret );
   } /* OpenConfFile */
 
 BOOL pm_process( char *FileName,
@@ -542,7 +562,7 @@ BOOL pm_process( char *FileName,
    */
   {
   int   result;
-  FILE *InFile;
+  myFILE *InFile;
   char *func = "params.c:pm_process() -";
 
   InFile = OpenConfFile( FileName );          /* Open the config file. */
@@ -562,7 +582,7 @@ BOOL pm_process( char *FileName,
     if( NULL == bufr )
       {
       DEBUG(0,("%s memory allocation failure.\n", func));
-      fclose(InFile);
+      myfile_close(InFile);
       return( False );
       }
     result = Parse( InFile, sfunc, pfunc );
@@ -571,7 +591,7 @@ BOOL pm_process( char *FileName,
     bSize = 0;
     }
 
-  fclose(InFile);
+  myfile_close(InFile);
 
   if( !result )                               /* Generic failure. */
     {
index 21e8eeb643973d71ee2b2fda3ba0a35d55d9405c..301f38867195bfd4936ed66f76783f0a9f3c2e2d 100644 (file)
@@ -47,50 +47,49 @@ static printer_t *printers = NULL;
 
 static void populate_printers(void)
 {
-       FILE *fp;
-
-       if ((fp = sys_popen("/usr/bin/lpstat -v", "r", False)) != NULL) {
-               char buf[BUFSIZ];
-
-               while (fgets(buf, sizeof (buf), fp) != NULL) {
-                       printer_t *ptmp;
-                       char *name, *tmp;
-
-                       /* eat "system/device for " */
-                       if (((tmp = strchr(buf, ' ')) == NULL) ||
-                           ((tmp = strchr(++tmp, ' ')) == NULL))
-                               continue;
-
-                       /*
-                        * In case we're only at the "for ".
-                        */
-
-            if(!strncmp("for ",++tmp,4))
-            {
-                               tmp=strchr(tmp, ' ');
-                               tmp++;
-                       }
-                       name = tmp;
-
-                       /* truncate the ": ..." */
-                       if ((tmp = strchr(name, ':')) != NULL)
-                               *tmp = '\0';
-
-                       /* add it to the cache */
-                       if ((ptmp = malloc(sizeof (*ptmp))) != NULL) {
-                               ZERO_STRUCTP(ptmp);
-                               if((ptmp->name = strdup(name)) == NULL)
-                                       DEBUG(0,("populate_printers: malloc fail in strdup !\n"));
-                               ptmp->next = printers;
-                               printers = ptmp;
-                       } else {
-                               DEBUG(0,("populate_printers: malloc fail for ptmp\n"));
-                       }
+       char **lines;
+       int i;
+
+       lines = file_lines_pload("/usr/bin/lpstat -v", NULL);
+       if (!lines) return;
+
+       for (i=0;lines[i];i++) {
+               printer_t *ptmp;
+               char *name, *tmp;
+               char *buf = lines[i];
+
+               /* eat "system/device for " */
+               if (((tmp = strchr(buf, ' ')) == NULL) ||
+                   ((tmp = strchr(++tmp, ' ')) == NULL))
+                       continue;
+
+               /*
+                * In case we're only at the "for ".
+                */
+
+               if(!strncmp("for ",++tmp,4)) {
+                       tmp=strchr(tmp, ' ');
+                       tmp++;
+               }
+               name = tmp;
+
+               /* truncate the ": ..." */
+               if ((tmp = strchr(name, ':')) != NULL)
+                       *tmp = '\0';
+               
+               /* add it to the cache */
+               if ((ptmp = malloc(sizeof (*ptmp))) != NULL) {
+                       ZERO_STRUCTP(ptmp);
+                       if((ptmp->name = strdup(name)) == NULL)
+                               DEBUG(0,("populate_printers: malloc fail in strdup !\n"));
+                       ptmp->next = printers;
+                       printers = ptmp;
+               } else {
+                       DEBUG(0,("populate_printers: malloc fail for ptmp\n"));
                }
-               sys_pclose(fp);
-       } else {
-               DEBUG(0,( "Unable to run lpstat!\n"));
        }
+
+       file_lines_free(lines);
 }
 
 
index 0a892bad05e34118759b2c2a5eab6d4fc49fefc4..eff718b1482721658370a51f865f4d13d929ab3a 100644 (file)
@@ -203,24 +203,22 @@ static SMB_BIG_UINT disk_free(char *path, BOOL small_query,
        (*dfree) = (*dsize) = 0;
        (*bsize) = 512;
 
-
        /*
         * If external disk calculation specified, use it.
         */
 
        dfree_command = lp_dfree_command();
        if (dfree_command && *dfree_command) {
-               pstring line;
                char *p;
-               FILE *pp;
+               char **lines;
+               pstring syscmd;
 
-               slprintf (line, sizeof(pstring) - 1, "%s %s", dfree_command, path);
-               DEBUG (3, ("disk_free: Running command %s\n", line));
+               slprintf(syscmd, sizeof(syscmd), "%s %s", dfree_command, path);
+               DEBUG (3, ("disk_free: Running command %s\n", syscmd));
 
-               pp = sys_popen(line, "r", False);
-               if (pp) {
-                       fgets(line, sizeof(pstring), pp);
-                       line[sizeof(pstring)-1] = '\0';
+               lines = file_lines_pload(syscmd, NULL);
+               if (lines) {
+                       char *line = lines[0];
                        if (strlen(line) > 0)
                                line[strlen(line)-1] = '\0';
 
@@ -237,7 +235,7 @@ static SMB_BIG_UINT disk_free(char *path, BOOL small_query,
                                *bsize = (SMB_BIG_UINT)strtoul(p, NULL, 10);
                        else
                                *bsize = 1024;
-                       sys_pclose (pp);
+                       file_lines_free(lines);
                        DEBUG (3, ("Parsed output of dfree, dsize=%u, dfree=%u, bsize=%u\n",
                                (unsigned int)*dsize, (unsigned int)*dfree, (unsigned int)*bsize));
 
@@ -247,7 +245,7 @@ static SMB_BIG_UINT disk_free(char *path, BOOL small_query,
                                *dfree = 1024;
                } else {
                        DEBUG (0, ("disk_free: sys_popen() failed for command %s. Error was : %s\n",
-                               line, strerror(errno) ));
+                               syscmd, strerror(errno) ));
                        fsusage(path, dfree, dsize);
                }
        } else
index 205d00b803da28473cea5b0f01e4dabc7e8b0ff1..17aa83b72b43ec7dc8677078d102a704d745e4e0 100644 (file)
@@ -626,40 +626,29 @@ static void fill_printq_info_52(connection_struct *conn, int snum, int uLevel,
 {
        int i,ok=0;
        pstring tok,driver,datafile,langmon,helpfile,datatype;
-       char *p,*q;
-       FILE *f;
-       pstring fname;
+       char *p;
+       char **lines, *line;
        
-       pstrcpy(fname,lp_driverfile());
-       f=sys_fopen(fname,"r");
-       if (!f) {
-               DEBUG(3,("fill_printq_info: Can't open %s - %s\n",fname,strerror(errno)));
+       lines = file_lines_load(lp_driverfile(),NULL);
+       if (!lines) {
+               DEBUG(3,("fill_printq_info: Can't open %s - %s\n",
+                        lp_driverfile(),strerror(errno)));
                desc->errcode=NERR_notsupported;
                return;
        }
        
-       if((p=(char *)malloc(8192*sizeof(char))) == NULL) {
-               DEBUG(0,("fill_printq_info: malloc fail !\n"));
-               desc->errcode=NERR_notsupported;
-               fclose(f);
-               return;
-       }
-
-       memset(p, '\0',8192*sizeof(char));
-       q=p;
-       
        /* lookup the long printer driver name in the file
           description */
-       while (f && !feof(f) && !ok) {
-               p = q;                  /* reset string pointer */
-               fgets(p,8191,f);
-               p[strlen(p)-1]='\0';
+       for (i=0;lines[i] && !ok;i++) {
+               p = lines[i];
                if (next_token(&p,tok,":",sizeof(tok)) &&
                    (strlen(lp_printerdriver(snum)) == strlen(tok)) &&
                    (!strncmp(tok,lp_printerdriver(snum),strlen(lp_printerdriver(snum)))))
                        ok=1;
        }
-       fclose(f);
+       line = strdup(p);
+       p = line;
+       file_lines_free(lines);
        
        /* driver file name */
        if (ok && !next_token(&p,driver,":",sizeof(driver))) ok = 0;
@@ -722,7 +711,7 @@ static void fill_printq_info_52(connection_struct *conn, int snum, int uLevel,
                DEBUG(3,("fill_printq_info: Can't supply driver files\n"));
                desc->errcode=NERR_notsupported;
        }
-       free(q);
+       free(line);
 }
 
 
@@ -804,56 +793,45 @@ static void fill_printq_info(connection_struct *conn, int snum, int uLevel,
 /* This function returns the number of files for a given driver */
 static int get_printerdrivernumber(int snum)
 {
-  int i=0,ok=0;
-  pstring tok;
-  char *p,*q;
-  FILE *f;
-  pstring fname;
-
-  pstrcpy(fname,lp_driverfile());
-
-  DEBUG(4,("In get_printerdrivernumber: %s\n",fname));
-  f=sys_fopen(fname,"r");
-  if (!f) {
-    DEBUG(3,("get_printerdrivernumber: Can't open %s - %s\n",fname,strerror(errno)));
-    return(0);
-  }
-
-  if((p=(char *)malloc(8192*sizeof(char))) == NULL) {
-    DEBUG(3,("get_printerdrivernumber: malloc fail !\n"));
-    fclose(f);
-    return 0;
-  }
-
-  q=p; /* need it to free memory because p change ! */
-
-  /* lookup the long printer driver name in the file description */
-  while (!feof(f) && !ok)
-  {
-    p = q;                     /* reset string pointer */
-    fgets(p,8191,f);
-    if (next_token(&p,tok,":",sizeof(tok)) &&
-      (!strncmp(tok,lp_printerdriver(snum),strlen(lp_printerdriver(snum))))) 
-       ok=1;
-  }
-  fclose(f);
-
-  if (ok) {
-    /* skip 5 fields */
-    i = 5;
-    while (*p && i) {
-      if (*p++ == ':') i--;
-    }
-    if (!*p || i)
-      return(0);
+       int i=0,ok=0;
+       pstring tok;
+       char *p;
+       char **lines, *line;
 
-    /* count the number of files */
-    while (next_token(&p,tok,",",sizeof(tok)))
-       i++;
-  }
-  free(q);
-
-  return(i);
+       lines = file_lines_load(lp_driverfile(), NULL);
+       if (!lines) {
+               DEBUG(3,("get_printerdrivernumber: Can't open %s - %s\n",
+                        lp_driverfile(),strerror(errno)));
+               return(0);
+       }
+       
+       /* lookup the long printer driver name in the file description */
+       for (i=0;lines[i] && !ok; i++) {
+               p = lines[i];
+               if (next_token(&p,tok,":",sizeof(tok)) &&
+                   (!strncmp(tok,lp_printerdriver(snum),strlen(lp_printerdriver(snum))))) 
+                       ok=1;
+       }
+       line = strdup(p);
+       p = line;
+       file_lines_free(lines);
+       
+       if (ok) {
+               /* skip 5 fields */
+               i = 5;
+               while (*p && i) {
+                       if (*p++ == ':') i--;
+               }
+               if (!*p || i)
+                       return(0);
+               
+               /* count the number of files */
+               while (next_token(&p,tok,",",sizeof(tok)))
+                       i++;
+       }
+       free(line);
+       
+       return(i);
 }
 
 static BOOL api_DosPrintQGetInfo(connection_struct *conn,
@@ -1105,22 +1083,15 @@ static int get_server_info(uint32 servertype,
                           struct srv_info_struct **servers,
                           char *domain)
 {
-  FILE *f;
-  pstring fname;
   int count=0;
   int alloced=0;
-  pstring line;
+  char **lines;
   BOOL local_list_only;
+  int i;
 
-  pstrcpy(fname,lp_lockdir());
-  trim_string(fname,NULL,"/");
-  pstrcat(fname,"/");
-  pstrcat(fname,SERVER_LIST);
-
-  f = sys_fopen(fname,"r");
-
-  if (!f) {
-    DEBUG(4,("Can't open %s - %s\n",fname,strerror(errno)));
+  lines = file_lines_load(lock_path(SERVER_LIST), NULL);
+  if (!lines) {
+    DEBUG(4,("Can't open %s - %s\n",lock_path(SERVER_LIST),strerror(errno)));
     return(0);
   }
 
@@ -1132,16 +1103,13 @@ static int get_server_info(uint32 servertype,
 
   DEBUG(4,("Servertype search: %8x\n",servertype));
 
-  while (!feof(f))
-  {
+  for (i=0;lines[i];i++) {
     fstring stype;
     struct srv_info_struct *s;
-    char *ptr = line;
+    char *ptr = lines[i];
     BOOL ok = True;
-    *ptr = 0;
 
-    fgets(line,sizeof(line)-1,f);
-    if (!*line) continue;
+    if (!*ptr) continue;
     
     if (count == alloced) {
       alloced += 10;
@@ -1209,7 +1177,7 @@ static int get_server_info(uint32 servertype,
       }
   }
   
-  fclose(f);
+  file_lines_free(lines);
   return(count);
 }