source: git/resources/feFopen.cc @ 2ea781

spielwiese
Last change on this file since 2ea781 was ba5e9e, checked in by Oleksandr Motsak <motsak@…>, 11 years ago
Changed configure-scripts to generate individual public config files for each package: resources, libpolys, singular (main) fix: sources should include correct corresponding config headers.
  • Property mode set to 100644
File size: 4.3 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <sys/types.h>
5#include <sys/stat.h>
6#include <unistd.h>
7#include <errno.h>
8
9#ifdef HAVE_CONFIG_H
10#include "resourcesconfig.h"
11#endif /* HAVE_CONFIG_H */
12
13#if defined(HAVE_PWD_H) && defined(HAVE_GETPWNAM)
14#include <pwd.h>
15#endif
16
17//#include <reporter/reporter.h>
18
19#include "feFopen.h"
20#include "feResource.h"
21
22
23
24extern "C" {
25void (*WerrorS_callback)(const char *s) = NULL;
26short errorreported=0;
27void WerrorS(const char *s)
28{
29  if (WerrorS_callback == NULL)
30  {
31     fwrite("   ? ",1,5,stderr);
32     fwrite((char *)s,1,strlen((char *)s),stderr);
33     fwrite("\n",1,1,stderr);
34     fflush(stderr);
35  }
36  else
37  {
38    WerrorS_callback(s);
39  }
40  errorreported = 1;
41}
42}
43
44/*****************************************************************
45 *
46 * File handling
47 *
48 *****************************************************************/
49
50FILE * feFopen(const char *path, const char *mode, char *where,
51               short useWerror, short path_only)
52{
53  char longpath[MAXPATHLEN];
54  if (path[0]=='~')
55  {
56    if (path[1] == DIR_SEP)
57    {
58      const char* home = getenv("HOME");
59#ifdef ix86_Win
60      if ((home==NULL)||(!access(home,X_OK)))
61        home = getenv("SINGHOME");
62#endif
63      if (home != NULL)
64      {
65        strcpy(longpath, home);
66        strcat(longpath, &(path[1]));
67        path = longpath;
68      }
69    }
70#if defined(HAVE_PWD_H) && defined(HAVE_GETPWNAM)
71    else
72    {
73      char* dir_sep;
74      struct passwd *pw_entry;
75      strcpy (longpath, path);
76      dir_sep = strchr(longpath, DIR_SEP);
77      if (dir_sep==NULL)
78      {
79        char buf[256];
80        strcpy(buf,"illegal ~ in filename >>");
81        strncat(buf,longpath,235);
82        strcat(buf,"<<");
83        WerrorS(buf);
84        return NULL;
85      }
86      *dir_sep = '\0';
87      pw_entry = getpwnam(&longpath[1]);
88      if (pw_entry != NULL)
89      {
90        strcpy(longpath, pw_entry->pw_dir);
91        dir_sep = strchr((char *)path, DIR_SEP);
92        strcat(longpath, dir_sep);
93        path = longpath;
94      }
95    }
96#endif
97  }
98  FILE * f=NULL;
99  if (! path_only)
100  {
101    struct stat statbuf;
102    int res = -1;
103    do
104    {
105      res = stat(path,&statbuf);
106    } while((res < 0) and (errno == EINTR));
107    if ((res == 0)
108    && (S_ISREG(statbuf.st_mode)))
109      f = myfopen(path,mode);
110  }
111  if (where!=NULL) strcpy(where,path);
112  if ((*mode=='r') &&
113      (path[0]!=DIR_SEP) &&
114      ! (path[0] == '.' && path[1] == DIR_SEP) &&
115      (f==NULL))
116  {
117    char found = 0;
118    char* spath = feResource('s');
119    char *s;
120
121    if (where==NULL) s=(char *)malloc(1024);
122    else             s=where;
123
124    if (spath!=NULL)
125    {
126      char *p,*q;
127      p = spath;
128      while( (q=strchr(p, fePathSep)) != NULL)
129      {
130        *q = '\0';
131        strcpy(s,p);
132        *q = fePathSep;
133        strcat(s, DIR_SEPP);
134        strcat(s, path);
135        if(!access(s, R_OK)) { found++; break; }
136        p = q+1;
137      }
138      if(!found)
139      {
140        strcpy(s,p);
141        strcat(s, DIR_SEPP);
142        strcat(s, path);
143      }
144      f=myfopen(s,mode);
145      if (f!=NULL)
146      {
147        if (where==NULL) free(s);
148        return f;
149      }
150    }
151    else
152    {
153      if (where!=NULL) strcpy(s/*where*/,path);
154      f=myfopen(path,mode);
155    }
156    if (where==NULL) free(s);
157  }
158  if ((f==NULL)&&(useWerror))
159  {
160    char buf[256];
161    strcpy(buf,"cannot open `");
162    strncat(buf,path,240);
163    strcat(buf,"`");
164    WerrorS(buf);
165  }
166  return f;
167}
168
169// Make sure that mode contains binary option
170FILE* myfopen(const char *path, const char *mode)
171{
172#if (defined(CYGWIN) || defined(ix86_Win))
173  char mmode[4];
174  int i;
175  int done = 0;
176
177  for (i=0;;i++)
178  {
179    mmode[i] = mode[i];
180    if (mode[i] == '\0') break;
181    if (mode[i] == 'w') done = 1;
182    if (mode[i] == 'a') done = 1;
183    if (mode[i] == 'b') done = 1;
184  }
185
186  if (! done)
187  {
188    mmode[i] = 'b';
189    mmode[i+1] = '\0';
190  }
191  return fopen(path, mmode);
192#else
193  return fopen(path, mode);
194#endif
195}
196// replace "\r\n" by " \n" and "\r" by "\n"
197
198size_t myfread(void *ptr, size_t size, size_t nmemb, FILE *stream)
199{
200  size_t got = fread(ptr, size, nmemb, stream) * size;
201  size_t i;
202
203  for (i=0; i<got; i++)
204  {
205    if ( ((char*) ptr)[i] == '\r')
206    {
207      if (i+1 < got && ((char*) ptr)[i+1] == '\n')
208        ((char*) ptr)[i] = ' ';
209      else
210        ((char*) ptr)[i] = '\n';
211    }
212  }
213  return got;
214}
Note: See TracBrowser for help on using the repository browser.