source: git/resources/feFopen.cc @ a3a6a0

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