History log of /openbsd-current/usr.bin/mg/fileio.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.112 11-Aug-2023 guenther

Replace use of the old BSD st_*timespec members in struct stat with
the POSIX-standard st_*tim members.

ok millert@


# 1.111 30-Mar-2023 op

don't access(conffile)

This removes a few access(2) calls in the configuration file handling.
startupfile() now opens and return the file and to avoid surprises it
also uses a caller-provided buffer to store the filename. This also
removes the extra adjustpath() that load() did: it has been moved to
evalfile() only.

with help, fixes and ok tb@


# 1.110 30-Mar-2023 op

strncpy -> strlcpy to properly NUL-terminate the copy of the path

otherwise paths longer than NFILEN (1024) given with -u won't
NUL-terminate `file'.


Revision tags: OPENBSD_7_3_BASE
# 1.109 08-Mar-2023 guenther

Delete obsolete /* ARGSUSED */ lint comments.

ok miod@ millert@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE OPENBSD_7_1_BASE OPENBSD_7_2_BASE
# 1.108 01-Mar-2021 lum

Put the hardcoded '\n' character which is found throughout mg into a
buffer specific variable. The diff should not produce any behavourial
changes in mg.


# 1.107 23-Feb-2021 lum

Allow the user to specify a path to the mg startup file on the command line.


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.106 22-Jun-2019 lum

This diff moves the check for a newline at the end of a buffer (when
saving it) earlier. This means data doesn't 'disappear' from the file
on disk while the user answers 'yes' or 'no'.


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.105 13-Apr-2018 florian

mg tries to write backups to the wrong directory when run under a
different effective user, i.e. when invoced via su and
backup-to-home-directory is enabled.

Problem pointed out and diff provied by Lucas Gabriel Vuotto
<lvuotto92 () gmail ! com>, thanks!

Subsequently slacked on for nearly a year by yours truly.

Then remembered when Han Boetes <hboetes () gmail ! com> came up with
a similar diff because of a problem report by Mark Willson where it
turned out that getlogin(2) is not very portable.

OK tb


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.104 30-May-2017 florian

fix -Wshadow warnings.
Found by bcallah, thanks!


Revision tags: OPENBSD_6_1_BASE
# 1.103 28-Jul-2016 tedu

strncmp is a more reasonable way to check the beginning of a string.


# 1.102 28-Jul-2016 tedu

these programs probably do not need to use TMPDIR. ok florian


Revision tags: OPENBSD_6_0_BASE
# 1.101 04-Jul-2016 guenther

Use fstatat() instead of crafting a filename to use with stat()

ok millert@


Revision tags: OPENBSD_5_9_BASE
# 1.100 26-Jan-2016 jasper

ensure the backup file has the same mtime as the original file, this is in line
with emacs' behaviour.

from Harald Dunkel via Han Boetes
ok lum@


Revision tags: OPENBSD_5_8_BASE
# 1.99 19-Mar-2015 bcallah

Clean up the includes in mg.
This does the following:
Moves all POSIX headers from sysdef.h into the individual .c files so that
each file now only includes what it needs. All headers are properly sorted.
Moves the remainder of sysdef.h to other files (mostly def.h) and deletes
sysdef.h now that it's no longer contains anything.
Tweak a comment that references sysdef.h so that it no longer does that.
ok florian@


Revision tags: OPENBSD_5_7_BASE
# 1.98 16-Nov-2014 guenther

Stop using <sys/param.h>; replace MAXPATHLEN with PATH_MAX, stop using MAX(),
and pull in <limits.h> for *_MAX constants.

inspired on a diff from Kamil Rytarowski (n54 (at) gmx.com)
ok bcallah@


Revision tags: OPENBSD_5_6_BASE
# 1.97 20-Mar-2014 lum

Add some missing dobeeps.
ok florian@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.96 18-May-2013 lum

and and -> and


Revision tags: OPENBSD_5_3_BASE
# 1.95 27-Nov-2012 jasper

- move prototype of expandtilder() to def.h; needed by an upcoming diff.


Revision tags: OPENBSD_5_2_BASE
# 1.94 10-Jul-2012 lum

Fix an outstanding debian bug: #658539

"looks good" kjell


# 1.93 18-Jun-2012 jasper

- add a pathnames.h
- some whitespace cleanup

ok lum@


# 1.92 15-Jun-2012 lum

Fixes a problem where if you try to open a file that doesn't exist and has
a name longer than LOGIN_NAME_MAX and also has a tilde at the front
e.g:

$ mg ~01234567890123456789012345678901

mg will give a "Login name too long" instead of opening a new buffer
named ~01234567890123456789012345678901


# 1.91 14-Jun-2012 lum

Allow mg to save backup files to a users home directory.
Suggestions from eric@ and Sunil Nimmagadda.
Remarks deraadt@.


# 1.90 11-Jun-2012 lum

Change error message to reflect the correct filename: nname is 'New
name' as opposed to tname 'Temp name'


# 1.89 25-May-2012 lum

Remove static FILE pointer used for handling files in fileio.c. Pass
by reference instead. This allows the mg startup file to open other
files without unexpected things happening.

Discussed with Sunil Nimmagadda.


# 1.88 23-May-2012 lum

Change how mg handles files beginning with a '~'.
Bring behaviour more into line with emacs.


# 1.87 12-Apr-2012 lum

Remove the conditional directives NO_MACRO and NO_STARTUP.
They have not compiled for numerous years.
ok kjell@ millert@


# 1.86 28-Mar-2012 lum

Close an opendir(). From Igor Zinovik.


Revision tags: OPENBSD_5_1_BASE
# 1.85 31-Aug-2011 lum

On a file write fail:

1. return an error value
2. show an error message

From Loganaden Velvindron with suggestion from millert@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.84 21-Jan-2011 kjell

It volates style(9), but in mg, #include"def.h" goes first. 'twas the way it was built. no binary change here. confirmed by lum@, tested by Henri Kemppainen


# 1.83 21-Jan-2011 kjell

Change len+memcmp check to a strncmp for prefix testing on
filename completion. Avoids a recurring portability headache.
ok lum@
"concerns assuaged to the soothing sounds of Martin Denny blambert@"
(I worry about that guy...)


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.82 15-Sep-2008 kjell

Enable dirty buffer detection in mg.
Emulate the emacs behavior: after suspend/resume, buffer switch,
or at save time, warn (prompt) the user if the file has been modified
on disk in the interim.
This has already saved my butt numerous times.
ok phessler


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.81 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.80 24-Dec-2006 kjell

Fix a bug where inserting a file resulted in an incorrect
line-number count for a buffer (M-X insert-file, M-> to reproduce).
While here, fix a number of bugs with incorrect line numbers
after swap point-and-mark

Originally reported via debian's bug tracking system. Fix tested by
Han Boetes and Deanna Phillips.


# 1.79 19-Nov-2006 deraadt

spacing


# 1.78 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.77 25-Jul-2006 kjell

Rename the header line of a buffer to b_headp, from the remarkably
unintuitive b_linep. No binary change.


# 1.76 29-Jun-2006 jason

- use <dirent.h> not <sys/dir.h> (this appeases FreeBSD's annoying #warning)
- grab <sys/time.h> before <sys/resource.h> like getrusage(2) says
ok kjell


# 1.75 01-Jun-2006 kjell

fix style


# 1.74 01-Jun-2006 jason

make // /~ path rewriting optional in adjustname() and use it everywhere
except for the command line specified files. ok kjell,cloder


# 1.73 03-May-2006 kjell

correct one more comment


# 1.72 03-May-2006 kjell

Do some KNF, clean up some unused junk that has lying around for 40-odd
cvs revisions, and fix a few comments to match reality


# 1.71 03-May-2006 kjell

len = strlen(foo); if foo[len - 1]... will do bad things if len == 0.
Avoid, and rearrange a test so it can't (hypothetically) overflow.


# 1.70 03-Apr-2006 deraadt

lint love; ok kjell


# 1.69 02-Apr-2006 kjell

test char against '\0', not 0


Revision tags: OPENBSD_3_9_BASE
# 1.68 20-Dec-2005 kjell

Clean up the ugly casted frees. In one case, this meant eliminating a nasty
struct/union/casting nightmare when building the list of names for filename
completion. In particular, be consistent about strduping and freeing
the list data.


# 1.67 20-Dec-2005 kjell

Do some delinting of strl-type functions. Also, remove a superfluous
word in the undo-list.


# 1.66 13-Dec-2005 kjell

do some silly de-linting


# 1.65 20-Nov-2005 kjell

#ifdef NO_DIR can go too. From Han Boetes.


# 1.64 20-Nov-2005 kjell

Kill the NO_BACKUP #ifdef


# 1.63 20-Nov-2005 deraadt

toast NO_DIRED #ifdef; ok kjell


# 1.62 18-Nov-2005 deraadt

greedy use of typedef struct was making code harder to read; ok kjell cloder


# 1.61 13-Nov-2005 kjell

Better error checking of snprintfs. From Han Boetes.


# 1.60 11-Nov-2005 kjell

Now that we have an editable minibuffer, revert parsing of
"//" and "/~" as '/' and '~' respectively. I know emacs does it,
but it is weird, and breaks things that foolishly use filenames
like "/tmp//crontab.xxxx".
Proddings from matthieu and deraadt. Error report from Bernd Ahlers.


# 1.59 11-Nov-2005 deraadt

spacing


# 1.58 18-Oct-2005 kjell

When attempting to open a filename, walk backwards through minibuffer:
a> if you run into the beginning of the string, use the whole thing
b> if you run into a // combo, use everything starting from the second /
c> if you run into a /~ combo, use everything starting from the ~
i.e. do like emacs.
From (and for) Jason Wright


# 1.57 17-Oct-2005 kjell

Fix string handling. LOGIN_NAME_MAX includes the NUL, so don't reserve extra
space. Fix usage of strlcpy. Correct test for terminating slash.

"looks good" deraadt@


# 1.56 14-Oct-2005 deraadt

more headers to please lint


# 1.55 14-Oct-2005 kjell

move the dired routines into dired.c where they belong.
ok cloder@


# 1.54 13-Oct-2005 kjell

Make dired buffer read-only by default.
Noticed by Han Boetes


# 1.53 13-Oct-2005 kjell

Use dired mode automatically if file specified for loading is
a directory. Modified version of patch from Han Boetes.
ok cloder@


# 1.52 13-Oct-2005 kjell

check if current line is null. fixes a core in dired.


# 1.51 13-Oct-2005 kjell

Fix dired mode. Make deletions work, and keystrokes match emacs.
* dired-other-window should default to current buffer's path.
* Remove redundant code (from Han Boetes)
* Move initialization (and since we're here, fix mail-mode initialization
too)
* Remove redundant keymap in dired.c, and replace it with
the better one that was being ignored in keymap.c.


Revision tags: OPENBSD_3_8_BASE
# 1.50 14-Jun-2005 kjell

Add explicit public domain notices to all public domain files.
ok millert@, deraadt@


# 1.49 03-Apr-2005 db

This is a no binary change which does:

- spelling, punctuation fixes
- variable declaration lineup
- use parentheses for return and sizeof
- K&R function declarations -> ANSI
- other minor code beautification

ok henning@


Revision tags: OPENBSD_3_7_BASE
# 1.48 10-Mar-2005 deraadt

spacing


# 1.47 10-Mar-2005 henning

the realpath() was needed to do do relative to absolute path
conversion. revert my diff that takes it out, and instead, on
realpath() failure (like when you have no perms on pwd) just return the
unexpanded path. everything still fine with that, just tab completeion
and the like does not work (obviously).
this was mainly for "sudo mg /etc/something" from ~ where ~ is
nfs-mounted with root mapped to -2
and now, that case works as well as tab completion on insert-file etc.


# 1.46 09-Mar-2005 jfb

fix mg's behaviour with regards to files on which we do not have
write access. diff originally from vincent@ and forgotten for
a while.

ok rohee@, "toss it in, i'll bitch if it doesn't work" henning@


# 1.45 01-Feb-2005 henning

make fbackupfile() return FALSE when the rename(2) from the
mkstemp()-derived name to the filename~ one fails
From: Han Boetes <han@mijncomputer.nl>


# 1.44 31-Jan-2005 millert

Safer backup file generation using mkstemp(); henning@ OK


# 1.43 11-Jan-2005 henning

no need to call realpath() in adjustname()
realpath returns an error if cwd is not writeable, and that is quite common
with NFS-mounted ~ and sudo mg /etc/something - annoying as hell. realpath
is not needed at all here, so zap it. vincent ok


Revision tags: OPENBSD_3_6_BASE
# 1.42 09-Jul-2004 vincent

when saving a file, check whether it is newline terminated and if it is not,
prompt the user and add the newline if he agrees.

tested by a lot of helpful tech@ guys, approved by henning


# 1.41 08-Jul-2004 vincent

simplify the file writing loop

looked at by ian, tested on i386, macppc, sparc64


Revision tags: OPENBSD_3_5_BASE
# 1.40 14-Nov-2003 vincent

read(2) returns ssize_t, not size_t. spotted by Andrey Matveev.


# 1.39 08-Nov-2003 jmc

typos from Jonathon Gray;


Revision tags: OPENBSD_3_4_BASE
# 1.38 15-Aug-2003 vincent

make dired work more like emacs. (well, make it _work_, and then add a
bunch of missing commands)


# 1.37 15-Aug-2003 vincent

actually extract the filename off the ls line. makes dired *slightly*
more useful.


# 1.36 08-May-2003 vincent

use NULL to indicate failure in functions that return pointers, not FALSE.
typo in comment

from Patrick Latifi (patrick.l@hermes.usherb.ca)


# 1.35 06-May-2003 vincent

fix a small memleak on errors. noticed by pat latifi.

ok millert, fgsch


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.34 22-Aug-2002 deraadt

knf


# 1.33 25-Jul-2002 vincent

replace the ugly and buggy adjustname function by a simple one using
simple APIs. makes mg not crash with 65k filenames...

ok art@


# 1.32 01-Jul-2002 vincent

KNF


# 1.31 21-Jun-2002 vincent

fix bad usage of strlcpy()'s return value. (made pointers point
beyond the boundaries of buffers)

ok deraadt@


# 1.30 22-Apr-2002 vincent

don't use /bin/cp to copy files.

ok deraadt@


# 1.29 22-Apr-2002 vincent

fix mem leak

ok deraadt@


# 1.28 15-Apr-2002 deraadt

use fchmod and fchown instead; vincent


Revision tags: OPENBSD_3_1_BASE
# 1.27 27-Mar-2002 millert

Fixes the the following problems (from zyrnix)
1) Mg crashes with column width of 1, rows > 2
2) Mg panics on resizing due to incomplete write
3) Mg doesn't compile with STARTUPFILE defined
deraadt@ OK


# 1.26 16-Mar-2002 vincent

Make these special buffers readonly by default. ok art@


# 1.25 11-Mar-2002 vincent

* Move to ANSI function definitions.
* Add a whole lot of consts where I thought it made sense

no ok, but no objections either...


# 1.24 22-Feb-2002 deraadt

if file mode known, try to avoid a race.. i think. millert spotted a bug in my first draft


# 1.23 14-Feb-2002 vincent

d_makename now takes a length parameter so we can remove the
strcpy call.


# 1.22 14-Feb-2002 deraadt

some KNF


# 1.21 13-Feb-2002 vincent

* Replace unsafe strcpy and strcat calls to safe strlcpy and strlcat.
* Be a little bit more verbose about some errors
* Fix some memory leaks in fileio.c

ok deraadt@, art@


Revision tags: OPENBSD_3_0_BASE
# 1.20 21-Sep-2001 wilfried

correct fork logic and rewrite for clarity, ok todd@


# 1.19 09-Jul-2001 deraadt

correct type on last arg to execl(); nordin@cse.ogi.edu


# 1.18 06-Jul-2001 art

Change the filename completion code to:
- not use an unprotected strcat.
- Append a '/' when a file is a symlink to a directory.
(tab completion on '/sys' is now less annoying.)
- actually make some more sense.


# 1.17 24-May-2001 art

Don't assume that map_table[0] is the fundamental map.


# 1.16 24-May-2001 mickey

spaces


# 1.15 24-May-2001 mickey

use strlcpy vs cpy+len, some other fixes


# 1.14 23-May-2001 art

Get rid of unnecessary casts of NULL.


# 1.13 23-May-2001 art

remove VOID. We're way beyond the point where this could build with an
ancient compiler.


# 1.12 03-May-2001 art

* Let make_file_list always append a '/' at the end of directories.
This makes filename TAB-completion more useful, now you don't need to
write that '/' manually.
* random cleanups including using snprintf and memcmp where open-coded
equivalents were used, waitpid instead of 'while (wait() != pid);', etc.


# 1.11 01-May-2001 art

Use opendir/readdir instead of open/read when making a file list.
This makes filename tab-completion work and takes dired a step further.


Revision tags: OPENBSD_2_9_BASE
# 1.10 29-Jan-2001 niklas

$OpenBSD$


# 1.9 24-Jan-2001 millert

Don't use rename to backup the file to a ~ file. We need to make a
copy instead so the original is edited in place for things like
cron and vipw.


Revision tags: OPENBSD_2_8_BASE
# 1.8 02-Aug-2000 millert

$HOME paranoia: never use getenv("HOME") w/o checking for NULL and non-zero


Revision tags: OPENBSD_2_7_BASE
# 1.7 13-Apr-2000 millert

The start of KNF + -Wall. The code has been run through indent but
needs hand fixup. I stopped at keymap.c...


# 1.6 27-Feb-2000 millert

Remove a now-bogus comment
Use pid_t
Use vfork(), not fork() when sensible


# 1.5 27-Feb-2000 millert

We don't have silly SYSV 14-character file name limits. Taken from
the BSD fileio.c. Note that this module is rife with PATH_MAX overflow
possibilities.


# 1.4 26-Feb-2000 millert

Use uid_t, gid_t and mode_t where applicable.
Pull in changes from the BSD-specific sysdef.h and fileio.c:
kill bogus unlinkdir() and rename() kludges since we have rmdir(2) and rename(2)
define SYMBLINK since we have symlinks
TODO: merge BSD spawn.c and ttyio.c


# 1.3 25-Feb-2000 deraadt

further pruning


# 1.2 25-Feb-2000 deraadt

make it minimally compile; millert


# 1.1 25-Feb-2000 deraadt

initial import of mg2a


# 1.111 30-Mar-2023 op

don't access(conffile)

This removes a few access(2) calls in the configuration file handling.
startupfile() now opens and return the file and to avoid surprises it
also uses a caller-provided buffer to store the filename. This also
removes the extra adjustpath() that load() did: it has been moved to
evalfile() only.

with help, fixes and ok tb@


# 1.110 30-Mar-2023 op

strncpy -> strlcpy to properly NUL-terminate the copy of the path

otherwise paths longer than NFILEN (1024) given with -u won't
NUL-terminate `file'.


