OpenCores
URL https://opencores.org/ocsvn/core_arm/core_arm/trunk

Subversion Repositories core_arm

[/] [core_arm/] [trunk/] [soft/] [sim/] [ti/] [io.c] - Blame information for rev 6

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 tarookumic
/* 2003: Konrad Eisele <eiselekd@web.de> */
2
 
3
#include <stdlib.h>
4
#ifdef UNIX
5
#include <unistd.h>
6
#include <sys/stat.h>
7
#include <strings.h>
8
#endif
9
#ifdef NT
10
#include <io.h>
11
#endif
12
 
13
#include <stdio.h>
14
#include <fcntl.h>
15
 
16
#include "tmki.h"
17
 
18
ti_namelist *ti_nlappend(ti_namelist *nl,const char *s) {
19
  int i;
20
  ti_namelist *n = (ti_namelist *)ti_alloc(sizeof(ti_namelist));
21
  if (!n) return NULL;
22
  nl ->n = n;
23
  i = strlen(s);
24
  if ( ((n ->s) = ti_alloc(i+1))) { strcpy(n ->s,s); }
25
  return n;
26
}
27
 
28
void ti_nlfree(ti_namelist *nl) {
29
  while (nl) {
30
    ti_namelist *n = nl ->n;
31
    if (nl ->s) free(nl ->s);
32
    nl ->s = 0;
33
    free(nl);
34
    nl = n;
35
  }
36
}
37
 
38
/* try all [nl->s]/<p><e> combinations
39
 * nl: list of prefixes
40
 * p: filename
41
 * e: extension
42
 * r: buffer to assemble dest finename
43
 * rc: buffer size
44
 * nr: return filename pointer in <r>
45
 * bin: binary open(NT)
46
 */
47
int ti_open_vp(ti_namelist *nl,const char *d,const char *f,const char *e,char *r,unsigned int rc,char **nr,int bin)
48
{
49
  ti_namelist x;
50
  int fd = -1;
51
  char b[TI_MAXPATH];
52
 
53
  if (d[0] == '/'
54
#ifdef NT
55
      || (d[0] && d[1] == ':' && d[2] == '/')
56
#endif
57
      ) {
58
    x.n = 0;
59
    x.s = b;
60
    b[0] = 0;
61
  }
62
  else {
63
    x.n = nl;
64
    x.s = b;
65
    strncpy(b, d, TI_MAXPATH);
66
    b[TI_MAXPATH-1] = 0;
67
    sys_unbashfilename(b, b);
68
  }
69
  for (nl = &x; nl; nl = nl->n) {
70
      if (strlen(nl->s) + strlen(f) + strlen(e) + 2 > rc)
71
        continue;
72
      strcpy(r, nl->s);
73
      if (r[0] && r[strlen(r)-1] != '/')
74
        strcat(r, "/");
75
      strcat(r, f);
76
      strcat(r, e);
77
      sys_bashfilename(r,r);
78
 
79
      /* see if we can open the file for reading */
80
#ifdef NT
81
      if ((fd=open(r,O_RDONLY | (bin ? _O_BINARY : _O_TEXT))) >= 0)
82
#else
83
      if ((fd=open(r,O_RDONLY )) >= 0)
84
#endif
85
      {
86
        /* in UNIX, further check that it's not a directory */
87
#ifdef UNIX
88
        struct stat statbuf;
89
        int ok =  ((fstat(fd, &statbuf) >= 0) && !S_ISDIR(statbuf.st_mode));
90
        if (!ok) {
91
          ti_print_err("ti_open_vp: %s stat failed or directory",r);
92
          close (fd); fd = -1;
93
        }
94
        else
95
#endif
96
        {
97
          char *slash;
98
          sys_unbashfilename(r, r);
99
          slash = strrchr(r, '/');
100
          if (slash) {
101
              *slash = 0;
102
              *nr = slash + 1;
103
          }
104
          else *nr = r;
105
          return (fd);
106
        }
107
      }
108
      else
109
        ti_print_err("failed to open %s\n", r);
110
  }
111
  *r = 0;
112
  return (-1);
113
}
114
 
115
 

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.