Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / lib / DtSearch / apndext.c
1 /*
2  *   COMPONENT_NAME: austext
3  *
4  *   FUNCTIONS: append_ext
5  *              replace_ext
6  *
7  *   ORIGINS: 27
8  *
9  *
10  *   (C) COPYRIGHT International Business Machines Corp. 1990,1995
11  *   All Rights Reserved
12  *   Licensed Materials - Property of IBM
13  *   US Government Users Restricted Rights - Use, duplication or
14  *   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
15  */
16 /*************************** APNDEXT.C *************************
17  * $XConsortium: apndext.c /main/5 1996/05/07 13:24:29 drk $
18  * append_ext:
19  * 1. Concatenates a file extension to a passed filename,
20  *      unless an extension is already provided in the name.
21  *      Moves the concatenated string to the passed buffer.
22  * 2. Checks for filename buffer overflow.
23  * 
24  * replace_ext:
25  * Same thing, except that if a dotted extension is already
26  * provided, it REPLACES the last extension with the passed one. 
27  *
28  * $Log$
29  * Revision 2.2  1995/10/25  22:25:29  miker
30  * Added prolog.
31  *
32  * Revision 2.1  1995/09/22  18:08:26  miker
33  * Freeze DtSearch 0.1, AusText 2.1.8
34  */
35 #include "SearchP.h"
36 #include <string.h>
37
38 /********************************************************/
39 /*                                                      */
40 /*                     append_ext                       */
41 /*                                                      */
42 /********************************************************/
43 void append_ext(char *buffer,           /* output assy area */
44                 int  buflen,            /* length of buffer */
45                 char *fname,            /* input prefix (file name) */
46                 char *fext)             /* input suffix (file ext .XXX) */
47 {
48     char        *endptr, *slashptr, *dotptr;
49
50     strncpy (buffer, fname, buflen);
51     *(buffer + buflen - 5) = '\0';      /* room for ".xxx" at end */
52
53     /* Look for FINAL dot and FINAL slash (directory delim) */
54     dotptr = slashptr = NULL;
55     for (endptr = buffer;  *endptr != 0;  endptr++) {
56         if (*endptr == LOCAL_SLASH)
57             slashptr = endptr;
58         else if (*endptr == '.')
59             dotptr = endptr;
60     }
61
62     /* If extension already exists (ie there's a dot in the name),
63      * and its past any dir delim, if any,
64      * return immediately:  the dot is a valid extension marker.
65      */
66     if (dotptr) {       /* extension already exists */
67         if (slashptr == NULL)
68             return;
69         else if (slashptr < dotptr)
70             return;
71     }
72
73     /* Extension does not exist or its in earlier dir name */
74     strcpy (endptr, fext);
75     return;
76 } /* append_ext() */
77
78
79 /********************************************************/
80 /*                                                      */
81 /*                    replace_ext                       */
82 /*                                                      */
83 /********************************************************/
84 void    replace_ext (char *buffer,      /* output assy area */
85                 int  buflen,            /* length of buffer */
86                 char *fname,            /* input prefix (file name) */
87                 char *fext)             /* input suffix (file ext .XXX) */
88 {
89     int         fnamelen;
90     char        *targ;
91
92     strncpy(buffer, fname, buflen - 1);
93     *(buffer + buflen - 1) = '\0';              /* just in case */
94     fnamelen = strlen(buffer);
95     if ((targ = strrchr(buffer, '.')) == NULL) {
96         strncpy (buffer + fnamelen, fext, buflen - fnamelen - 1);
97         *(buffer + buflen - 1) = '\0';  /* again just in case */
98     }
99     else {
100         while (*targ != 0 && *fext != 0) *targ++ = *fext++;
101         if (*fext == 0) *targ = 0;
102     }
103     return;
104 } /* replace_ext() */
105
106 /*************************** APNDEXT.C ****************************/