Revision tags: OPENBSD_7_3_BASE
# 1.109 08-Mar-2023 guenther

Delete obsolete /* ARGSUSED */ lint comments.

ok miod@ millert@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE OPENBSD_7_1_BASE OPENBSD_7_2_BASE
# 1.108 01-Mar-2021 lum

Put the hardcoded '\n' character which is found throughout mg into a
buffer specific variable. The diff should not produce any behavourial
changes in mg.


# 1.107 23-Feb-2021 lum

Allow the user to specify a path to the mg startup file on the command line.


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.106 22-Jun-2019 lum

This diff moves the check for a newline at the end of a buffer (when
saving it) earlier. This means data doesn't 'disappear' from the file
on disk while the user answers 'yes' or 'no'.


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.105 13-Apr-2018 florian

mg tries to write backups to the wrong directory when run under a
different effective user, i.e. when invoced via su and
backup-to-home-directory is enabled.

Problem pointed out and diff provied by Lucas Gabriel Vuotto
<lvuotto92 () gmail ! com>, thanks!

Subsequently slacked on for nearly a year by yours truly.

Then remembered when Han Boetes <hboetes () gmail ! com> came up with
a similar diff because of a problem report by Mark Willson where it
turned out that getlogin(2) is not very portable.

OK tb


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.104 30-May-2017 florian

fix -Wshadow warnings.
Found by bcallah, thanks!


Revision tags: OPENBSD_6_1_BASE
# 1.103 28-Jul-2016 tedu

strncmp is a more reasonable way to check the beginning of a string.


# 1.102 28-Jul-2016 tedu

these programs probably do not need to use TMPDIR. ok florian


Revision tags: OPENBSD_6_0_BASE
# 1.101 04-Jul-2016 guenther

Use fstatat() instead of crafting a filename to use with stat()

ok millert@


Revision tags: OPENBSD_5_9_BASE
# 1.100 26-Jan-2016 jasper

ensure the backup file has the same mtime as the original file, this is in line
with emacs' behaviour.

from Harald Dunkel via Han Boetes
ok lum@


Revision tags: OPENBSD_5_8_BASE
# 1.99 19-Mar-2015 bcallah

Clean up the includes in mg.
This does the following:
Moves all POSIX headers from sysdef.h into the individual .c files so that
each file now only includes what it needs. All headers are properly sorted.
Moves the remainder of sysdef.h to other files (mostly def.h) and deletes
sysdef.h now that it's no longer contains anything.
Tweak a comment that references sysdef.h so that it no longer does that.
ok florian@


Revision tags: OPENBSD_5_7_BASE
# 1.98 16-Nov-2014 guenther

Stop using <sys/param.h>; replace MAXPATHLEN with PATH_MAX, stop using MAX(),
and pull in <limits.h> for *_MAX constants.

inspired on a diff from Kamil Rytarowski (n54 (at) gmx.com)
ok bcallah@


Revision tags: OPENBSD_5_6_BASE
# 1.97 20-Mar-2014 lum

Add some missing dobeeps.
ok florian@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.96 18-May-2013 lum

and and -> and


Revision tags: OPENBSD_5_3_BASE
# 1.95 27-Nov-2012 jasper

- move prototype of expandtilder() to def.h; needed by an upcoming diff.


Revision tags: OPENBSD_5_2_BASE
# 1.94 10-Jul-2012 lum

Fix an outstanding debian bug: #658539

"looks good" kjell


# 1.93 18-Jun-2012 jasper

- add a pathnames.h
- some whitespace cleanup

ok lum@


# 1.92 15-Jun-2012 lum

Fixes a problem where if you try to open a file that doesn't exist and has
a name longer than LOGIN_NAME_MAX and also has a tilde at the front
e.g:

$ mg ~01234567890123456789012345678901

mg will give a "Login name too long" instead of opening a new buffer
named ~01234567890123456789012345678901


# 1.91 14-Jun-2012 lum

Allow mg to save backup files to a users home directory.
Suggestions from eric@ and Sunil Nimmagadda.
Remarks deraadt@.


# 1.90 11-Jun-2012 lum

Change error message to reflect the correct filename: nname is 'New
name' as opposed to tname 'Temp name'


# 1.89 25-May-2012 lum

Remove static FILE pointer used for handling files in fileio.c. Pass
by reference instead. This allows the mg startup file to open other
files without unexpected things happening.

Discussed with Sunil Nimmagadda.


# 1.88 23-May-2012 lum

Change how mg handles files beginning with a '~'.
Bring behaviour more into line with emacs.


# 1.87 12-Apr-2012 lum

Remove the conditional directives NO_MACRO and NO_STARTUP.
They have not compiled for numerous years.
ok kjell@ millert@


# 1.86 28-Mar-2012 lum

Close an opendir(). From Igor Zinovik.


Revision tags: OPENBSD_5_1_BASE
# 1.85 31-Aug-2011 lum

On a file write fail:

1. return an error value
2. show an error message

From Loganaden Velvindron with suggestion from millert@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.84 21-Jan-2011 kjell

It volates style(9), but in mg, #include"def.h" goes first. 'twas the way it was built. no binary change here. confirmed by lum@, tested by Henri Kemppainen


# 1.83 21-Jan-2011 kjell

Change len+memcmp check to a strncmp for prefix testing on
filename completion. Avoids a recurring portability headache.
ok lum@
"concerns assuaged to the soothing sounds of Martin Denny blambert@"
(I worry about that guy...)


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.82 15-Sep-2008 kjell

Enable dirty buffer detection in mg.
Emulate the emacs behavior: after suspend/resume, buffer switch,
or at save time, warn (prompt) the user if the file has been modified
on disk in the interim.
This has already saved my butt numerous times.
ok phessler


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.81 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.80 24-Dec-2006 kjell

Fix a bug where inserting a file resulted in an incorrect
line-number count for a buffer (M-X insert-file, M-> to reproduce).
While here, fix a number of bugs with incorrect line numbers
after swap point-and-mark

Originally reported via debian's bug tracking system. Fix tested by
Han Boetes and Deanna Phillips.


# 1.79 19-Nov-2006 deraadt

spacing


# 1.78 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.77 25-Jul-2006 kjell

Rename the header line of a buffer to b_headp, from the remarkably
unintuitive b_linep. No binary change.


# 1.76 29-Jun-2006 jason

- use <dirent.h> not <sys/dir.h> (this appeases FreeBSD's annoying #warning)
- grab <sys/time.h> before <sys/resource.h> like getrusage(2) says
ok kjell


# 1.75 01-Jun-2006 kjell

fix style


# 1.74 01-Jun-2006 jason

make // /~ path rewriting optional in adjustname() and use it everywhere
except for the command line specified files. ok kjell,cloder


# 1.73 03-May-2006 kjell

correct one more comment


# 1.72 03-May-2006 kjell

Do some KNF, clean up some unused junk that has lying around for 40-odd
cvs revisions, and fix a few comments to match reality


# 1.71 03-May-2006 kjell

len = strlen(foo); if foo[len - 1]... will do bad things if len == 0.
Avoid, and rearrange a test so it can't (hypothetically) overflow.


# 1.70 03-Apr-2006 deraadt

lint love; ok kjell


# 1.69 02-Apr-2006 kjell

test char against '\0', not 0


Revision tags: OPENBSD_3_9_BASE
# 1.68 20-Dec-2005 kjell

Clean up the ugly casted frees. In one case, this meant eliminating a nasty
struct/union/casting nightmare when building the list of names for filename
completion. In particular, be consistent about strduping and freeing
the list data.


# 1.67 20-Dec-2005 kjell

Do some delinting of strl-type functions. Also, remove a superfluous
word in the undo-list.


# 1.66 13-Dec-2005 kjell

do some silly de-linting


# 1.65 20-Nov-2005 kjell

#ifdef NO_DIR can go too. From Han Boetes.


# 1.64 20-Nov-2005 kjell

Kill the NO_BACKUP #ifdef


# 1.63 20-Nov-2005 deraadt

toast NO_DIRED #ifdef; ok kjell


# 1.62 18-Nov-2005 deraadt

greedy use of typedef struct was making code harder to read; ok kjell cloder


# 1.61 13-Nov-2005 kjell

Better error checking of snprintfs. From Han Boetes.


# 1.60 11-Nov-2005 kjell

Now that we have an editable minibuffer, revert parsing of
"//" and "/~" as '/' and '~' respectively. I know emacs does it,
but it is weird, and breaks things that foolishly use filenames
like "/tmp//crontab.xxxx".
Proddings from matthieu and deraadt. Error report from Bernd Ahlers.


# 1.59 11-Nov-2005 deraadt

spacing


# 1.58 18-Oct-2005 kjell

When attempting to open a filename, walk backwards through minibuffer:
a> if you run into the beginning of the string, use the whole thing
b> if you run into a // combo, use everything starting from the second /
c> if you run into a /~ combo, use everything starting from the ~
i.e. do like emacs.
From (and for) Jason Wright


# 1.57 17-Oct-2005 kjell

Fix string handling. LOGIN_NAME_MAX includes the NUL, so don't reserve extra
space. Fix usage of strlcpy. Correct test for terminating slash.

"looks good" deraadt@


# 1.56 14-Oct-2005 deraadt

more headers to please lint


# 1.55 14-Oct-2005 kjell

move the dired routines into dired.c where they belong.
ok cloder@


# 1.54 13-Oct-2005 kjell

Make dired buffer read-only by default.
Noticed by Han Boetes


# 1.53 13-Oct-2005 kjell

Use dired mode automatically if file specified for loading is
a directory. Modified version of patch from Han Boetes.
ok cloder@


# 1.52 13-Oct-2005 kjell

check if current line is null. fixes a core in dired.


# 1.51 13-Oct-2005 kjell

Fix dired mode. Make deletions work, and keystrokes match emacs.
* dired-other-window should default to current buffer's path.
* Remove redundant code (from Han Boetes)
* Move initialization (and since we're here, fix mail-mode initialization
too)
* Remove redundant keymap in dired.c, and replace it with
the better one that was being ignored in keymap.c.


Revision tags: OPENBSD_3_8_BASE
# 1.50 14-Jun-2005 kjell

Add explicit public domain notices to all public domain files.
ok millert@, deraadt@


# 1.49 03-Apr-2005 db

This is a no binary change which does:

- spelling, punctuation fixes
- variable declaration lineup
- use parentheses for return and sizeof
- K&R function declarations -> ANSI
- other minor code beautification

ok henning@


Revision tags: OPENBSD_3_7_BASE
# 1.48 10-Mar-2005 deraadt

spacing


# 1.47 10-Mar-2005 henning

the realpath() was needed to do do relative to absolute path
conversion. revert my diff that takes it out, and instead, on
realpath() failure (like when you have no perms on pwd) just return the
unexpanded path. everything still fine with that, just tab completeion
and the like does not work (obviously).
this was mainly for "sudo mg /etc/something" from ~ where ~ is
nfs-mounted with root mapped to -2
and now, that case works as well as tab completion on insert-file etc.


# 1.46 09-Mar-2005 jfb

fix mg's behaviour with regards to files on which we do not have
write access. diff originally from vincent@ and forgotten for
a while.

ok rohee@, "toss it in, i'll bitch if it doesn't work" henning@


# 1.45 01-Feb-2005 henning

make fbackupfile() return FALSE when the rename(2) from the
mkstemp()-derived name to the filename~ one fails
From: Han Boetes <han@mijncomputer.nl>


# 1.44 31-Jan-2005 millert

Safer backup file generation using mkstemp(); henning@ OK


# 1.43 11-Jan-2005 henning

no need to call realpath() in adjustname()
realpath returns an error if cwd is not writeable, and that is quite common
with NFS-mounted ~ and sudo mg /etc/something - annoying as hell. realpath
is not needed at all here, so zap it. vincent ok


Revision tags: OPENBSD_3_6_BASE
# 1.42 09-Jul-2004 vincent

when saving a file, check whether it is newline terminated and if it is not,
prompt the user and add the newline if he agrees.

tested by a lot of helpful tech@ guys, approved by henning


# 1.41 08-Jul-2004 vincent

simplify the file writing loop

looked at by ian, tested on i386, macppc, sparc64


Revision tags: OPENBSD_3_5_BASE
# 1.40 14-Nov-2003 vincent

read(2) returns ssize_t, not size_t. spotted by Andrey Matveev.


# 1.39 08-Nov-2003 jmc

typos from Jonathon Gray;


Revision tags: OPENBSD_3_4_BASE
# 1.38 15-Aug-2003 vincent

make dired work more like emacs. (well, make it _work_, and then add a
bunch of missing commands)


# 1.37 15-Aug-2003 vincent

actually extract the filename off the ls line. makes dired *slightly*
more useful.


# 1.36 08-May-2003 vincent

use NULL to indicate failure in functions that return pointers, not FALSE.
typo in comment

from Patrick Latifi (patrick.l@hermes.usherb.ca)


# 1.35 06-May-2003 vincent

fix a small memleak on errors. noticed by pat latifi.

ok millert, fgsch


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.34 22-Aug-2002 deraadt

knf


# 1.33 25-Jul-2002 vincent

replace the ugly and buggy adjustname function by a simple one using
simple APIs. makes mg not crash with 65k filenames...

ok art@


# 1.32 01-Jul-2002 vincent

KNF


# 1.31 21-Jun-2002 vincent

fix bad usage of strlcpy()'s return value. (made pointers point
beyond the boundaries of buffers)

ok deraadt@


# 1.30 22-Apr-2002 vincent

don't use /bin/cp to copy files.

ok deraadt@


# 1.29 22-Apr-2002 vincent

fix mem leak

ok deraadt@


# 1.28 15-Apr-2002 deraadt

use fchmod and fchown instead; vincent


Revision tags: OPENBSD_3_1_BASE
# 1.27 27-Mar-2002 millert

Fixes the the following problems (from zyrnix)
1) Mg crashes with column width of 1, rows > 2
2) Mg panics on resizing due to incomplete write
3) Mg doesn't compile with STARTUPFILE defined
deraadt@ OK


# 1.26 16-Mar-2002 vincent

Make these special buffers readonly by default. ok art@


# 1.25 11-Mar-2002 vincent

* Move to ANSI function definitions.
* Add a whole lot of consts where I thought it made sense

no ok, but no objections either...


# 1.24 22-Feb-2002 deraadt

if file mode known, try to avoid a race.. i think. millert spotted a bug in my first draft


# 1.23 14-Feb-2002 vincent

d_makename now takes a length parameter so we can remove the
strcpy call.


# 1.22 14-Feb-2002 deraadt

some KNF


# 1.21 13-Feb-2002 vincent

* Replace unsafe strcpy and strcat calls to safe strlcpy and strlcat.
* Be a little bit more verbose about some errors
* Fix some memory leaks in fileio.c

ok deraadt@, art@


Revision tags: OPENBSD_3_0_BASE
# 1.20 21-Sep-2001 wilfried

correct fork logic and rewrite for clarity, ok todd@


# 1.19 09-Jul-2001 deraadt

correct type on last arg to execl(); nordin@cse.ogi.edu


# 1.18 06-Jul-2001 art

Change the filename completion code to:
- not use an unprotected strcat.
- Append a '/' when a file is a symlink to a directory.
(tab completion on '/sys' is now less annoying.)
- actually make some more sense.


# 1.17 24-May-2001 art

Don't assume that map_table[0] is the fundamental map.


# 1.16 24-May-2001 mickey

spaces


# 1.15 24-May-2001 mickey

use strlcpy vs cpy+len, some other fixes


# 1.14 23-May-2001 art

Get rid of unnecessary casts of NULL.


# 1.13 23-May-2001 art

remove VOID. We're way beyond the point where this could build with an
ancient compiler.


# 1.12 03-May-2001 art

* Let make_file_list always append a '/' at the end of directories.
This makes filename TAB-completion more useful, now you don't need to
write that '/' manually.
* random cleanups including using snprintf and memcmp where open-coded
equivalents were used, waitpid instead of 'while (wait() != pid);', etc.


