home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume3 / getpath / getpath.c next >
C/C++ Source or Header  |  1989-02-03  |  8KB  |  201 lines

  1.  
  2. /************************************************************************
  3.  *                                                                      *
  4.  *                getpath.c     -----    get DOS path                   *
  5.  *                                                                      *
  6.  *                 Version 1.00  ---   May 19, 1988                     *
  7.  *                                                                      *
  8.  *              Copyright (c) 1988 by David O. Tinker                   *
  9.  *            All Rights Reserved except as noted below.                *
  10.  *                                                                      *
  11.  * This software may be used without charge for non-commercial purposes *
  12.  * provided this Copyright notice is not removed. Permission is granted *
  13.  * to make alterations to this software, provided credit is given to    *
  14.  * the original author in any altered version.                          *
  15.  *                                                                      *
  16.  *                      David O. Tinker                                 *
  17.  *                      Department of Biochemistry                      *
  18.  *                      University of Toronto                           *
  19.  *                      Toronto, Canada, M5S 1A8                        *
  20.  *                      E-mail:                                         *
  21.  *                      dtinker@utgpu.UUCP                              *
  22.  *                      dtinker@utoronto.BITNET                         *
  23.  *                                                                      *
  24.  ***********************************************************************/
  25.  
  26.  
  27. #include "getpath.h"
  28.  
  29. /************************************************
  30.  *                                              *
  31.  * path_t *getpath(filename)                    *
  32.  * char *filename;                              *
  33.  *                                              *
  34.  * Instantiate structure to hold path data      *
  35.  *                                              *
  36.  * Input: DOS File Name string,                 *
  37.  *        Format: [d:][\path]name[.ext]         *
  38.  *                                              *
  39.  * Returns: Structure of type path_t            *
  40.  *          (see getpath.h                      *
  41.  *                                              *
  42.  * Calling:                                     *
  43.  *      char *file_name;                        *
  44.  *      path_t *path, *getpath();               *
  45.  *      ...                                     *
  46.  *      path = getpath(file_name);              *
  47.  *                                              *
  48.  * Library Functions Required:                  *
  49.  *      *getcwd(2);                             *
  50.  *      (get current working directory)         *
  51.  *      *strcat(2);                             *
  52.  *                                              *
  53.  * Bugs:                                        *
  54.  *      Does not check for legal filename       *
  55.  *                                              *
  56.  *                                              *
  57.  * Author:      David O. Tinker                 *
  58.  *                                              *
  59.  ***********************************************/
  60.  
  61. path_t *getpath(filename)
  62. char *filename;
  63. {
  64.         path_t *new;                    /* structure to hold data           */
  65.         char *file_disk,                /* disk location of active file     */
  66.              *path,                     /* path to active file              */
  67.              *file_name,                /* name of active file (no .ext)    */
  68.              *file_ext,                 /* extension of active file         */
  69.              *fil_loc,                  /* temporary cwd buffer             */
  70.              *curr_path;                /* current working directory        */
  71.         int i, j = 0, k, name_len, no_of_dirs = 0;
  72.         BOOLEAN path_p = FALSE;
  73.         char *directory[10],       /* 10 subdirectories ought be enough ! */
  74.              buff[13];             /* buffer to hold file & directory names */
  75.         void error();
  76.  
  77.         name_len = strlen(filename);
  78.  
  79.         /* get current working directory */
  80.  
  81.         fil_loc= getcwd(NULL,64);
  82.         curr_path = CALLOC((strlen(fil_loc) + 2), char);
  83.         curr_path[0] = '\134';
  84.         curr_path = strcat(curr_path, fil_loc);
  85.         free((char *)fil_loc);
  86.  
  87.         /* find disk and directory level of filename */
  88.  
  89.         file_disk = CALLOC(3, char);
  90.         for (i = 0; i < name_len; i++) {
  91.                 if((filename[i] == '\072') || (filename[i] == '\057')
  92.                    || (filename[i] == '\134'))
  93.                         path_p = TRUE;
  94.                 if(filename[i] == '\072') { /* set file_disk name */
  95.                         j = i;
  96.                         file_disk[0] = filename[--j];
  97.                         file_disk[1] = filename[i];
  98.                         j += 2;
  99.                 }
  100.                 if((filename[i] == '\057') || (filename[i] == '\134'))
  101.                         ++no_of_dirs;
  102.         }
  103.  
  104.         if(no_of_dirs) {
  105.                 buff[0] = '\134';
  106.                 buff[1] = '\0';
  107.                 directory[0] = CALLOC(2, char);
  108.                 strcpy(directory[0], buff);
  109.         }
  110.  
  111.         for(i = 1, k = 0; i < no_of_dirs; i++) {
  112.                 if(i == 1)
  113.                         ++j;
  114.                 buff[k] = filename[j];
  115.                 ++j;
  116.                 while((filename[j] != '\057') && (filename[j] != '\134')) {
  117.                         ++k;
  118.                         buff[k] = filename[j];
  119.                         ++j;
  120.                         if(filename[j] == '\0') break;
  121.                 }
  122.                 buff[++k] = '\0';
  123.                 directory[i] = CALLOC((strlen(buff) + 1), char);
  124.                 strcpy(directory[i], buff);
  125.                 k = 0;
  126.         }
  127.         for(i = 0, k = 1; i < no_of_dirs; i++) {
  128.                 k += strlen(directory[i]);
  129.         }
  130.         if(no_of_dirs) {
  131.                 ++j;
  132.                 path = CALLOC(k, char);
  133.                 strcpy(path, directory[0]);
  134.                 for(i = 1; i < no_of_dirs; i++)
  135.                         path = strcat(path, directory[i]);
  136.         }
  137.         else 
  138.                 path = CALLOC(1, char);
  139.  
  140.         /* parse remaining characters in filename for name and ext */
  141.         for(i = 0, k = 0; i < 8; i++) {
  142.                 buff[k] = filename[j];
  143.                 ++k;
  144.                 ++j;
  145.                 if((filename[j] == '.') || (filename[j] == '\0')) {
  146.                         if(filename[j] == '.') ++j;
  147.                         break;
  148.                 }
  149.         }
  150.         buff[k] = '\0';
  151.         file_name = CALLOC((strlen(buff) + 1), char);
  152.         strcpy(file_name, buff);
  153.         if(filename[j] != '\0'){
  154.                 for(i = 0, k = 0; i < 3; i++) {
  155.                         buff[k] = filename[j];
  156.                         ++k;
  157.                         ++j;
  158.                         if(filename[j] == '\0') break;
  159.                 }
  160.                 buff[k] = '\0';
  161.                 file_ext = CALLOC((strlen(buff) + 1), char);
  162.                 strcpy(file_ext, buff);
  163.         }
  164.         else
  165.                 file_ext = CALLOC(1, char);
  166.                 
  167.         if(!path_p) {
  168.                 path = CALLOC((strlen(curr_path) + 1), char);
  169.                 strcpy(path, curr_path);
  170.         }
  171.  
  172.         free((char *)curr_path);
  173.         
  174.         if((new = MALLOC(path_t)) == NULL) {
  175.                 error("Can't instantiate Path");
  176.                 return(NULL);
  177.         }
  178.  
  179.         else {
  180.                 new->fil_disk = file_disk;
  181.                 new->fil_path = path;
  182.                 new->fil_name = file_name;
  183.                 new->fil_ext = file_ext;
  184.                 return(new);
  185.         }
  186.         /* all done */
  187. }
  188.  
  189.  
  190. /****************************************************************
  191.  * General Error Handling Utility                               *
  192.  ***************************************************************/
  193.  
  194. void error(string)
  195. char *string;
  196. {
  197.         printf("ERROR : %s\n", string);
  198.         exit(1);
  199. }
  200.  
  201.