lib/util_file.c

説明を見る。
00001 /*
00002  * Unix SMB/CIFS implementation.
00003  * SMB parameters and setup
00004  * Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
00005  * 
00006  * This program is free software; you can redistribute it and/or modify it under
00007  * the terms of the GNU General Public License as published by the Free
00008  * Software Foundation; either version 2 of the License, or (at your option)
00009  * any later version.
00010  * 
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00014  * more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License along with
00017  * this program; if not, write to the Free Software Foundation, Inc., 675
00018  * Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 #include "includes.h"
00022 
00023 #ifndef MAP_FAILED
00024 #define MAP_FAILED ((void *)-1)
00025 #endif
00026 
00027 /****************************************************************************
00028  Read a line from a file with possible \ continuation chars. 
00029  Blanks at the start or end of a line are stripped.
00030  The string will be allocated if s2 is NULL.
00031 ****************************************************************************/
00032 
00033 char *fgets_slash(char *s2,int maxlen,XFILE *f)
00034 {
00035         char *s=s2;
00036         int len = 0;
00037         int c;
00038         BOOL start_of_line = True;
00039 
00040         if (x_feof(f)) {
00041                 return(NULL);
00042         }
00043 
00044         if (maxlen <2) {
00045                 return(NULL);
00046         }
00047 
00048         if (!s2) {
00049                 maxlen = MIN(maxlen,8);
00050                 s = (char *)SMB_MALLOC(maxlen);
00051         }
00052 
00053         if (!s) {
00054                 return(NULL);
00055         }
00056 
00057         *s = 0;
00058 
00059         while (len < maxlen-1) {
00060                 c = x_getc(f);
00061                 switch (c) {
00062                         case '\r':
00063                                 break;
00064                         case '\n':
00065                                 while (len > 0 && s[len-1] == ' ') {
00066                                         s[--len] = 0;
00067                                 }
00068                                 if (len > 0 && s[len-1] == '\\') {
00069                                         s[--len] = 0;
00070                                         start_of_line = True;
00071                                         break;
00072                                 }
00073                                 return(s);
00074                         case EOF:
00075                                 if (len <= 0 && !s2)  {
00076                                         SAFE_FREE(s);
00077                                 }
00078                                 return(len>0?s:NULL);
00079                         case ' ':
00080                                 if (start_of_line) {
00081                                         break;
00082                                 }
00083                         default:
00084                                 start_of_line = False;
00085                                 s[len++] = c;
00086                                 s[len] = 0;
00087                 }
00088 
00089                 if (!s2 && len > maxlen-3) {
00090                         maxlen *= 2;
00091                         s = (char *)SMB_REALLOC(s,maxlen);
00092                         if (!s) {
00093                                 DEBUG(0,("fgets_slash: failed to expand buffer!\n"));
00094                                 return(NULL);
00095                         }
00096                 }
00097         }
00098         return(s);
00099 }
00100 
00101 /****************************************************************************
00102  Load from a pipe into memory.
00103 ****************************************************************************/
00104 
00105 char *file_pload(char *syscmd, size_t *size)
00106 {
00107         int fd, n;
00108         char *p;
00109         pstring buf;
00110         size_t total;
00111         
00112         fd = sys_popen(syscmd);
00113         if (fd == -1) {
00114                 return NULL;
00115         }
00116 
00117         p = NULL;
00118         total = 0;
00119 
00120         while ((n = read(fd, buf, sizeof(buf))) > 0) {
00121                 p = (char *)SMB_REALLOC(p, total + n + 1);
00122                 if (!p) {
00123                         DEBUG(0,("file_pload: failed to expand buffer!\n"));
00124                         close(fd);
00125                         return NULL;
00126                 }
00127                 memcpy(p+total, buf, n);
00128                 total += n;
00129         }
00130 
00131         if (p) {
00132                 p[total] = 0;
00133         }
00134 
00135         /* FIXME: Perhaps ought to check that the command completed
00136          * successfully (returned 0); if not the data may be
00137          * truncated. */
00138         sys_pclose(fd);
00139 
00140         if (size) {
00141                 *size = total;
00142         }
00143 
00144         return p;
00145 }
00146 
00147 /****************************************************************************
00148  Load a file into memory from a fd.
00149  Truncate at maxsize. If maxsize == 0 - no limit.
00150 ****************************************************************************/ 
00151 
00152 char *fd_load(int fd, size_t *psize, size_t maxsize)
00153 {
00154         SMB_STRUCT_STAT sbuf;
00155         size_t size;
00156         char *p;
00157 
00158         if (sys_fstat(fd, &sbuf) != 0) {
00159                 return NULL;
00160         }
00161 
00162         size = sbuf.st_size;
00163         if (maxsize) {
00164                 size = MIN(size, maxsize);
00165         }
00166 
00167         p = (char *)SMB_MALLOC(size+1);
00168         if (!p) {
00169                 return NULL;
00170         }
00171 
00172         if (read(fd, p, size) != size) {
00173                 SAFE_FREE(p);
00174                 return NULL;
00175         }
00176         p[size] = 0;
00177 
00178         if (psize) {
00179                 *psize = size;
00180         }
00181 
00182         return p;
00183 }
00184 
00185 /****************************************************************************
00186  Load a file into memory.
00187 ****************************************************************************/
00188 
00189 char *file_load(const char *fname, size_t *size, size_t maxsize)
00190 {
00191         int fd;
00192         char *p;
00193 
00194         if (!fname || !*fname) {
00195                 return NULL;
00196         }
00197         
00198         fd = open(fname,O_RDONLY);
00199         if (fd == -1) {
00200                 return NULL;
00201         }
00202 
00203         p = fd_load(fd, size, maxsize);
00204         close(fd);
00205         return p;
00206 }
00207 
00208 /*******************************************************************
00209  unmap or free memory
00210 *******************************************************************/
00211 
00212 BOOL unmap_file(void* start, size_t size)
00213 {
00214 #ifdef HAVE_MMAP
00215         if ( munmap( start, size ) != 0 ) {
00216                 DEBUG( 1, ("map_file: Failed to unmap address %p "
00217                         "of size %u - %s\n", 
00218                         start, (unsigned int)size, strerror(errno) ));
00219                 return False;
00220         }
00221         return True;
00222 #else
00223         SAFE_FREE( start );
00224         return True;
00225 #endif
00226 }
00227 
00228 /*******************************************************************
00229  mmap (if possible) or read a file.
00230 ********************************************************************/
00231 
00232 void *map_file(char *fname, size_t size)
00233 {
00234         size_t s2 = 0;
00235         void *p = NULL;
00236 #ifdef HAVE_MMAP
00237         int fd;
00238         fd = open(fname, O_RDONLY, 0);
00239         if (fd == -1) {
00240                 DEBUG(2,("map_file: Failed to load %s - %s\n", fname, strerror(errno)));
00241                 return NULL;
00242         }
00243         p = mmap(NULL, size, PROT_READ, MAP_SHARED|MAP_FILE, fd, 0);
00244         close(fd);
00245         if (p == MAP_FAILED) {
00246                 DEBUG(1,("map_file: Failed to mmap %s - %s\n", fname, strerror(errno)));
00247                 return NULL;
00248         }
00249 #endif
00250         if (!p) {
00251                 p = file_load(fname, &s2, 0);
00252                 if (!p) {
00253                         return NULL;
00254                 }
00255                 if (s2 != size) {
00256                         DEBUG(1,("map_file: incorrect size for %s - got %lu expected %lu\n",
00257                                  fname, (unsigned long)s2, (unsigned long)size));
00258                         SAFE_FREE(p);   
00259                         return NULL;
00260                 }
00261         }
00262         return p;
00263 }
00264 
00265 /****************************************************************************
00266  Parse a buffer into lines.
00267 ****************************************************************************/
00268 
00269 static char **file_lines_parse(char *p, size_t size, int *numlines)
00270 {
00271         int i;
00272         char *s, **ret;
00273 
00274         if (!p) {
00275                 return NULL;
00276         }
00277 
00278         for (s = p, i=0; s < p+size; s++) {
00279                 if (s[0] == '\n') i++;
00280         }
00281 
00282         ret = SMB_MALLOC_ARRAY(char *, i+2);
00283         if (!ret) {
00284                 SAFE_FREE(p);
00285                 return NULL;
00286         }       
00287         memset(ret, 0, sizeof(ret[0])*(i+2));
00288 
00289         ret[0] = p;
00290         for (s = p, i=0; s < p+size; s++) {
00291                 if (s[0] == '\n') {
00292                         s[0] = 0;
00293                         i++;
00294                         ret[i] = s+1;
00295                 }
00296                 if (s[0] == '\r') {
00297                         s[0] = 0;
00298                 }
00299         }
00300 
00301         /* remove any blank lines at the end */
00302         while (i > 0 && ret[i-1][0] == 0) {
00303                 i--;
00304         }
00305 
00306         if (numlines) {
00307                 *numlines = i;
00308         }
00309 
00310         return ret;
00311 }
00312 
00313 /****************************************************************************
00314  Load a file into memory and return an array of pointers to lines in the file
00315  must be freed with file_lines_free(). 
00316 ****************************************************************************/
00317 
00318 char **file_lines_load(const char *fname, int *numlines, size_t maxsize)
00319 {
00320         char *p;
00321         size_t size = 0;
00322 
00323         p = file_load(fname, &size, maxsize);
00324         if (!p) {
00325                 return NULL;
00326         }
00327 
00328         return file_lines_parse(p, size, numlines);
00329 }
00330 
00331 /****************************************************************************
00332  Load a fd into memory and return an array of pointers to lines in the file
00333  must be freed with file_lines_free(). If convert is true calls unix_to_dos on
00334  the list.
00335 ****************************************************************************/
00336 
00337 char **fd_lines_load(int fd, int *numlines, size_t maxsize)
00338 {
00339         char *p;
00340         size_t size;
00341 
00342         p = fd_load(fd, &size, maxsize);
00343         if (!p) {
00344                 return NULL;
00345         }
00346 
00347         return file_lines_parse(p, size, numlines);
00348 }
00349 
00350 /****************************************************************************
00351  Load a pipe into memory and return an array of pointers to lines in the data
00352  must be freed with file_lines_free(). 
00353 ****************************************************************************/
00354 
00355 char **file_lines_pload(char *syscmd, int *numlines)
00356 {
00357         char *p;
00358         size_t size;
00359 
00360         p = file_pload(syscmd, &size);
00361         if (!p) {
00362                 return NULL;
00363         }
00364 
00365         return file_lines_parse(p, size, numlines);
00366 }
00367 
00368 /****************************************************************************
00369  Free lines loaded with file_lines_load.
00370 ****************************************************************************/
00371 
00372 void file_lines_free(char **lines)
00373 {
00374         if (!lines) {
00375                 return;
00376         }
00377         SAFE_FREE(lines[0]);
00378         SAFE_FREE(lines);
00379 }
00380 
00381 /****************************************************************************
00382  Take a list of lines and modify them to produce a list where \ continues
00383  a line.
00384 ****************************************************************************/
00385 
00386 void file_lines_slashcont(char **lines)
00387 {
00388         int i, j;
00389 
00390         for (i=0; lines[i];) {
00391                 int len = strlen(lines[i]);
00392                 if (lines[i][len-1] == '\\') {
00393                         lines[i][len-1] = ' ';
00394                         if (lines[i+1]) {
00395                                 char *p = &lines[i][len];
00396                                 while (p < lines[i+1]) {
00397                                         *p++ = ' ';
00398                                 }
00399                                 for (j = i+1; lines[j]; j++) {
00400                                         lines[j] = lines[j+1];
00401                                 }
00402                         }
00403                 } else {
00404                         i++;
00405                 }
00406         }
00407 }
00408 
00409 /****************************************************************************
00410  Save a lump of data into a file. Mostly used for debugging.
00411 ****************************************************************************/
00412 
00413 BOOL file_save(const char *fname, void *packet, size_t length)
00414 {
00415         int fd;
00416         fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
00417         if (fd == -1) {
00418                 return False;
00419         }
00420         if (write(fd, packet, length) != (size_t)length) {
00421                 return False;
00422         }
00423         close(fd);
00424         return True;
00425 }

Sambaに対してSat Aug 29 21:23:00 2009に生成されました。  doxygen 1.4.7