version 1.19, 2003/07/28 16:13:53 |
version 1.20, 2003/07/28 18:35:36 |
|
|
#include <paths.h> |
#include <paths.h> |
#include <stdarg.h> |
#include <stdarg.h> |
#include <stdlib.h> |
#include <stdlib.h> |
|
#include <stdio.h> |
#include <string.h> |
#include <string.h> |
#include <unistd.h> |
#include <unistd.h> |
|
|
#include "EXTERN.h" |
|
#include "common.h" |
#include "common.h" |
#include "INTERN.h" |
|
#include "util.h" |
#include "util.h" |
#include "backupfile.h" |
#include "backupfile.h" |
|
|
|
|
/* Rename a file, copying it if necessary. */ |
/* Rename a file, copying it if necessary. */ |
|
|
int |
int |
move_file(char *from, char *to) |
move_file(const char *from, const char *to) |
{ |
{ |
int i, fromfd; |
int fromfd; |
|
ssize_t i; |
|
|
/* to stdout? */ |
/* to stdout? */ |
|
|
|
|
/* Backup the original file. */ |
/* Backup the original file. */ |
|
|
int |
int |
backup_file(char *orig) |
backup_file(const char *orig) |
{ |
{ |
char bakname[MAXPATHLEN], *s, *simplename; |
struct stat filestat; |
dev_t orig_device; |
char bakname[MAXPATHLEN], *s, *simplename; |
ino_t orig_inode; |
dev_t orig_device; |
|
ino_t orig_inode; |
|
|
if (backup_type == none || stat(orig, &filestat) != 0) |
if (backup_type == none || stat(orig, &filestat) != 0) |
return 0; /* nothing to do */ |
return 0; /* nothing to do */ |
|
|
* Copy a file. |
* Copy a file. |
*/ |
*/ |
int |
int |
copy_file(char *from, char *to) |
copy_file(const char *from, const char *to) |
{ |
{ |
int tofd, fromfd, i; |
int tofd, fromfd; |
|
ssize_t i; |
|
|
tofd = open(to, O_CREAT|O_TRUNC|O_WRONLY, 0666); |
tofd = open(to, O_CREAT|O_TRUNC|O_WRONLY, 0666); |
if (tofd < 0) |
if (tofd < 0) |
|
|
* Allocate a unique area for a string. |
* Allocate a unique area for a string. |
*/ |
*/ |
char * |
char * |
savestr(char *s) |
savestr(const char *s) |
{ |
{ |
char *rv, *t; |
char *rv; |
|
|
if (!s) |
if (!s) |
s = "Oops"; |
s = "Oops"; |
t = s; |
rv = strdup(s); |
while (*t++) |
|
; |
|
rv = malloc((size_t) (t - s)); |
|
if (rv == NULL) { |
if (rv == NULL) { |
if (using_plan_a) |
if (using_plan_a) |
out_of_mem = TRUE; |
out_of_mem = TRUE; |
else |
else |
fatal("out of memory\n"); |
fatal("out of memory\n"); |
} else { |
|
t = rv; |
|
while ((*t++ = *s++)) |
|
; |
|
} |
} |
return rv; |
return rv; |
} |
} |
|
|
* Vanilla terminal output (buffered). |
* Vanilla terminal output (buffered). |
*/ |
*/ |
void |
void |
say(char *fmt, ...) |
say(const char *fmt, ...) |
{ |
{ |
va_list ap; |
va_list ap; |
|
|
|
|
* Terminal output, pun intended. |
* Terminal output, pun intended. |
*/ |
*/ |
void |
void |
fatal(char *fmt, ...) |
fatal(const char *fmt, ...) |
{ |
{ |
va_list ap; |
va_list ap; |
|
|
|
|
* Say something from patch, something from the system, then silence . . . |
* Say something from patch, something from the system, then silence . . . |
*/ |
*/ |
void |
void |
pfatal(char *fmt, ...) |
pfatal(const char *fmt, ...) |
{ |
{ |
va_list ap; |
va_list ap; |
int errnum = errno; |
int errnum = errno; |
|
|
* Get a response from the user, somehow or other. |
* Get a response from the user, somehow or other. |
*/ |
*/ |
void |
void |
ask(char *fmt, ...) |
ask(const char *fmt, ...) |
{ |
{ |
va_list ap; |
va_list ap; |
int ttyfd, r; |
int ttyfd; |
|
ssize_t r; |
bool tty2 = isatty(2); |
bool tty2 = isatty(2); |
|
|
va_start(ap, fmt); |
va_start(ap, fmt); |
|
|
*/ |
*/ |
|
|
void |
void |
makedirs(char *filename, bool striplast) |
makedirs(const char *filename, bool striplast) |
{ |
{ |
char *tmpbuf; |
char *tmpbuf; |
|
|
|
|
* Make filenames more reasonable. |
* Make filenames more reasonable. |
*/ |
*/ |
char * |
char * |
fetchname(char *at, int strip_leading, int assume_exists) |
fetchname(const char *at, int strip_leading, int assume_exists) |
{ |
{ |
char *fullname, *name, *t, tmpbuf[200]; |
char *fullname, *name, *t, tmpbuf[200]; |
int sleading = strip_leading; |
int sleading = strip_leading; |
|
struct stat filestat; |
|
|
if (!at || *at == '\0') |
if (at == NULL || *at == '\0') |
return NULL; |
return NULL; |
while (isspace(*at)) |
while (isspace(*at)) |
at++; |
at++; |