source: git/resource/feFopen.cc @ b9b41a

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