2 Unix SMB/Netbios implementation. Version 1.9. SMB parameters and setup
4 Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
7 The Free Software Foundation, Inc.
9 This file is part of the Midnight Commander.
11 The Midnight Commander is free software: you can redistribute it
12 and/or modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation, either version 3 of the License,
14 or (at your option) any later version.
16 The Midnight Commander is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 extern int DEBUGLEVEL
;
32 /***************************************************************
33 Signal function to tell us we timed out.
34 ****************************************************************/
42 /***************************************************************
43 Lock or unlock a fd for a known lock type. Abandon after waitsecs
45 ****************************************************************/
48 do_file_lock (int fd
, int waitsecs
, int type
)
50 SMB_STRUCT_FLOCK lock
;
54 CatchSignal (SIGALRM
, SIGNAL_CAST gotalarm_sig
);
57 lock
.l_whence
= SEEK_SET
;
63 ret
= fcntl (fd
, SMB_F_SETLKW
, &lock
);
65 CatchSignal (SIGALRM
, SIGNAL_CAST SIG_DFL
);
69 DEBUG (0, ("do_file_lock: failed to %s file.\n", type
== F_UNLCK
? "unlock" : "lock"));
76 /***************************************************************
77 Lock an fd. Abandon after waitsecs seconds.
78 ****************************************************************/
81 file_lock (int fd
, int type
, int secs
, int *plock_depth
)
88 if ((*plock_depth
) == 0)
90 if (!do_file_lock (fd
, secs
, type
))
92 DEBUG (10, ("file_lock: locking file failed, error = %s.\n",
93 unix_error_string (errno
)));
101 /***************************************************************
102 Unlock an fd. Abandon after waitsecs seconds.
103 ****************************************************************/
106 file_unlock (int fd
, int *plock_depth
)
110 if (*plock_depth
== 1)
111 ret
= do_file_lock (fd
, 5, F_UNLCK
);
116 DEBUG (10, ("file_unlock: unlocking file failed, error = %s.\n",
117 unix_error_string (errno
)));
121 /***************************************************************
122 locks a file for enumeration / modification.
123 update to be set = True if modification is required.
124 ****************************************************************/
127 startfilepwent (char *pfile
, char *s_readbuf
, int bufsize
, int *file_lock_depth
, BOOL update
)
133 DEBUG (0, ("startfilepwent: No file set\n"));
136 DEBUG (10, ("startfilepwent: opening file %s\n", pfile
));
138 fp
= sys_fopen (pfile
, update
? "r+b" : "rb");
142 DEBUG (0, ("startfilepwent: unable to open file %s\n", pfile
));
146 /* Set a buffer to do more efficient reads */
147 setvbuf (fp
, s_readbuf
, _IOFBF
, bufsize
);
149 if (!file_lock (fileno (fp
), (update
? F_WRLCK
: F_RDLCK
), 5, file_lock_depth
))
151 DEBUG (0, ("startfilepwent: unable to lock file %s\n", pfile
));
156 /* Make sure it is only rw by the owner */
159 /* We have a lock on the file. */
163 /***************************************************************
164 End enumeration of the file.
165 ****************************************************************/
167 endfilepwent (void *vp
, int *file_lock_depth
)
169 FILE *fp
= (FILE *) vp
;
171 file_unlock (fileno (fp
), file_lock_depth
);
173 DEBUG (7, ("endfilepwent: closed file.\n"));
177 /*************************************************************************
178 Return the current position in the file list as an SMB_BIG_UINT.
179 This must be treated as an opaque token.
180 *************************************************************************/
182 getfilepwpos (void *vp
)
184 return (SMB_BIG_UINT
) sys_ftell ((FILE *) vp
);
187 /*************************************************************************
188 Set the current position in the file list from an SMB_BIG_UINT.
189 This must be treated as an opaque token.
190 *************************************************************************/
192 setfilepwpos (void *vp
, SMB_BIG_UINT tok
)
194 return !sys_fseek ((FILE *) vp
, (SMB_OFF_T
) tok
, SEEK_SET
);
197 /*************************************************************************
198 gets a line out of a file.
199 line is of format "xxxx:xxxxxx:xxxxx:".
200 lines with "#" at the front are ignored.
201 *************************************************************************/
203 getfileline (void *vp
, char *linebuf
, int linebuf_size
)
205 /* Static buffers we will return. */
206 FILE *fp
= (FILE *) vp
;
213 DEBUG (0, ("getfileline: Bad file pointer.\n"));
218 * Scan the file, a line at a time.
224 fgets (linebuf
, linebuf_size
, fp
);
231 * Check if the string is terminated with a newline - if not
232 * then we must keep reading and discard until we get one.
235 linebuf_len
= strlen (linebuf
);
236 if (linebuf
[linebuf_len
- 1] != '\n')
239 while (!ferror (fp
) && !feof (fp
))
250 linebuf
[linebuf_len
- 1] = '\0';
253 #ifdef DEBUG_PASSWORD
254 DEBUG (100, ("getfileline: got line |%s|\n", linebuf
));
256 if ((linebuf
[0] == 0) && feof (fp
))
258 DEBUG (4, ("getfileline: end of file reached\n"));
262 if (linebuf
[0] == '#' || linebuf
[0] == '\0')
264 DEBUG (6, ("getfileline: skipping comment or blank line\n"));
268 p
= (unsigned char *) strchr (linebuf
, ':');
271 DEBUG (0, ("getfileline: malformed line entry (no :)\n"));
280 /****************************************************************************
281 read a line from a file with possible \ continuation chars.
282 Blanks at the start or end of a line are stripped.
283 The string will be allocated if s2 is NULL
284 ****************************************************************************/
286 fgets_slash (char *s2
, int maxlen
, FILE * f
)
291 BOOL start_of_line
= True
;
298 maxlen
= MIN (maxlen
, 8);
299 s
= (char *) Realloc (s
, maxlen
);
302 if (!s
|| maxlen
< 2)
307 while (len
< maxlen
- 1)
315 while (len
> 0 && s
[len
- 1] == ' ')
319 if (len
> 0 && s
[len
- 1] == '\\')
322 start_of_line
= True
;
329 return (len
> 0 ? s
: NULL
);
334 start_of_line
= False
;
338 if (!s2
&& len
> maxlen
- 3)
341 s
= (char *) Realloc (s
, maxlen
);