source: git/libpolys/resources/feFopen.cc @ f323dd1

spielwiese
Last change on this file since f323dd1 was f323dd1, checked in by Hans Schoenemann <hannes@…>, 12 years ago
chg: move feResource to findexec lib
  • Property mode set to 100644
File size: 3.7 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
8#include "config.h"
9#include <misc/auxiliary.h>
10
11#include <reporter/reporter.h>
12
13#include "feFopen.h"
14#include <findexec/feResource.h>
15
16
17
18/*****************************************************************
19 *
20 * File handling
21 *
22 *****************************************************************/
23
24FILE * feFopen(const char *path, const char *mode, char *where,
25               BOOLEAN useWerror, BOOLEAN path_only)
26{
27  char longpath[MAXPATHLEN];
28  if (path[0]=='~')
29  {
30    if (path[1] == DIR_SEP)
31    {
32      const char* home = getenv("HOME");
33#ifdef ix86_Win
34      if ((home==NULL)||(!access(home,X_OK)))
35        home = getenv("SINGHOME");
36#endif
37      if (home != NULL)
38      {
39        strcpy(longpath, home);
40        strcat(longpath, &(path[1]));
41        path = longpath;
42      }
43    }
44#if defined(HAVE_PWD_H) && defined(HAVE_GETPWNAM)
45    else
46    {
47      char* dir_sep;
48      struct passwd *pw_entry;
49      strcpy (longpath, path);
50      dir_sep = strchr(longpath, DIR_SEP);
51      if (dir_sep==NULL)
52      {
53        Werror(" illegal ~ in filename >>%s<<",longpath);
54        return NULL;
55      }
56      *dir_sep = '\0';
57      pw_entry = getpwnam(&longpath[1]);
58      if (pw_entry != NULL)
59      {
60        strcpy(longpath, pw_entry->pw_dir);
61        dir_sep = strchr((char *)path, DIR_SEP);
62        strcat(longpath, dir_sep);
63        path = longpath;
64      }
65    }
66#endif
67  }
68  FILE * f=NULL;
69  if (! path_only)
70  {
71    struct stat statbuf;
72    if ((stat(path,&statbuf)==0)
73    && (S_ISREG(statbuf.st_mode)))
74      f = myfopen(path,mode);
75  }
76  if (where!=NULL) strcpy(where,path);
77  if ((*mode=='r') &&
78      (path[0]!=DIR_SEP) &&
79      ! (path[0] == '.' && path[1] == DIR_SEP) &&
80      (f==NULL))
81  {
82    char found = 0;
83    char* spath = feResource('s');
84    char *s;
85
86    if (where==NULL) s=(char *)malloc(1024);
87    else             s=where;
88
89    if (spath!=NULL)
90    {
91      char *p,*q;
92      p = spath;
93      while( (q=strchr(p, fePathSep)) != NULL)
94      {
95        *q = '\0';
96        strcpy(s,p);
97        *q = fePathSep;
98        strcat(s, DIR_SEPP);
99        strcat(s, path);
100        if(!access(s, R_OK)) { found++; break; }
101        p = q+1;
102      }
103      if(!found)
104      {
105        strcpy(s,p);
106        strcat(s, DIR_SEPP);
107        strcat(s, path);
108      }
109      f=myfopen(s,mode);
110      if (f!=NULL)
111      {
112        if (where==NULL) free((ADDRESS)s);
113        return f;
114      }
115    }
116    else
117    {
118      if (where!=NULL) strcpy(s/*where*/,path);
119      f=myfopen(path,mode);
120    }
121    if (where==NULL) free((ADDRESS)s);
122  }
123  if ((f==NULL)&&(useWerror))
124    Werror("cannot open `%s`",path);
125  return f;
126}
127
128FILE * feFopen(const char *path, const char *mode)
129{
130  return feFopen (path, mode, 0,0,0);
131}
132
133// Make sure that mode contains binary option
134FILE* myfopen(const char *path, const char *mode)
135{
136#if (defined(CYGWIN) || defined(ix86_Win))
137  char mmode[4];
138  int i;
139  int done = 0;
140
141  for (i=0;;i++)
142  {
143    mmode[i] = mode[i];
144    if (mode[i] == '\0') break;
145    if (mode[i] == 'w') done = 1;
146    if (mode[i] == 'a') done = 1;
147    if (mode[i] == 'b') done = 1;
148  }
149
150  if (! done)
151  {
152    mmode[i] = 'b';
153    mmode[i+1] = '\0';
154  }
155  return fopen(path, mmode);
156#else
157  return fopen(path, mode);
158#endif
159}
160// replace "\r\n" by " \n" and "\r" by "\n"
161
162size_t myfread(void *ptr, size_t size, size_t nmemb, FILE *stream)
163{
164  size_t got = fread(ptr, size, nmemb, stream) * size;
165  size_t i;
166
167  for (i=0; i<got; i++)
168  {
169    if ( ((char*) ptr)[i] == '\r')
170    {
171      if (i+1 < got && ((char*) ptr)[i+1] == '\n')
172        ((char*) ptr)[i] = ' ';
173      else
174        ((char*) ptr)[i] = '\n';
175    }
176  }
177  return got;
178}
Note: See TracBrowser for help on using the repository browser.