# 1.11 01-May-2001 art

Use opendir/readdir instead of open/read when making a file list.
This makes filename tab-completion work and takes dired a step further.


Revision tags: OPENBSD_2_9_BASE
# 1.10 29-Jan-2001 niklas

$OpenBSD$


# 1.9 24-Jan-2001 millert

Don't use rename to backup the file to a ~ file. We need to make a
copy instead so the original is edited in place for things like
cron and vipw.


Revision tags: OPENBSD_2_8_BASE
# 1.8 02-Aug-2000 millert

$HOME paranoia: never use getenv("HOME") w/o checking for NULL and non-zero


Revision tags: OPENBSD_2_7_BASE
# 1.7 13-Apr-2000 millert

The start of KNF + -Wall. The code has been run through indent but
needs hand fixup. I stopped at keymap.c...


# 1.6 27-Feb-2000 millert

Remove a now-bogus comment
Use pid_t
Use vfork(), not fork() when sensible


# 1.5 27-Feb-2000 millert

We don't have silly SYSV 14-character file name limits. Taken from
the BSD fileio.c. Note that this module is rife with PATH_MAX overflow
possibilities.


# 1.4 26-Feb-2000 millert

Use uid_t, gid_t and mode_t where applicable.
Pull in changes from the BSD-specific sysdef.h and fileio.c:
kill bogus unlinkdir() and rename() kludges since we have rmdir(2) and rename(2)
define SYMBLINK since we have symlinks
TODO: merge BSD spawn.c and ttyio.c


# 1.3 25-Feb-2000 deraadt

further pruning


# 1.2 25-Feb-2000 deraadt

make it minimally compile; millert


# 1.1 25-Feb-2000 deraadt

initial import of mg2a


# 1.109 08-Mar-2023 guenther

Delete obsolete /* ARGSUSED */ lint comments.

ok miod@ millert@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE OPENBSD_7_1_BASE OPENBSD_7_2_BASE
# 1.108 01-Mar-2021 lum

Put the hardcoded '\n' character which is found throughout mg into a
buffer specific variable. The diff should not produce any behavourial
changes in mg.


# 1.107 23-Feb-2021 lum

Allow the user to specify a path to the mg startup file on the command line.


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.106 22-Jun-2019 lum

This diff moves the check for a newline at the end of a buffer (when
saving it) earlier. This means data doesn't 'disappear' from the file
on disk while the user answers 'yes' or 'no'.


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.105 13-Apr-2018 florian

mg tries to write backups to the wrong directory when run under a
different effective user, i.e. when invoced via su and
backup-to-home-directory is enabled.

Problem pointed out and diff provied by Lucas Gabriel Vuotto
<lvuotto92 () gmail ! com>, thanks!

Subsequently slacked on for nearly a year by yours truly.

Then remembered when Han Boetes <hboetes () gmail ! com> came up with
a similar diff because of a problem report by Mark Willson where it
turned out that getlogin(2) is not very portable.

OK tb


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.104 30-May-2017 florian

fix -Wshadow warnings.
Found by bcallah, thanks!


Revision tags: OPENBSD_6_1_BASE
# 1.103 28-Jul-2016 tedu

strncmp is a more reasonable way to check the beginning of a string.


# 1.102 28-Jul-2016 tedu

these programs probably do not need to use TMPDIR. ok florian


Revision tags: OPENBSD_6_0_BASE
# 1.101 04-Jul-2016 guenther

Use fstatat() instead of crafting a filename to use with stat()

ok millert@


Revision tags: OPENBSD_5_9_BASE
# 1.100 26-Jan-2016 jasper

ensure the backup file has the same mtime as the original file, this is in line
with emacs' behaviour.

from Harald Dunkel via Han Boetes
ok lum@


Revision tags: OPENBSD_5_8_BASE
# 1.99 19-Mar-2015 bcallah

Clean up the includes in mg.
This does the following:
Moves all POSIX headers from sysdef.h into the individual .c files so that
each file now only includes what it needs. All headers are properly sorted.
Moves the remainder of sysdef.h to other files (mostly def.h) and deletes
sysdef.h now that it's no longer contains anything.
Tweak a comment that references sysdef.h so that it no longer does that.
ok florian@


Revision tags: OPENBSD_5_7_BASE
# 1.98 16-Nov-2014 guenther

Stop using <sys/param.h>; replace MAXPATHLEN with PATH_MAX, stop using MAX(),
and pull in <limits.h> for *_MAX constants.

inspired on a diff from Kamil Rytarowski (n54 (at) gmx.com)
ok bcallah@


Revision tags: OPENBSD_5_6_BASE
# 1.97 20-Mar-2014 lum

Add some missing dobeeps.
ok florian@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.96 18-May-2013 lum

and and -> and


Revision tags: OPENBSD_5_3_BASE
# 1.95 27-Nov-2012 jasper

- move prototype of expandtilder() to def.h; needed by an upcoming diff.


Revision tags: OPENBSD_5_2_BASE
# 1.94 10-Jul-2012 lum

Fix an outstanding debian bug: #658539

"looks good" kjell


# 1.93 18-Jun-2012 jasper

- add a pathnames.h
- some whitespace cleanup

ok lum@


# 1.92 15-Jun-2012 lum

Fixes a problem where if you try to open a file that doesn't exist and has
a name longer than LOGIN_NAME_MAX and also has a tilde at the front
e.g:

$ mg ~01234567890123456789012345678901

mg will give a "Login name too long" instead of opening a new buffer
named ~01234567890123456789012345678901


# 1.91 14-Jun-2012 lum

Allow mg to save backup files to a users home directory.
Suggestions from eric@ and Sunil Nimmagadda.
Remarks deraadt@.


# 1.90 11-Jun-2012 lum

Change error message to reflect the correct filename: nname is 'New
name' as opposed to tname 'Temp name'


# 1.89 25-May-2012 lum

Remove static FILE pointer used for handling files in fileio.c. Pass
by reference instead. This allows the mg startup file to open other
files without unexpected things happening.

Discussed with Sunil Nimmagadda.


# 1.88 23-May-2012 lum

Change how mg handles files beginning with a '~'.
Bring behaviour more into line with emacs.


# 1.87 12-Apr-2012 lum

Remove the conditional directives NO_MACRO and NO_STARTUP.
They have not compiled for numerous years.
ok kjell@ millert@


# 1.86 28-Mar-2012 lum

Close an opendir(). From Igor Zinovik.


Revision tags: OPENBSD_5_1_BASE
# 1.85 31-Aug-2011 lum

On a file write fail:

1. return an error value
2. show an error message

From Loganaden Velvindron with suggestion from millert@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.84 21-Jan-2011 kjell

It volates style(9), but in mg, #include"def.h" goes first. 'twas the way it was built. no binary change here. confirmed by lum@, tested by Henri Kemppainen


# 1.83 21-Jan-2011 kjell

Change len+memcmp check to a strncmp for prefix testing on
filename completion. Avoids a recurring portability headache.
ok lum@
"concerns assuaged to the soothing sounds of Martin Denny blambert@"
(I worry about that guy...)


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.82 15-Sep-2008 kjell

Enable dirty buffer detection in mg.
Emulate the emacs behavior: after suspend/resume, buffer switch,
or at save time, warn (prompt) the user if the file has been modified
on disk in the interim.
This has already saved my butt numerous times.
ok phessler


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.81 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.80 24-Dec-2006 kjell

Fix a bug where inserting a file resulted in an incorrect
line-number count for a buffer (M-X insert-file, M-> to reproduce).
While here, fix a number of bugs with incorrect line numbers
after swap point-and-mark

Originally reported via debian's bug tracking system. Fix tested by
Han Boetes and Deanna Phillips.


# 1.79 19-Nov-2006 deraadt

spacing


# 1.78 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.77 25-Jul-2006 kjell

Rename the header line of a buffer to b_headp, from the remarkably
unintuitive b_linep. No binary change.


# 1.76 29-Jun-2006 jason

- use <dirent.h> not <sys/dir.h> (this appeases FreeBSD's annoying #warning)
- grab <sys/time.h> before <sys/resource.h> like getrusage(2) says
ok kjell


# 1.75 01-Jun-2006 kjell

fix style


# 1.74 01-Jun-2006 jason

make // /~ path rewriting optional in adjustname() and use it everywhere
except for the command line specified files. ok kjell,cloder


# 1.73 03-May-2006 kjell

correct one more comment


# 1.72 03-May-2006 kjell

Do some KNF, clean up some unused junk that has lying around for 40-odd
cvs revisions, and fix a few comments to match reality


# 1.71 03-May-2006 kjell

len = strlen(foo); if foo[len - 1]... will do bad things if len == 0.
Avoid, and rearrange a test so it can't (hypothetically) overflow.


# 1.70 03-Apr-2006 deraadt

lint love; ok kjell


# 1.69 02-Apr-2006 kjell

test char against '\0', not 0


Revision tags: OPENBSD_3_9_BASE
# 1.68 20-Dec-2005 kjell

Clean up the ugly casted frees. In one case, this meant eliminating a nasty
struct/union/casting nightmare when building the list of names for filename
completion. In particular, be consistent about strduping and freeing
the list data.


# 1.67 20-Dec-2005 kjell

Do some delinting of strl-type functions. Also, remove a superfluous
word in the undo-list.


# 1.66 13-Dec-2005 kjell

do some silly de-linting


# 1.65 20-Nov-2005 kjell

#ifdef NO_DIR can go too. From Han Boetes.


# 1.64 20-Nov-2005 kjell

Kill the NO_BACKUP #ifdef


# 1.63 20-Nov-2005 deraadt

toast NO_DIRED #ifdef; ok kjell


# 1.62 18-Nov-2005 deraadt

greedy use of typedef struct was making code harder to read; ok kjell cloder


# 1.61 13-Nov-2005 kjell

Better error checking of snprintfs. From Han Boetes.


# 1.60 11-Nov-2005 kjell

Now that we have an editable minibuffer, revert parsing of
"//" and "/~" as '/' and '~' respectively. I know emacs does it,
but it is weird, and breaks things that foolishly use filenames
like "/tmp//crontab.xxxx".
Proddings from matthieu and deraadt. Error report from Bernd Ahlers.


# 1.59 11-Nov-2005 deraadt

spacing


# 1.58 18-Oct-2005 kjell

When attempting to open a filename, walk backwards through minibuffer:
a> if you run into the beginning of the string, use the whole thing
b> if you run into a // combo, use everything starting from the second /
c> if you run into a /~ combo, use everything starting from the ~
i.e. do like emacs.
From (and for) Jason Wright


# 1.57 17-Oct-2005 kjell

Fix string handling. LOGIN_NAME_MAX includes the NUL, so don't reserve extra
space. Fix usage of strlcpy. Correct test for terminating slash.

"looks good" deraadt@


# 1.56 14-Oct-2005 deraadt

more headers to please lint


# 1.55 14-Oct-2005 kjell

move the dired routines into dired.c where they belong.
ok cloder@


# 1.54 13-Oct-2005 kjell

Make dired buffer read-only by default.
Noticed by Han Boetes


# 1.53 13-Oct-2005 kjell

Use dired mode automatically if file specified for loading is
a directory. Modified version of patch from Han Boetes.
ok cloder@


# 1.52 13-Oct-2005 kjell

check if current line is null. fixes a core in dired.


# 1.51 13-Oct-2005 kjell

Fix dired mode. Make deletions work, and keystrokes match emacs.
* dired-other-window should default to current buffer's path.
* Remove redundant code (from Han Boetes)
* Move initialization (and since we're here, fix mail-mode initialization
too)
* Remove redundant keymap in dired.c, and replace it with
the better one that was being ignored in keymap.c.


Revision tags: OPENBSD_3_8_BASE
# 1.50 14-Jun-2005 kjell

Add explicit public domain notices to all public domain files.
ok millert@, deraadt@


# 1.49 03-Apr-2005 db

This is a no binary change which does:

- spelling, punctuation fixes
- variable declaration lineup
- use parentheses for return and sizeof
- K&R function declarations -> ANSI
- other minor code beautification

ok henning@


Revision tags: OPENBSD_3_7_BASE
# 1.48 10-Mar-2005 deraadt

spacing


# 1.47 10-Mar-2005 henning

the realpath() was needed to do do relative to absolute path
conversion. revert my diff that takes it out, and instead, on
realpath() failure (like when you have no perms on pwd) just return the
unexpanded path. everything still fine with that, just tab completeion
and the like does not work (obviously).
this was mainly for "sudo mg /etc/something" from ~ where ~ is
nfs-mounted with root mapped to -2
and now, that case works as well as tab completion on insert-file etc.


# 1.46 09-Mar-2005 jfb

fix mg's behaviour with regards to files on which we do not have
write access. diff originally from vincent@ and forgotten for
a while.

ok rohee@, "toss it in, i'll bitch if it doesn't work" henning@


# 1.45 01-Feb-2005 henning

make fbackupfile() return FALSE when the rename(2) from the
mkstemp()-derived name to the filename~ one fails
From: Han Boetes <han@mijncomputer.nl>


# 1.44 31-Jan-2005 millert

Safer backup file generation using mkstemp(); henning@ OK


# 1.43 11-Jan-2005 henning

no need to call realpath() in adjustname()
realpath returns an error if cwd is not writeable, and that is quite common
with NFS-mounted ~ and sudo mg /etc/something - annoying as hell. realpath
is not needed at all here, so zap it. vincent ok


Revision tags: OPENBSD_3_6_BASE
# 1.42 09-Jul-2004 vincent

when saving a file, check whether it is newline terminated and if it is not,
prompt the user and add the newline if he agrees.

tested by a lot of helpful tech@ guys, approved by henning


# 1.41 08-Jul-2004 vincent

simplify the file writing loop

looked at by ian, tested on i386, macppc, sparc64


Revision tags: OPENBSD_3_5_BASE
# 1.40 14-Nov-2003 vincent

read(2) returns ssize_t, not size_t. spotted by Andrey Matveev.


# 1.39 08-Nov-2003 jmc

typos from Jonathon Gray;


Revision tags: OPENBSD_3_4_BASE
# 1.38 15-Aug-2003 vincent

make dired work more like emacs. (well, make it _work_, and then add a
bunch of missing commands)


# 1.37 15-Aug-2003 vincent

actually extract the filename off the ls line. makes dired *slightly*
more useful.


# 1.36 08-May-2003 vincent

use NULL to indicate failure in functions that return pointers, not FALSE.
typo in comment

from Patrick Latifi (patrick.l@hermes.usherb.ca)


# 1.35 06-May-2003 vincent

fix a small memleak on errors. noticed by pat latifi.

ok millert, fgsch


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.34 22-Aug-2002 deraadt

knf


# 1.33 25-Jul-2002 vincent

replace the ugly and buggy adjustname function by a simple one using
simple APIs. makes mg not crash with 65k filenames...

ok art@


# 1.32 01-Jul-2002 vincent

KNF


# 1.31 21-Jun-2002 vincent

fix bad usage of strlcpy()'s return value. (made pointers point
beyond the boundaries of buffers)

ok deraadt@


# 1.30 22-Apr-2002 vincent

don't use /bin/cp to copy files.

ok deraadt@


# 1.29 22-Apr-2002 vincent

fix mem leak

ok deraadt@


# 1.28 15-Apr-2002 deraadt

use fchmod and fchown instead; vincent


Revision tags: OPENBSD_3_1_BASE
# 1.27 27-Mar-2002 millert

Fixes the the following problems (from zyrnix)
1) Mg crashes with column width of 1, rows > 2
2) Mg panics on resizing due to incomplete write
3) Mg doesn't compile with STARTUPFILE defined
deraadt@ OK


# 1.26 16-Mar-2002 vincent

Make these special buffers readonly by default. ok art@


# 1.25 11-Mar-2002 vincent

* Move to ANSI function definitions.
* Add a whole lot of consts where I thought it made sense

no ok, but no objections either...


# 1.24 22-Feb-2002 deraadt

if file mode known, try to avoid a race.. i think. millert spotted a bug in my first draft


# 1.23 14-Feb-2002 vincent

d_makename now takes a length parameter so we can remove the
strcpy call.


# 1.22 14-Feb-2002 deraadt

some KNF


# 1.21 13-Feb-2002 vincent

* Replace unsafe strcpy and strcat calls to safe strlcpy and strlcat.
* Be a little bit more verbose about some errors
* Fix some memory leaks in fileio.c

ok deraadt@, art@


Revision tags: OPENBSD_3_0_BASE
# 1.20 21-Sep-2001 wilfried

correct fork logic and rewrite for clarity, ok todd@


# 1.19 09-Jul-2001 deraadt

correct type on last arg to execl(); nordin@cse.ogi.edu


# 1.18 06-Jul-2001 art

Change the filename completion code to:
- not use an unprotected strcat.
- Append a '/' when a file is a symlink to a directory.
(tab completion on '/sys' is now less annoying.)
- actually make some more sense.


# 1.17 24-May-2001 art

Don't assume that map_table[0] is the fundamental map.


# 1.16 24-May-2001 mickey

spaces


# 1.15 24-May-2001 mickey

use strlcpy vs cpy+len, some other fixes


# 1.14 23-May-2001 art

Get rid of unnecessary casts of NULL.


# 1.13 23-May-2001 art

remove VOID. We're way beyond the point where this could build with an
ancient compiler.


# 1.12 03-May-2001 art

