home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / diskutil / fdf / elib / dirs.c < prev    next >
C/C++ Source or Header  |  1993-08-05  |  2KB  |  91 lines

  1. #include <osbind.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #include "elib.h"
  7.  
  8.  
  9.  
  10. /*
  11.  * is_special
  12.  *
  13.  * given a directory name, return non-zero if it is 'special' (i.e. '.' or
  14.  * '..') or zero if it is normal
  15.  */
  16. int is_special(char *d_name)
  17.  
  18. {
  19.     int d_len;
  20.  
  21.     if ((d_name == NULL) || ((d_len = strlen(d_name)) > 2))
  22.         return 0;
  23.     else if (d_len == 2)
  24.         return (!strcmp(d_name, ".."));
  25.     else if (d_len == 1)
  26.         return (d_name[0] == '.');
  27.     else    /* null directory name */
  28.         return 0;
  29. }
  30.  
  31.  
  32.  
  33. /*
  34.  * append_dir_to_path
  35.  *
  36.  * given a path and a directory name, return a pointer to memory with
  37.  * the directory appended to the path
  38.  */
  39.  
  40. char *append_dir_to_path(char *path, char *dir)
  41.  
  42. {
  43.     static char *dir_sep = PATH_SEPARATOR;
  44.     char *ret_val, sep_byte;
  45.     int path_len, mem_nec;
  46.  
  47.     mem_nec = (path_len = strlen(path)) +
  48.               (sep_byte = ((path[path_len-1] != dir_sep[0]) ? 1 : 0)) +
  49.               strlen(dir) + 1;
  50.     if ((ret_val = malloc((size_t) mem_nec)) == NULL) {
  51.         printf("append_dir_to_path: memory allocation failure!\n");
  52.         exit(-1);
  53.     }
  54.     else {
  55.         strcpy(ret_val, path);
  56.         if (sep_byte)
  57.             strcat(ret_val, dir_sep);
  58.         strcat(ret_val, dir);
  59.  
  60.         return ret_val;
  61.     }
  62. }
  63.  
  64.  
  65.  
  66. /*
  67.  *    format_dir()
  68.  *
  69.  *    Input:
  70.  *        dir - the directory as specified by user
  71.  *        append_slash - non-zero if a slash should be appended, zero otherwise
  72.  *    Output:
  73.  *        formatted_dir - a formatted version of 'dir'
  74.  */
  75.  
  76. void format_dir(char *dir, char app_slash, char *formatted_dir)
  77. {
  78.     char cur_dir[FILENAME_MAX];
  79.     int i;
  80.  
  81.     getcwd(cur_dir, FILENAME_MAX);
  82.     chdir(dir);
  83.     getcwd(formatted_dir, FILENAME_MAX);
  84.     for (i=0; formatted_dir[i] != EOS; i++)
  85.         if (formatted_dir[i] == '/')
  86.             formatted_dir[i] = '\\';
  87.     if ((app_slash) && (formatted_dir[i - 1] != '\\'))
  88.         strcat(formatted_dir, "\\");
  89.     chdir(cur_dir);
  90. }
  91.