]> www.wagner.pp.ru Git - oss/catdoc.git/blob - src/fileutil.c
657b02252375cf2a9caa7c7b1bccd801c28767a3
[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?(q+1):NULL)) {
75                 q=strchr(p,LIST_SEP);
76                 if (q) {
77                         if (!prepare_path_buf(path_buf,p,q)) continue;
78                 } else {
79                         if (!prepare_path_buf(path_buf,p,p+strlen(p))) continue;
80                 }
81                 strcat(path_buf,dir_sep); /* always one char */
82                 if (strlen(path_buf)+strlen(name)>=PATH_BUF_SIZE) 
83                         continue; /* Ignore too deeply nested directories */
84                 strcat(path_buf,name);
85                 if (access(path_buf,0)==0) {
86                         free(name); 
87                         return strdup(path_buf);
88                 }
89         }
90         /* if we are here, nothing found */
91         free(name); 
92         return NULL;
93 }
94
95 /************************************************************************/
96 /* Searches for charset with given name and put pointer to malloced copy*/
97 /* of its name into first arg if found. Otherwise leaves first arg      */
98 /*  unchanged. Returns non-zero on success                              */ 
99 /************************************************************************/
100 int check_charset(char **filename,const char *charset) {
101         char *tmppath;
102         if (!strncmp(charset,"utf-8",6)) {
103                 *filename=strdup("utf-8");
104                 return 1;
105         }   
106         tmppath=find_file(stradd(charset,CHARSET_EXT),charset_path);
107         if (tmppath&& *tmppath) {
108                 *filename=strdup(charset);
109                 free(tmppath);
110                 return 1;
111         }
112         return 0;
113 }
114
115 /**********************************************************************/
116 /*  Returns malloced string containing concatenation of two           */
117 /*  arguments                                                         */
118 /**********************************************************************/
119 char *stradd(const char *s1,const char *s2) 
120 { char *res;
121         res=malloc(strlen(s1)+strlen(s2)+1);
122         if (!res) {
123                 fprintf (stderr,"Out of memory!");
124                 exit(1);
125         }
126         strcpy(res,s1);
127         strcat(res,s2);
128         return res;
129 }  
130
131
132 /*
133  * In DOS, argv[0] contain full path to the program, and it is a custom
134  * to keep configuration files in same directory as program itself
135  */
136 #ifdef __MSDOS__
137 char *exe_dir(void) {
138         static char pathbuf[PATH_BUF_SIZE];
139         char *q;
140         strcpy(pathbuf,_argv[0]); /* DOS ensures, that our exe path is no
141                                                                  longer than PATH_BUF_SIZE*/
142         q=strrchr(pathbuf,DIR_SEP);
143         if (q) {
144                 *q=0;
145         } else {
146                 pathbuf[0]=0;
147         }
148         return pathbuf;
149 }
150 char *add_exe_path(const char *name) {
151         static char path[PATH_BUF_SIZE];
152         char *mypath=exe_dir();
153         /* No snprintf in Turbo C 2.0 library, so just check by hand
154            and exit if something goes wrong */
155         if (strchr(name,'%')) {
156                 /* there is substitution */
157                 if (strlen(name)-1+strlen(mypath)>=PATH_BUF_SIZE) {
158                         fprintf(stderr,"Invalid config file. file name \"%s\" too long "
159                                         "after substitution\n",name);
160                         exit(1);
161                 }   
162                 sprintf(path,name,exe_dir());
163                 return path;
164         } else {
165                 return name;
166         }  
167 }
168 #endif 
169 /*********************************************************************/
170 /* Prints out list of available charsets, i.e. names without extension *
171  * of all .txt files in the charset path + internally-supported utf-8  *
172  ************************************************************************/ 
173
174 void list_charsets(void) {
175         const char *p;
176         char *q;
177         char path_buf[PATH_BUF_SIZE];
178         char dir_sep[2]={DIR_SEP,0};
179 #ifdef __MSDOS__
180         struct ffblk ffblock;
181         int res,col;
182 #else
183         glob_t glob_buf;
184         int count,glob_flags=GLOB_ERR;
185 #endif
186         char **ptr;
187         for (p=charset_path;p;p=(q?(q+1):NULL)) {
188                 q=strchr(p,LIST_SEP);
189                 if (q) {
190                         if (q-p>=PATH_BUF_SIZE) {
191                                 /* Oops, dir name too long, perhabs broken config file */
192                                 continue;
193                         }
194                         strncpy(path_buf,p,q-p);
195                         path_buf[q-p]=0;
196                 } else {
197                         if (strlen(p)>=PATH_BUF_SIZE) continue;
198                         strcpy(path_buf,p);
199                 }
200                 /* Empty list element means current directory */
201                 if (!*path_buf) {
202                         path_buf[0]='.';
203                         path_buf[1]=0;
204 #ifdef __MSDOS__
205                 } else {
206                         strcpy(path_buf,add_exe_path(path_buf)); /* safe, becouse
207                                                                                                                 add_exe_path knows about PATH_BUF_SIZE */
208 #endif
209                 }
210                 strcat(path_buf,dir_sep); /* always one char */
211                 if (strlen(path_buf)+6>=PATH_BUF_SIZE)
212                         continue; /* Ignore too deeply nested directories */
213                 strcat(path_buf,"*.txt");
214 #ifdef __MSDOS__
215                 res=findfirst(path_buf,&ffblock,FA_RDONLY | FA_HIDDEN | FA_ARCH);
216                 col=1;
217                 printf("Available charsets:\n"); 
218                 while (!res) {
219                         char name[12],*src,*dest;
220                         dest=name;
221                         src=ffblock.ff_name;
222                         for (dest=name,src=ffblock.ff_name;*src && *src !='.';dest++,src++)
223                                 *dest=tolower(*src);
224                         *dest++=(col<5)?'\t':'\n';
225                         if (++col>5) col=1;
226                         *dest=0;
227                         printf("%10s",name);
228                         res=findnext(&ffblock);
229                 }
230 #else
231                 switch (glob(path_buf,glob_flags,NULL,&glob_buf)) {
232                         case 0:
233 #ifdef GLOB_NOMATCH
234                         case GLOB_NOMATCH:
235 #endif
236                                 break;
237                         default:
238                                 perror("catdoc");
239                                 exit(1);
240                 }
241                 glob_flags|=GLOB_APPEND;
242 #endif
243         }
244 #ifdef __MSDOS__
245         fputs("utf-8\n",stdout);
246 #else
247         count=0;printf("Available charsets:"); 
248         for (ptr=glob_buf.gl_pathv;*ptr;ptr++) {
249                 printf("%c",(count++)%5?'\t':'\n');
250                 p=strrchr(*ptr,dir_sep[0]);
251                 if (!p) continue;
252                 p++;
253                 if ((q=strchr(p,'.'))) *q=0;
254                 fputs(p,stdout);
255         }  
256         printf("%c",(count++)%5?'\t':'\n');
257         fputs("utf-8",stdout);
258         printf("\n");
259         globfree(&glob_buf);
260 #endif   
261 }