* Let make_file_list always append a '/' at the end of directories.
This makes filename TAB-completion more useful, now you don't need to
write that '/' manually.
* random cleanups including using snprintf and memcmp where open-coded
equivalents were used, waitpid instead of 'while (wait() != pid);', etc.


# 1.11 01-May-2001 art

Use opendir/readdir instead of open/read when making a file list.
This makes filename tab-completion work and takes dired a step further.


Revision tags: OPENBSD_2_9_BASE
# 1.10 29-Jan-2001 niklas

$OpenBSD$


# 1.9 24-Jan-2001 millert

Don't use rename to backup the file to a ~ file. We need to make a
copy instead so the original is edited in place for things like
cron and vipw.


Revision tags: OPENBSD_2_8_BASE
# 1.8 02-Aug-2000 millert

$HOME paranoia: never use getenv("HOME") w/o checking for NULL and non-zero


Revision tags: OPENBSD_2_7_BASE
# 1.7 13-Apr-2000 millert

The start of KNF + -Wall. The code has been run through indent but
needs hand fixup. I stopped at keymap.c...


# 1.6 27-Feb-2000 millert

Remove a now-bogus comment
Use pid_t
Use vfork(), not fork() when sensible


# 1.5 27-Feb-2000 millert

We don't have silly SYSV 14-character file name limits. Taken from
the BSD fileio.c. Note that this module is rife with PATH_MAX overflow
possibilities.


# 1.4 26-Feb-2000 millert

Use uid_t, gid_t and mode_t where applicable.
Pull in changes from the BSD-specific sysdef.h and fileio.c:
kill bogus unlinkdir() and rename() kludges since we have rmdir(2) and rename(2)
define SYMBLINK since we have symlinks
TODO: merge BSD spawn.c and ttyio.c


# 1.3 25-Feb-2000 deraadt

further pruning


# 1.2 25-Feb-2000 deraadt

make it minimally compile; millert


# 1.1 25-Feb-2000 deraadt

initial import of mg2a


# 1.108 01-Mar-2021 lum

Put the hardcoded '\n' character which is found throughout mg into a
buffer specific variable. The diff should not produce any behavourial
changes in mg.


# 1.107 23-Feb-2021 lum

Allow the user to specify a path to the mg startup file on the command line.


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.106 22-Jun-2019 lum

This diff moves the check for a newline at the end of a buffer (when
saving it) earlier. This means data doesn't 'disappear' from the file
on disk while the user answers 'yes' or 'no'.


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.105 13-Apr-2018 florian

mg tries to write backups to the wrong directory when run under a
different effective user, i.e. when invoced via su and
backup-to-home-directory is enabled.

Problem pointed out and diff provied by Lucas Gabriel Vuotto
<lvuotto92 () gmail ! com>, thanks!

Subsequently slacked on for nearly a year by yours truly.

Then remembered when Han Boetes <hboetes () gmail ! com> came up with
a similar diff because of a problem report by Mark Willson where it
turned out that getlogin(2) is not very portable.

OK tb


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.104 30-May-2017 florian

fix -Wshadow warnings.
Found by bcallah, thanks!


Revision tags: OPENBSD_6_1_BASE
# 1.103 28-Jul-2016 tedu

strncmp is a more reasonable way to check the beginning of a string.


# 1.102 28-Jul-2016 tedu

these programs probably do not need to use TMPDIR. ok florian


Revision tags: OPENBSD_6_0_BASE
# 1.101 04-Jul-2016 guenther

Use fstatat() instead of crafting a filename to use with stat()

ok millert@


Revision tags: OPENBSD_5_9_BASE
# 1.100 26-Jan-2016 jasper

ensure the backup file has the same mtime as the original file, this is in line
with emacs' behaviour.

from Harald Dunkel via Han Boetes
ok lum@


Revision tags: OPENBSD_5_8_BASE
# 1.99 19-Mar-2015 bcallah

Clean up the includes in mg.
This does the following:
Moves all POSIX headers from sysdef.h into the individual .c files so that
each file now only includes what it needs. All headers are properly sorted.
Moves the remainder of sysdef.h to other files (mostly def.h) and deletes
sysdef.h now that it's no longer contains anything.
Tweak a comment that references sysdef.h so that it no longer does that.
ok florian@


Revision tags: OPENBSD_5_7_BASE
# 1.98 16-Nov-2014 guenther

Stop using <sys/param.h>; replace MAXPATHLEN with PATH_MAX, stop using MAX(),
and pull in <limits.h> for *_MAX constants.

inspired on a diff from Kamil Rytarowski (n54 (at) gmx.com)
ok bcallah@


Revision tags: OPENBSD_5_6_BASE
# 1.97 20-Mar-2014 lum

Add some missing dobeeps.
ok florian@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.96 18-May-2013 lum

and and -> and


Revision tags: OPENBSD_5_3_BASE
# 1.95 27-Nov-2012 jasper

- move prototype of expandtilder() to def.h; needed by an upcoming diff.


Revision tags: OPENBSD_5_2_BASE
# 1.94 10-Jul-2012 lum

Fix an outstanding debian bug: #658539

"looks good" kjell


# 1.93 18-Jun-2012 jasper

- add a pathnames.h
- some whitespace cleanup

ok lum@


# 1.92 15-Jun-2012 lum

Fixes a problem where if you try to open a file that doesn't exist and has
a name longer than LOGIN_NAME_MAX and also has a tilde at the front
e.g:

$ mg ~01234567890123456789012345678901

mg will give a "Login name too long" instead of opening a new buffer
named ~01234567890123456789012345678901


# 1.91 14-Jun-2012 lum

Allow mg to save backup files to a users home directory.
Suggestions from eric@ and Sunil Nimmagadda.
Remarks deraadt@.


# 1.90 11-Jun-2012 lum

Change error message to reflect the correct filename: nname is 'New
name' as opposed to tname 'Temp name'


# 1.89 25-May-2012 lum

Remove static FILE pointer used for handling files in fileio.c. Pass
by reference instead. This allows the mg startup file to open other
files without unexpected things happening.

Discussed with Sunil Nimmagadda.


# 1.88 23-May-2012 lum

Change how mg handles files beginning with a '~'.
Bring behaviour more into line with emacs.


# 1.87 12-Apr-2012 lum

Remove the conditional directives NO_MACRO and NO_STARTUP.
They have not compiled for numerous years.
ok kjell@ millert@


# 1.86 28-Mar-2012 lum

Close an opendir(). From Igor Zinovik.


Revision tags: OPENBSD_5_1_BASE
# 1.85 31-Aug-2011 lum

On a file write fail:

1. return an error value
2. show an error message

From Loganaden Velvindron with suggestion from millert@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.84 21-Jan-2011 kjell

It volates style(9), but in mg, #include"def.h" goes first. 'twas the way it was built. no binary change here. confirmed by lum@, tested by Henri Kemppainen


# 1.83 21-Jan-2011 kjell

Change len+memcmp check to a strncmp for prefix testing on
filename completion. Avoids a recurring portability headache.
ok lum@
"concerns assuaged to the soothing sounds of Martin Denny blambert@"
(I worry about that guy...)


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.82 15-Sep-2008 kjell

Enable dirty buffer detection in mg.
Emulate the emacs behavior: after suspend/resume, buffer switch,
or at save time, warn (prompt) the user if the file has been modified
on disk in the interim.
This has already saved my butt numerous times.
ok phessler


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.81 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.80 24-Dec-2006 kjell

Fix a bug where inserting a file resulted in an incorrect
line-number count for a buffer (M-X insert-file, M-> to reproduce).
While here, fix a number of bugs with incorrect line numbers
after swap point-and-mark

Originally reported via debian's bug tracking system. Fix tested by
Han Boetes and Deanna Phillips.


# 1.79 19-Nov-2006 deraadt

spacing


# 1.78 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.77 25-Jul-2006 kjell

Rename the header line of a buffer to b_headp, from the remarkably
unintuitive b_linep. No binary change.


# 1.76 29-Jun-2006 jason

- use <dirent.h> not <sys/dir.h> (this appeases FreeBSD's annoying #warning)
- grab <sys/time.h> before <sys/resource.h> like getrusage(2) says
ok kjell


# 1.75 01-Jun-2006 kjell

fix style


# 1.74 01-Jun-2006 jason

make // /~ path rewriting optional in adjustname() and use it everywhere
except for the command line specified files. ok kjell,cloder


# 1.73 03-May-2006 kjell

correct one more comment


# 1.72 03-May-2006 kjell

Do some KNF, clean up some unused junk that has lying around for 40-odd
cvs revisions, and fix a few comments to match reality


# 1.71 03-May-2006 kjell

len = strlen(foo); if foo[len - 1]... will do bad things if len == 0.
Avoid, and rearrange a test so it can't (hypothetically) overflow.


# 1.70 03-Apr-2006 deraadt

lint love; ok kjell


# 1.69 02-Apr-2006 kjell

test char against '\0', not 0


Revision tags: OPENBSD_3_9_BASE
# 1.68 20-Dec-2005 kjell

Clean up the ugly casted frees. In one case, this meant eliminating a nasty
struct/union/casting nightmare when building the list of names for filename
completion. In particular, be consistent about strduping and freeing
the list data.


# 1.67 20-Dec-2005 kjell

Do some delinting of strl-type functions. Also, remove a superfluous
word in the undo-list.


# 1.66 13-Dec-2005 kjell

do some silly de-linting


# 1.65 20-Nov-2005 kjell

#ifdef NO_DIR can go too. From Han Boetes.


# 1.64 20-Nov-2005 kjell

Kill the NO_BACKUP #ifdef


# 1.63 20-Nov-2005 deraadt

toast NO_DIRED #ifdef; ok kjell


# 1.62 18-Nov-2005 deraadt

greedy use of typedef struct was making code harder to read; ok kjell cloder


# 1.61 13-Nov-2005 kjell

Better error checking of snprintfs. From Han Boetes.


# 1.60 11-Nov-2005 kjell

Now that we have an editable minibuffer, revert parsing of
"//" and "/~" as '/' and '~' respectively. I know emacs does it,
but it is weird, and breaks things that foolishly use filenames
like "/tmp//crontab.xxxx".
Proddings from matthieu and deraadt. Error report from Bernd Ahlers.


# 1.59 11-Nov-2005 deraadt

spacing


# 1.58 18-Oct-2005 kjell

When attempting to open a filename, walk backwards through minibuffer:
a> if you run into the beginning of the string, use the whole thing
b> if you run into a // combo, use everything starting from the second /
c> if you run into a /~ combo, use everything starting from the ~
i.e. do like emacs.
From (and for) Jason Wright


# 1.57 17-Oct-2005 kjell

Fix string handling. LOGIN_NAME_MAX includes the NUL, so don't reserve extra
space. Fix usage of strlcpy. Correct test for terminating slash.

"looks good" deraadt@


# 1.56 14-Oct-2005 deraadt

more headers to please lint


# 1.55 14-Oct-2005 kjell

move the dired routines into dired.c where they belong.
ok cloder@


# 1.54 13-Oct-2005 kjell

Make dired buffer read-only by default.
Noticed by Han Boetes


# 1.53 13-Oct-2005 kjell

Use dired mode automatically if file specified for loading is
a directory. Modified version of patch from Han Boetes.
ok cloder@


# 1.52 13-Oct-2005 kjell

check if current line is null. fixes a core in dired.


# 1.51 13-Oct-2005 kjell

Fix dired mode. Make deletions work, and keystrokes match emacs.
* dired-other-window should default to current buffer's path.
* Remove redundant code (from Han Boetes)
* Move initialization (and since we're here, fix mail-mode initialization
too)
* Remove redundant keymap in dired.c, and replace it with
the better one that was being ignored in keymap.c.


Revision tags: OPENBSD_3_8_BASE
# 1.50 14-Jun-2005 kjell

Add explicit public domain notices to all public domain files.
ok millert@, deraadt@


# 1.49 03-Apr-2005 db

This is a no binary change which does:

- spelling, punctuation fixes
- variable declaration lineup
- use parentheses for return and sizeof
- K&R function declarations -> ANSI
- other minor code beautification

ok henning@


Revision tags: OPENBSD_3_7_BASE
# 1.48 10-Mar-2005 deraadt

spacing


# 1.47 10-Mar-2005 henning

the realpath() was needed to do do relative to absolute path
conversion. revert my diff that takes it out, and instead, on
realpath() failure (like when you have no perms on pwd) just return the
unexpanded path. everything still fine with that, just tab completeion
and the like does not work (obviously).
this was mainly for "sudo mg /etc/something" from ~ where ~ is
nfs-mounted with root mapped to -2
and now, that case works as well as tab completion on insert-file etc.


# 1.46 09-Mar-2005 jfb

fix mg's behaviour with regards to files on which we do not have
write access. diff originally from vincent@ and forgotten for
a while.

ok rohee@, "toss it in, i'll bitch if it doesn't work" henning@


# 1.45 01-Feb-2005 henning

make fbackupfile() return FALSE when the rename(2) from the
mkstemp()-derived name to the filename~ one fails
From: Han Boetes <han@mijncomputer.nl>


# 1.44 31-Jan-2005 millert

Safer backup file generation using mkstemp(); henning@ OK


# 1.43 11-Jan-2005 henning

no need to call realpath() in adjustname()
realpath returns an error if cwd is not writeable, and that is quite common
with NFS-mounted ~ and sudo mg /etc/something - annoying as hell. realpath
is not needed at all here, so zap it. vincent ok


Revision tags: OPENBSD_3_6_BASE
# 1.42 09-Jul-2004 vincent

when saving a file, check whether it is newline terminated and if it is not,
prompt the user and add the newline if he agrees.

tested by a lot of helpful tech@ guys, approved by henning


# 1.41 08-Jul-2004 vincent

simplify the file writing loop

looked at by ian, tested on i386, macppc, sparc64


Revision tags: OPENBSD_3_5_BASE
# 1.40 14-Nov-2003 vincent

read(2) returns ssize_t, not size_t. spotted by Andrey Matveev.


# 1.39 08-Nov-2003 jmc

typos from Jonathon Gray;


Revision tags: OPENBSD_3_4_BASE
# 1.38 15-Aug-2003 vincent

make dired work more like emacs. (well, make it _work_, and then add a
bunch of missing commands)


# 1.37 15-Aug-2003 vincent

actually extract the filename off the ls line. makes dired *slightly*
more useful.


# 1.36 08-May-2003 vincent

use NULL to indicate failure in functions that return pointers, not FALSE.
typo in comment

from Patrick Latifi (patrick.l@hermes.usherb.ca)


# 1.35 06-May-2003 vincent

fix a small memleak on errors. noticed by pat latifi.

ok millert, fgsch


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.34 22-Aug-2002 deraadt

knf


# 1.33 25-Jul-2002 vincent

replace the ugly and buggy adjustname function by a simple one using
simple APIs. makes mg not crash with 65k filenames...

ok art@


# 1.32 01-Jul-2002 vincent

KNF


# 1.31 21-Jun-2002 vincent

fix bad usage of strlcpy()'s return value. (made pointers point
beyond the boundaries of buffers)

ok deraadt@


# 1.30 22-Apr-2002 vincent

don't use /bin/cp to copy files.

ok deraadt@


# 1.29 22-Apr-2002 vincent

fix mem leak

ok deraadt@


# 1.28 15-Apr-2002 deraadt

use fchmod and fchown instead; vincent


Revision tags: OPENBSD_3_1_BASE
# 1.27 27-Mar-2002 millert

Fixes the the following problems (from zyrnix)
1) Mg crashes with column width of 1, rows > 2
2) Mg panics on resizing due to incomplete write
3) Mg doesn't compile with STARTUPFILE defined
deraadt@ OK


# 1.26 16-Mar-2002 vincent

Make these special buffers readonly by default. ok art@


# 1.25 11-Mar-2002 vincent

* Move to ANSI function definitions.
* Add a whole lot of consts where I thought it made sense

no ok, but no objections either...


# 1.24 22-Feb-2002 deraadt

if file mode known, try to avoid a race.. i think. millert spotted a bug in my first draft


# 1.23 14-Feb-2002 vincent

d_makename now takes a length parameter so we can remove the
strcpy call.


# 1.22 14-Feb-2002 deraadt

some KNF


# 1.21 13-Feb-2002 vincent

* Replace unsafe strcpy and strcat calls to safe strlcpy and strlcat.
* Be a little bit more verbose about some errors
* Fix some memory leaks in fileio.c

ok deraadt@, art@


Revision tags: OPENBSD_3_0_BASE
# 1.20 21-Sep-2001 wilfried

correct fork logic and rewrite for clarity, ok todd@


# 1.19 09-Jul-2001 deraadt

correct type on last arg to execl(); nordin@cse.ogi.edu


# 1.18 06-Jul-2001 art

Change the filename completion code to:
- not use an unprotected strcat.
- Append a '/' when a file is a symlink to a directory.
(tab completion on '/sys' is now less annoying.)
- actually make some more sense.


# 1.17 24-May-2001 art

Don't assume that map_table[0] is the fundamental map.


# 1.16 24-May-2001 mickey

spaces


# 1.15 24-May-2001 mickey

use strlcpy vs cpy+len, some other fixes


# 1.14 23-May-2001 art

Get rid of unnecessary casts of NULL.


# 1.13 23-May-2001 art

remove VOID. We're way beyond the point where this could build with an
ancient compiler.


# 1.12 03-May-2001 art

