]> www.wagner.pp.ru Git - oss/catdoc.git/blob - src/fileutil.c
Recreated CVS repository from working copy
[oss/catdoc.git] / src / fileutil.c
1 /*
2   Copyright 1998-2003 Victor Wagner
3   Copyright 2003 Alex Ott
4   This file is released under the GPL.  Details can be
5   found in the file COPYING accompanying this distribution.
6 */
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <sys/stat.h>
14 #include <stdlib.h>
15 #include "catdoc.h"
16 #if defined(MSDOS) && !defined(__MSDOS__)
17 #define __MSDOS__
18 #endif
19 #if defined(__MSDOS__) || defined(_WIN32)
20 #include <dir.h>
21 #include <dos.h>
22 #else
23 #include <glob.h>
24 #endif
25
26
27 /************************************************************************/
28 /*  Copies component of string starting with p and ending one char      */
29 /*  before q into path_buf, expanding ~ if neccessary                   */
30 /************************************************************************/
31 int prepare_path_buf(char *path_buf, const char *start, const char *end) {
32         if (*start == '~' && start[1] == DIR_SEP) {
33                 char *home=getenv("HOME");
34                 start++;
35                 if (!home) {
36                         if (end-start>PATH_BUF_SIZE) return 0;
37                         strncpy(path_buf,start,end-start);
38                         path_buf[end-start]=0;
39                 } else {
40                         int l = strlen(home);
41                         if (l+(end-start)>PATH_BUF_SIZE) return 0;
42                         strcpy(path_buf,home);
43                         strncpy(path_buf+l,start,end-start);
44                         path_buf[end-start+l]=0;
45                 }       
46         } else {        
47                         if (end-start>PATH_BUF_SIZE) return 0;
48                         strncpy(path_buf,start,end-start);
49                         path_buf[end-start]=0;
50         }
51         /* Empty list element means current directory */
52         if (!*path_buf) {
53                 path_buf[0]='.';
54                 path_buf[1]=0;
55 #ifdef __MSDOS__
56         } else {
57                 strcpy(path_buf,add_exe_path(path_buf)); /* safe, becouse
58                                                                                                         add_exe_path knows about PATH_BUF_SIZE */
59 #endif
60         }
61         return 1;
62
63 }       
64 /************************************************************************/
65 /* Searches for file name in specified list of directories. Sets        */
66 /* Returns dynamically allocated full path or NULL. if nothing          */
67 /* appropriate   Expects name to be dynamically allocated and frees it  */
68 /************************************************************************/
69 char *find_file(char *name, const char *path)
70 { const char *p;
71         char *q;
72         char path_buf[PATH_BUF_SIZE];
73         char dir_sep[2]={DIR_SEP,0};
74         for (p=path;p;p=q+1) {
75                 q=strchr(p,LIST_SEP);
76
77                 if (q) {
78                         if (!prepare_path_buf(path_buf,p,q)) continue;
79                 } else {
80                         q--;
81                         if (!prepare_path_buf(path_buf,p,p+strlen(p))) continue;
82                 }
83                 strcat(path_buf,dir_sep); /* always one char */
84                 if (strlen(path_buf)+strlen(name)>=PATH_BUF_SIZE) 
85                         continue; /* Ignore too deeply nested directories */
86                 strcat(path_buf,name);
87                 if (access(path_buf,0)==0) {
88                         free(name); 
89                         return strdup(path_buf);
90                 }
91         }
92         /* if we are here, nothing found */
93         free(name); 
94         return NULL;
95 }
96
97 /************************************************************************/
98 /* Searches for charset with given name and put pointer to malloced copy*/
99 /* of its name into first arg if found. Otherwise leaves first arg      */
100 /*  unchanged. Returns non-zero on success                              */ 
101 /************************************************************************/
102 int check_charset(char **filename,const char *charset) {
103         char *tmppath;
104         if (!strncmp(charset,"utf-8",6)) {
105                 *filename=strdup("utf-8");
106                 return 1;
107         }   
108         tmppath=find_file(stradd(charset,CHARSET_EXT),charset_path);
109         if (tmppath&& *tmppath) {
110                 *filename=strdup(charset);
111                 free(tmppath);
112                 return 1;
113         }
114         return 0;
115 }
116
117 /**********************************************************************/
118 /*  Returns malloced string containing concatenation of two           */
119 /*  arguments                                                         */
120 /**********************************************************************/
121 char *stradd(const char *s1,const char *s2) 
122 { char *res;
123         res=malloc(strlen(s1)+strlen(s2)+1);
124         if (!res) {
125                 fprintf (stderr,"Out of memory!");
126                 exit(1);
127         }
128         strcpy(res,s1);
129         strcat(res,s2);
130         return res;
131 }  
132
133
134 /*
135  * In DOS, argv[0] contain full path to the program, and it is a custom
136  * to keep configuration files in same directory as program itself
137  */
138 #ifdef __MSDOS__
139 char *exe_dir(void) {
140         static char pathbuf[PATH_BUF_SIZE];
141         char *q;
142         strcpy(pathbuf,_argv[0]); /* DOS ensures, that our exe path is no
143                                                                  longer than PATH_BUF_SIZE*/
144         q=strrchr(pathbuf,DIR_SEP);
145         if (q) {
146                 *q=0;
147         } else {
148                 pathbuf[0]=0;
149         }
150         return pathbuf;
151 }
152 char *add_exe_path(const char *name) {
153         static char path[PATH_BUF_SIZE];
154         char *mypath=exe_dir();
155         /* No snprintf in Turbo C 2.0 library, so just check by hand
156            and exit if something goes wrong */
157         if (strchr(name,'%')) {
158                 /* there is substitution */
159                 if (strlen(name)-1+strlen(mypath)>=PATH_BUF_SIZE) {
160                         fprintf(stderr,"Invalid config file. file name \"%s\" too long "
161                                         "after substitution\n",name);
162                         exit(1);
163                 }   
164                 sprintf(path,name,exe_dir());
165                 return path;
166         } else {
167                 return name;
168         }  
169 }
170 #endif 
171 /*********************************************************************/
172 /* Prints out list of available charsets, i.e. names without extension *
173  * of all .txt files in the charset path + internally-supported utf-8  *
174  ************************************************************************/ 
175
176 void list_charsets(void) {
177         const char *p;
178         char *q;
179         char path_buf[PATH_BUF_SIZE];
180         char dir_sep[2]={DIR_SEP,0};
181 #ifdef __MSDOS__
182         struct ffblk ffblock;
183         int res,col;
184 #else
185         glob_t glob_buf;
186         int count,glob_flags=GLOB_ERR;
187 #endif
188         char **ptr;
189         for (p=charset_path;p;p=q+1) {
190                 q=strchr(p,LIST_SEP);
191
192                 if (q) {
193                         if (q-p>=PATH_BUF_SIZE) {
194                                 /* Oops, dir name too long, perhabs broken config file */
195                                 continue;
196                         }
197                         strncpy(path_buf,p,q-p);
198                         path_buf[q-p]=0;
199                 } else {
200                         q--;
201                         if (strlen(p)>=PATH_BUF_SIZE) continue;
202                         strcpy(path_buf,p);
203                 }
204                 /* Empty list element means current directory */
205                 if (!*path_buf) {
206                         path_buf[0]='.';
207                         path_buf[1]=0;
208 #ifdef __MSDOS__
209                 } else {
210                         strcpy(path_buf,add_exe_path(path_buf)); /* safe, becouse
211                                                                                                                 add_exe_path knows about PATH_BUF_SIZE */
212 #endif
213                 }
214                 strcat(path_buf,dir_sep); /* always one char */
215                 if (strlen(path_buf)+6>=PATH_BUF_SIZE)
216                         continue; /* Ignore too deeply nested directories */
217                 strcat(path_buf,"*.txt");
218 #ifdef __MSDOS__
219                 res=findfirst(path_buf,&ffblock,FA_RDONLY | FA_HIDDEN | FA_ARCH);
220                 col=1;
221                 printf("Available charsets:\n"); 
222                 while (!res) {
223                         char name[12],*src,*dest;
224                         dest=name;
225                         src=ffblock.ff_name;
226                         for (dest=name,src=ffblock.ff_name;*src && *src !='.';dest++,src++)
227                                 *dest=tolower(*src);
228                         *dest++=(col<5)?'\t':'\n';
229                         if (++col>5) col=1;
230                         *dest=0;
231                         printf("%10s",name);
232                         res=findnext(&ffblock);
233                 }
234 #else
235                 switch (glob(path_buf,glob_flags,NULL,&glob_buf)) {
236                         case 0:
237 #ifdef GLOB_NOMATCH
238                         case GLOB_NOMATCH:
239 #endif
240                                 break;
241                         default:
242                                 perror("catdoc");
243                                 exit(1);
244                 }
245                 glob_flags|=GLOB_APPEND;
246 #endif
247         }
248 #ifdef __MSDOS__
249         fputs("utf-8\n",stdout);
250 #else
251         count=0;printf("Available charsets:"); 
252         for (ptr=glob_buf.gl_pathv;*ptr;ptr++) {
253                 printf("%c",(count++)%5?'\t':'\n');
254                 p=strrchr(*ptr,dir_sep[0]);
255                 if (!p) continue;
256                 p++;
257                 if ((q=strchr(p,'.'))) *q=0;
258                 fputs(p,stdout);
259         }  
260         printf("%c",(count++)%5?'\t':'\n');
261         fputs("utf-8",stdout);
262         printf("\n");
263         globfree(&glob_buf);
264 #endif   
265 }