Mail Archives: djgpp-workers/1998/02/01/16:54:26
I got tired of porting them from scratch for every GNU package that uses
them. IMHO, it's high time we had our own versions. So here you go:
*** /dev/null Sun Feb 1 00:26:14 1998
--- src/libc/compat/unistd/basename.c Sat Jan 31 21:27:12 1998
***************
*** 0 ****
--- 1,27 ----
+ /* Copyright (C) 1997 DJ Delorie, see COPYING.DJ for details */
+ #include <unistd.h>
+ #include <libc/unconst.h>
+
+ char *
+ basename (const char *fname)
+ {
+ const char *base = fname;
+
+ if (fname && *fname)
+ {
+ if (fname[1] == ':')
+ {
+ fname += 2;
+ base = fname;
+ }
+
+ while (*fname)
+ {
+ if (*fname == '\\' || *fname == '/')
+ base = fname + 1;
+ fname++;
+ }
+ }
+
+ return unconst (base, char *);
+ }
*** /dev/null Sun Feb 1 00:26:22 1998
--- src/libc/compat/unistd/basename.txh Sun Feb 1 00:09:42 1998
***************
*** 0 ****
--- 1,38 ----
+
+ @c ----------------------------------------------------------------------
+
+ @node basename, file system
+ @subheading Syntax
+
+ @example
+ #include <unistd.h>
+
+ char * basename (const char *fname);
+ @end example
+
+ @subheading Description
+
+ This function returns the @dfn{basename} of the file, which is the last
+ part of its full name given by @var{fname}, with the drive letter and
+ leading directories stripped off. For example, the basename of
+ @code{c:/foo/bar/file.ext} is @code{file.ext}, and the basename of
+ @code{a:foo} is @code{foo}. Trailing slashes and backslashes are
+ significant: the basename of @code{c:/foo/bar/} is an empty string after
+ the rightmost slash.
+
+ This function treats both forward- and backslashes like directory
+ separators, so it can handle file names with mixed styles of slashes.
+
+ @subheading Return Value
+
+ A pointer into the original file name where the basename starts. Note
+ that this is @strong{not} a new buffer allocated with @code{malloc}. If
+ @var{fname} is a NULL pointer, the function will return a NULL pointer.
+
+ @subheading Example
+
+ @example
+ if (strcmp (basename (file_name, "gcc.exe")) == 0)
+ printf ("The file %s is the GNU C/C++ compiler\n", file_name);
+ @end example
+
*** /dev/null Sun Feb 1 00:26:32 1998
--- src/libc/compat/unistd/dirname.c Sun Feb 1 00:07:18 1998
***************
*** 0 ****
--- 1,61 ----
+ /* Copyright (C) 1997 DJ Delorie, see COPYING.DJ for details */
+ #include <stdlib.h>
+ #include <string.h>
+ #include <unistd.h>
+
+ char *
+ dirname (const char *fname)
+ {
+ const char *p = fname;
+ const char *slash = NULL;
+
+ if (fname)
+ {
+ size_t dirlen;
+ char * dirpart;
+
+ if (*fname && fname[1] == ':')
+ {
+ slash = fname + 1;
+ p += 2;
+ }
+
+ /* Find the rightmost slash. */
+ while (*p)
+ {
+ if (*p == '/' || *p == '\\')
+ slash = p;
+ p++;
+ }
+
+ if (slash == NULL)
+ {
+ fname = ".";
+ dirlen = 1;
+ }
+ else
+ {
+ /* Remove any trailing slashes. */
+ while (slash > fname && (slash[-1] == '/' || slash[-1] == '\\'))
+ slash--;
+
+ /* How long is the directory we will return? */
+ dirlen = slash - fname + (slash == fname || slash[-1] == ':');
+ if (*slash == ':' && dirlen == 1)
+ dirlen += 2;
+ }
+
+ dirpart = (char *)malloc (dirlen + 1);
+ if (dirpart != NULL)
+ {
+ strncpy (dirpart, fname, dirlen);
+ if (slash && *slash == ':' && dirlen == 3)
+ dirpart[2] = '.'; /* for "x:foo" return "x:." */
+ dirpart[dirlen] = '\0';
+ }
+
+ return dirpart;
+ }
+
+ return NULL;
+ }
*** /dev/null Sun Feb 1 00:26:39 1998
--- src/libc/compat/unistd/dirname.txh Sun Feb 1 00:21:50 1998
***************
*** 0 ****
--- 1,35 ----
+
+ @c ----------------------------------------------------------------------
+
+ @node dirname, file system
+ @subheading Syntax
+
+ @example
+ #include <unistd.h>
+
+ char * dirname (const char *fname);
+ @end example
+
+ @subheading Description
+
+ This function returns the directory part of the argument @var{fname}
+ copied to a buffer allocated by calling @code{malloc}. The directory
+ part is everything up to but not including the rightmost slash (either
+ forward- or backslash) in @var{fname}. If @var{fname} includes a drive
+ letter but no slashes, the function will return @code{@var{x}:.} where
+ @var{x} is the drive letter. If @var{fname} includes neither the drive
+ letter nor any slashes, @code{"."} will be returned. Trailing slashes
+ are removed from the result, unless it is a root directory, with or
+ without a drive letter.
+
+ @subheading Return value
+
+ The directory part in malloc'ed storage, or a NULL pointer of either
+ there's not enough free memory, or @var{fname} is a NULL pointer.
+
+ @subheading Example
+
+ @example
+ printf ("The parent of current directory is %s\n",
+ dirname (getcwd (0, PATH_MAX)));
+ @end example
*** src/libc/compat/unistd/makefile.~0 Tue Jun 13 06:18:16 1995
--- src/libc/compat/unistd/makefile Sun Feb 1 00:25:02 1998
***************
*** 1,6 ****
--- 1,8 ----
# Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details
TOP=../..
+ SRC += basename.c
+ SRC += dirname.c
SRC += fsync.c
SRC += ftruncat.c
SRC += getdtabl.c
*** src/docs/kb/wc202.t~2 Sat Jan 31 20:43:50 1998
--- src/docs/kb/wc202.txi Sun Feb 1 00:24:04 1998
*************** Windows9X.
*** 323,325 ****
--- 323,328 ----
The @code{sys_siglist[]} array is now available with the names of all
the signals, and the function @code{psignal} can be used to print
messages which include the signal names.
+
+ The new functions @code{basename} and @code{dirname} can be used to
+ extract directory and basename parts from file names.
*** include/unistd.h~0 Sun Oct 6 00:34:18 1996
--- include/unistd.h Sat Jan 31 21:20:04 1998
***************
*** 120,126 ****
--- 120,128 ----
/* additional access() checks */
#define D_OK 0x10
+ char * basename(const char *_fn);
int brk(void *_heaptop);
+ char * dirname(const char *_fn);
int __file_exists(const char *_fn);
int fsync(int _fd);
int ftruncate(int, off_t);
- Raw text -