* Let make_file_list always append a '/' at the end of directories.
This makes filename TAB-completion more useful, now you don't need to
write that '/' manually.
* random cleanups including using snprintf and memcmp where open-coded
equivalents were used, waitpid instead of 'while (wait() != pid);', etc.


# 1.11 01-May-2001 art

Use opendir/readdir instead of open/read when making a file list.
This makes filename tab-completion work and takes dired a step further.


Revision tags: OPENBSD_2_9_BASE
# 1.10 29-Jan-2001 niklas

$OpenBSD$


# 1.9 24-Jan-2001 millert

Don't use rename to backup the file to a ~ file. We need to make a
copy instead so the original is edited in place for things like
cron and vipw.


Revision tags: OPENBSD_2_8_BASE
# 1.8 02-Aug-2000 millert

$HOME paranoia: never use getenv("HOME") w/o checking for NULL and non-zero


Revision tags: OPENBSD_2_7_BASE
# 1.7 13-Apr-2000 millert

The start of KNF + -Wall. The code has been run through indent but
needs hand fixup. I stopped at keymap.c...


# 1.6 27-Feb-2000 millert

Remove a now-bogus comment
Use pid_t
Use vfork(), not fork() when sensible


# 1.5 27-Feb-2000 millert

We don't have silly SYSV 14-character file name limits. Taken from
the BSD fileio.c. Note that this module is rife with PATH_MAX overflow
possibilities.


# 1.4 26-Feb-2000 millert

Use uid_t, gid_t and mode_t where applicable.
Pull in changes from the BSD-specific sysdef.h and fileio.c:
kill bogus unlinkdir() and rename() kludges since we have rmdir(2) and rename(2)
define SYMBLINK since we have symlinks
TODO: merge BSD spawn.c and ttyio.c


# 1.3 25-Feb-2000 deraadt

further pruning


# 1.2 25-Feb-2000 deraadt

make it minimally compile; millert


# 1.1 25-Feb-2000 deraadt

initial import of mg2a


# 1.107 23-Feb-2021 lum

Allow the user to specify a path to the mg startup file on the command line.


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.106 22-Jun-2019 lum

This diff moves the check for a newline at the end of a buffer (when
saving it) earlier. This means data doesn't 'disappear' from the file
on disk while the user answers 'yes' or 'no'.


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.105 13-Apr-2018 florian

mg tries to write backups to the wrong directory when run under a
different effective user, i.e. when invoced via su and
backup-to-home-directory is enabled.

Problem pointed out and diff provied by Lucas Gabriel Vuotto
<lvuotto92 () gmail ! com>, thanks!

Subsequently slacked on for nearly a year by yours truly.

Then remembered when Han Boetes <hboetes () gmail ! com> came up with
a similar diff because of a problem report by Mark Willson where it
turned out that getlogin(2) is not very portable.

OK tb


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.104 30-May-2017 florian

fix -Wshadow warnings.
Found by bcallah, thanks!


Revision tags: OPENBSD_6_1_BASE
# 1.103 28-Jul-2016 tedu

strncmp is a more reasonable way to check the beginning of a string.


# 1.102 28-Jul-2016 tedu

these programs probably do not need to use TMPDIR. ok florian


Revision tags: OPENBSD_6_0_BASE
# 1.101 04-Jul-2016 guenther

Use fstatat() instead of crafting a filename to use with stat()

ok millert@


Revision tags: OPENBSD_5_9_BASE
# 1.100 26-Jan-2016 jasper

ensure the backup file has the same mtime as the original file, this is in line
with emacs' behaviour.

from Harald Dunkel via Han Boetes
ok lum@


Revision tags: OPENBSD_5_8_BASE
# 1.99 19-Mar-2015 bcallah

Clean up the includes in mg.
This does the following:
Moves all POSIX headers from sysdef.h into the individual .c files so that
each file now only includes what it needs. All headers are properly sorted.
Moves the remainder of sysdef.h to other files (mostly def.h) and deletes
sysdef.h now that it's no longer contains anything.
Tweak a comment that references sysdef.h so that it no longer does that.
ok florian@


Revision tags: OPENBSD_5_7_BASE
# 1.98 16-Nov-2014 guenther

Stop using <sys/param.h>; replace MAXPATHLEN with PATH_MAX, stop using MAX(),
and pull in <limits.h> for *_MAX constants.

inspired on a diff from Kamil Rytarowski (n54 (at) gmx.com)
ok bcallah@


Revision tags: OPENBSD_5_6_BASE
# 1.97 20-Mar-2014 lum

Add some missing dobeeps.
ok florian@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.96 18-May-2013 lum

and and -> and


Revision tags: OPENBSD_5_3_BASE
# 1.95 27-Nov-2012 jasper

- move prototype of expandtilder() to def.h; needed by an upcoming diff.


Revision tags: OPENBSD_5_2_BASE
# 1.94 10-Jul-2012 lum

Fix an outstanding debian bug: #658539

"looks good" kjell


# 1.93 18-Jun-2012 jasper

- add a pathnames.h
- some whitespace cleanup

ok lum@


# 1.92 15-Jun-2012 lum

Fixes a problem where if you try to open a file that doesn't exist and has
a name longer than LOGIN_NAME_MAX and also has a tilde at the front
e.g:

$ mg ~01234567890123456789012345678901

mg will give a "Login name too long" instead of opening a new buffer
named ~01234567890123456789012345678901


# 1.91 14-Jun-2012 lum

Allow mg to save backup files to a users home directory.
Suggestions from eric@ and Sunil Nimmagadda.
Remarks deraadt@.


# 1.90 11-Jun-2012 lum

Change error message to reflect the correct filename: nname is 'New
name' as opposed to tname 'Temp name'


# 1.89 25-May-2012 lum

Remove static FILE pointer used for handling files in fileio.c. Pass
by reference instead. This allows the mg startup file to open other
files without unexpected things happening.

Discussed with Sunil Nimmagadda.


# 1.88 23-May-2012 lum

Change how mg handles files beginning with a '~'.
Bring behaviour more into line with emacs.


# 1.87 12-Apr-2012 lum

Remove the conditional directives NO_MACRO and NO_STARTUP.
They have not compiled for numerous years.
ok kjell@ millert@


# 1.86 28-Mar-2012 lum

Close an opendir(). From Igor Zinovik.


Revision tags: OPENBSD_5_1_BASE
# 1.85 31-Aug-2011 lum

On a file write fail:

1. return an error value
2. show an error message

From Loganaden Velvindron with suggestion from millert@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.84 21-Jan-2011 kjell

It volates style(9), but in mg, #include"def.h" goes first. 'twas the way it was built. no binary change here. confirmed by lum@, tested by Henri Kemppainen


# 1.83 21-Jan-2011 kjell

Change len+memcmp check to a strncmp for prefix testing on
filename completion. Avoids a recurring portability headache.
ok lum@
"concerns assuaged to the soothing sounds of Martin Denny blambert@"
(I worry about that guy...)


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.82 15-Sep-2008 kjell

Enable dirty buffer detection in mg.
Emulate the emacs behavior: after suspend/resume, buffer switch,
or at save time, warn (prompt) the user if the file has been modified
on disk in the interim.
This has already saved my butt numerous times.
ok phessler


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.81 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.80 24-Dec-2006 kjell

Fix a bug where inserting a file resulted in an incorrect
line-number count for a buffer (M-X insert-file, M-> to reproduce).
While here, fix a number of bugs with incorrect line numbers
after swap point-and-mark

Originally reported via debian's bug tracking system. Fix tested by
Han Boetes and Deanna Phillips.


# 1.79 19-Nov-2006 deraadt

spacing


# 1.78 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.77 25-Jul-2006 kjell

Rename the header line of a buffer to b_headp, from the remarkably
unintuitive b_linep. No binary change.


# 1.76 29-Jun-2006 jason

- use <dirent.h> not <sys/dir.h> (this appeases FreeBSD's annoying #warning)
- grab <sys/time.h> before <sys/resource.h> like getrusage(2) says
ok kjell


# 1.75 01-Jun-2006 kjell

fix style


# 1.74 01-Jun-2006 jason

make // /~ path rewriting optional in adjustname() and use it everywhere
except for the command line specified files. ok kjell,cloder


# 1.73 03-May-2006 kjell

correct one more comment


# 1.72 03-May-2006 kjell

Do some KNF, clean up some unused junk that has lying around for 40-odd
cvs revisions, and fix a few comments to match reality


# 1.71 03-May-2006 kjell

len = strlen(foo); if foo[len - 1]... will do bad things if len == 0.
Avoid, and rearrange a test so it can't (hypothetically) overflow.


# 1.70 03-Apr-2006 deraadt

lint love; ok kjell


# 1.69 02-Apr-2006 kjell

test char against '\0', not 0


Revision tags: OPENBSD_3_9_BASE
# 1.68 20-Dec-2005 kjell

Clean up the ugly casted frees. In one case, this meant eliminating a nasty
struct/union/casting nightmare when building the list of names for filename
completion. In particular, be consistent about strduping and freeing
the list data.


# 1.67 20-Dec-2005 kjell

Do some delinting of strl-type functions. Also, remove a superfluous
word in the undo-list.


# 1.66 13-Dec-2005 kjell

do some silly de-linting


# 1.65 20-Nov-2005 kjell

#ifdef NO_DIR can go too. From Han Boetes.


# 1.64 20-Nov-2005 kjell

Kill the NO_BACKUP #ifdef


# 1.63 20-Nov-2005 deraadt

toast NO_DIRED #ifdef; ok kjell


# 1.62 18-Nov-2005 deraadt

greedy use of typedef struct was making code harder to read; ok kjell cloder


# 1.61 13-Nov-2005 kjell

Better error checking of snprintfs. From Han Boetes.


# 1.60 11-Nov-2005 kjell

Now that we have an editable minibuffer, revert parsing of
"//" and "/~" as '/' and '~' respectively. I know emacs does it,
but it is weird, and breaks things that foolishly use filenames
like "/tmp//crontab.xxxx".
Proddings from matthieu and deraadt. Error report from Bernd Ahlers.


# 1.59 11-Nov-2005 deraadt

spacing


# 1.58 18-Oct-2005 kjell

When attempting to open a filename, walk backwards through minibuffer:
a> if you run into the beginning of the string, use the whole thing
b> if you run into a // combo, use everything starting from the second /
c> if you run into a /~ combo, use everything starting from the ~
i.e. do like emacs.
From (and for) Jason Wright


# 1.57 17-Oct-2005 kjell

Fix string handling. LOGIN_NAME_MAX includes the NUL, so don't reserve extra
space. Fix usage of strlcpy. Correct test for terminating slash.

"looks good" deraadt@


# 1.56 14-Oct-2005 deraadt

more headers to please lint


# 1.55 14-Oct-2005 kjell

move the dired routines into dired.c where they belong.
ok cloder@


# 1.54 13-Oct-2005 kjell

Make dired buffer read-only by default.
Noticed by Han Boetes


# 1.53 13-Oct-2005 kjell

Use dired mode automatically if file specified for loading is
a directory. Modified version of patch from Han Boetes.
ok cloder@


# 1.52 13-Oct-2005 kjell

check if current line is null. fixes a core in dired.


# 1.51 13-Oct-2005 kjell

Fix dired mode. Make deletions work, and keystrokes match emacs.
* dired-other-window should default to current buffer's path.
* Remove redundant code (from Han Boetes)
* Move initialization (and since we're here, fix mail-mode initialization
too)
* Remove redundant keymap in dired.c, and replace it with
the better one that was being ignored in keymap.c.


Revision tags: OPENBSD_3_8_BASE
# 1.50 14-Jun-2005 kjell

Add explicit public domain notices to all public domain files.
ok millert@, deraadt@


# 1.49 03-Apr-2005 db

This is a no binary change which does:

- spelling, punctuation fixes
- variable declaration lineup
- use parentheses for return and sizeof
- K&R function declarations -> ANSI
- other minor code beautification

ok henning@


Revision tags: OPENBSD_3_7_BASE
# 1.48 10-Mar-2005 deraadt

spacing


# 1.47 10-Mar-2005 henning

the realpath() was needed to do do relative to absolute path
conversion. revert my diff that takes it out, and instead, on
realpath() failure (like when you have no perms on pwd) just return the
unexpanded path. everything still fine with that, just tab completeion
and the like does not work (obviously).
this was mainly for "sudo mg /etc/something" from ~ where ~ is
nfs-mounted with root mapped to -2
and now, that case works as well as tab completion on insert-file etc.


# 1.46 09-Mar-2005 jfb

fix mg's behaviour with regards to files on which we do not have
write access. diff originally from vincent@ and forgotten for
a while.

ok rohee@, "toss it in, i'll bitch if it doesn't work" henning@


# 1.45 01-Feb-2005 henning

make fbackupfile() return FALSE when the rename(2) from the
mkstemp()-derived name to the filename~ one fails
From: Han Boetes <han@mijncomputer.nl>


# 1.44 31-Jan-2005 millert

Safer backup file generation using mkstemp(); henning@ OK


# 1.43 11-Jan-2005 henning

no need to call realpath() in adjustname()
realpath returns an error if cwd is not writeable, and that is quite common
with NFS-mounted ~ and sudo mg /etc/something - annoying as hell. realpath
is not needed at all here, so zap it. vincent ok


Revision tags: OPENBSD_3_6_BASE
# 1.42 09-Jul-2004 vincent

when saving a file, check whether it is newline terminated and if it is not,
prompt the user and add the newline if he agrees.

tested by a lot of helpful tech@ guys, approved by henning


# 1.41 08-Jul-2004 vincent

simplify the file writing loop

looked at by ian, tested on i386, macppc, sparc64


Revision tags: OPENBSD_3_5_BASE
# 1.40 14-Nov-2003 vincent

read(2) returns ssize_t, not size_t. spotted by Andrey Matveev.


# 1.39 08-Nov-2003 jmc

typos from Jonathon Gray;


Revision tags: OPENBSD_3_4_BASE
# 1.38 15-Aug-2003 vincent

make dired work more like emacs. (well, make it _work_, and then add a
bunch of missing commands)


# 1.37 15-Aug-2003 vincent

actually extract the filename off the ls line. makes dired *slightly*
more useful.


# 1.36 08-May-2003 vincent

use NULL to indicate failure in functions that return pointers, not FALSE.
typo in comment

from Patrick Latifi (patrick.l@hermes.usherb.ca)


# 1.35 06-May-2003 vincent

fix a small memleak on errors. noticed by pat latifi.

ok millert, fgsch


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.34 22-Aug-2002 deraadt

knf


# 1.33 25-Jul-2002 vincent

replace the ugly and buggy adjustname function by a simple one using
simple APIs. makes mg not crash with 65k filenames...

ok art@


# 1.32 01-Jul-2002 vincent

KNF


# 1.31 21-Jun-2002 vincent

fix bad usage of strlcpy()'s return value. (made pointers point
beyond the boundaries of buffers)

ok deraadt@


# 1.30 22-Apr-2002 vincent

don't use /bin/cp to copy files.

ok deraadt@


# 1.29 22-Apr-2002 vincent

fix mem leak

ok deraadt@


# 1.28 15-Apr-2002 deraadt

use fchmod and fchown instead; vincent


Revision tags: OPENBSD_3_1_BASE
# 1.27 27-Mar-2002 millert

Fixes the the following problems (from zyrnix)
1) Mg crashes with column width of 1, rows > 2
2) Mg panics on resizing due to incomplete write
3) Mg doesn't compile with STARTUPFILE defined
deraadt@ OK


# 1.26 16-Mar-2002 vincent

Make these special buffers readonly by default. ok art@


# 1.25 11-Mar-2002 vincent

* Move to ANSI function definitions.
* Add a whole lot of consts where I thought it made sense

no ok, but no objections either...


# 1.24 22-Feb-2002 deraadt

if file mode known, try to avoid a race.. i think. millert spotted a bug in my first draft


# 1.23 14-Feb-2002 vincent

d_makename now takes a length parameter so we can remove the
strcpy call.


# 1.22 14-Feb-2002 deraadt

some KNF


# 1.21 13-Feb-2002 vincent

* Replace unsafe strcpy and strcat calls to safe strlcpy and strlcat.
* Be a little bit more verbose about some errors
* Fix some memory leaks in fileio.c

ok deraadt@, art@


Revision tags: OPENBSD_3_0_BASE
# 1.20 21-Sep-2001 wilfried

correct fork logic and rewrite for clarity, ok todd@


# 1.19 09-Jul-2001 deraadt

correct type on last arg to execl(); nordin@cse.ogi.edu


# 1.18 06-Jul-2001 art

Change the filename completion code to:
- not use an unprotected strcat.
- Append a '/' when a file is a symlink to a directory.
(tab completion on '/sys' is now less annoying.)
- actually make some more sense.


# 1.17 24-May-2001 art

Don't assume that map_table[0] is the fundamental map.


# 1.16 24-May-2001 mickey

spaces


# 1.15 24-May-2001 mickey

use strlcpy vs cpy+len, some other fixes


# 1.14 23-May-2001 art

Get rid of unnecessary casts of NULL.


# 1.13 23-May-2001 art

remove VOID. We're way beyond the point where this could build with an
ancient compiler.


# 1.12 03-May-2001 art

* Let make_file_list always append a '/' at the end of directories.
This makes filename TAB-completion more useful, now you don't need to
write that '/' manually.
* random cleanups including using snprintf and memcmp where open-coded
equivalents were used, waitpid instead of 'while (wait() != pid);', etc.


