1 //////////////////////////////////////////////////////////////////////////////
2 //Copyright 2008
3 //  Andrew Gacek, Steven Holte, Gopalan Nadathur, Xiaochu Qi, Zach Snow
4 //////////////////////////////////////////////////////////////////////////////
5 // This file is part of Teyjus.                                             //
6 //                                                                          //
7 // Teyjus is free software: you can redistribute it and/or modify           //
8 // it under the terms of the GNU General Public License as published by     //
9 // the Free Software Foundation, either version 3 of the License, or        //
10 // (at your option) any later version.                                      //
11 //                                                                          //
12 // Teyjus is distributed in the hope that it will be useful,                //
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of           //
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
15 // GNU General Public License for more details.                             //
16 //                                                                          //
17 // You should have received a copy of the GNU General Public License        //
18 // along with Teyjus.  If not, see <http://www.gnu.org/licenses/>.          //
19 //////////////////////////////////////////////////////////////////////////////
20 #include <stdlib.h>
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include "../include/standardlib.h"
27 #include "ld_message.h"
28 #include "file.h"
29 #include "loader.h"
30 
31 int fd;
32 
LD_FILE_GetString(char * buffer,int length)33 void LD_FILE_GetString(char* buffer,int length)
34 {
35   read(fd,buffer,length);
36   buffer[length]='\0';
37 }
38 
LD_FILE_Open(char * modname,char * extension)39 void LD_FILE_Open(char* modname, char* extension)
40 {
41   char* buf=(char *)EM_malloc(sizeof(char)*(strlen(modname)+strlen(extension)+1));
42   sprintf(buf,"%s%s",modname,extension);
43 
44 
45   fd=open(buf,O_RDONLY|O_BINARY,0000);
46   if(fd==-1)
47   {
48     perror(buf);
49     EM_THROW(LD_FILE_OpenError);
50   }
51 }
52 
LD_FILE_Close()53 void LD_FILE_Close()
54 {
55   if(-1!=close(fd))
56     EM_THROW(LD_FILE_CloseError);
57 }
58 
LD_FILE_Exists(char * modname,char * extension)59 int LD_FILE_Exists(char* modname, char* extension)
60 {
61   struct stat buf;
62   char* filepath=(char *)EM_malloc(sizeof(char)*(strlen(modname)+strlen(extension)+3));
63   sprintf(filepath,"./%s%s",modname,extension);
64 
65   if(-1==stat(filepath,&buf))
66   {
67     perror(filepath);
68     free(filepath);
69     return 0;
70   }
71 
72   free(filepath);
73   return 1;
74 }
75 
LD_FILE_Link(char * modname)76 void LD_FILE_Link(char* modname)
77 {
78   EM_THROW(LD_FILE_LinkFailure);
79 }
80 
LD_FILE_ModTime(char * modname,char * extension)81 int LD_FILE_ModTime(char* modname, char* extension)
82 {
83   struct stat buf;
84   char* filepath=(char *)EM_malloc(sizeof(char)*(strlen(modname)+strlen(extension)+3));
85   sprintf(filepath,"./%s%s",modname,extension);
86 
87   if(-1==stat(filepath,&buf))
88   {
89     perror(filepath);
90     free(filepath);
91     EM_THROW(LD_FILE_OpenError);
92   }
93 
94   free(filepath);
95   return buf.st_mtime;
96 }
97 
98 
LD_FILE_GETWORD()99 Word LD_FILE_GETWORD()
100 {
101   Word tmp;
102   if(sizeof(tmp)!=read(fd,&tmp,sizeof(tmp)))
103     EM_THROW(LD_FILE_ReadError);
104 #if BYTE_ORDER == LITTLE_ENDIAN
105 # if __WORDSIZE == 32
106   return (Word)bswap_32((unsigned int)tmp);
107 # elif __WORDSIZE == 64
108   return bswap_64(tmp);
109 # endif
110 #elif BYTE_ORDER == BIG_ENDIAN
111   return tmp;
112 #endif
113 }
114 
115 
LD_FILE_GET4()116 int LD_FILE_GET4()
117 {
118   int tmp;
119   read(fd,&tmp,sizeof(tmp));
120 #if BYTE_ORDER == LITTLE_ENDIAN
121   return bswap_32(tmp);
122 #elif BYTE_ORDER == BIG_ENDIAN
123   return tmp;
124 #endif
125 }
126 
127 
LD_FILE_GET2()128 TwoBytes LD_FILE_GET2()
129 {
130   TwoBytes tmp;
131   if(sizeof(tmp)!=read(fd,&tmp,sizeof(tmp)))
132     EM_THROW(LD_FILE_ReadError);
133 #if BYTE_ORDER == LITTLE_ENDIAN
134   return bswap_16(tmp);
135 #elif BYTE_ORDER == BIG_ENDIAN
136   return tmp;
137 #endif
138 }
139 
LD_FILE_GET1()140 Byte LD_FILE_GET1()
141 {
142   Byte tmp;
143   if(sizeof(tmp)!=read(fd,&tmp,sizeof(tmp)))
144     EM_THROW(LD_FILE_ReadError);
145   return tmp;
146 }
147 
148 //#ifdef DEBUG
149 int pfd;
150 
LD_FILE_OpenPipe()151 void LD_FILE_OpenPipe()
152 {
153   int m_pipe[2];
154   if(-1==pipe(m_pipe))
155     EM_THROW(LD_FILE_OpenError);
156   fd=m_pipe[0];
157   pfd=m_pipe[1];
158 }
159 
LD_FILE_PipePUT1(Byte b)160 void LD_FILE_PipePUT1(Byte b)
161 {
162   write(pfd,&b,sizeof(b));
163 }
164 
LD_FILE_PipePUT2(TwoBytes s)165 void LD_FILE_PipePUT2(TwoBytes s)
166 {
167   write(pfd,&s,sizeof(s));
168 }
169 
LD_FILE_PipePUTWORD(Word w)170 void LD_FILE_PipePUTWORD(Word w)
171 {
172   write(pfd,&w,sizeof(w));
173 }
174 
LD_FILE_PipePutString(char * str)175 void LD_FILE_PipePutString(char* str)
176 {
177   char len=strlen(str);
178   write(pfd,&len,sizeof(len));
179   write(pfd,str,len);
180 }
181 
LD_FILE_ClosePipe()182 void LD_FILE_ClosePipe()
183 {
184   close(fd);
185   close(pfd);
186 }
187 
188 //#endif
189