home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / Apps / ArchiveUtils / Freeze / dirname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-20  |  1.6 KB  |  61 lines

  1. /* dirname.c -- return all but the last element in a path
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <sys/param.h>
  19. #include <stdlib.h>
  20. #include <strings.h>
  21.  
  22. /* Return the leading directories part of PATH,
  23.    allocated with malloc.  If out of memory, return 0.
  24.    Assumes that trailing slashes have already been
  25.    removed.  
  26.    Changed so it returns a pointer to the value allocated in
  27.    a static variable (newpath)
  28. */
  29.  
  30. char *dirname (char *path)
  31. {
  32.   static char newpath[MAXPATHLEN];
  33.   char *slash;
  34.   int length;    /* Length of result, a la strlen. */
  35.  
  36.   slash = rindex (path, '/');
  37.   if (slash == 0){
  38.       newpath[0] = '.';
  39.       newpath[1] = '\000';
  40.       return newpath;
  41.   }
  42.  
  43.   /* Remove any trailing slashes from result. */
  44.   while (slash > path && *slash == '/')
  45.     --slash;
  46.  
  47.   length = slash - path + 1;
  48.   /* newpath = malloc (length + 1); */
  49.   if (newpath == 0)
  50.     return 0;
  51.   strncpy (newpath, path, length);
  52.   newpath[length] = 0;
  53.   return newpath;
  54. }
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.