source: git/resource/feFopen.cc @ 5df856

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