2 * Unix SMB/CIFS implementation.
3 * SMB parameters and setup
4 * Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
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 2 of the License, or (at your option)
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
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 675
18 * Mass Ave, Cambridge, MA 02139, USA.
24 #define MAP_FAILED ((void *)-1)
27 /****************************************************************************
28 Read a line from a file with possible \ continuation chars.
29 Blanks at the start or end of a line are stripped.
30 The string will be allocated if s2 is NULL.
31 ****************************************************************************/
33 char *fgets_slash(char *s2
,int maxlen
,XFILE
*f
)
38 BOOL start_of_line
= True
;
49 maxlen
= MIN(maxlen
,8);
50 s
= (char *)SMB_MALLOC(maxlen
);
59 while (len
< maxlen
-1) {
65 while (len
> 0 && s
[len
-1] == ' ') {
68 if (len
> 0 && s
[len
-1] == '\\') {
75 if (len
<= 0 && !s2
) {
84 start_of_line
= False
;
89 if (!s2
&& len
> maxlen
-3) {
91 s
= (char *)SMB_REALLOC(s
,maxlen
);
93 DEBUG(0,("fgets_slash: failed to expand buffer!\n"));
101 /****************************************************************************
102 Load from a pipe into memory.
103 ****************************************************************************/
105 char *file_pload(char *syscmd
, size_t *size
)
112 fd
= sys_popen(syscmd
);
120 while ((n
= read(fd
, buf
, sizeof(buf
))) > 0) {
121 p
= (char *)SMB_REALLOC(p
, total
+ n
+ 1);
123 DEBUG(0,("file_pload: failed to expand buffer!\n"));
127 memcpy(p
+total
, buf
, n
);
135 /* FIXME: Perhaps ought to check that the command completed
136 * successfully (returned 0); if not the data may be
147 /****************************************************************************
148 Load a file into memory from a fd.
149 Truncate at maxsize. If maxsize == 0 - no limit.
150 ****************************************************************************/
152 char *fd_load(int fd
, size_t *psize
, size_t maxsize
)
154 SMB_STRUCT_STAT sbuf
;
158 if (sys_fstat(fd
, &sbuf
) != 0) {
164 size
= MIN(size
, maxsize
);
167 p
= (char *)SMB_MALLOC(size
+1);
172 if (read(fd
, p
, size
) != size
) {
185 /****************************************************************************
186 Load a file into memory.
187 ****************************************************************************/
189 char *file_load(const char *fname
, size_t *size
, size_t maxsize
)
194 if (!fname
|| !*fname
) {
198 fd
= open(fname
,O_RDONLY
);
203 p
= fd_load(fd
, size
, maxsize
);
208 /*******************************************************************
210 *******************************************************************/
212 BOOL
unmap_file(void* start
, size_t size
)
215 if ( munmap( start
, size
) != 0 ) {
216 DEBUG( 1, ("map_file: Failed to unmap address %p "
218 start
, (unsigned int)size
, strerror(errno
) ));
228 /*******************************************************************
229 mmap (if possible) or read a file.
230 ********************************************************************/
232 void *map_file(char *fname
, size_t size
)
238 fd
= open(fname
, O_RDONLY
, 0);
240 DEBUG(2,("map_file: Failed to load %s - %s\n", fname
, strerror(errno
)));
243 p
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
|MAP_FILE
, fd
, 0);
245 if (p
== MAP_FAILED
) {
246 DEBUG(1,("map_file: Failed to mmap %s - %s\n", fname
, strerror(errno
)));
251 p
= file_load(fname
, &s2
, 0);
256 DEBUG(1,("map_file: incorrect size for %s - got %lu expected %lu\n",
257 fname
, (unsigned long)s2
, (unsigned long)size
));
265 /****************************************************************************
266 Parse a buffer into lines.
267 ****************************************************************************/
269 static char **file_lines_parse(char *p
, size_t size
, int *numlines
)
278 for (s
= p
, i
=0; s
< p
+size
; s
++) {
279 if (s
[0] == '\n') i
++;
282 ret
= SMB_MALLOC_ARRAY(char *, i
+2);
287 memset(ret
, 0, sizeof(ret
[0])*(i
+2));
290 for (s
= p
, i
=0; s
< p
+size
; s
++) {
301 /* remove any blank lines at the end */
302 while (i
> 0 && ret
[i
-1][0] == 0) {
313 /****************************************************************************
314 Load a file into memory and return an array of pointers to lines in the file
315 must be freed with file_lines_free().
316 ****************************************************************************/
318 char **file_lines_load(const char *fname
, int *numlines
, size_t maxsize
)
323 p
= file_load(fname
, &size
, maxsize
);
328 return file_lines_parse(p
, size
, numlines
);
331 /****************************************************************************
332 Load a fd into memory and return an array of pointers to lines in the file
333 must be freed with file_lines_free(). If convert is true calls unix_to_dos on
335 ****************************************************************************/
337 char **fd_lines_load(int fd
, int *numlines
, size_t maxsize
)
342 p
= fd_load(fd
, &size
, maxsize
);
347 return file_lines_parse(p
, size
, numlines
);
350 /****************************************************************************
351 Load a pipe into memory and return an array of pointers to lines in the data
352 must be freed with file_lines_free().
353 ****************************************************************************/
355 char **file_lines_pload(char *syscmd
, int *numlines
)
360 p
= file_pload(syscmd
, &size
);
365 return file_lines_parse(p
, size
, numlines
);
368 /****************************************************************************
369 Free lines loaded with file_lines_load.
370 ****************************************************************************/
372 void file_lines_free(char **lines
)
381 /****************************************************************************
382 Take a list of lines and modify them to produce a list where \ continues
384 ****************************************************************************/
386 void file_lines_slashcont(char **lines
)
390 for (i
=0; lines
[i
];) {
391 int len
= strlen(lines
[i
]);
392 if (lines
[i
][len
-1] == '\\') {
393 lines
[i
][len
-1] = ' ';
395 char *p
= &lines
[i
][len
];
396 while (p
< lines
[i
+1]) {
399 for (j
= i
+1; lines
[j
]; j
++) {
400 lines
[j
] = lines
[j
+1];
409 /****************************************************************************
410 Save a lump of data into a file. Mostly used for debugging.
411 ****************************************************************************/
413 BOOL
file_save(const char *fname
, void *packet
, size_t length
)
416 fd
= open(fname
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
420 if (write(fd
, packet
, length
) != (size_t)length
) {