home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / PP0803.ZIP / QFN.C < prev    next >
C/C++ Source or Header  |  1989-02-14  |  4KB  |  87 lines

  1. /* QFN.C   Qualify Filename, OS/2 version
  2.    Copyright (c) 1989 Ziff Communications Co.
  3.    PC Magazine * Ray Duncan * Feb 14, 1989 */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. #define API unsigned extern far pascal  /* OS/2 API functions */
  9.  
  10. API DosChDir(char far *, unsigned long);
  11. API DosQCurDisk(int far *, unsigned long far *);
  12. API DosQCurDir(int, void far *, int far *);
  13. API DosSelectDisk(int);
  14.  
  15. static char qbuff[80];                  /* receives qualified filename */
  16.  
  17. char *qfn(char *p)
  18. {
  19.     char tbuff[80];                     /* target directory */
  20.     char cpath[80];                     /* current path at entry */
  21.     int cdrive;                         /* current drive at entry */
  22.     int cpsiz = sizeof(cpath);          /* size of path buffer */
  23.     int qbsiz = sizeof(qbuff);          /* size of qual. name buffer */
  24.     unsigned long drvmap;               /* bitmap for valid drives */
  25.     char *q;                            /* scratch pointer */
  26.     int i;                              /* scratch variable */
  27.  
  28.     DosQCurDisk(&cdrive, &drvmap);      /* get current drive */
  29.  
  30.     DosQCurDir(0, &cpath[1], &cpsiz);   /* get current directory */
  31.     cpath[0] = '\\';                    /* and prepend backslash */
  32.  
  33.     if((strlen(p) >= 2) && (p[1] == ':'))  /* any drive specified? */
  34.     {
  35.         i = (p[0] | 0x20)-'a'+1;        /* get binary drive code */
  36.         if(DosSelectDisk(i))            /* switch to new drive */
  37.             goto errexit;               /* return if bad drive */
  38.         DosQCurDir(0, &cpath[1], &cpsiz);  /* get current directory again */
  39.         p += 2;                         /* bump ptr past drive */
  40.     }
  41.  
  42.     strcpy(tbuff, p);             /* copy target pathname to local buffer */
  43.     q = strrchr(tbuff, '\\');           /* look for last backslash */
  44.  
  45.     if (q != NULL)                      /* any path specified? */
  46.     {
  47.         *q = 0;                         /* yes, make path ASCIIZ */
  48.         if(q == tbuff)                  /* select directory */
  49.         {
  50.             if(DosChDir("\\", 0L))      /* target is root */
  51.                 goto errexit;
  52.         }
  53.         else
  54.         {
  55.             if(DosChDir(tbuff, 0L))     /* target is not root */
  56.             goto errexit;
  57.         }
  58.         q += 1;                         /* point to filename */
  59.     }
  60.     else q = tbuff;            /* if no path specified, point to filename */
  61.  
  62.     if(q[0] == '.') goto errexit; /* filename may not be directory alias */
  63.  
  64.     /* drive and/or path are selected, build qualified filename */
  65.  
  66.     DosQCurDisk(&i, &drvmap);           /* get target drive */
  67.     qbuff[0] = i+'a'-1;                 /* and convert to ASCII */
  68.  
  69.     qbuff[1] = ':';                     /* add drive delimiter */
  70.     qbuff[2] = '\\';                    /* and root backslash */
  71.  
  72.     DosQCurDir(0, &qbuff[3], &qbsiz);   /* get target directory */
  73.     i = strlen(qbuff);                  /* length of drive+path */
  74.     if(i != 3) qbuff[i++] = '\\'; /* if not root, add trailing backslash */
  75.     strcpy(qbuff+i, q);                 /* copy in filename */
  76.  
  77.     DosChDir(cpath, 0L);                /* restore original path */
  78.     DosSelectDisk(cdrive);              /* restore original drive */
  79.  
  80.     return(strlwr(qbuff)); /* fold pathname to lower case, return pointer */
  81.  
  82. errexit:                                /* common error exit point */
  83.     DosChDir(cpath, 0L);                /* restore original path */
  84.     DosSelectDisk(cdrive);              /* restore original drive */
  85.     return(NULL);                       /* return null pointer */
  86. }
  87.