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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
/************************************************************************/
/* DIO -- Disk I/O routines for a Unix Enviromment. */
/* */
/* Makes calls to the UNIX I/O and directory searching routines. */
/* All of these get implemented in a pretty straight forward way. */
/* */
/* Portability Notes: */
/* These routines are intended to run on BSD UNIX and UNICOS. No */
/* attempt has been made to make them any more portable than this. */
/* There are some minor differences between the two, which are */
/* selectively compiled depending if BSD is defined. */
/* 1. The mkdir system service is not present on some systems, and */
/* may require superuser priveleges to implement using mknod. */
/* In this case, use 'popen("mkdir ...","r",...)' */
/* 2. The Berkeley directory searching routines are used. These */
/* can be relatively simply implemented in other UNIX's. */
/* */
/* History: */
/* dakr-ages rjs Original version adapted from werong. */
/* 31-oct-89 pjt _trace_ added as defined() option, errno */
/* -nov-89 rjs dexpand_c routine */
/* 6-dec-89 pjt extended bug call */
/* 26-jan-90 rjs Reincluded <stdio.h>, which is needed by Unicos. */
/* 27-apr-90 rjs Added ddelete_c routine. */
/* 26-aug-93 rjs Added hrmdir. */
/* 5-nov-94 rjs Improve POSIX compliance. */
/* 26-Oct-95 rjs Honour TMPDIR environment variable, if set. */
/* 10-Jan-96 rjs Make sure scratch file names are unique. */
/* 17-jun-02 pjt MIR4 changes, and proper prototypes */
/* 5-nov-04 jwr Changed a few size_t to ssize_t or off_t */
/* 3-jan-05 pjt ssize casting to appease the compiler */
/* use SSIZE_MAX to protect from bad casting ? */
/* 2-mar-05 pjt template->templat for C++, just in case */
/* 02-dec-11 pkgw Fix semantics of I/O syscalls in dread, dwrite */
/************************************************************************/
#if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H
#include "config.h"
#endif
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#define direct dirent
#include <stdio.h>
#include <errno.h>
#include "miriad.h"
#define MAXPATH 128
#ifndef NULL
# define NULL 0
#endif
#define Malloc(x) malloc((unsigned)(x))
#define Strcat (void)strcat
#define Strcpy (void)strcpy
#define Lseek(a,b,c) (off_t)lseek(a,(off_t)(b),c)
struct dent {
char path[MAXPATH];
DIR *dir;
};
/************************************************************************/
void ddelete_c(char *path,int *iostat)
/*
This deletes a file, and returns an i/o status.
------------------------------------------------------------------------*/
{
*iostat = ( unlink(path) ? errno : 0 );
}
/************************************************************************/
void dtrans_c(char *inpath,char *outpath,int *iostat)
/*
Translate a directory spec into the local format. On a UNIX machine,
this merely involves adding a slash to the end of the name.
Input:
inpath Input directory spec.
Output:
outpath Output directory spec.
iostat Error return.
------------------------------------------------------------------------*/
{
char *s;
*iostat = 0;
Strcpy(outpath,inpath);
s = outpath + strlen(outpath) - 1;
if(*s != '/')Strcat(outpath,"/");
}
/************************************************************************/
void dmkdir_c(char *path,int *iostat)
/*
Create a directory. This might be a privileged operation on some systems,
in which case dmkdir_c will have to work by using popen(3) and mkdir(1).
Input:
path Name of directory to create. This will usually have a
trailing slash, which needs to be trimmed off.
Output:
iostat Errror status.
------------------------------------------------------------------------*/
{
char Path[MAXPATH],*s;
/* Usually the path will end in a '/', so get rid of it. */
Strcpy(Path,path);
s = Path + strlen(Path) - 1;
if(*s == '/')*s = 0;
*iostat = 0;
if(mkdir(Path,0777) < 0) *iostat = errno;
}
/************************************************************************/
void drmdir_c(char *path,int *iostat)
/*
Delete a directory. This might be a privileged operation on some systems,
in which case drmdir_c will have to work by using popen(3) and rmdir(1).
Input:
path Name of directory to remove. This will usually have a
trailing slash, which needs to be trimmed off.
Output:
iostat Errror status.
------------------------------------------------------------------------*/
{
char Path[MAXPATH],*s;
/* Usually the path will end in a '/', so get rid of it. */
Strcpy(Path,path);
s = Path + strlen(Path) - 1;
if(*s == '/')*s = 0;
*iostat = 0;
if(rmdir(Path) < 0) *iostat = errno;
}
/************************************************************************/
void dopen_c(int *fd,char *name,char *status,off_t *size,int *iostat)
/*
Open a file.
Input:
name Name of file to create (in host format).
status Either "read", "write", "append" or "scratch".
"scratch" files are using $TMPDIR, if present, else current.
Output:
fd File descriptor.
size Size of file.
iostat I/O status.
------------------------------------------------------------------------*/
{
int is_scratch,pid,flags=0;
char *s,sname[MAXPATH];
is_scratch = *iostat = 0;
s = name;
if (!strcmp(status,"read")) flags = O_RDONLY;
else if(!strcmp(status,"write")) flags = O_CREAT|O_TRUNC|O_RDWR;
else if(!strcmp(status,"append")) flags = O_CREAT|O_RDWR;
else if(!strcmp(status,"scratch")){
flags = O_CREAT|O_TRUNC|O_RDWR;
is_scratch = 1;
s = getenv("TMPDIR");
pid = getpid();
if(s != NULL)sprintf(sname,"%s/%s.%d",s,name,pid);
else sprintf(sname,"%s.%d",name,pid);
s = sname;
} else bug_c('f',"dopen_c: Unrecognised status");
#ifdef O_LARGEFILE
flags |= O_LARGEFILE;
#endif
if((*fd = open(s,flags,0644)) < 0){*iostat = errno; return;}
*size = Lseek(*fd,0,SEEK_END);
/* If its a scratch file, unlink it now, so that the file will disappear
when it is closed (or this program crashes). */
if(is_scratch)(void)unlink(s);
}
/************************************************************************/
void dclose_c(int fd,int *iostat)
/*
This subroutine does unbelievably complex stuff.
------------------------------------------------------------------------*/
{
*iostat = ( close(fd) < 0 ? errno : 0 );
}
/************************************************************************/
void dread_c(int fd, char *buffer,off_t offset,size_t length,int *iostat)
/*
Read from a file.
------------------------------------------------------------------------*/
{
ssize_t nread;
#ifdef debug
if (length >= SSIZE_MAX) bugv_c('f',"dread_c: possible incomplete read");
#endif
if(Lseek(fd,offset,SEEK_SET) < 0) { *iostat = errno; return; }
while (length) {
nread = read(fd,buffer,length);
if(nread < 0) {
if(errno == EINTR)
nread = 0; /* should reattempt the system call identically */
else {
*iostat = errno;
return;
}
} else if(nread == 0) {
/* unexpected EOF -- no good errno code for this */
*iostat = EIO;
return;
}
length -= nread;
}
}
/************************************************************************/
void dwrite_c(int fd, char *buffer,off_t offset,size_t length,int *iostat)
/*
Write to a file.
------------------------------------------------------------------------*/
{
ssize_t nwrite;
#ifdef debug
if (length >= SSIZE_MAX) bugv_c('f',"dwrite_c: possible incomplete write");
#endif
if(Lseek(fd,offset,SEEK_SET) < 0) { *iostat = errno; return; }
while (length) {
nwrite = write(fd,buffer,length);
if(nwrite < 0) {
if(errno == EINTR)
nwrite = 0; /* should reattempt the system call identically */
else {
*iostat = errno;
return;
}
}
length -= nwrite;
}
}
/************************************************************************/
/*ARGSUSED*/
void dwait_c(int fd,int *iostat)
/*
This nominally waits for i/o to a file to finish. Things work synchronously
in UNIX.
------------------------------------------------------------------------*/
{
*iostat = 0;
}
/************************************************************************/
int dexpand_c(char *templat,char *output,int length)
/*
This expands wildcards, matching them with files.
Input:
templat The input character string, containing the wildcards.
length The length of the output buffer.
Output:
output All the files matching "template". Filenames are separated
by commas.
------------------------------------------------------------------------*/
{
FILE *fd;
char line[MAXPATH],*s;
int l;
Strcpy(line,"echo ");
Strcat(line,templat);
fd = popen(line,"r");
if(fd == NULL) return(-1);
s = output;
while(fgets(s,length,fd)){
l = strlen(s);
if( length-l <= 1 ){(void)pclose(fd); return(-1);}
*(s+l-1) = ',';
s += l;
length -= l;
}
if(s != output) *--s = 0;
(void)pclose(fd);
return(s-output);
}
/************************************************************************/
void dopendir_c(char **contxt,char *path)
/*
Open a directory, and prepare to read from it.
------------------------------------------------------------------------*/
{
struct dent *d;
*contxt = Malloc(sizeof(struct dent));
d = (struct dent *)*contxt;
Strcpy(d->path,path);
d->dir = opendir(path);
}
/************************************************************************/
void dclosedir_c(char *contxt)
/*
Close a directory.
------------------------------------------------------------------------*/
{
struct dent *d;
d = (struct dent *)contxt;
(void)closedir(d->dir);
free(contxt);
}
/************************************************************************/
/*ARGSUSED*/
void dreaddir_c(char *contxt,char *path,int length)
/*
Read a directory entry.
------------------------------------------------------------------------*/
{
struct dent *d;
struct direct *dp;
struct stat buf;
char npath[MAXPATH];
d = (struct dent *)contxt;
do dp = readdir(d->dir);
while(dp != NULL && (!strcmp(dp->d_name,".") || !strcmp(dp->d_name,"..")));
if(dp == NULL)
*path = 0;
else{
Strcpy(path,dp->d_name);
Strcpy(npath,d->path); Strcat(npath,path);
(void)stat(npath,&buf);
if(S_IFDIR & buf.st_mode)Strcat(path,"/");
}
}
|