www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/1996/06/04/13:42:11

From: k3040e4 AT c210 DOT edvz DOT uni-linz DOT ac DOT at (Oberhumer Markus)
Message-Id: <199606041731.TAA13345@c210.edvz.uni-linz.ac.at>
Subject: basename()
To: djgpp-workers AT delorie DOT com
Date: Tue, 4 Jun 1996 19:31:53 -0200 (MET DST)
Return-Read-To: markus DOT oberhumer AT jk DOT uni-linz DOT ac DOT at

===============================================================================
Markus F.X.J. Oberhumer <markus DOT oberhumer AT jk DOT uni-linz DOT ac DOT at>

Subject: basename()
To: djgpp-workers AT delorie DOT com
===============================================================================

This is my suggestion for basename() as found in Linux and
many other UNIX variants.

Prototype should probably reside in <unistd.h>

Regards,
Markus




/* src/libc/dos/dir/basename.c */
/* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <libc/unconst.h>


/* always returns the same value like fnsplit() */
char *
basename (const char *path)
{
  const char *pp, *pe;
  const char *b;

  pp = path;

  if ((isalpha(*pp) || strchr("[\\]^_`", *pp)) && (pp[1] == ':'))
    pp += 2;

  if ((pe = strrchr(pp, '\\')) != 0)
    pp = pe + 1;
  if ((pe = strrchr(pp, '/')) != 0)
    pp = pe + 1;
  b = pp;

  /* Special case: "c:/path/." or "c:/path/.."
     These mean FILENAME, not EXTENSION.  */
  while (*pp == '.')
    ++pp;

  pe = strrchr(pp, '.');
  if (!pe)
    pe = strchr(pp, '\0');
  if (pp != pe)
    b = pp;

  return unconst(b, char *);
}


#if defined(TEST) || defined(TEST_COMPAT)

#include <stdio.h>
#ifdef TEST_COMPAT
#include <dir.h>
#endif

static int
test (const char *n)
{
#ifdef TEST_COMPAT
  char name[256], ext[80];

  printf("%-50s '%s'\n", n, basename(n));

  fnsplit(n,NULL,NULL,name,ext);
  strcat(name,ext);
  if (strcmp(basename(n),name) != 0)
  {
    printf("  error: fnsplit() returns '%s'\n", name);
    return 1;
  }
#else
  printf("%-50s '%s'\n", n, basename(n));
#endif
  return 0;
}

int
main(int argc, char *argv[])
{
  int i;
  int r = 0;

  if (argc < 2)
  {
    r |= test("c:");
    r |= test("c:/");
    r |= test("c:/.");
    r |= test("c:/..");
    r |= test("c:../");
    r |= test("c:../test.dat");
    r |= test("c:/dos");
    r |= test("c:/dos/");
    r |= test("c:/dos.622");
    r |= test("c:/dos.622/");
    r |= test("c:a.file.with.many.dots");
    r |= test("c:/a.file.with.many.dots");
    r |= test("c:.a.file.with.many.dots");
    r |= test("c:..a.file.with.many.dots");
    r |= test("c:/.cshrc");
    r |= test("c:/.csh.rc");
  }

  for (i = 1; i < argc; i++)
    r |= test(argv[i]);

  return r;
}

#endif


*** e:unistd.h	Sun Jun 18 05:48:26 1995
--- unistd.h	Sun Jun 02 18:50:18 1996
***************
*** 120,123 ****
--- 120,124 ----
  #define D_OK	0x10
  
+ char *		basename(const char *_path);
  int		brk(void *_heaptop);
  int		__file_exists(const char *_fn);
***************
*** 125,135 ****
  int		ftruncate(int, off_t);
  int		getdtablesize(void);
! int		gethostname(char *buf, int size);
  int		getpagesize(void);
! char *		getwd(char *__buffer);
  int		nice(int _increment);
  void *		sbrk(int _delta);
  int		sync(void);
! int		truncate(const char*, off_t);
  unsigned int	usleep(unsigned int _useconds);
  pid_t		vfork(void);
--- 126,136 ----
  int		ftruncate(int, off_t);
  int		getdtablesize(void);
! int		gethostname(char *_buf, int _size);
  int		getpagesize(void);
! char *		getwd(char *_buffer);
  int		nice(int _increment);
  void *		sbrk(int _delta);
  int		sync(void);
! int		truncate(const char *_file, off_t _size);
  unsigned int	usleep(unsigned int _useconds);
  pid_t		vfork(void);

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019