home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / advos2 / ch09 / qfn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-12  |  3.9 KB  |  104 lines

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