00001
00002
00003
00004
00005
00006 #include <stdio.h>
00007 #include <string.h>
00008 #include <stdlib.h>
00009 #include <sys/types.h>
00010 #include <sys/stat.h>
00011 #include <fcntl.h>
00012 #include <unistd.h>
00013 #include <pthread.h>
00014
00015 #include "libjio.h"
00016 #include "common.h"
00017 #include "trans.h"
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 struct jfs *jfopen(const char *path, const char *mode)
00033 {
00034 int flags;
00035 int pos_at_the_beginning;
00036 struct jfs *fs;
00037
00038 if (strlen(mode) < 1)
00039 return NULL;
00040
00041 if (mode[0] == 'r') {
00042 pos_at_the_beginning = 1;
00043 if (strlen(mode) > 1 && strchr(mode, '+'))
00044 flags = O_RDWR;
00045 else
00046 flags = O_RDONLY;
00047 } else if (mode[0] == 'a') {
00048 if (strlen(mode) > 1 && strchr(mode, '+'))
00049 pos_at_the_beginning = 1;
00050 else
00051 pos_at_the_beginning = 0;
00052 flags = O_RDWR | O_CREAT | O_APPEND;
00053 } else if (mode[0] == 'w') {
00054 pos_at_the_beginning = 1;
00055 flags = O_RDWR | O_CREAT | O_TRUNC;
00056 } else {
00057 return NULL;
00058 }
00059
00060 fs = jopen(path, flags, 0666, 0);
00061 if (fs == NULL)
00062 return NULL;
00063
00064 if (pos_at_the_beginning)
00065 lseek(fs->fd, 0, SEEK_SET);
00066 else
00067 lseek(fs->fd, 0, SEEK_END);
00068
00069 return fs;
00070 }
00071
00072
00073 int jfclose(struct jfs *stream)
00074 {
00075 int rv;
00076 rv = jclose(stream);
00077 free(stream);
00078
00079 if (rv == 0)
00080 return 0;
00081 else
00082 return EOF;
00083 }
00084
00085
00086 struct jfs *jfreopen(const char *path, const char *mode, struct jfs *stream)
00087 {
00088 if (stream)
00089 jfclose(stream);
00090
00091 stream = jfopen(path, mode);
00092 return stream;
00093 }
00094
00095
00096 size_t jfread(void *ptr, size_t size, size_t nmemb, struct jfs *stream)
00097 {
00098 int rv;
00099 rv = jread(stream, ptr, size * nmemb);
00100
00101 if (rv <= 0)
00102 return 0;
00103
00104 return rv / size;
00105 }
00106
00107
00108 size_t jfwrite(const void *ptr, size_t size, size_t nmemb, struct jfs *stream)
00109 {
00110 int rv;
00111 rv = jwrite(stream, ptr, size * nmemb);
00112
00113 if (rv <= 0)
00114 return 0;
00115
00116 return rv / size;
00117 }
00118
00119
00120 int jfileno(struct jfs *stream)
00121 {
00122 return stream->fd;
00123 }
00124
00125
00126 int jfeof(struct jfs *stream)
00127 {
00128
00129
00130
00131
00132
00133
00134 off_t curpos, endpos;
00135
00136 pthread_mutex_lock(&(stream->lock));
00137
00138 curpos = lseek(jfileno(stream), 0, SEEK_CUR);
00139 endpos = lseek(jfileno(stream), 0, SEEK_END);
00140
00141 lseek(jfileno(stream), curpos, SEEK_SET);
00142
00143 pthread_mutex_unlock(&(stream->lock));
00144
00145 if (curpos >= endpos)
00146 return 1;
00147 else
00148 return 0;
00149 }
00150
00151
00152 void jclearerr(struct jfs *stream)
00153 {
00154
00155
00156 }
00157
00158
00159 int jferror(struct jfs *stream)
00160 {
00161
00162
00163 return 0;
00164 }
00165
00166
00167 int jfseek(struct jfs *stream, long offset, int whence)
00168 {
00169 off_t pos;
00170
00171 pthread_mutex_lock(&(stream->lock));
00172 pos = lseek(stream->fd, offset, whence);
00173 pthread_mutex_unlock(&(stream->lock));
00174
00175
00176 if (pos == -1)
00177 return 1;
00178
00179 return 0;
00180 }
00181
00182
00183 long jftell(struct jfs *stream)
00184 {
00185
00186 return (long) lseek(stream->fd, 0, SEEK_CUR);
00187 }
00188
00189
00190 void jrewind(struct jfs *stream)
00191 {
00192 lseek(stream->fd, 0, SEEK_SET);
00193 }
00194
00195
00196
00197
00198 FILE *jfsopen(struct jfs *stream, const char *mode)
00199 {
00200 return fdopen(stream->fd, mode);
00201 }
00202