| 1 |
/*=========================================================================== |
| 2 |
Copyright (C) 1995-2009 European Southern Observatory (ESO) |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of the GNU General Public License as |
| 6 |
published by the Free Software Foundation; either version 2 of |
| 7 |
the License, or (at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public |
| 15 |
License along with this program; if not, write to the Free |
| 16 |
Software Foundation, Inc., 675 Massachusetts Ave, Cambridge, |
| 17 |
MA 02139, USA. |
| 18 |
|
| 19 |
Correspondence concerning ESO-MIDAS should be addressed as follows: |
| 20 |
Internet e-mail: [email protected] |
| 21 |
Postal address: European Southern Observatory |
| 22 |
Data Management Division |
| 23 |
Karl-Schwarzschild-Strasse 2 |
| 24 |
D 85748 Garching bei Muenchen |
| 25 |
GERMANY |
| 26 |
===========================================================================*/ |
| 27 |
|
| 28 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 29 |
.TYPE Module |
| 30 |
.NAME osf |
| 31 |
.LANGUAGE C |
| 32 |
.AUTHOR IPG-ESO Garching |
| 33 |
.CATEGORY Host operating system interfaces. File management. |
| 34 |
.COMMENTS |
| 35 |
File management. These routines provide miscellaneous operations |
| 36 |
on files and directories. The routines use the physical file name |
| 37 |
to access files. |
| 38 |
The routines return a non-negative integer on successful return. |
| 39 |
Otherwise, a value of -1 is set to indicate an error |
| 40 |
condition and the variable ``oserror'' contains the |
| 41 |
system error code. |
| 42 |
Each file has a host independent logical filename |
| 43 |
to be handled by the portable directory mechanism. |
| 44 |
Associated to each file there is a status structure with the |
| 45 |
owner, protection flags and dates of creation and modification. |
| 46 |
.VERSION 0.0 25-Aug-1986 Definition J. D. Ponz |
| 47 |
.VERSION 2.0 19-Feb-1992 osfinfo uses another filestatus structure. CG. |
| 48 |
.VERSION 2.1 29-May-1992 osfcreate set default mask when mode is 0. |
| 49 |
.VERSION 2.2 13-Jul-1992 blocksize removed. SCO/PC does not support it. CG. |
| 50 |
|
| 51 |
090326 last modif |
| 52 |
-----------------------------------------------------------*/ |
| 53 |
|
| 54 |
#include <stdio.h> |
| 55 |
#include <stdlib.h> /* Function definitions */ |
| 56 |
#include <errno.h> |
| 57 |
#include <sys/types.h> |
| 58 |
#include <sys/stat.h> |
| 59 |
#include <fcntl.h> |
| 60 |
#include <string.h> |
| 61 |
#include <osparms.h> |
| 62 |
#include <osfile.h> |
| 63 |
#include <proto_os.h> /* ANSI-C prototyping */ |
| 64 |
|
| 65 |
#define OSFMASK 0666 /* This value is also restricted by umask() */ |
| 66 |
|
| 67 |
int osfcreate(phname, nobyt, mode) |
| 68 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 69 |
.PURPOSE Creates a new file or rewrites an existing one (no version number ). |
| 70 |
Access is done by physical file name. Owner and group identifications |
| 71 |
of the process creating the file define the owner and group |
| 72 |
identification of the file. |
| 73 |
The argument mode gives the file protection code. |
| 74 |
.RETURNS 0 if all o.k. |
| 75 |
on error -1, and the system error code is set |
| 76 |
.REMARKS System dependencies: |
| 77 |
--UNIX: creat(2),write(2),close(2) |
| 78 |
------------------------------------------------------------*/ |
| 79 |
char *phname; /* IN : physical filename */ |
| 80 |
size_t nobyt; /* IN : number of bytes to be allocated */ |
| 81 |
int mode; /* IN : file mode */ |
| 82 |
|
| 83 |
{ |
| 84 |
int fid; |
| 85 |
|
| 86 |
char c = ' '; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
if ((fid = creat(phname,(mode ? mode:OSFMASK) )) < 0) |
| 91 |
{ /* create file failed */ |
| 92 |
oserror = errno; |
| 93 |
return(-1); |
| 94 |
} |
| 95 |
|
| 96 |
if (nobyt > 0) |
| 97 |
{ /* position on last byte of the file */ |
| 98 |
if (osdseek(fid,(nobyt-1),FILE_START) < 1) |
| 99 |
{ |
| 100 |
oserror = errno; |
| 101 |
return(-1); |
| 102 |
} |
| 103 |
|
| 104 |
if (write(fid,&c,1) != 1) |
| 105 |
{ /* write last byte */ |
| 106 |
(void) close(fid); |
| 107 |
oserror = errno; |
| 108 |
return(-1); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
if (close(fid) < 0) |
| 113 |
{ /* close file */ |
| 114 |
oserror = errno; |
| 115 |
return(-1); |
| 116 |
} |
| 117 |
|
| 118 |
return(1); /* return number of bytes ( = 1) */ |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
int osfdelete(phname) |
| 123 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 124 |
.PURPOSE Deletes file on disk by removing the directory entry corresponding |
| 125 |
to the physical name. The file has to be closed. |
| 126 |
.RETURNS Value 0 on normal return, -1 on error and oserror is set. |
| 127 |
.REMARKS System dependencies: |
| 128 |
--UNIX: unlink(2) |
| 129 |
------------------------------------------------------------*/ |
| 130 |
char *phname; /* IN : physical filename */ |
| 131 |
{ |
| 132 |
if (unlink(phname) < 0) { |
| 133 |
oserror = errno; |
| 134 |
return(-1); |
| 135 |
} |
| 136 |
return(0); |
| 137 |
} |
| 138 |
int osfcontrol(phname, function, value1, value2) |
| 139 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 140 |
.PURPOSE Change file protection and/or owner. Access to the |
| 141 |
file is by physical name. The argument function defines the |
| 142 |
operation to be done, value1 is the protection code or the owner |
| 143 |
code and value2 is free or the actual group identification. |
| 144 |
.RETURNS Value 0 on normal return, -1 on error (oserror is set). |
| 145 |
.REMARKS System dependencies: |
| 146 |
--UNIX: chmod(2), chown(2) |
| 147 |
------------------------------------------------------------*/ |
| 148 |
char *phname; /* IN : physical filename */ |
| 149 |
int function; /* IN : function code */ |
| 150 |
int value1; /* IN : protection or owner code */ |
| 151 |
int value2; /* IN : 0 or group code */ |
| 152 |
{ |
| 153 |
|
| 154 |
switch (function) { |
| 155 |
case CHMOD: /* change file protection */ |
| 156 |
if(chmod(phname, value1) < 0) { |
| 157 |
oserror = errno; |
| 158 |
return(-1); |
| 159 |
} |
| 160 |
break; |
| 161 |
case CHOWN: /* change owner and group of the file */ |
| 162 |
if(chown(phname, value1, value2) < 0) { |
| 163 |
oserror = errno; |
| 164 |
return(-1); |
| 165 |
} |
| 166 |
break; |
| 167 |
default: |
| 168 |
oserror = EINVAL; |
| 169 |
return(-1); |
| 170 |
} |
| 171 |
return(0); |
| 172 |
} |
| 173 |
int osfinfo(phname, status) |
| 174 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 175 |
.PURPOSE Get file status information. Access to the file is by physical name. |
| 176 |
.RETURNS Value 0 on normal return, -1 on error (check oserror). |
| 177 |
.REMARKS System dependencies: |
| 178 |
--UNIX: stat(2) |
| 179 |
------------------------------------------------------------*/ |
| 180 |
char *phname; /* IN : physical filename */ |
| 181 |
struct filestatus *status; /* OUT : output file status */ |
| 182 |
{ |
| 183 |
struct stat buf; |
| 184 |
|
| 185 |
/* |
| 186 |
** get file status and return it to the structure ``filestatus'' |
| 187 |
*/ |
| 188 |
if (stat(phname,&buf) == -1) { |
| 189 |
oserror = errno; |
| 190 |
return(-1); |
| 191 |
} |
| 192 |
/* |
| 193 |
** transfer values obtained to structure ``filestatus'' |
| 194 |
*/ |
| 195 |
status->filesize = buf.st_size; |
| 196 |
status->owner = buf.st_uid; |
| 197 |
status->date = buf.st_mtime; |
| 198 |
status->protection = buf.st_mode; |
| 199 |
return(0); |
| 200 |
} |
| 201 |
|
| 202 |
int osfmkdir(phname) |
| 203 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 204 |
.PURPOSE Create a new directory |
| 205 |
.RETURNS Value 0 on normal return, -1 on error (check oserror). |
| 206 |
.REMARKS Use system with mkdir |
| 207 |
------------------------------------------------------------*/ |
| 208 |
char *phname; /* IN : physical directory name */ |
| 209 |
{ |
| 210 |
char *cmd; |
| 211 |
|
| 212 |
cmd = malloc((size_t)(8 + (int)strlen(phname))); |
| 213 |
strcpy(cmd, "mkdir "); |
| 214 |
strcat(cmd, phname); |
| 215 |
oserror = system(cmd); |
| 216 |
free(cmd); |
| 217 |
return(oserror ? -1 : 0); |
| 218 |
} |
| 219 |
|
| 220 |
int osfrename(oldname, newname) |
| 221 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 222 |
.PURPOSE Renames a disk file by giving old and new physical names. |
| 223 |
.RETURNS Value 0 on normal return, -1 on error (check oserror). |
| 224 |
.REMARKS System dependencies: |
| 225 |
--UNIX: link(2), unlink(2) |
| 226 |
------------------------------------------------------------*/ |
| 227 |
char *oldname; /* IN : old physical filename */ |
| 228 |
char *newname; /* IN : new physical filename */ |
| 229 |
|
| 230 |
{ |
| 231 |
|
| 232 |
unlink(newname); /* remove newname if exist */ |
| 233 |
|
| 234 |
if (link(oldname, newname) < 0) |
| 235 |
{ |
| 236 |
char *cmd; |
| 237 |
|
| 238 |
cmd = (char *) malloc((size_t)(8 + (int)strlen(oldname) + (int)strlen(newname))); |
| 239 |
strcpy(cmd,"mv -f "); |
| 240 |
strcat(cmd,oldname); |
| 241 |
strcat(cmd," "); |
| 242 |
strcat(cmd,newname); |
| 243 |
oserror = system(cmd); |
| 244 |
free(cmd); |
| 245 |
return(oserror ? -1 : 0); |
| 246 |
} |
| 247 |
|
| 248 |
if(unlink(oldname) < 0) |
| 249 |
{ |
| 250 |
oserror=errno; |
| 251 |
return(-1); |
| 252 |
} |
| 253 |
|
| 254 |
return(0); |
| 255 |
} |
| 256 |
|
| 257 |
int osftranslate(logname, phname) |
| 258 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 259 |
.PURPOSE Translate logical names into physical names. i.e. $KEYS ==> myfile.key |
| 260 |
.METHOD Searches the system variable list for the logname. |
| 261 |
.RETURNS length of physical name on normal return, |
| 262 |
-1 when logname does not exist (phname empty). |
| 263 |
.REMARKS System dependencies: |
| 264 |
--UNIX: getenv(3). |
| 265 |
------------------------------------------------------------*/ |
| 266 |
char *logname; /* IN : logical name */ |
| 267 |
char *phname; /* OUT: translated physical filename */ |
| 268 |
|
| 269 |
{ |
| 270 |
register int nr; |
| 271 |
|
| 272 |
char *getenv(); |
| 273 |
register char *t, *ptr, cc; |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
/* Do the logical translation */ |
| 278 |
|
| 279 |
if ((t = getenv(logname)) == NULL) |
| 280 |
{ |
| 281 |
*phname = '\0'; |
| 282 |
return(-1); |
| 283 |
} |
| 284 |
|
| 285 |
ptr = phname; |
| 286 |
for (nr=0; ; nr++) /* copy phys. filename */ |
| 287 |
{ |
| 288 |
cc = *t++; |
| 289 |
if (cc == '\0') |
| 290 |
{ |
| 291 |
*ptr = '\0'; |
| 292 |
return (nr); |
| 293 |
} |
| 294 |
*ptr++ = cc; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
int osfphname(logname, phname) |
| 299 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 300 |
.PURPOSE Translate logical names into physical names. i.e. $KEYS ==> myfile.key |
| 301 |
.METHOD Searches the system variable list for the logname. |
| 302 |
.RETURNS Value 0 on normal return, -1 when logname does not exist |
| 303 |
(phname empty). |
| 304 |
.REMARKS System dependencies: |
| 305 |
--UNIX: getenv(3). |
| 306 |
------------------------------------------------------------*/ |
| 307 |
char *logname; /* IN : logical name */ |
| 308 |
char *phname; /* OUT: translated physical filename */ |
| 309 |
{ |
| 310 |
char *getenv(); |
| 311 |
register char *t1; |
| 312 |
|
| 313 |
/* |
| 314 |
** Do the logical translation |
| 315 |
*/ |
| 316 |
if ((t1 = getenv(logname)) == NULL){ |
| 317 |
*phname = '\0'; |
| 318 |
return(-1); |
| 319 |
} |
| 320 |
strcpy(phname,t1); |
| 321 |
return(0); |
| 322 |
} |
| 323 |
int osflgname(phname, logname) |
| 324 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 325 |
.PURPOSE Physical into logical filename translation. |
| 326 |
The logical filename is a structure consisting of three |
| 327 |
character strings to define the logical path (node, device and |
| 328 |
directory), the logical name of the file and the type of file. |
| 329 |
.RETURNS Value 0 on normal return, -1 if structure limits are too small. |
| 330 |
.REMARKS System dependencies: |
| 331 |
--UNIX: none |
| 332 |
------------------------------------------------------------*/ |
| 333 |
char *phname; /* IN : input physical filename */ |
| 334 |
struct lname *logname; /* OUT: translated logical name */ |
| 335 |
{ |
| 336 |
char *locph, *sep; |
| 337 |
int len; |
| 338 |
|
| 339 |
#ifdef NO_STRCHR /* Some pure BSD systems */ |
| 340 |
#define strrchr rindex |
| 341 |
#endif |
| 342 |
|
| 343 |
#ifndef strrchr /* On IBM6000 strchr() is a macro */ |
| 344 |
extern char *strrchr(); |
| 345 |
#endif |
| 346 |
|
| 347 |
locph=phname; |
| 348 |
/* |
| 349 |
** Look for the last occurrence of the separator "/". From the |
| 350 |
** the beginning of the physical name till this separator |
| 351 |
** is the logical path name. |
| 352 |
*/ |
| 353 |
if((sep=strrchr(locph,'/'))!=NULL) { |
| 354 |
if((len=sep-locph)>_LPLEN) { |
| 355 |
oserror = EINVAL; |
| 356 |
return(-1); |
| 357 |
} |
| 358 |
strncpy(logname->lpath,locph,len); |
| 359 |
locph += len+1; |
| 360 |
} |
| 361 |
/* |
| 362 |
** Look for the last occurrence of the separator ".". From this |
| 363 |
** separator till the end of the physical name |
| 364 |
** is the file type. |
| 365 |
*/ |
| 366 |
if((sep=strrchr(locph,'.'))!=NULL) { |
| 367 |
if((len=strlen(sep+1))>_LTLEN) { |
| 368 |
oserror = EINVAL; |
| 369 |
return(-1); |
| 370 |
} |
| 371 |
strncpy(logname->ltype,sep+1,len); |
| 372 |
*sep = '\0'; |
| 373 |
} |
| 374 |
/* |
| 375 |
** Between both separators "/" and "." |
| 376 |
** is the logical file name. |
| 377 |
*/ |
| 378 |
if((len=strlen(locph))>_LFLEN) { |
| 379 |
oserror = EINVAL; |
| 380 |
return(-1); |
| 381 |
} |
| 382 |
strncpy(logname->lfile,locph,len); |
| 383 |
return(0); |
| 384 |
} |