summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/pmerge.c
blob: 3b219b1a0c331eeff32a9c5caaf29e12153799a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
**  pmerge() - Portable replacement for fnmerge(), _makepath(), etc.
**
**  Forms a full DOS pathname from drive, path, file, and extension
**  specifications.
**
**  Arguments: 1 - Buffer to receive full pathname
**             2 - Drive
**             3 - Path
**             4 - Name
**             5 - Extension
**
**  Returns: Nothing
**
**  public domain by Bob Stout
*/

#include <string.h>

#define LAST_CHAR(s) ((s)[strlen(s) - 1])

void pmerge(char *path, char *drive, char *dir, char *fname, char *ext)
{
      *path = '\0';

      if (drive && *drive)
      {
            strcat(path, drive);
            if (':' != LAST_CHAR(path))
                  strcat(path, ":");
      }

      if (dir && *dir)
      {
            char *p;

            strcat(path, dir);
            for (p = path; *p; ++p)
                  if ('/' == *p)
                        *p = '\\';
            if ('\\' != LAST_CHAR(path))
                  strcat(path, "\\");
      }

      if (fname && *fname)
      {
            strcat(path, fname);

            if (ext && *ext)
            {
                  if ('.' != *ext)
                        strcat(path, ".");
                  strcat(path, ext);
            }
      }
}

#ifdef TEST

#include <stdio.h>

int main(int argc, char *argv[])
{
      char pathname[FILENAME_MAX];

      pmerge(pathname, argv[1], argv[2], argv[3], argv[4]);
      printf("pmerge (%s, %s, %s, %s) returned:\n %s\n",
            argv[1], argv[2], argv[3], argv[4], pathname);
}