root/trunk/txlib/txfpath.c

Revision 11, 5.3 kB (checked in by jvw, 3 years ago)

DFSee 8.01 level; fix crash on huge PATH; History-popup improved (4OS2 compatible)

Line 
1//
2//                     TxWin, Textmode Windowing Library
3//
4//   Original code Copyright (c) 1995-2005 Fsys Software and Jan van Wijk
5//
6// ==========================================================================
7//
8// This file contains Original Code and/or Modifications of Original Code as
9// defined in and that are subject to the GNU Lesser General Public License.
10// You may not use this file except in compliance with the License.
11// BY USING THIS FILE YOU AGREE TO ALL TERMS AND CONDITIONS OF THE LICENSE.
12// A copy of the License is provided with the Original Code and Modifications,
13// and is also available at http://www.dfsee.com/txwin/lgpl.htm
14//
15// This library is free software; you can redistribute it and/or modify
16// it under the terms of the GNU Lesser General Public License as published
17// by the Free Software Foundation; either version 2.1 of the License,
18// or (at your option) any later version.
19//
20// This library is distributed in the hope that it will be useful,
21// but WITHOUT ANY WARRANTY; without even the implied warranty of
22// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23// See the GNU Lesser General Public License for more details.
24//
25// You should have received a copy of the GNU Lesser General Public License
26// along with this library; (lgpl.htm) if not, write to the Free Software
27// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28//
29// Questions on TxWin licensing can be directed to: txwin@fsys.nl
30//
31// ==========================================================================
32//
33// TxLib filesystem functions, find file/path section
34//
35
36#include <txlib.h>                              // TxLib interface
37
38
39/*****************************************************************************/
40// Find file in a path specified by environment variable (like PATH)
41/*****************************************************************************/
42char *TxFindByPath                              // RET   ptr to filename
43(
44   char               *fname,                   // IN    filename string
45   char               *path,                    // IN    env-var with path
46   char               *found                    // OUT   found file
47)
48{
49   char               *rc  = NULL;
50   char               *p;
51   char               *this;
52   TXLN                try;
53   FILE               *fp;
54   char               *ep;
55
56   ENTER();
57   TRACES(( "Find '%s' in paths specified by: '%s'\n", fname, path));
58
59   if ((ep = getenv( path)) != NULL)
60   {
61      TRACES(( "path is: '%s'\n", ep));
62      p = ep;
63      do
64      {
65         this = p;
66         p = strchr( p, FS_ENVP_SEP);
67         if (p != NULL)
68         {
69            *p++ = '\0';
70         }
71         strcpy( try, this);
72         strcat( try, FS_PATH_STR);
73         strcat( try, fname);
74         TRACES(( "Trying fspec: '%s'\n", try));
75         if ((fp = fopen( try, "r")) != NULL)
76         {
77            fclose( fp);
78            strcpy( found, try);
79            rc = found;
80         }
81      } while ((p != NULL) && (rc == NULL));
82   }
83   RETURN( rc);
84}                                               // end 'TxFindByPath'
85/*---------------------------------------------------------------------------*/
86
87
88/*****************************************************************************/
89// Find file in current-dir, exe-dir or PATH, and when found, open for read
90/*****************************************************************************/
91FILE *TxFindAndOpenFile                         // RET   opened file handle
92(
93   char               *fname,                   // IN    filename string
94   char               *path,                    // IN    env-var with path
95   char               *found                    // OUT   found file
96)
97{
98   TXLN                try;
99   FILE               *fp;
100   char               *pp;
101
102   ENTER();
103
104   TRACES(( "Find file: '%s' using path: '%s'\n", fname, path));
105
106   strcpy( try, fname);
107   if ((fp = fopen( try, "r")) == NULL)         // try full-spec/current dir
108   {
109      TRACES(( "FAIL in current dir or absolute path: '%s'\n", try));
110      strcpy( try, TxaExeArgv(0));
111      if ((pp = strrchr( try, FS_PATH_SEP)) != NULL)
112      {
113         *(pp +1) = 0;                          // try executable directory
114      }
115      else                                      // if no executable DIR known,
116      {
117         strcpy( try, FS_PATH_STR);             // try root directory
118      }
119      strcat( try, fname);                      // append fname
120
121      if ((fp = fopen( try, "r")) == NULL)      // not here either, try PATH
122      {
123         TRACES(( "FAIL in EXE-directory or root:'%s'\n", try));
124         if (TxFindByPath( fname, "PATH", try) != NULL)
125         {
126            fp = fopen( try, "r");
127         }
128         else
129         {
130            TRACES(( "FAIL in paths for env-var: '%s'\n", "PATH"));
131         }
132      }
133   }
134   if (fp!= NULL)                               // found the file
135   {
136      strcpy( found, try);                      // set the found filename
137      TRACES(("found file: '%s'\n", found));
138   }                                            // and return the (read) fp
139   RETURN (fp);
140}                                               // end 'TxFindAndOpenFile'
141/*---------------------------------------------------------------------------*/
Note: See TracBrowser for help on using the browser.