source: git/findexec/feFopen.cc @ 26b713

spielwiese
Last change on this file since 26b713 was 24db42, checked in by Hans Schoenemann <hannes@…>, 11 years ago
fix: bug_tr357 (configure for findexec:getpwnam)
  • Property mode set to 100644
File size: 4.1 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#if defined(HAVE_PWD_H) && defined(HAVE_GETPWNAM)
11#include <pwd.h>
12#endif
13
14//#include <reporter/reporter.h>
15
16#include "feFopen.h"
17#include "feResource.h"
18
19
20
21extern "C" {
22void (*WerrorS_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 ix86_Win
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    if ((stat(path,&statbuf)==0)
100    && (S_ISREG(statbuf.st_mode)))
101      f = myfopen(path,mode);
102  }
103  if (where!=NULL) strcpy(where,path);
104  if ((*mode=='r') &&
105      (path[0]!=DIR_SEP) &&
106      ! (path[0] == '.' && path[1] == DIR_SEP) &&
107      (f==NULL))
108  {
109    char found = 0;
110    char* spath = feResource('s');
111    char *s;
112
113    if (where==NULL) s=(char *)malloc(1024);
114    else             s=where;
115
116    if (spath!=NULL)
117    {
118      char *p,*q;
119      p = spath;
120      while( (q=strchr(p, fePathSep)) != NULL)
121      {
122        *q = '\0';
123        strcpy(s,p);
124        *q = fePathSep;
125        strcat(s, DIR_SEPP);
126        strcat(s, path);
127        if(!access(s, R_OK)) { found++; break; }
128        p = q+1;
129      }
130      if(!found)
131      {
132        strcpy(s,p);
133        strcat(s, DIR_SEPP);
134        strcat(s, path);
135      }
136      f=myfopen(s,mode);
137      if (f!=NULL)
138      {
139        if (where==NULL) free(s);
140        return f;
141      }
142    }
143    else
144    {
145      if (where!=NULL) strcpy(s/*where*/,path);
146      f=myfopen(path,mode);
147    }
148    if (where==NULL) free(s);
149  }
150  if ((f==NULL)&&(useWerror))
151  {
152    char buf[256];
153    strcpy(buf,"cannot open `");
154    strncat(buf,path,240);
155    strcat(buf,"`");
156    WerrorS(buf);
157  }
158  return f;
159}
160
161// Make sure that mode contains binary option
162FILE* myfopen(const char *path, const char *mode)
163{
164#if (defined(CYGWIN) || defined(ix86_Win))
165  char mmode[4];
166  int i;
167  int done = 0;
168
169  for (i=0;;i++)
170  {
171    mmode[i] = mode[i];
172    if (mode[i] == '\0') break;
173    if (mode[i] == 'w') done = 1;
174    if (mode[i] == 'a') done = 1;
175    if (mode[i] == 'b') done = 1;
176  }
177
178  if (! done)
179  {
180    mmode[i] = 'b';
181    mmode[i+1] = '\0';
182  }
183  return fopen(path, mmode);
184#else
185  return fopen(path, mode);
186#endif
187}
188// replace "\r\n" by " \n" and "\r" by "\n"
189
190size_t myfread(void *ptr, size_t size, size_t nmemb, FILE *stream)
191{
192  size_t got = fread(ptr, size, nmemb, stream) * size;
193  size_t i;
194
195  for (i=0; i<got; i++)
196  {
197    if ( ((char*) ptr)[i] == '\r')
198    {
199      if (i+1 < got && ((char*) ptr)[i+1] == '\n')
200        ((char*) ptr)[i] = ' ';
201      else
202        ((char*) ptr)[i] = '\n';
203    }
204  }
205  return got;
206}
Note: See TracBrowser for help on using the repository browser.