# 1.11 01-May-2001 art

Use opendir/readdir instead of open/read when making a file list.
This makes filename tab-completion work and takes dired a step further.


Revision tags: OPENBSD_2_9_BASE
# 1.10 29-Jan-2001 niklas

$OpenBSD$


# 1.9 24-Jan-2001 millert

Don't use rename to backup the file to a ~ file. We need to make a
copy instead so the original is edited in place for things like
cron and vipw.


Revision tags: OPENBSD_2_8_BASE
# 1.8 02-Aug-2000 millert

$HOME paranoia: never use getenv("HOME") w/o checking for NULL and non-zero


Revision tags: OPENBSD_2_7_BASE
# 1.7 13-Apr-2000 millert

The start of KNF + -Wall. The code has been run through indent but
needs hand fixup. I stopped at keymap.c...


# 1.6 27-Feb-2000 millert

Remove a now-bogus comment
Use pid_t
Use vfork(), not fork() when sensible


# 1.5 27-Feb-2000 millert

We don't have silly SYSV 14-character file name limits. Taken from
the BSD fileio.c. Note that this module is rife with PATH_MAX overflow
possibilities.


# 1.4 26-Feb-2000 millert

Use uid_t, gid_t and mode_t where applicable.
Pull in changes from the BSD-specific sysdef.h and fileio.c:
kill bogus unlinkdir() and rename() kludges since we have rmdir(2) and rename(2)
define SYMBLINK since we have symlinks
TODO: merge BSD spawn.c and ttyio.c


# 1.3 25-Feb-2000 deraadt

further pruning


# 1.2 25-Feb-2000 deraadt

make it minimally compile; millert


# 1.1 25-Feb-2000 deraadt

initial import of mg2a


# 1.106 22-Jun-2019 lum

This diff moves the check for a newline at the end of a buffer (when
saving it) earlier. This means data doesn't 'disappear' from the file
on disk while the user answers 'yes' or 'no'.


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.105 13-Apr-2018 florian

mg tries to write backups to the wrong directory when run under a
different effective user, i.e. when invoced via su and
backup-to-home-directory is enabled.

Problem pointed out and diff provied by Lucas Gabriel Vuotto
<lvuotto92 () gmail ! com>, thanks!

Subsequently slacked on for nearly a year by yours truly.

Then remembered when Han Boetes <hboetes () gmail ! com> came up with
a similar diff because of a problem report by Mark Willson where it
turned out that getlogin(2) is not very portable.

OK tb


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.104 30-May-2017 florian

fix -Wshadow warnings.
Found by bcallah, thanks!


Revision tags: OPENBSD_6_1_BASE
# 1.103 28-Jul-2016 tedu

strncmp is a more reasonable way to check the beginning of a string.


# 1.102 28-Jul-2016 tedu

these programs probably do not need to use TMPDIR. ok florian


Revision tags: OPENBSD_6_0_BASE
# 1.101 04-Jul-2016 guenther

Use fstatat() instead of crafting a filename to use with stat()

ok millert@


Revision tags: OPENBSD_5_9_BASE
# 1.100 26-Jan-2016 jasper

ensure the backup file has the same mtime as the original file, this is in line
with emacs' behaviour.

from Harald Dunkel via Han Boetes
ok lum@


Revision tags: OPENBSD_5_8_BASE
# 1.99 19-Mar-2015 bcallah

Clean up the includes in mg.
This does the following:
Moves all POSIX headers from sysdef.h into the individual .c files so that
each file now only includes what it needs. All headers are properly sorted.
Moves the remainder of sysdef.h to other files (mostly def.h) and deletes
sysdef.h now that it's no longer contains anything.
Tweak a comment that references sysdef.h so that it no longer does that.
ok florian@


Revision tags: OPENBSD_5_7_BASE
# 1.98 16-Nov-2014 guenther

Stop using <sys/param.h>; replace MAXPATHLEN with PATH_MAX, stop using MAX(),
and pull in <limits.h> for *_MAX constants.

inspired on a diff from Kamil Rytarowski (n54 (at) gmx.com)
ok bcallah@


Revision tags: OPENBSD_5_6_BASE
# 1.97 20-Mar-2014 lum

Add some missing dobeeps.
ok florian@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.96 18-May-2013 lum

and and -> and


Revision tags: OPENBSD_5_3_BASE
# 1.95 27-Nov-2012 jasper

- move prototype of expandtilder() to def.h; needed by an upcoming diff.


Revision tags: OPENBSD_5_2_BASE
# 1.94 10-Jul-2012 lum

Fix an outstanding debian bug: #658539

"looks good" kjell


# 1.93 18-Jun-2012 jasper

- add a pathnames.h
- some whitespace cleanup

ok lum@


# 1.92 15-Jun-2012 lum

Fixes a problem where if you try to open a file that doesn't exist and has
a name longer than LOGIN_NAME_MAX and also has a tilde at the front
e.g:

$ mg ~01234567890123456789012345678901

mg will give a "Login name too long" instead of opening a new buffer
named ~01234567890123456789012345678901


# 1.91 14-Jun-2012 lum

Allow mg to save backup files to a users home directory.
Suggestions from eric@ and Sunil Nimmagadda.
Remarks deraadt@.


# 1.90 11-Jun-2012 lum

Change error message to reflect the correct filename: nname is 'New
name' as opposed to tname 'Temp name'


# 1.89 25-May-2012 lum

Remove static FILE pointer used for handling files in fileio.c. Pass
by reference instead. This allows the mg startup file to open other
files without unexpected things happening.

Discussed with Sunil Nimmagadda.


# 1.88 23-May-2012 lum

Change how mg handles files beginning with a '~'.
Bring behaviour more into line with emacs.


# 1.87 12-Apr-2012 lum

Remove the conditional directives NO_MACRO and NO_STARTUP.
They have not compiled for numerous years.
ok kjell@ millert@


# 1.86 28-Mar-2012 lum

Close an opendir(). From Igor Zinovik.


Revision tags: OPENBSD_5_1_BASE
# 1.85 31-Aug-2011 lum

On a file write fail:

1. return an error value
2. show an error message

From Loganaden Velvindron with suggestion from millert@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.84 21-Jan-2011 kjell

It volates style(9), but in mg, #include"def.h" goes first. 'twas the way it was built. no binary change here. confirmed by lum@, tested by Henri Kemppainen


# 1.83 21-Jan-2011 kjell

Change len+memcmp check to a strncmp for prefix testing on
filename completion. Avoids a recurring portability headache.
ok lum@
"concerns assuaged to the soothing sounds of Martin Denny blambert@"
(I worry about that guy...)


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.82 15-Sep-2008 kjell

Enable dirty buffer detection in mg.
Emulate the emacs behavior: after suspend/resume, buffer switch,
or at save time, warn (prompt) the user if the file has been modified
on disk in the interim.
This has already saved my butt numerous times.
ok phessler


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.81 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.80 24-Dec-2006 kjell

Fix a bug where inserting a file resulted in an incorrect
line-number count for a buffer (M-X insert-file, M-> to reproduce).
While here, fix a number of bugs with incorrect line numbers
after swap point-and-mark

Originally reported via debian's bug tracking system. Fix tested by
Han Boetes and Deanna Phillips.


# 1.79 19-Nov-2006 deraadt

spacing


# 1.78 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.77 25-Jul-2006 kjell

Rename the header line of a buffer to b_headp, from the remarkably
unintuitive b_linep. No binary change.


# 1.76 29-Jun-2006 jason

- use <dirent.h> not <sys/dir.h> (this appeases FreeBSD's annoying #warning)
- grab <sys/time.h> before <sys/resource.h> like getrusage(2) says
ok kjell


# 1.75 01-Jun-2006 kjell

fix style


# 1.74 01-Jun-2006 jason

make // /~ path rewriting optional in adjustname() and use it everywhere
except for the command line specified files. ok kjell,cloder


# 1.73 03-May-2006 kjell

correct one more comment


# 1.72 03-May-2006 kjell

Do some KNF, clean up some unused junk that has lying around for 40-odd
cvs revisions, and fix a few comments to match reality


# 1.71 03-May-2006 kjell

len = strlen(foo); if foo[len - 1]... will do bad things if len == 0.
Avoid, and rearrange a test so it can't (hypothetically) overflow.


# 1.70 03-Apr-2006 deraadt

lint love; ok kjell


# 1.69 02-Apr-2006 kjell

test char against '\0', not 0


Revision tags: OPENBSD_3_9_BASE
# 1.68 20-Dec-2005 kjell

Clean up the ugly casted frees. In one case, this meant eliminating a nasty
struct/union/casting nightmare when building the list of names for filename
completion. In particular, be consistent about strduping and freeing
the list data.


# 1.67 20-Dec-2005 kjell

Do some delinting of strl-type functions. Also, remove a superfluous
word in the undo-list.


# 1.66 13-Dec-2005 kjell

do some silly de-linting


# 1.65 20-Nov-2005 kjell

#ifdef NO_DIR can go too. From Han Boetes.


# 1.64 20-Nov-2005 kjell

Kill the NO_BACKUP #ifdef


# 1.63 20-Nov-2005 deraadt

toast NO_DIRED #ifdef; ok kjell


# 1.62 18-Nov-2005 deraadt

greedy use of typedef struct was making code harder to read; ok kjell cloder


# 1.61 13-Nov-2005 kjell

Better error checking of snprintfs. From Han Boetes.


# 1.60 11-Nov-2005 kjell

Now that we have an editable minibuffer, revert parsing of
"//" and "/~" as '/' and '~' respectively. I know emacs does it,
but it is weird, and breaks things that foolishly use filenames
like "/tmp//crontab.xxxx".
Proddings from matthieu and deraadt. Error report from Bernd Ahlers.


# 1.59 11-Nov-2005 deraadt

spacing


# 1.58 18-Oct-2005 kjell

When attempting to open a filename, walk backwards through minibuffer:
a> if you run into the beginning of the string, use the whole thing
b> if you run into a // combo, use everything starting from the second /
c> if you run into a /~ combo, use everything starting from the ~
i.e. do like emacs.
From (and for) Jason Wright


# 1.57 17-Oct-2005 kjell

Fix string handling. LOGIN_NAME_MAX includes the NUL, so don't reserve extra
space. Fix usage of strlcpy. Correct test for terminating slash.

"looks good" deraadt@


# 1.56 14-Oct-2005 deraadt

more headers to please lint


# 1.55 14-Oct-2005 kjell

move the dired routines into dired.c where they belong.
ok cloder@


# 1.54 13-Oct-2005 kjell

Make dired buffer read-only by default.
Noticed by Han Boetes


# 1.53 13-Oct-2005 kjell

Use dired mode automatically if file specified for loading is
a directory. Modified version of patch from Han Boetes.
ok cloder@


# 1.52 13-Oct-2005 kjell

check if current line is null. fixes a core in dired.


# 1.51 13-Oct-2005 kjell

Fix dired mode. Make deletions work, and keystrokes match emacs.
* dired-other-window should default to current buffer's path.
* Remove redundant code (from Han Boetes)
* Move initialization (and since we're here, fix mail-mode initialization
too)
* Remove redundant keymap in dired.c, and replace it with
the better one that was being ignored in keymap.c.


Revision tags: OPENBSD_3_8_BASE
# 1.50 14-Jun-2005 kjell

Add explicit public domain notices to all public domain files.
ok millert@, deraadt@


# 1.49 03-Apr-2005 db

This is a no binary change which does:

- spelling, punctuation fixes
- variable declaration lineup
- use parentheses for return and sizeof
- K&R function declarations -> ANSI
- other minor code beautification

ok henning@


Revision tags: OPENBSD_3_7_BASE
# 1.48 10-Mar-2005 deraadt

spacing


# 1.47 10-Mar-2005 henning

the realpath() was needed to do do relative to absolute path
conversion. revert my diff that takes it out, and instead, on
realpath() failure (like when you have no perms on pwd) just return the
unexpanded path. everything still fine with that, just tab completeion
and the like does not work (obviously).
this was mainly for "sudo mg /etc/something" from ~ where ~ is
nfs-mounted with root mapped to -2
and now, that case works as well as tab completion on insert-file etc.


# 1.46 09-Mar-2005 jfb

fix mg's behaviour with regards to files on which we do not have
write access. diff originally from vincent@ and forgotten for
a while.

ok rohee@, "toss it in, i'll bitch if it doesn't work" henning@


# 1.45 01-Feb-2005 henning

make fbackupfile() return FALSE when the rename(2) from the
mkstemp()-derived name to the filename~ one fails
From: Han Boetes <han@mijncomputer.nl>


# 1.44 31-Jan-2005 millert

Safer backup file generation using mkstemp(); henning@ OK


# 1.43 11-Jan-2005 henning

no need to call realpath() in adjustname()
realpath returns an error if cwd is not writeable, and that is quite common
with NFS-mounted ~ and sudo mg /etc/something - annoying as hell. realpath
is not needed at all here, so zap it. vincent ok


Revision tags: OPENBSD_3_6_BASE
# 1.42 09-Jul-2004 vincent

when saving a file, check whether it is newline terminated and if it is not,
prompt the user and add the newline if he agrees.

tested by a lot of helpful tech@ guys, approved by henning


# 1.41 08-Jul-2004 vincent

simplify the file writing loop

looked at by ian, tested on i386, macppc, sparc64


Revision tags: OPENBSD_3_5_BASE
# 1.40 14-Nov-2003 vincent

read(2) returns ssize_t, not size_t. spotted by Andrey Matveev.


# 1.39 08-Nov-2003 jmc

typos from Jonathon Gray;


Revision tags: OPENBSD_3_4_BASE
# 1.38 15-Aug-2003 vincent

make dired work more like emacs. (well, make it _work_, and then add a
bunch of missing commands)


# 1.37 15-Aug-2003 vincent

actually extract the filename off the ls line. makes dired *slightly*
more useful.


# 1.36 08-May-2003 vincent

use NULL to indicate failure in functions that return pointers, not FALSE.
typo in comment

from Patrick Latifi (patrick.l@hermes.usherb.ca)


# 1.35 06-May-2003 vincent

fix a small memleak on errors. noticed by pat latifi.

ok millert, fgsch


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.34 22-Aug-2002 deraadt

knf


# 1.33 25-Jul-2002 vincent

replace the ugly and buggy adjustname function by a simple one using
simple APIs. makes mg not crash with 65k filenames...

ok art@


# 1.32 01-Jul-2002 vincent

KNF


# 1.31 21-Jun-2002 vincent

fix bad usage of strlcpy()'s return value. (made pointers point
beyond the boundaries of buffers)

ok deraadt@


# 1.30 22-Apr-2002 vincent

don't use /bin/cp to copy files.

ok deraadt@


# 1.29 22-Apr-2002 vincent

fix mem leak

ok deraadt@


# 1.28 15-Apr-2002 deraadt

use fchmod and fchown instead; vincent


Revision tags: OPENBSD_3_1_BASE
# 1.27 27-Mar-2002 millert

Fixes the the following problems (from zyrnix)
1) Mg crashes with column width of 1, rows > 2
2) Mg panics on resizing due to incomplete write
3) Mg doesn't compile with STARTUPFILE defined
deraadt@ OK


# 1.26 16-Mar-2002 vincent

Make these special buffers readonly by default. ok art@


# 1.25 11-Mar-2002 vincent

* Move to ANSI function definitions.
* Add a whole lot of consts where I thought it made sense

no ok, but no objections either...


# 1.24 22-Feb-2002 deraadt

if file mode known, try to avoid a race.. i think. millert spotted a bug in my first draft


# 1.23 14-Feb-2002 vincent

d_makename now takes a length parameter so we can remove the
strcpy call.


# 1.22 14-Feb-2002 deraadt

some KNF


# 1.21 13-Feb-2002 vincent

* Replace unsafe strcpy and strcat calls to safe strlcpy and strlcat.
* Be a little bit more verbose about some errors
* Fix some memory leaks in fileio.c

ok deraadt@, art@


Revision tags: OPENBSD_3_0_BASE
# 1.20 21-Sep-2001 wilfried

correct fork logic and rewrite for clarity, ok todd@


# 1.19 09-Jul-2001 deraadt

correct type on last arg to execl(); nordin@cse.ogi.edu


# 1.18 06-Jul-2001 art

Change the filename completion code to:
- not use an unprotected strcat.
- Append a '/' when a file is a symlink to a directory.
(tab completion on '/sys' is now less annoying.)
- actually make some more sense.


# 1.17 24-May-2001 art

Don't assume that map_table[0] is the fundamental map.


# 1.16 24-May-2001 mickey

spaces


# 1.15 24-May-2001 mickey

use strlcpy vs cpy+len, some other fixes


# 1.14 23-May-2001 art

Get rid of unnecessary casts of NULL.


# 1.13 23-May-2001 art

remove VOID. We're way beyond the point where this could build with an
ancient compiler.


# 1.12 03-May-2001 art

* Let make_file_list always append a '/' at the end of directories.
This makes filename TAB-completion more useful, now you don't need to
write that '/' manually.
* random cleanups including using snprintf and memcmp where open-coded
equivalents were used, waitpid instead of 'while (wait() != pid);', etc.


# 1.11 01-May-2001 art

Use opendir/readdir instead of open/read when making a file list.
This makes filename tab-completion work and takes dired a step further.


Revision tags: OPENBSD_2_9_BASE
# 1.10 29-Jan-2001 niklas

