summaryrefslogtreecommitdiffstats
path: root/usr.bin/patch/backupfile.c
diff options
context:
space:
mode:
authormillert <millert@openbsd.org>1999-01-03 04:49:28 +0000
committermillert <millert@openbsd.org>1999-01-03 04:49:28 +0000
commit5f0451dd9518077f7dc62a66390c56469babe25e (patch)
treee60374b8ccd12e130b31a81e2039692b8b49e353 /usr.bin/patch/backupfile.c
parentwe have basename(3) and dirname(3) in libc (albeit with slightly different semantics (diff)
downloadwireguard-openbsd-5f0451dd9518077f7dc62a66390c56469babe25e.tar.xz
wireguard-openbsd-5f0451dd9518077f7dc62a66390c56469babe25e.zip
back out last change, other parts of patch rely on basename() returning a ptr to a part of name, not a new string
Diffstat (limited to 'usr.bin/patch/backupfile.c')
-rw-r--r--usr.bin/patch/backupfile.c56
1 files changed, 53 insertions, 3 deletions
diff --git a/usr.bin/patch/backupfile.c b/usr.bin/patch/backupfile.c
index c96e7608ecc..321d49e587b 100644
--- a/usr.bin/patch/backupfile.c
+++ b/usr.bin/patch/backupfile.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: backupfile.c,v 1.4 1999/01/03 04:20:07 millert Exp $ */
+/* $OpenBSD: backupfile.c,v 1.5 1999/01/03 04:49:28 millert Exp $ */
/* backupfile.c -- make Emacs style backup file names
Copyright (C) 1990 Free Software Foundation, Inc.
@@ -14,14 +14,13 @@
Some algorithms adapted from GNU Emacs. */
#ifndef lint
-static char rcsid[] = "$OpenBSD: backupfile.c,v 1.4 1999/01/03 04:20:07 millert Exp $";
+static char rcsid[] = "$OpenBSD: backupfile.c,v 1.5 1999/01/03 04:49:28 millert Exp $";
#endif /* not lint */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
-#include <libgen.h>
#include <sys/types.h>
#include "backupfile.h"
#include "config.h"
@@ -73,6 +72,8 @@ enum backup_type backup_type = none;
to numbered) backup file name. */
char *simple_backup_suffix = "~";
+char *basename ();
+char *dirname ();
static char *concat ();
char *find_backup_file_name ();
static char *make_version_name ();
@@ -106,6 +107,7 @@ find_backup_file_name (file)
}
highest_backup = max_backup_version (base_versions, dir);
free (base_versions);
+ free (dir);
if (backup_type == numbered_existing && highest_backup == 0)
return concat (file, simple_backup_suffix);
return make_version_name (file, highest_backup + 1);
@@ -205,6 +207,54 @@ concat (str1, str2)
return newstr;
}
+/* Return NAME with any leading path stripped off. */
+
+char *
+basename (name)
+ char *name;
+{
+ char *base;
+
+ base = strrchr (name, '/');
+ return base ? base + 1 : name;
+}
+
+/* Return the leading directories part of PATH,
+ allocated with malloc. If out of memory, return 0.
+ Assumes that trailing slashes have already been
+ removed. */
+
+char *
+dirname (path)
+ char *path;
+{
+ char *newpath;
+ char *slash;
+ int length; /* Length of result, not including NUL. */
+
+ slash = strrchr (path, '/');
+ if (slash == 0)
+ {
+ /* File is in the current directory. */
+ path = ".";
+ length = 1;
+ }
+ else
+ {
+ /* Remove any trailing slashes from result. */
+ while (slash > path && *slash == '/')
+ --slash;
+
+ length = slash - path + 1;
+ }
+ newpath = malloc (length + 1);
+ if (newpath == 0)
+ return 0;
+ strncpy (newpath, path, length);
+ newpath[length] = 0;
+ return newpath;
+}
+
/* If ARG is an unambiguous match for an element of the
null-terminated array OPTLIST, return the index in OPTLIST
of the matched element, else -1 if it does not match any element