xref: /openbsd-src/usr.bin/mg/file.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: file.c,v 1.95 2014/04/09 20:50:03 florian Exp $	*/
2 
3 /* This file is in the public domain. */
4 
5 /*
6  *	File commands.
7  */
8 
9 #include "def.h"
10 
11 #include <sys/stat.h>
12 
13 #include <libgen.h>
14 
15 size_t xdirname(char *, const char *, size_t);
16 
17 /*
18  * Insert a file into the current buffer.  Real easy - just call the
19  * insertfile routine with the file name.
20  */
21 /* ARGSUSED */
22 int
23 fileinsert(int f, int n)
24 {
25 	char	 fname[NFILEN], *bufp, *adjf;
26 
27 	if (getbufcwd(fname, sizeof(fname)) != TRUE)
28 		fname[0] = '\0';
29 	bufp = eread("Insert file: ", fname, NFILEN,
30 	    EFNEW | EFCR | EFFILE | EFDEF);
31 	if (bufp == NULL)
32 		return (ABORT);
33 	else if (bufp[0] == '\0')
34 		return (FALSE);
35 	adjf = adjustname(bufp, TRUE);
36 	if (adjf == NULL)
37 		return (FALSE);
38 	return (insertfile(adjf, NULL, FALSE));
39 }
40 
41 /*
42  * Select a file for editing.  Look around to see if you can find the file
43  * in another buffer; if you can find it, just switch to the buffer.  If
44  * you cannot find the file, create a new buffer, read in the text, and
45  * switch to the new buffer.
46  */
47 /* ARGSUSED */
48 int
49 filevisit(int f, int n)
50 {
51 	struct buffer	*bp;
52 	char	 fname[NFILEN], *bufp, *adjf;
53 	int	 status;
54 
55 	if (getbufcwd(fname, sizeof(fname)) != TRUE)
56 		fname[0] = '\0';
57 	bufp = eread("Find file: ", fname, NFILEN,
58 	    EFNEW | EFCR | EFFILE | EFDEF);
59 	if (bufp == NULL)
60 		return (ABORT);
61 	else if (bufp[0] == '\0')
62 		return (FALSE);
63 	adjf = adjustname(fname, TRUE);
64 	if (adjf == NULL)
65 		return (FALSE);
66 	if ((bp = findbuffer(adjf)) == NULL)
67 		return (FALSE);
68 	curbp = bp;
69 	if (showbuffer(bp, curwp, WFFULL) != TRUE)
70 		return (FALSE);
71 	if (bp->b_fname[0] == '\0') {
72 		if ((status = readin(adjf)) != TRUE)
73 			killbuffer(bp);
74 		return (status);
75 	}
76 	return (TRUE);
77 }
78 
79 /*
80  * Replace the current file with an alternate one. Semantics for finding
81  * the replacement file are the same as 'filevisit', except the current
82  * buffer is killed before the switch. If the kill fails, or is aborted,
83  * revert to the original file.
84  */
85 /* ARGSUSED */
86 int
87 filevisitalt(int f, int n)
88 {
89 	struct buffer	*bp;
90 	char	 fname[NFILEN], *bufp, *adjf;
91 	int	 status;
92 
93 	if (getbufcwd(fname, sizeof(fname)) != TRUE)
94 		fname[0] = '\0';
95 	bufp = eread("Find alternate file: ", fname, NFILEN,
96 	    EFNEW | EFCR | EFFILE | EFDEF);
97 	if (bufp == NULL)
98 		return (ABORT);
99 	else if (bufp[0] == '\0')
100 		return (FALSE);
101 
102 	status = killbuffer(curbp);
103 	if (status == ABORT || status == FALSE)
104 		return (ABORT);
105 
106 	adjf = adjustname(fname, TRUE);
107 	if (adjf == NULL)
108 		return (FALSE);
109 	if ((bp = findbuffer(adjf)) == NULL)
110 		return (FALSE);
111 	curbp = bp;
112 	if (showbuffer(bp, curwp, WFFULL) != TRUE)
113 		return (FALSE);
114 	if (bp->b_fname[0] == '\0') {
115 		if ((status = readin(adjf)) != TRUE)
116 			killbuffer(bp);
117 		return (status);
118 	}
119 	return (TRUE);
120 }
121 
122 int
123 filevisitro(int f, int n)
124 {
125 	int error;
126 
127 	error = filevisit(f, n);
128 	if (error != TRUE)
129 		return (error);
130 	curbp->b_flag |= BFREADONLY;
131 	return (TRUE);
132 }
133 
134 /*
135  * Pop to a file in the other window.  Same as the last function, but uses
136  * popbuf instead of showbuffer.
137  */
138 /* ARGSUSED */
139 int
140 poptofile(int f, int n)
141 {
142 	struct buffer	*bp;
143 	struct mgwin	*wp;
144 	char	 fname[NFILEN], *adjf, *bufp;
145 	int	 status;
146 
147 	if (getbufcwd(fname, sizeof(fname)) != TRUE)
148 		fname[0] = '\0';
149 	if ((bufp = eread("Find file in other window: ", fname, NFILEN,
150 	    EFNEW | EFCR | EFFILE | EFDEF)) == NULL)
151 		return (ABORT);
152 	else if (bufp[0] == '\0')
153 		return (FALSE);
154 	adjf = adjustname(fname, TRUE);
155 	if (adjf == NULL)
156 		return (FALSE);
157 	if ((bp = findbuffer(adjf)) == NULL)
158 		return (FALSE);
159 	if (bp == curbp)
160 		return (splitwind(f, n));
161 	if ((wp = popbuf(bp, WNONE)) == NULL)
162 		return (FALSE);
163 	curbp = bp;
164 	curwp = wp;
165 	if (bp->b_fname[0] == '\0') {
166 		if ((status = readin(adjf)) != TRUE)
167 			killbuffer(bp);
168 		return (status);
169 	}
170 	return (TRUE);
171 }
172 
173 /*
174  * Read the file "fname" into the current buffer.  Make all of the text
175  * in the buffer go away, after checking for unsaved changes.  This is
176  * called by the "read" command, the "visit" command, and the mainline
177  * (for "mg file").
178  */
179 int
180 readin(char *fname)
181 {
182 	struct mgwin	*wp;
183 	struct stat	 statbuf;
184 	int	 status, i, ro = FALSE;
185 	PF	*ael;
186 	char	 dp[NFILEN];
187 
188 	/* might be old */
189 	if (bclear(curbp) != TRUE)
190 		return (TRUE);
191 	/* Clear readonly. May be set by autoexec path */
192 	curbp->b_flag &= ~BFREADONLY;
193 	if ((status = insertfile(fname, fname, TRUE)) != TRUE) {
194 		dobeep();
195 		ewprintf("File is not readable: %s", fname);
196 		return (FALSE);
197 	}
198 
199 	for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
200 		if (wp->w_bufp == curbp) {
201 			wp->w_dotp = wp->w_linep = bfirstlp(curbp);
202 			wp->w_doto = 0;
203 			wp->w_markp = NULL;
204 			wp->w_marko = 0;
205 		}
206 	}
207 
208 	/*
209 	 * Call auto-executing function if we need to.
210 	 */
211 	if ((ael = find_autoexec(fname)) != NULL) {
212 		for (i = 0; ael[i] != NULL; i++)
213 			(*ael[i])(0, 1);
214 		free(ael);
215 	}
216 
217 	/* no change */
218 	curbp->b_flag &= ~BFCHG;
219 
220 	/*
221 	 * Set the buffer READONLY flag if any of following are true:
222 	 *   1. file is a directory.
223 	 *   2. file is read-only.
224 	 *   3. file doesn't exist and directory is read-only.
225 	 */
226 	if (fisdir(fname) == TRUE) {
227 		ro = TRUE;
228 	} else if ((access(fname, W_OK) == -1)) {
229 		if (errno != ENOENT) {
230 			ro = TRUE;
231 		} else if (errno == ENOENT) {
232 			(void)xdirname(dp, fname, sizeof(dp));
233 			(void)strlcat(dp, "/", sizeof(dp));
234 
235 			/* Missing directory; keep buffer rw, like emacs */
236 			if (stat(dp, &statbuf) == -1 && errno == ENOENT) {
237 				if (eyorn("Missing directory, create") == TRUE)
238 					(void)do_makedir(dp);
239 			} else if (access(dp, W_OK) == -1 && errno == EACCES) {
240 				ewprintf("File not found and directory"
241 				    " write-protected");
242 				ro = TRUE;
243 			}
244 		}
245 	}
246 	if (ro == TRUE)
247 		curbp->b_flag |= BFREADONLY;
248 
249 	if (startrow) {
250 		gotoline(FFARG, startrow);
251 		startrow = 0;
252 	}
253 
254 	undo_add_modified();
255 	return (status);
256 }
257 
258 /*
259  * NB, getting file attributes is done here under control of a flag
260  * rather than in readin, which would be cleaner.  I was concerned
261  * that some operating system might require the file to be open
262  * in order to get the information.  Similarly for writing.
263  */
264 
265 /*
266  * Insert a file in the current buffer, after dot. If file is a directory,
267  * and 'replacebuf' is TRUE, invoke dired mode, else die with an error.
268  * If file is a regular file, set mark at the end of the text inserted;
269  * point at the beginning.  Return a standard status. Print a summary
270  * (lines read, error message) out as well. This routine also does the
271  * read end of backup processing.  The BFBAK flag, if set in a buffer,
272  * says that a backup should be taken.  It is set when a file is read in,
273  * but not on a new file. You don't need to make a backup copy of nothing.
274  */
275 
276 static char	*line = NULL;
277 static int	linesize = 0;
278 
279 int
280 insertfile(char *fname, char *newname, int replacebuf)
281 {
282 	struct buffer	*bp;
283 	struct line	*lp1, *lp2;
284 	struct line	*olp;			/* line we started at */
285 	struct mgwin	*wp;
286 	int	 nbytes, s, nline = 0, siz, x, x2;
287 	int	 opos;			/* offset we started at */
288 	int	 oline;			/* original line number */
289         FILE    *ffp;
290 
291 	if (replacebuf == TRUE)
292 		x = undo_enable(FFRAND, 0);
293 	else
294 		x = undo_enabled();
295 
296 	lp1 = NULL;
297 	if (line == NULL) {
298 		line = malloc(NLINE);
299 		if (line == NULL)
300 			panic("out of memory");
301 		linesize = NLINE;
302 	}
303 
304 	/* cheap */
305 	bp = curbp;
306 	if (newname != NULL) {
307 		(void)strlcpy(bp->b_fname, newname, sizeof(bp->b_fname));
308 		(void)xdirname(bp->b_cwd, newname, sizeof(bp->b_cwd));
309 		(void)strlcat(bp->b_cwd, "/", sizeof(bp->b_cwd));
310 	}
311 
312 	/* hard file open */
313 	if ((s = ffropen(&ffp, fname, (replacebuf == TRUE) ? bp : NULL))
314 	    == FIOERR)
315 		goto out;
316 	if (s == FIOFNF) {
317 		/* file not found */
318 		if (newname != NULL)
319 			ewprintf("(New file)");
320 		else
321 			ewprintf("(File not found)");
322 		goto out;
323 	} else if (s == FIODIR) {
324 		/* file was a directory */
325 		if (replacebuf == FALSE) {
326 			dobeep();
327 			ewprintf("Cannot insert: file is a directory, %s",
328 			    fname);
329 			goto cleanup;
330 		}
331 		killbuffer(bp);
332 		bp = dired_(fname);
333 		undo_enable(FFRAND, x);
334 		if (bp == NULL)
335 			return (FALSE);
336 		curbp = bp;
337 		return (showbuffer(bp, curwp, WFFULL | WFMODE));
338 	} else {
339 		(void)xdirname(bp->b_cwd, fname, sizeof(bp->b_cwd));
340 		(void)strlcat(bp->b_cwd, "/", sizeof(bp->b_cwd));
341 	}
342 	opos = curwp->w_doto;
343 	oline = curwp->w_dotline;
344 	/*
345 	 * Open a new line at dot and start inserting after it.
346 	 * We will delete this newline after insertion.
347 	 * Disable undo, as we create the undo record manually.
348 	 */
349 	x2 = undo_enable(FFRAND, 0);
350 	(void)lnewline();
351 	olp = lback(curwp->w_dotp);
352 	undo_enable(FFRAND, x2);
353 
354 	nline = 0;
355 	siz = 0;
356 	while ((s = ffgetline(ffp, line, linesize, &nbytes)) != FIOERR) {
357 retry:
358 		siz += nbytes + 1;
359 		switch (s) {
360 		case FIOSUC:
361 			/* FALLTHRU */
362 		case FIOEOF:
363 			++nline;
364 			if ((lp1 = lalloc(nbytes)) == NULL) {
365 				/* keep message on the display */
366 				s = FIOERR;
367 				undo_add_insert(olp, opos,
368 				    siz - nbytes - 1 - 1);
369 				goto endoffile;
370 			}
371 			bcopy(line, &ltext(lp1)[0], nbytes);
372 			lp2 = lback(curwp->w_dotp);
373 			lp2->l_fp = lp1;
374 			lp1->l_fp = curwp->w_dotp;
375 			lp1->l_bp = lp2;
376 			curwp->w_dotp->l_bp = lp1;
377 			if (s == FIOEOF) {
378 				undo_add_insert(olp, opos, siz - 1);
379 				goto endoffile;
380 			}
381 			break;
382 		case FIOLONG: {
383 				/* a line too long to fit in our buffer */
384 				char	*cp;
385 				int	newsize;
386 
387 				newsize = linesize * 2;
388 				if (newsize < 0 ||
389 				    (cp = malloc(newsize)) == NULL) {
390 					dobeep();
391 					ewprintf("Could not allocate %d bytes",
392 					    newsize);
393 						s = FIOERR;
394 						goto endoffile;
395 				}
396 				bcopy(line, cp, linesize);
397 				free(line);
398 				line = cp;
399 				s = ffgetline(ffp, line + linesize, linesize,
400 				    &nbytes);
401 				nbytes += linesize;
402 				linesize = newsize;
403 				if (s == FIOERR)
404 					goto endoffile;
405 				goto retry;
406 			}
407 		default:
408 			dobeep();
409 			ewprintf("Unknown code %d reading file", s);
410 			s = FIOERR;
411 			break;
412 		}
413 	}
414 endoffile:
415 	/* ignore errors */
416 	(void)ffclose(ffp, NULL);
417 	/* don't zap an error */
418 	if (s == FIOEOF) {
419 		if (nline == 1)
420 			ewprintf("(Read 1 line)");
421 		else
422 			ewprintf("(Read %d lines)", nline);
423 	}
424 	/* set mark at the end of the text */
425 	curwp->w_dotp = curwp->w_markp = lback(curwp->w_dotp);
426 	curwp->w_marko = llength(curwp->w_markp);
427 	curwp->w_markline = oline + nline + 1;
428 	/*
429 	 * if we are at the end of the file, ldelnewline is a no-op,
430 	 * but we still need to decrement the line and markline counts
431 	 * as we've accounted for this fencepost in our arithmetic
432 	 */
433 	if (lforw(curwp->w_dotp) == curwp->w_bufp->b_headp) {
434 		curwp->w_bufp->b_lines--;
435 		curwp->w_markline--;
436 	} else
437 		(void)ldelnewline();
438 	curwp->w_dotp = olp;
439 	curwp->w_doto = opos;
440 	curwp->w_dotline = oline;
441 	if (olp == curbp->b_headp)
442 		curwp->w_dotp = lforw(olp);
443 	if (newname != NULL)
444 		bp->b_flag |= BFCHG | BFBAK;	/* Need a backup.	 */
445 	else
446 		bp->b_flag |= BFCHG;
447 	/*
448 	 * If the insert was at the end of buffer, set lp1 to the end of
449 	 * buffer line, and lp2 to the beginning of the newly inserted text.
450 	 * (Otherwise lp2 is set to NULL.)  This is used below to set
451 	 * pointers in other windows correctly if they are also at the end of
452 	 * buffer.
453 	 */
454 	lp1 = bp->b_headp;
455 	if (curwp->w_markp == lp1) {
456 		lp2 = curwp->w_dotp;
457 	} else {
458 		/* delete extraneous newline */
459 		(void)ldelnewline();
460 out:		lp2 = NULL;
461 	}
462 	for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
463 		if (wp->w_bufp == curbp) {
464 			wp->w_rflag |= WFMODE | WFEDIT;
465 			if (wp != curwp && lp2 != NULL) {
466 				if (wp->w_dotp == lp1)
467 					wp->w_dotp = lp2;
468 				if (wp->w_markp == lp1)
469 					wp->w_markp = lp2;
470 				if (wp->w_linep == lp1)
471 					wp->w_linep = lp2;
472 			}
473 		}
474 	}
475 	bp->b_lines += nline;
476 cleanup:
477 	undo_enable(FFRAND, x);
478 
479 	/* return FALSE if error */
480 	return (s != FIOERR);
481 }
482 
483 /*
484  * Ask for a file name and write the contents of the current buffer to that
485  * file.  Update the remembered file name and clear the buffer changed flag.
486  * This handling of file names is different from the earlier versions and
487  * is more compatible with Gosling EMACS than with ITS EMACS.
488  */
489 /* ARGSUSED */
490 int
491 filewrite(int f, int n)
492 {
493 	struct stat     statbuf;
494 	int	 s;
495 	char	 fname[NFILEN], bn[NBUFN], tmp[NFILEN + 25];
496 	char	*adjfname, *bufp;
497         FILE    *ffp;
498 
499 	if (getbufcwd(fname, sizeof(fname)) != TRUE)
500 		fname[0] = '\0';
501 	if ((bufp = eread("Write file: ", fname, NFILEN,
502 	    EFDEF | EFNEW | EFCR | EFFILE)) == NULL)
503 		return (ABORT);
504 	else if (bufp[0] == '\0')
505 		return (FALSE);
506 
507 	adjfname = adjustname(fname, TRUE);
508 	if (adjfname == NULL)
509 		return (FALSE);
510 
511         /* Check if file exists; write checks done later */
512         if (stat(adjfname, &statbuf) == 0) {
513 		if (S_ISDIR(statbuf.st_mode)) {
514 			dobeep();
515 			ewprintf("%s is a directory", adjfname);
516 			return (FALSE);
517 		}
518 		snprintf(tmp, sizeof(tmp), "File `%s' exists; overwrite",
519 		    adjfname);
520 		if ((s = eyorn(tmp)) != TRUE)
521                         return (s);
522         }
523 
524 	/* old attributes are no longer current */
525 	bzero(&curbp->b_fi, sizeof(curbp->b_fi));
526 	if ((s = writeout(&ffp, curbp, adjfname)) == TRUE) {
527 		(void)strlcpy(curbp->b_fname, adjfname, sizeof(curbp->b_fname));
528 		if (getbufcwd(curbp->b_cwd, sizeof(curbp->b_cwd)) != TRUE)
529 			(void)strlcpy(curbp->b_cwd, "/", sizeof(curbp->b_cwd));
530 		if (augbname(bn, curbp->b_fname, sizeof(bn))
531 		    == FALSE)
532 			return (FALSE);
533 		free(curbp->b_bname);
534 		if ((curbp->b_bname = strdup(bn)) == NULL)
535 			return (FALSE);
536 		(void)fupdstat(curbp);
537 		curbp->b_flag &= ~(BFBAK | BFCHG);
538 		upmodes(curbp);
539 		undo_add_boundary(FFRAND, 1);
540 		undo_add_modified();
541 	}
542 	return (s);
543 }
544 
545 /*
546  * Save the contents of the current buffer back into its associated file.
547  */
548 #ifndef	MAKEBACKUP
549 #define	MAKEBACKUP TRUE
550 #endif /* !MAKEBACKUP */
551 static int	makebackup = MAKEBACKUP;
552 
553 /* ARGSUSED */
554 int
555 filesave(int f, int n)
556 {
557 	if (curbp->b_fname[0] == '\0')
558 		return (filewrite(f, n));
559 	else
560 		return (buffsave(curbp));
561 }
562 
563 /*
564  * Save the contents of the buffer argument into its associated file.  Do
565  * nothing if there have been no changes (is this a bug, or a feature?).
566  * Error if there is no remembered file name. If this is the first write
567  * since the read or visit, then a backup copy of the file is made.
568  * Allow user to select whether or not to make backup files by looking at
569  * the value of makebackup.
570  */
571 int
572 buffsave(struct buffer *bp)
573 {
574 	int	 s;
575         FILE    *ffp;
576 
577 	/* return, no changes */
578 	if ((bp->b_flag & BFCHG) == 0) {
579 		ewprintf("(No changes need to be saved)");
580 		return (TRUE);
581 	}
582 
583 	/* must have a name */
584 	if (bp->b_fname[0] == '\0') {
585 		dobeep();
586 		ewprintf("No file name");
587 		return (FALSE);
588 	}
589 
590 	/* Ensure file has not been modified elsewhere */
591 	/* We don't use the ignore flag here */
592 	if (fchecktime(bp) != TRUE) {
593 		if ((s = eyesno("File has changed on disk since last save. "
594 		    "Save anyway")) != TRUE)
595 			return (s);
596 	}
597 
598 	if (makebackup && (bp->b_flag & BFBAK)) {
599 		s = fbackupfile(bp->b_fname);
600 		/* hard error */
601 		if (s == ABORT)
602 			return (FALSE);
603 		/* softer error */
604 		if (s == FALSE &&
605 		    (s = eyesno("Backup error, save anyway")) != TRUE)
606 			return (s);
607 	}
608 	if ((s = writeout(&ffp, bp, bp->b_fname)) == TRUE) {
609 		(void)fupdstat(bp);
610 		bp->b_flag &= ~(BFCHG | BFBAK);
611 		upmodes(bp);
612 		undo_add_boundary(FFRAND, 1);
613 		undo_add_modified();
614 	}
615 	return (s);
616 }
617 
618 /*
619  * Since we don't have variables (we probably should) this is a command
620  * processor for changing the value of the make backup flag.  If no argument
621  * is given, sets makebackup to true, so backups are made.  If an argument is
622  * given, no backup files are made when saving a new version of a file.
623  */
624 /* ARGSUSED */
625 int
626 makebkfile(int f, int n)
627 {
628 	if (f & FFARG)
629 		makebackup = n > 0;
630 	else
631 		makebackup = !makebackup;
632 	ewprintf("Backup files %sabled", makebackup ? "en" : "dis");
633 	return (TRUE);
634 }
635 
636 /*
637  * NB: bp is passed to both ffwopen and ffclose because some
638  * attribute information may need to be updated at open time
639  * and others after the close.  This is OS-dependent.  Note
640  * that the ff routines are assumed to be able to tell whether
641  * the attribute information has been set up in this buffer
642  * or not.
643  */
644 
645 /*
646  * This function performs the details of file writing; writing the file
647  * in buffer bp to file fn. Uses the file management routines in the
648  * "fileio.c" package. Most of the grief is checking of some sort.
649  * You may want to call fupdstat() after using this function.
650  */
651 int
652 writeout(FILE ** ffp, struct buffer *bp, char *fn)
653 {
654 	struct stat	statbuf;
655 	int	 s;
656 	char     dp[NFILEN];
657 
658 	if (stat(fn, &statbuf) == -1 && errno == ENOENT) {
659 		errno = 0;
660 		(void)xdirname(dp, fn, sizeof(dp));
661 		(void)strlcat(dp, "/", sizeof(dp));
662 		if (access(dp, W_OK) && errno == EACCES) {
663 			dobeep();
664 			ewprintf("Directory %s write-protected", dp);
665 			return (FIOERR);
666 		} else if (errno == ENOENT) {
667    			dobeep();
668 			ewprintf("%s: no such directory", dp);
669 			return (FIOERR);
670 		}
671         }
672 	/* open writes message */
673 	if ((s = ffwopen(ffp, fn, bp)) != FIOSUC)
674 		return (FALSE);
675 	s = ffputbuf(*ffp, bp);
676 	if (s == FIOSUC) {
677 		/* no write error */
678 		s = ffclose(*ffp, bp);
679 		if (s == FIOSUC)
680 			ewprintf("Wrote %s", fn);
681 	} else {
682 		/* print a message indicating write error */
683 		(void)ffclose(*ffp, bp);
684 		dobeep();
685 		ewprintf("Unable to write %s", fn);
686 	}
687 	return (s == FIOSUC);
688 }
689 
690 /*
691  * Tag all windows for bp (all windows if bp == NULL) as needing their
692  * mode line updated.
693  */
694 void
695 upmodes(struct buffer *bp)
696 {
697 	struct mgwin	*wp;
698 
699 	for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
700 		if (bp == NULL || curwp->w_bufp == bp)
701 			wp->w_rflag |= WFMODE;
702 }
703 
704 /*
705  * dirname using strlcpy semantic.
706  * Like dirname() except an empty string is returned in
707  * place of "/". This means we can always add a trailing
708  * slash and be correct.
709  * Address portability issues by copying argument
710  * before using. Some implementations modify the input string.
711  */
712 size_t
713 xdirname(char *dp, const char *path, size_t dplen)
714 {
715 	char ts[NFILEN];
716 	size_t len;
717 
718 	(void)strlcpy(ts, path, NFILEN);
719 	len = strlcpy(dp, dirname(ts), dplen);
720 	if (dplen > 0 && dp[0] == '/' && dp[1] == '\0') {
721 		dp[0] = '\0';
722 		len = 0;
723 	}
724 	return (len);
725 }
726 
727 /*
728  * basename using strlcpy/strlcat semantic.
729  * Address portability issue by copying argument
730  * before using: some implementations modify the input string.
731  */
732 size_t
733 xbasename(char *bp, const char *path, size_t bplen)
734 {
735 	char ts[NFILEN];
736 
737 	(void)strlcpy(ts, path, NFILEN);
738 	return (strlcpy(bp, basename(ts), bplen));
739 }
740