$OpenBSD$


# 1.9 24-Jan-2001 millert

Don't use rename to backup the file to a ~ file. We need to make a
copy instead so the original is edited in place for things like
cron and vipw.


Revision tags: OPENBSD_2_8_BASE
# 1.8 02-Aug-2000 millert

$HOME paranoia: never use getenv("HOME") w/o checking for NULL and non-zero


Revision tags: OPENBSD_2_7_BASE
# 1.7 13-Apr-2000 millert

The start of KNF + -Wall. The code has been run through indent but
needs hand fixup. I stopped at keymap.c...


# 1.6 27-Feb-2000 millert

Remove a now-bogus comment
Use pid_t
Use vfork(), not fork() when sensible


# 1.5 27-Feb-2000 millert

We don't have silly SYSV 14-character file name limits. Taken from
the BSD fileio.c. Note that this module is rife with PATH_MAX overflow
possibilities.


# 1.4 26-Feb-2000 millert

Use uid_t, gid_t and mode_t where applicable.
Pull in changes from the BSD-specific sysdef.h and fileio.c:
kill bogus unlinkdir() and rename() kludges since we have rmdir(2) and rename(2)
define SYMBLINK since we have symlinks
TODO: merge BSD spawn.c and ttyio.c


# 1.3 25-Feb-2000 deraadt

further pruning


# 1.2 25-Feb-2000 deraadt

make it minimally compile; millert


# 1.1 25-Feb-2000 deraadt

initial import of mg2a


# 1.105 13-Apr-2018 florian

mg tries to write backups to the wrong directory when run under a
different effective user, i.e. when invoced via su and
backup-to-home-directory is enabled.

Problem pointed out and diff provied by Lucas Gabriel Vuotto
<lvuotto92 () gmail ! com>, thanks!

Subsequently slacked on for nearly a year by yours truly.

Then remembered when Han Boetes <hboetes () gmail ! com> came up with
a similar diff because of a problem report by Mark Willson where it
turned out that getlogin(2) is not very portable.

OK tb


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.104 30-May-2017 florian

fix -Wshadow warnings.
Found by bcallah, thanks!


Revision tags: OPENBSD_6_1_BASE
# 1.103 28-Jul-2016 tedu

strncmp is a more reasonable way to check the beginning of a string.


# 1.102 28-Jul-2016 tedu

these programs probably do not need to use TMPDIR. ok florian


Revision tags: OPENBSD_6_0_BASE
# 1.101 04-Jul-2016 guenther

Use fstatat() instead of crafting a filename to use with stat()

ok millert@


Revision tags: OPENBSD_5_9_BASE
# 1.100 26-Jan-2016 jasper

ensure the backup file has the same mtime as the original file, this is in line
with emacs' behaviour.

from Harald Dunkel via Han Boetes
ok lum@


Revision tags: OPENBSD_5_8_BASE
# 1.99 19-Mar-2015 bcallah

Clean up the includes in mg.
This does the following:
Moves all POSIX headers from sysdef.h into the individual .c files so that
each file now only includes what it needs. All headers are properly sorted.
Moves the remainder of sysdef.h to other files (mostly def.h) and deletes
sysdef.h now that it's no longer contains anything.
Tweak a comment that references sysdef.h so that it no longer does that.
ok florian@


Revision tags: OPENBSD_5_7_BASE
# 1.98 16-Nov-2014 guenther

Stop using <sys/param.h>; replace MAXPATHLEN with PATH_MAX, stop using MAX(),
and pull in <limits.h> for *_MAX constants.

inspired on a diff from Kamil Rytarowski (n54 (at) gmx.com)
ok bcallah@


Revision tags: OPENBSD_5_6_BASE
# 1.97 20-Mar-2014 lum

Add some missing dobeeps.
ok florian@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.96 18-May-2013 lum

and and -> and


Revision tags: OPENBSD_5_3_BASE
# 1.95 27-Nov-2012 jasper

- move prototype of expandtilder() to def.h; needed by an upcoming diff.


Revision tags: OPENBSD_5_2_BASE
# 1.94 10-Jul-2012 lum

Fix an outstanding debian bug: #658539

"looks good" kjell


# 1.93 18-Jun-2012 jasper

- add a pathnames.h
- some whitespace cleanup

ok lum@


# 1.92 15-Jun-2012 lum

Fixes a problem where if you try to open a file that doesn't exist and has
a name longer than LOGIN_NAME_MAX and also has a tilde at the front
e.g:

$ mg ~01234567890123456789012345678901

mg will give a "Login name too long" instead of opening a new buffer
named ~01234567890123456789012345678901


# 1.91 14-Jun-2012 lum

Allow mg to save backup files to a users home directory.
Suggestions from eric@ and Sunil Nimmagadda.
Remarks deraadt@.


# 1.90 11-Jun-2012 lum

Change error message to reflect the correct filename: nname is 'New
name' as opposed to tname 'Temp name'


# 1.89 25-May-2012 lum

Remove static FILE pointer used for handling files in fileio.c. Pass
by reference instead. This allows the mg startup file to open other
files without unexpected things happening.

Discussed with Sunil Nimmagadda.


# 1.88 23-May-2012 lum

Change how mg handles files beginning with a '~'.
Bring behaviour more into line with emacs.


# 1.87 12-Apr-2012 lum

Remove the conditional directives NO_MACRO and NO_STARTUP.
They have not compiled for numerous years.
ok kjell@ millert@


# 1.86 28-Mar-2012 lum

Close an opendir(). From Igor Zinovik.


Revision tags: OPENBSD_5_1_BASE
# 1.85 31-Aug-2011 lum

On a file write fail:

1. return an error value
2. show an error message

From Loganaden Velvindron with suggestion from millert@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.84 21-Jan-2011 kjell

It volates style(9), but in mg, #include"def.h" goes first. 'twas the way it was built. no binary change here. confirmed by lum@, tested by Henri Kemppainen


# 1.83 21-Jan-2011 kjell

Change len+memcmp check to a strncmp for prefix testing on
filename completion. Avoids a recurring portability headache.
ok lum@
"concerns assuaged to the soothing sounds of Martin Denny blambert@"
(I worry about that guy...)


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.82 15-Sep-2008 kjell

Enable dirty buffer detection in mg.
Emulate the emacs behavior: after suspend/resume, buffer switch,
or at save time, warn (prompt) the user if the file has been modified
on disk in the interim.
This has already saved my butt numerous times.
ok phessler


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.81 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.80 24-Dec-2006 kjell

Fix a bug where inserting a file resulted in an incorrect
line-number count for a buffer (M-X insert-file, M-> to reproduce).
While here, fix a number of bugs with incorrect line numbers
after swap point-and-mark

Originally reported via debian's bug tracking system. Fix tested by
Han Boetes and Deanna Phillips.


# 1.79 19-Nov-2006 deraadt

spacing


# 1.78 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.77 25-Jul-2006 kjell

Rename the header line of a buffer to b_headp, from the remarkably
unintuitive b_linep. No binary change.


# 1.76 29-Jun-2006 jason

- use <dirent.h> not <sys/dir.h> (this appeases FreeBSD's annoying #warning)
- grab <sys/time.h> before <sys/resource.h> like getrusage(2) says
ok kjell


# 1.75 01-Jun-2006 kjell

fix style


# 1.74 01-Jun-2006 jason

make // /~ path rewriting optional in adjustname() and use it everywhere
except for the command line specified files. ok kjell,cloder


# 1.73 03-May-2006 kjell

correct one more comment


# 1.72 03-May-2006 kjell

Do some KNF, clean up some unused junk that has lying around for 40-odd
cvs revisions, and fix a few comments to match reality


# 1.71 03-May-2006 kjell

len = strlen(foo); if foo[len - 1]... will do bad things if len == 0.
Avoid, and rearrange a test so it can't (hypothetically) overflow.


# 1.70 03-Apr-2006 deraadt

lint love; ok kjell


# 1.69 02-Apr-2006 kjell

test char against '\0', not 0


Revision tags: OPENBSD_3_9_BASE
# 1.68 20-Dec-2005 kjell

Clean up the ugly casted frees. In one case, this meant eliminating a nasty
struct/union/casting nightmare when building the list of names for filename
completion. In particular, be consistent about strduping and freeing
the list data.


# 1.67 20-Dec-2005 kjell

Do some delinting of strl-type functions. Also, remove a superfluous
word in the undo-list.


# 1.66 13-Dec-2005 kjell

do some silly de-linting


# 1.65 20-Nov-2005 kjell

#ifdef NO_DIR can go too. From Han Boetes.


# 1.64 20-Nov-2005 kjell

Kill the NO_BACKUP #ifdef


# 1.63 20-Nov-2005 deraadt

toast NO_DIRED #ifdef; ok kjell


# 1.62 18-Nov-2005 deraadt

greedy use of typedef struct was making code harder to read; ok kjell cloder


# 1.61 13-Nov-2005 kjell

Better error checking of snprintfs. From Han Boetes.


# 1.60 11-Nov-2005 kjell

Now that we have an editable minibuffer, revert parsing of
"//" and "/~" as '/' and '~' respectively. I know emacs does it,
but it is weird, and breaks things that foolishly use filenames
like "/tmp//crontab.xxxx".
Proddings from matthieu and deraadt. Error report from Bernd Ahlers.


# 1.59 11-Nov-2005 deraadt

spacing


# 1.58 18-Oct-2005 kjell

When attempting to open a filename, walk backwards through minibuffer:
a> if you run into the beginning of the string, use the whole thing
b> if you run into a // combo, use everything starting from the second /
c> if you run into a /~ combo, use everything starting from the ~
i.e. do like emacs.
From (and for) Jason Wright


# 1.57 17-Oct-2005 kjell

Fix string handling. LOGIN_NAME_MAX includes the NUL, so don't reserve extra
space. Fix usage of strlcpy. Correct test for terminating slash.

"looks good" deraadt@


# 1.56 14-Oct-2005 deraadt

more headers to please lint


# 1.55 14-Oct-2005 kjell

move the dired routines into dired.c where they belong.
ok cloder@


# 1.54 13-Oct-2005 kjell

Make dired buffer read-only by default.
Noticed by Han Boetes


# 1.53 13-Oct-2005 kjell

Use dired mode automatically if file specified for loading is
a directory. Modified version of patch from Han Boetes.
ok cloder@


# 1.52 13-Oct-2005 kjell

check if current line is null. fixes a core in dired.


# 1.51 13-Oct-2005 kjell

Fix dired mode. Make deletions work, and keystrokes match emacs.
* dired-other-window should default to current buffer's path.
* Remove redundant code (from Han Boetes)
* Move initialization (and since we're here, fix mail-mode initialization
too)
* Remove redundant keymap in dired.c, and replace it with
the better one that was being ignored in keymap.c.


Revision tags: OPENBSD_3_8_BASE
# 1.50 14-Jun-2005 kjell

Add explicit public domain notices to all public domain files.
ok millert@, deraadt@


# 1.49 03-Apr-2005 db

This is a no binary change which does:

- spelling, punctuation fixes
- variable declaration lineup
- use parentheses for return and sizeof
- K&R function declarations -> ANSI
- other minor code beautification

ok henning@


Revision tags: OPENBSD_3_7_BASE
# 1.48 10-Mar-2005 deraadt

spacing


# 1.47 10-Mar-2005 henning

the realpath() was needed to do do relative to absolute path
conversion. revert my diff that takes it out, and instead, on
realpath() failure (like when you have no perms on pwd) just return the
unexpanded path. everything still fine with that, just tab completeion
and the like does not work (obviously).
this was mainly for "sudo mg /etc/something" from ~ where ~ is
nfs-mounted with root mapped to -2
and now, that case works as well as tab completion on insert-file etc.


# 1.46 09-Mar-2005 jfb

fix mg's behaviour with regards to files on which we do not have
write access. diff originally from vincent@ and forgotten for
a while.

ok rohee@, "toss it in, i'll bitch if it doesn't work" henning@


# 1.45 01-Feb-2005 henning

make fbackupfile() return FALSE when the rename(2) from the
mkstemp()-derived name to the filename~ one fails
From: Han Boetes <han@mijncomputer.nl>


# 1.44 31-Jan-2005 millert

Safer backup file generation using mkstemp(); henning@ OK


# 1.43 11-Jan-2005 henning

no need to call realpath() in adjustname()
realpath returns an error if cwd is not writeable, and that is quite common
with NFS-mounted ~ and sudo mg /etc/something - annoying as hell. realpath
is not needed at all here, so zap it. vincent ok


Revision tags: OPENBSD_3_6_BASE
# 1.42 09-Jul-2004 vincent

when saving a file, check whether it is newline terminated and if it is not,
prompt the user and add the newline if he agrees.

tested by a lot of helpful tech@ guys, approved by henning


# 1.41 08-Jul-2004 vincent

simplify the file writing loop

looked at by ian, tested on i386, macppc, sparc64


Revision tags: OPENBSD_3_5_BASE
# 1.40 14-Nov-2003 vincent

read(2) returns ssize_t, not size_t. spotted by Andrey Matveev.


# 1.39 08-Nov-2003 jmc

typos from Jonathon Gray;


Revision tags: OPENBSD_3_4_BASE
# 1.38 15-Aug-2003 vincent

make dired work more like emacs. (well, make it _work_, and then add a
bunch of missing commands)


# 1.37 15-Aug-2003 vincent

actually extract the filename off the ls line. makes dired *slightly*
more useful.


# 1.36 08-May-2003 vincent

use NULL to indicate failure in functions that return pointers, not FALSE.
typo in comment

from Patrick Latifi (patrick.l@hermes.usherb.ca)


# 1.35 06-May-2003 vincent

fix a small memleak on errors. noticed by pat latifi.

ok millert, fgsch


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.34 22-Aug-2002 deraadt

knf


# 1.33 25-Jul-2002 vincent

replace the ugly and buggy adjustname function by a simple one using
simple APIs. makes mg not crash with 65k filenames...

ok art@


# 1.32 01-Jul-2002 vincent

KNF


# 1.31 21-Jun-2002 vincent

fix bad usage of strlcpy()'s return value. (made pointers point
beyond the boundaries of buffers)

ok deraadt@


# 1.30 22-Apr-2002 vincent

don't use /bin/cp to copy files.

ok deraadt@


# 1.29 22-Apr-2002 vincent

fix mem leak

ok deraadt@


# 1.28 15-Apr-2002 deraadt

use fchmod and fchown instead; vincent


Revision tags: OPENBSD_3_1_BASE
# 1.27 27-Mar-2002 millert

Fixes the the following problems (from zyrnix)
1) Mg crashes with column width of 1, rows > 2
2) Mg panics on resizing due to incomplete write
3) Mg doesn't compile with STARTUPFILE defined
deraadt@ OK


# 1.26 16-Mar-2002 vincent

Make these special buffers readonly by default. ok art@


# 1.25 11-Mar-2002 vincent

* Move to ANSI function definitions.
* Add a whole lot of consts where I thought it made sense

no ok, but no objections either...


# 1.24 22-Feb-2002 deraadt

if file mode known, try to avoid a race.. i think. millert spotted a bug in my first draft


# 1.23 14-Feb-2002 vincent

d_makename now takes a length parameter so we can remove the
strcpy call.


# 1.22 14-Feb-2002 deraadt

some KNF


# 1.21 13-Feb-2002 vincent

* Replace unsafe strcpy and strcat calls to safe strlcpy and strlcat.
* Be a little bit more verbose about some errors
* Fix some memory leaks in fileio.c

ok deraadt@, art@


Revision tags: OPENBSD_3_0_BASE
# 1.20 21-Sep-2001 wilfried

correct fork logic and rewrite for clarity, ok todd@


# 1.19 09-Jul-2001 deraadt

correct type on last arg to execl(); nordin@cse.ogi.edu


# 1.18 06-Jul-2001 art

Change the filename completion code to:
- not use an unprotected strcat.
- Append a '/' when a file is a symlink to a directory.
(tab completion on '/sys' is now less annoying.)
- actually make some more sense.


# 1.17 24-May-2001 art

Don't assume that map_table[0] is the fundamental map.


# 1.16 24-May-2001 mickey

spaces


# 1.15 24-May-2001 mickey

use strlcpy vs cpy+len, some other fixes


# 1.14 23-May-2001 art

Get rid of unnecessary casts of NULL.


# 1.13 23-May-2001 art

remove VOID. We're way beyond the point where this could build with an
ancient compiler.


# 1.12 03-May-2001 art

* Let make_file_list always append a '/' at the end of directories.
This makes filename TAB-completion more useful, now you don't need to
write that '/' manually.
* random cleanups including using snprintf and memcmp where open-coded
equivalents were used, waitpid instead of 'while (wait() != pid);', etc.


# 1.11 01-May-2001 art

Use opendir/readdir instead of open/read when making a file list.
This makes filename tab-completion work and takes dired a step further.


Revision tags: OPENBSD_2_9_BASE
# 1.10 29-Jan-2001 niklas

$OpenBSD$


# 1.9 24-Jan-2001 millert

Don't use rename to backup the file to a ~ file. We need to make a
copy instead so the original is edited in place for things like
cron and vipw.


Revision tags: OPENBSD_2_8_BASE
# 1.8 02-Aug-2000 millert

$HOME paranoia: never use getenv("HOME") w/o checking for NULL and non-zero


Revision tags: OPENBSD_2_7_BASE
# 1.7 13-Apr-2000 millert

