home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Texteditors / Origami / Sources / src / lib / dir_name.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-27  |  1.4 KB  |  70 lines

  1. /*{{{}}}*/
  2. /*{{{  #includes*/
  3. #ifdef CONFIG_H
  4. #   include "config.h"
  5. #endif
  6.  
  7. #include <sys/types.h>
  8. #include <ctype.h>
  9. #include <string.h>
  10. #include <pwd.h>
  11. #include <stdlib.h>
  12. #include <limits.h>
  13. #include <stdio.h>
  14. #include <unistd.h>
  15.  
  16. #include <h/envvar_str.h>
  17. #include <h/os.h>
  18. #include <lib/ori_add_lib.h>
  19. /*}}}  */
  20.  
  21. /*{{{  dir_name*/
  22. /* Get the directory part of a given filename s, copy it to d with the
  23.  * trailing / and return d. If no directory is inside s, 0 is returned
  24.  */
  25. unsigned char *dir_name(unsigned char * const d,unsigned char const *s)
  26. {
  27.   unsigned char *c,*path_end;
  28.  
  29.   for (c=path_end=d;;)
  30.    { unsigned char x;
  31.  
  32.      x= *s;
  33.      switch (x)
  34.       { case '\0':
  35.            if (path_end==d)
  36.               return(0);
  37.            else
  38.             { *path_end='\0';
  39.               return(d);
  40.             }
  41.         case PATH_C:
  42.            path_end=c+1;
  43.         default:
  44.            *c++=x;
  45.            s++;
  46.            continue;
  47.       }
  48.    }
  49. }
  50. /*}}}  */
  51. /*{{{  join_fd*/
  52. /* if argument f is not a absolut path, and argument df is leaded by a
  53.  * directory part, copy dirname(df)/f to f and return f.
  54.  * In all other cases return 0.
  55.  */
  56. unsigned char *join_fd(unsigned char * const f,unsigned char const*const df)
  57. {
  58.   unsigned char d_buff[_POSIX_PATH_MAX+1];
  59.  
  60.   if (!IS_ROOT(f) && dir_name(d_buff,df))
  61.    { strcat((char*)d_buff,(char*)f);
  62.      strcpy((char*)f,(char*)d_buff);
  63.  
  64.      return(f);
  65.    }
  66.  
  67.   return(0);
  68. }
  69. /*}}}  */
  70.