diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 1999-01-03 05:33:50 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 1999-01-03 05:33:50 +0000 |
commit | fa79b12122e966be5518c9d5de1aa22f1b431020 (patch) | |
tree | 391ba58678a319d10415743a5846f3bda39e773b /usr.bin/patch | |
parent | 74747574a48ac4e2d4962b444d6fb969cb8644bd (diff) |
Use libc basename(3) and dirname(3) instead of defining our own. Also clean up some nasty assumptions that basename() returns a pointer that lies within its argument
Diffstat (limited to 'usr.bin/patch')
-rw-r--r-- | usr.bin/patch/backupfile.c | 56 | ||||
-rw-r--r-- | usr.bin/patch/common.h | 3 | ||||
-rw-r--r-- | usr.bin/patch/inp.c | 28 | ||||
-rw-r--r-- | usr.bin/patch/util.c | 25 | ||||
-rw-r--r-- | usr.bin/patch/util.h | 3 |
5 files changed, 31 insertions, 84 deletions
diff --git a/usr.bin/patch/backupfile.c b/usr.bin/patch/backupfile.c index 321d49e587b..c7aeb51214d 100644 --- a/usr.bin/patch/backupfile.c +++ b/usr.bin/patch/backupfile.c @@ -1,4 +1,4 @@ -/* $OpenBSD: backupfile.c,v 1.5 1999/01/03 04:49:28 millert Exp $ */ +/* $OpenBSD: backupfile.c,v 1.6 1999/01/03 05:33:48 millert Exp $ */ /* backupfile.c -- make Emacs style backup file names Copyright (C) 1990 Free Software Foundation, Inc. @@ -14,13 +14,14 @@ Some algorithms adapted from GNU Emacs. */ #ifndef lint -static char rcsid[] = "$OpenBSD: backupfile.c,v 1.5 1999/01/03 04:49:28 millert Exp $"; +static char rcsid[] = "$OpenBSD: backupfile.c,v 1.6 1999/01/03 05:33:48 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" @@ -72,8 +73,6 @@ 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 (); @@ -107,7 +106,6 @@ 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); @@ -207,54 +205,6 @@ 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 diff --git a/usr.bin/patch/common.h b/usr.bin/patch/common.h index 8a0203c2df0..dd1b812fc8a 100644 --- a/usr.bin/patch/common.h +++ b/usr.bin/patch/common.h @@ -1,4 +1,4 @@ -/* $OpenBSD: common.h,v 1.9 1997/09/22 05:45:26 millert Exp $ */ +/* $OpenBSD: common.h,v 1.10 1999/01/03 05:33:48 millert Exp $ */ #define DEBUGGING @@ -36,6 +36,7 @@ #include <signal.h> #include <stdlib.h> #include <unistd.h> +#include <libgen.h> #undef malloc #undef realloc diff --git a/usr.bin/patch/inp.c b/usr.bin/patch/inp.c index bab3562943a..f27866571d2 100644 --- a/usr.bin/patch/inp.c +++ b/usr.bin/patch/inp.c @@ -1,7 +1,7 @@ -/* $OpenBSD: inp.c,v 1.7 1998/11/25 00:30:25 espie Exp $ */ +/* $OpenBSD: inp.c,v 1.8 1999/01/03 05:33:48 millert Exp $ */ #ifndef lint -static char rcsid[] = "$OpenBSD: inp.c,v 1.7 1998/11/25 00:30:25 espie Exp $"; +static char rcsid[] = "$OpenBSD: inp.c,v 1.8 1999/01/03 05:33:48 millert Exp $"; #endif /* not lint */ #include "EXTERN.h" @@ -78,6 +78,9 @@ char *filename; Reg2 LINENUM iline; char lbuf[MAXLINELEN]; + if (!filename || *filename == '\0') + return FALSE; + statfailed = stat(filename, &filestat); if (statfailed && ok_to_create_file) { if (verbose) @@ -102,26 +105,23 @@ char *filename; || ((filestat.st_mode & 0022) == 0 && filestat.st_uid != myuid)) { struct stat cstat; char *cs = Nullch; - char *filebase; - int pathlen; + char *filebase, *filedir; filebase = basename(filename); - pathlen = filebase - filename; + filedir = dirname(filename); - /* Put any leading path into `s'. - Leave room in lbuf for the diff command. */ + /* Leave room in lbuf for the diff command. */ s = lbuf + 20; - strncpy(s, filename, pathlen); -#define try(f, a1, a2) (Snprintf(s + pathlen, sizeof lbuf - (s + pathlen - lbuf), f, a1, a2), stat(s, &cstat) == 0) - if ( try("RCS/%s%s", filebase, RCSSUFFIX) - || try("RCS/%s" , filebase, 0) - || try( "%s%s", filebase, RCSSUFFIX)) { +#define try(f, a1, a2, a3) (Snprintf(s, sizeof lbuf - 20, f, a1, a2, a3), stat(s, &cstat) == 0) + if ( try("%s/RCS/%s%s", filedir, filebase, RCSSUFFIX) + || try("%s/RCS/%s%s", filedir, filebase, "") + || try( "%s/%s%s", filedir, filebase, RCSSUFFIX)) { Snprintf(buf, sizeof buf, CHECKOUT, filename); Snprintf(lbuf, sizeof lbuf, RCSDIFF, filename); cs = "RCS"; - } else if ( try("SCCS/%s%s", SCCSPREFIX, filebase) - || try( "%s%s", SCCSPREFIX, filebase)) { + } else if ( try("%s/SCCS/%s%s", filedir, SCCSPREFIX, filebase) + || try( "%s/%s%s", filedir, SCCSPREFIX, filebase)) { Snprintf(buf, sizeof buf, GET, s); Snprintf(lbuf, sizeof lbuf, SCCSDIFF, s, filename); cs = "SCCS"; diff --git a/usr.bin/patch/util.c b/usr.bin/patch/util.c index 33eeee7ef46..6efca26d882 100644 --- a/usr.bin/patch/util.c +++ b/usr.bin/patch/util.c @@ -1,7 +1,7 @@ -/* $OpenBSD: util.c,v 1.4 1997/09/22 05:45:27 millert Exp $ */ +/* $OpenBSD: util.c,v 1.5 1999/01/03 05:33:48 millert Exp $ */ #ifndef lint -static char rcsid[] = "$OpenBSD: util.c,v 1.4 1997/09/22 05:45:27 millert Exp $"; +static char rcsid[] = "$OpenBSD: util.c,v 1.5 1999/01/03 05:33:48 millert Exp $"; #endif /* not lint */ #include "EXTERN.h" @@ -380,7 +380,7 @@ int assume_exists; char tmpbuf[200]; int sleading = strip_leading; - if (!at) + if (!at || *at == '\0') return Nullch; while (isspace(*at)) at++; @@ -416,17 +416,14 @@ int assume_exists; if (stat(name, &filestat) && !assume_exists) { char *filebase = basename(name); - int pathlen = filebase - name; - - /* Put any leading path into `tmpbuf'. */ - strncpy(tmpbuf, name, pathlen); - -#define try(f, a1, a2) (Snprintf(tmpbuf + pathlen, sizeof tmpbuf - pathlen, f, a1, a2), stat(tmpbuf, &filestat) == 0) - if ( try("RCS/%s%s", filebase, RCSSUFFIX) - || try("RCS/%s%s", filebase, "") - || try( "%s%s", filebase, RCSSUFFIX) - || try("SCCS/%s%s", SCCSPREFIX, filebase) - || try( "%s%s", SCCSPREFIX, filebase)) + char *filedir = dirname(name); + +#define try(f, a1, a2, a3) (Snprintf(tmpbuf, sizeof tmpbuf, f, a1, a2), stat(tmpbuf, &filestat) == 0) + if ( try("%s/RCS/%s%s", filedir, filebase, RCSSUFFIX) + || try("%s/RCS/%s%s", filedir, filebase, "") + || try( "%s/%s%s", filedir, filebase, RCSSUFFIX) + || try("%s/SCCS/%s%s", filedir, SCCSPREFIX, filebase) + || try( "%s/%s%s", filedir, SCCSPREFIX, filebase)) return name; free(name); name = Nullch; diff --git a/usr.bin/patch/util.h b/usr.bin/patch/util.h index a396135b652..e57966db80a 100644 --- a/usr.bin/patch/util.h +++ b/usr.bin/patch/util.h @@ -1,4 +1,4 @@ -/* $OpenBSD: util.h,v 1.2 1996/06/10 11:21:35 niklas Exp $ */ +/* $OpenBSD: util.h,v 1.3 1999/01/03 05:33:49 millert Exp $ */ /* and for those machine that can't handle a variable argument list */ @@ -79,4 +79,3 @@ char *savestr(); void set_signals(); void ignore_signals(); void makedirs(); -char *basename(); |