The start of KNF + -Wall. The code has been run through indent but
needs hand fixup. I stopped at keymap.c...


# 1.6 27-Feb-2000 millert

Remove a now-bogus comment
Use pid_t
Use vfork(), not fork() when sensible


# 1.5 27-Feb-2000 millert

We don't have silly SYSV 14-character file name limits. Taken from
the BSD fileio.c. Note that this module is rife with PATH_MAX overflow
possibilities.


# 1.4 26-Feb-2000 millert

Use uid_t, gid_t and mode_t where applicable.
Pull in changes from the BSD-specific sysdef.h and fileio.c:
kill bogus unlinkdir() and rename() kludges since we have rmdir(2) and rename(2)
define SYMBLINK since we have symlinks
TODO: merge BSD spawn.c and ttyio.c


# 1.3 25-Feb-2000 deraadt

further pruning


# 1.2 25-Feb-2000 deraadt

make it minimally compile; millert


# 1.1 25-Feb-2000 deraadt

initial import of mg2a


Revision tags: OPENBSD_6_2_BASE
# 1.104 30-May-2017 florian

fix -Wshadow warnings.
Found by bcallah, thanks!


Revision tags: OPENBSD_6_1_BASE
# 1.103 28-Jul-2016 tedu

strncmp is a more reasonable way to check the beginning of a string.


# 1.102 28-Jul-2016 tedu

these programs probably do not need to use TMPDIR. ok florian


Revision tags: OPENBSD_6_0_BASE
# 1.101 04-Jul-2016 guenther

Use fstatat() instead of crafting a filename to use with stat()

ok millert@


Revision tags: OPENBSD_5_9_BASE
# 1.100 26-Jan-2016 jasper

ensure the backup file has the same mtime as the original file, this is in line
with emacs' behaviour.

from Harald Dunkel via Han Boetes
ok lum@


Revision tags: OPENBSD_5_8_BASE
# 1.99 19-Mar-2015 bcallah

Clean up the includes in mg.
This does the following:
Moves all POSIX headers from sysdef.h into the individual .c files so that
each file now only includes what it needs. All headers are properly sorted.
Moves the remainder of sysdef.h to other files (mostly def.h) and deletes
sysdef.h now that it's no longer contains anything.
Tweak a comment that references sysdef.h so that it no longer does that.
ok florian@


Revision tags: OPENBSD_5_7_BASE
# 1.98 16-Nov-2014 guenther

Stop using <sys/param.h>; replace MAXPATHLEN with PATH_MAX, stop using MAX(),
and pull in <limits.h> for *_MAX constants.

inspired on a diff from Kamil Rytarowski (n54 (at) gmx.com)
ok bcallah@


Revision tags: OPENBSD_5_6_BASE
# 1.97 20-Mar-2014 lum

Add some missing dobeeps.
ok florian@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.96 18-May-2013 lum

and and -> and


Revision tags: OPENBSD_5_3_BASE
# 1.95 27-Nov-2012 jasper

- move prototype of expandtilder() to def.h; needed by an upcoming diff.


Revision tags: OPENBSD_5_2_BASE
# 1.94 10-Jul-2012 lum

Fix an outstanding debian bug: #658539

"looks good" kjell


# 1.93 18-Jun-2012 jasper

- add a pathnames.h
- some whitespace cleanup

ok lum@


# 1.92 15-Jun-2012 lum

Fixes a problem where if you try to open a file that doesn't exist and has
a name longer than LOGIN_NAME_MAX and also has a tilde at the front
e.g:

$ mg ~01234567890123456789012345678901

mg will give a "Login name too long" instead of opening a new buffer
named ~01234567890123456789012345678901


# 1.91 14-Jun-2012 lum

Allow mg to save backup files to a users home directory.
Suggestions from eric@ and Sunil Nimmagadda.
Remarks deraadt@.


# 1.90 11-Jun-2012 lum

Change error message to reflect the correct filename: nname is 'New
name' as opposed to tname 'Temp name'


# 1.89 25-May-2012 lum

Remove static FILE pointer used for handling files in fileio.c. Pass
by reference instead. This allows the mg startup file to open other
files without unexpected things happening.

Discussed with Sunil Nimmagadda.


# 1.88 23-May-2012 lum

Change how mg handles files beginning with a '~'.
Bring behaviour more into line with emacs.


# 1.87 12-Apr-2012 lum

Remove the conditional directives NO_MACRO and NO_STARTUP.
They have not compiled for numerous years.
ok kjell@ millert@


# 1.86 28-Mar-2012 lum

Close an opendir(). From Igor Zinovik.


Revision tags: OPENBSD_5_1_BASE
# 1.85 31-Aug-2011 lum

On a file write fail:

1. return an error value
2. show an error message

From Loganaden Velvindron with suggestion from millert@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.84 21-Jan-2011 kjell

It volates style(9), but in mg, #include"def.h" goes first. 'twas the way it was built. no binary change here. confirmed by lum@, tested by Henri Kemppainen


# 1.83 21-Jan-2011 kjell

Change len+memcmp check to a strncmp for prefix testing on
filename completion. Avoids a recurring portability headache.
ok lum@
"concerns assuaged to the soothing sounds of Martin Denny blambert@"
(I worry about that guy...)


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE
# 1.82 15-Sep-2008 kjell

Enable dirty buffer detection in mg.
Emulate the emacs behavior: after suspend/resume, buffer switch,
or at save time, warn (prompt) the user if the file has been modified
on disk in the interim.
This has already saved my butt numerous times.
ok phessler


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.81 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.80 24-Dec-2006 kjell

Fix a bug where inserting a file resulted in an incorrect
line-number count for a buffer (M-X insert-file, M-> to reproduce).
While here, fix a number of bugs with incorrect line numbers
after swap point-and-mark

Originally reported via debian's bug tracking system. Fix tested by
Han Boetes and Deanna Phillips.


# 1.79 19-Nov-2006 deraadt

spacing


# 1.78 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.77 25-Jul-2006 kjell

Rename the header line of a buffer to b_headp, from the remarkably
unintuitive b_linep. No binary change.


# 1.76 29-Jun-2006 jason

- use <dirent.h> not <sys/dir.h> (this appeases FreeBSD's annoying #warning)
- grab <sys/time.h> before <sys/resource.h> like getrusage(2) says
ok kjell


# 1.75 01-Jun-2006 kjell

fix style


# 1.74 01-Jun-2006 jason

make // /~ path rewriting optional in adjustname() and use it everywhere
except for the command line specified files. ok kjell,cloder


# 1.73 03-May-2006 kjell

correct one more comment


# 1.72 03-May-2006 kjell

Do some KNF, clean up some unused junk that has lying around for 40-odd
cvs revisions, and fix a few comments to match reality


# 1.71 03-May-2006 kjell

len = strlen(foo); if foo[len - 1]... will do bad things if len == 0.
Avoid, and rearrange a test so it can't (hypothetically) overflow.


# 1.70 03-Apr-2006 deraadt

lint love; ok kjell


# 1.69 02-Apr-2006 kjell

test char against '\0', not 0


Revision tags: OPENBSD_3_9_BASE
# 1.68 20-Dec-2005 kjell

Clean up the ugly casted frees. In one case, this meant eliminating a nasty
struct/union/casting nightmare when building the list of names for filename
completion. In particular, be consistent about strduping and freeing
the list data.


# 1.67 20-Dec-2005 kjell

Do some delinting of strl-type functions. Also, remove a superfluous
word in the undo-list.


# 1.66 13-Dec-2005 kjell

do some silly de-linting


# 1.65 20-Nov-2005 kjell

#ifdef NO_DIR can go too. From Han Boetes.


# 1.64 20-Nov-2005 kjell

Kill the NO_BACKUP #ifdef


# 1.63 20-Nov-2005 deraadt

toast NO_DIRED #ifdef; ok kjell


# 1.62 18-Nov-2005 deraadt

greedy use of typedef struct was making code harder to read; ok kjell cloder


# 1.61 13-Nov-2005 kjell

Better error checking of snprintfs. From Han Boetes.


# 1.60 11-Nov-2005 kjell

Now that we have an editable minibuffer, revert parsing of
"//" and "/~" as '/' and '~' respectively. I know emacs does it,
but it is weird, and breaks things that foolishly use filenames
like "/tmp//crontab.xxxx".
Proddings from matthieu and deraadt. Error report from Bernd Ahlers.


# 1.59 11-Nov-2005 deraadt

spacing


# 1.58 18-Oct-2005 kjell

When attempting to open a filename, walk backwards through minibuffer:
a> if you run into the beginning of the string, use the whole thing
b> if you run into a // combo, use everything starting from the second /
c> if you run into a /~ combo, use everything starting from the ~
i.e. do like emacs.
From (and for) Jason Wright


# 1.57 17-Oct-2005 kjell

Fix string handling. LOGIN_NAME_MAX includes the NUL, so don't reserve extra
space. Fix usage of strlcpy. Correct test for terminating slash.

"looks good" deraadt@


# 1.56 14-Oct-2005 deraadt

more headers to please lint


# 1.55 14-Oct-2005 kjell

move the dired routines into dired.c where they belong.
ok cloder@


# 1.54 13-Oct-2005 kjell

Make dired buffer read-only by default.
Noticed by Han Boetes


# 1.53 13-Oct-2005 kjell

Use dired mode automatically if file specified for loading is
a directory. Modified version of patch from Han Boetes.
ok cloder@


# 1.52 13-Oct-2005 kjell

check if current line is null. fixes a core in dired.


# 1.51 13-Oct-2005 kjell

Fix dired mode. Make deletions work, and keystrokes match emacs.
* dired-other-window should default to current buffer's path.
* Remove redundant code (from Han Boetes)
* Move initialization (and since we're here, fix mail-mode initialization
too)
* Remove redundant keymap in dired.c, and replace it with
the better one that was being ignored in keymap.c.


Revision tags: OPENBSD_3_8_BASE
# 1.50 14-Jun-2005 kjell

Add explicit public domain notices to all public domain files.
ok millert@, deraadt@


# 1.49 03-Apr-2005 db

This is a no binary change which does:

- spelling, punctuation fixes
- variable declaration lineup
- use parentheses for return and sizeof
- K&R function declarations -> ANSI
- other minor code beautification

ok henning@


Revision tags: OPENBSD_3_7_BASE
# 1.48 10-Mar-2005 deraadt

spacing


# 1.47 10-Mar-2005 henning

the realpath() was needed to do do relative to absolute path
conversion. revert my diff that takes it out, and instead, on
realpath() failure (like when you have no perms on pwd) just return the
unexpanded path. everything still fine with that, just tab completeion
and the like does not work (obviously).
this was mainly for "sudo mg /etc/something" from ~ where ~ is
nfs-mounted with root mapped to -2
and now, that case works as well as tab completion on insert-file etc.


# 1.46 09-Mar-2005 jfb

fix mg's behaviour with regards to files on which we do not have
write access. diff originally from vincent@ and forgotten for
a while.

ok rohee@, "toss it in, i'll bitch if it doesn't work" henning@


# 1.45 01-Feb-2005 henning

make fbackupfile() return FALSE when the rename(2) from the
mkstemp()-derived name to the filename~ one fails
From: Han Boetes <han@mijncomputer.nl>


# 1.44 31-Jan-2005 millert

Safer backup file generation using mkstemp(); henning@ OK


# 1.43 11-Jan-2005 henning

no need to call realpath() in adjustname()
realpath returns an error if cwd is not writeable, and that is quite common
with NFS-mounted ~ and sudo mg /etc/something - annoying as hell. realpath
is not needed at all here, so zap it. vincent ok


Revision tags: OPENBSD_3_6_BASE
# 1.42 09-Jul-2004 vincent

when saving a file, check whether it is newline terminated and if it is not,
prompt the user and add the newline if he agrees.

tested by a lot of helpful tech@ guys, approved by henning


# 1.41 08-Jul-2004 vincent

simplify the file writing loop

looked at by ian, tested on i386, macppc, sparc64


Revision tags: OPENBSD_3_5_BASE
# 1.40 14-Nov-2003 vincent

read(2) returns ssize_t, not size_t. spotted by Andrey Matveev.


# 1.39 08-Nov-2003 jmc

typos from Jonathon Gray;


Revision tags: OPENBSD_3_4_BASE
# 1.38 15-Aug-2003 vincent

make dired work more like emacs. (well, make it _work_, and then add a
bunch of missing commands)


# 1.37 15-Aug-2003 vincent

actually extract the filename off the ls line. makes dired *slightly*
more useful.


# 1.36 08-May-2003 vincent

use NULL to indicate failure in functions that return pointers, not FALSE.
typo in comment

from Patrick Latifi (patrick.l@hermes.usherb.ca)


# 1.35 06-May-2003 vincent

fix a small memleak on errors. noticed by pat latifi.

ok millert, fgsch


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.34 22-Aug-2002 deraadt

knf


# 1.33 25-Jul-2002 vincent

replace the ugly and buggy adjustname function by a simple one using
simple APIs. makes mg not crash with 65k filenames...

ok art@


# 1.32 01-Jul-2002 vincent

KNF


# 1.31 21-Jun-2002 vincent

fix bad usage of strlcpy()'s return value. (made pointers point
beyond the boundaries of buffers)

ok deraadt@


# 1.30 22-Apr-2002 vincent

don't use /bin/cp to copy files.

ok deraadt@


# 1.29 22-Apr-2002 vincent

fix mem leak

ok deraadt@


# 1.28 15-Apr-2002 deraadt

use fchmod and fchown instead; vincent


Revision tags: OPENBSD_3_1_BASE
# 1.27 27-Mar-2002 millert

Fixes the the following problems (from zyrnix)
1) Mg crashes with column width of 1, rows > 2
2) Mg panics on resizing due to incomplete write
3) Mg doesn't compile with STARTUPFILE defined
deraadt@ OK


# 1.26 16-Mar-2002 vincent

Make these special buffers readonly by default. ok art@


# 1.25 11-Mar-2002 vincent

* Move to ANSI function definitions.
* Add a whole lot of consts where I thought it made sense

no ok, but no objections either...


# 1.24 22-Feb-2002 deraadt

if file mode known, try to avoid a race.. i think. millert spotted a bug in my first draft


# 1.23 14-Feb-2002 vincent

d_makename now takes a length parameter so we can remove the
strcpy call.


# 1.22 14-Feb-2002 deraadt

some KNF


# 1.21 13-Feb-2002 vincent

* Replace unsafe strcpy and strcat calls to safe strlcpy and strlcat.
* Be a little bit more verbose about some errors
* Fix some memory leaks in fileio.c

ok deraadt@, art@


Revision tags: OPENBSD_3_0_BASE
# 1.20 21-Sep-2001 wilfried

correct fork logic and rewrite for clarity, ok todd@


# 1.19 09-Jul-2001 deraadt

correct type on last arg to execl(); nordin@cse.ogi.edu


# 1.18 06-Jul-2001 art

Change the filename completion code to:
- not use an unprotected strcat.
- Append a '/' when a file is a symlink to a directory.
(tab completion on '/sys' is now less annoying.)
- actually make some more sense.


# 1.17 24-May-2001 art

Don't assume that map_table[0] is the fundamental map.


# 1.16 24-May-2001 mickey

spaces


# 1.15 24-May-2001 mickey

use strlcpy vs cpy+len, some other fixes


# 1.14 23-May-2001 art

Get rid of unnecessary casts of NULL.


# 1.13 23-May-2001 art

remove VOID. We're way beyond the point where this could build with an
ancient compiler.


# 1.12 03-May-2001 art

* Let make_file_list always append a '/' at the end of directories.
This makes filename TAB-completion more useful, now you don't need to
write that '/' manually.
* random cleanups including using snprintf and memcmp where open-coded
equivalents were used, waitpid instead of 'while (wait() != pid);', etc.


# 1.11 01-May-2001 art

Use opendir/readdir instead of open/read when making a file list.
This makes filename tab-completion work and takes dired a step further.


Revision tags: OPENBSD_2_9_BASE
# 1.10 29-Jan-2001 niklas

$OpenBSD$


# 1.9 24-Jan-2001 millert

Don't use rename to backup the file to a ~ file. We need to make a
copy instead so the original is edited in place for things like
cron and vipw.


Revision tags: OPENBSD_2_8_BASE
# 1.8 02-Aug-2000 millert

$HOME paranoia: never use getenv("HOME") w/o checking for NULL and non-zero


Revision tags: OPENBSD_2_7_BASE
# 1.7 13-Apr-2000 millert

The start of KNF + -Wall. The code has been run through indent but
needs hand fixup. I stopped at keymap.c...


# 1.6 27-Feb-2000 millert

Remove a now-bogus comment
Use pid_t
Use vfork(), not fork() when sensible


# 1.5 27-Feb-2000 millert

We don't have silly SYSV 14-character file name limits. Taken from
the BSD fileio.c. Note that this module is rife with PATH_MAX overflow
possibilities.


# 1.4 26-Feb-2000 millert

Use uid_t, gid_t and mode_t where applicable.
Pull in changes from the BSD-specific sysdef.h and fileio.c:
kill bogus unlinkdir() and rename() kludges since we have rmdir(2) and rename(2)
define SYMBLINK since we have symlinks
TODO: merge BSD spawn.c and ttyio.c


# 1.3 25-Feb-2000 deraadt

further pruning


# 1.2 25-Feb-2000 deraadt

make it minimally compile; millert


# 1.1 25-Feb-2000 deraadt

initial import of mg2a