2
* Copyright 2000, International Business Machines Corporation and others.
5
* This software has been released under the terms of the IBM Public
6
* License. For details, see the LICENSE file in the top-level source
7
* directory or online at http://www.openafs.org/dl/license10.html
10
/* File-oriented utility functions */
12
#include <afsconfig.h>
13
#include <afs/param.h>
15
RCSID("$Header: /afs/sipb.mit.edu/project/openafs/debian/cvs/openafs/src/util/fileutil.c,v 1.1.1.6 2001/10/14 18:07:02 hartmans Exp $");
26
#include "errmap_nt.h"
39
#include <sys/types.h>
49
* renamefile() -- rename a file (Unix rename() semantics)
52
renamefile(const char *oldname, const char *newname)
57
if (!MoveFileEx(oldname, newname, MOVEFILE_REPLACE_EXISTING)) {
59
errno = nterr_nt2unix(GetLastError(), EIO);
63
rc = rename(oldname, newname);
70
* FilepathNormalizeEx() -- normalize file path; i.e., use only forward (or only
71
* backward) slashes, remove multiple and trailing slashes.
74
FilepathNormalizeEx(char *path, int slashType)
78
char slash = '/'; /* Default to forward slashes */
80
if (slashType == FPN_BACK_SLASHES)
84
/* use only forward slashes; remove multiple slashes */
85
for (pP = pCopyFrom = path; *pCopyFrom != '\0'; pCopyFrom++) {
86
if ((*pCopyFrom == '/') || (*pCopyFrom == '\\')) {
98
/* strip off trailing slash (unless specifies root) */
100
if ((*pP == slash) && (pP != path)) {
102
/* check for "X:/" */
103
if (*(pP - 1) != ':') {
115
FilepathNormalize(char *path)
117
FilepathNormalizeEx(path, FPN_FORWARD_SLASHES);
120
/* Open a file for buffered I/O */
121
bufio_p BufioOpen(char *path, int oflag, int mode)
125
bp = (bufio_p)malloc(sizeof(bufio_t));
130
bp->fd = _open(path, oflag, mode);
132
bp->fd = open(path, oflag, mode);
134
if (bp->fd == BUFIO_INVALID_FD) {
146
/* Read the next line of a file up to len-1 bytes into buf,
147
* and strip off the carriage return. buf is null terminated.
148
* Returns -1 on EOF or error, length of string on success.
150
int BufioGets(bufio_p bp, char *buf, int buflen)
156
if (!buf || buflen <= 1 || !bp || bp->eof) {
166
rc = _read(bp->fd, bp->buf, BUFIO_BUFSIZE);
168
rc = read(bp->fd, bp->buf, BUFIO_BUFSIZE);
173
} else if (rc == 0) {
193
if (tlen >= buflen-1) {
204
/* Close a buffered I/O handle */
205
int BufioClose(bufio_p bp)