File: stat_tmpl.c

package info (click to toggle)
zlibc 0.9k-4.3
  • links: PTS
  • area: main
  • in suites: buster, sid, stretch
  • size: 624 kB
  • ctags: 461
  • sloc: sh: 3,195; ansic: 3,077; csh: 128; makefile: 60; sed: 4
file content (171 lines) | stat: -rw-r--r-- 3,427 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#ifndef _STAT_VER
#define _STAT_VER 0
#endif

static int STAT(z)(int ver, __const char *name, char *newname, STATTYPE *buf)
{


  int fd,st,ft;
  unsigned char size_buffer[4];

  ft = zlib_getfiletype(name,-1);

  if( (ft & PM_READ_MASK) == PM_LEAVE_COMPR ){
    errno = ENOENT ;
    return -1;
  }

  if ((ft & PM_SIZE_COMPR_MASK) == PM_SIZE_COMPR) {
      STAT(___zlibc_x)(ver,newname,buf);
  } else {
      fd=zlib_real_open(newname, O_RDONLY | O_NDELAY, 0);

      if ( fd < 0 ){
	  if ( errno == ENOENT )
	      return -1;
	  return 2;
      } else {  
	st = fSTAT;
	
	if(ver == _STAT_VER) {
	  /* If we do know the layout of stuct stat, do some smart things
	   * here, else don't bother */
	  if ( st < 0 ){
	    close(fd);
	    return st;
	  }	
	  if ( ! S_ISREG ( buf->st_mode )) {
	    /* not a regular file. Don't waste time determining its size */
	    return st;
	  }

	  lseek(fd, -4 , SEEK_END);
	  read(fd, size_buffer, 4 );

	  buf->st_size = size_buffer[0] + ( size_buffer[1] << 8 ) + 
	    (size_buffer[2] << 16 ) + (size_buffer[3] << 24 );
	}
	close(fd);
      }	  

  }

  if(ver == _STAT_VER) {
    /* If we happen to know the layout of stuct stat, do some smart
     * things here, else don't bother */


    if ((ft & PM_READ_MASK) == PM_SHOW_PIPE) {
      buf->st_mode ^= S_IFIFO | S_IFREG;
    }
    
    if (!(ft & (PM_CREATE_COMPR | PM_APPEND_COMPR | PM_UNCOMPR_BEFORE_WRITE)))
      buf->st_mode &= ~0222;
    
#ifdef NEGATE_INODES
    buf->st_ino= -buf->st_ino;
#endif
  }
  return 0;

}

static int STAT(zlibc_x)(int ver, __const char *file_name, STATTYPE *buf)
{

  int st, olderrno;

  olderrno = errno;
  errno = 0;

  st = STAT(___zlibc_x)(ver, file_name, buf );

  if ( st >= 0 || errno != ENOENT )
    return st;

  {
    char newname[MAXPATHLEN + MAXEXTLEN + 1];
    zlib_initialise();

    if ( zlib_mode & CM_DISAB ) {
      errno = ENOENT;
      return st;
    }

    if ( zlib_mode & CM_VERBOSE )
      fprintf(stderr,"stating %s\n", file_name);

    strncpy(newname,file_name,MAXPATHLEN);
    strcat(newname,zlib_ext);

    st = STAT(z)(ver, file_name,newname, buf);

    if ( zlib_mode & CM_VERBOSE )
      fprintf(stderr,"stated %s, rv=%d\n", file_name, st);

    if ( st == 2 ){
      st = STAT(___zlibc_x)(ver, newname, buf);
      if(ver == _STAT_VER)
	buf->st_size = 0;
    }

    if ( st < 0 ){
      errno = ENOENT;
      return st;
    }

  }

  errno = olderrno;
  return st;

}

static int STAT(zlibc_lx)(int ver, __const char *file_name, STATTYPE *buf)
{
  int st, olderrno;

  olderrno = errno;
  st = STAT(___zlibc_lx)(ver,file_name, buf);

  if ( st >= 0 || errno != ENOENT )
    return st;

  {
    char newname[MAXPATHLEN + MAXEXTLEN + 1];

    zlib_initialise();
    if ( zlib_mode & CM_DISAB ) {
      errno = ENOENT;
      return st;
    }
    if ( zlib_mode & CM_VERBOSE )
      fprintf(stderr,"lstating %s\n", file_name);

    strncpy(newname,file_name,MAXPATHLEN);
    strcat(newname,zlib_ext);

    st = STAT(___zlibc_lx)(ver,newname, buf);

    if ( st < 0 ){
      errno = ENOENT;
      return st;
    }

    if (ver == _STAT_VER && S_ISLNK(buf->st_mode) ){
      if ( (signed int) buf->st_size > zlib_extlen )
	buf->st_size -= zlib_extlen;
      errno = olderrno;
      return st;
    }
     
    st = STAT(z)(ver,file_name,newname,buf);
    if ( st == 2 )
      /* could not zstat it, keep first info */
      st=0;
  }

  errno = olderrno;
  return st;
}