summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorStef Walter <stef@thewalter.net>2004-04-03 22:13:01 +0000
committerStef Walter <stef@thewalter.net>2004-04-03 22:13:01 +0000
commitbec23685136f78a24a78a6d8840d3865ffbccea9 (patch)
tree1453e0c5f87bb689a3df9c8e524d05d65bcca657 /src
parent3eca787a2b83ff8cf60d2be9e588d4a558b85a85 (diff)
Unix porting.
Diffstat (limited to 'src')
-rw-r--r--src/compat.c59
-rw-r--r--src/compat.h141
-rw-r--r--src/drive.h13
-rw-r--r--src/list.c11
-rw-r--r--src/main.c8
-rw-r--r--src/ntfs.h10
-rw-r--r--src/ntfsx.c4
-rw-r--r--src/posix.c16
-rw-r--r--src/scrounge.c22
-rw-r--r--src/scrounge.h2
-rw-r--r--src/unicode.c6
-rw-r--r--src/usuals.h8
12 files changed, 190 insertions, 110 deletions
diff --git a/src/compat.c b/src/compat.c
index 14b85e4..171f551 100644
--- a/src/compat.c
+++ b/src/compat.c
@@ -87,7 +87,20 @@ int getopt(int nargc, char* const* nargv, const char* ostr)
return (-1);
}
- if (place[1] && *++place == '-') /* found "--" */
+ if (place[1] && *++place == '-') #include <sys/types.h>
+
+ void *malloc ();
+
+ /* Allocate an N-byte block of memory from the heap.
+ If N is zero, allocate a 1-byte block. */
+
+ void *
+ rpl_malloc (size_t n)
+ {
+ if (n == 0)
+ n = 1;
+ return malloc (n);
+ }/* found "--" */
{
++optind;
place = EMSG;
@@ -138,7 +151,20 @@ int getopt(int nargc, char* const* nargv, const char* ostr)
return (optopt); /* dump back option letter */
}
#endif
+ #include <sys/types.h>
+
+ void *malloc ();
+
+ /* Allocate an N-byte block of memory from the heap.
+ If N is zero, allocate a 1-byte block. */
+ void *
+ rpl_malloc (size_t n)
+ {
+ if (n == 0)
+ n = 1;
+ return malloc (n);
+ }
#ifndef HAVE_ERR_H
@@ -344,18 +370,18 @@ wchar_t* itow(int val, wchar_t* out, int radix)
#endif
#ifndef HAVE_ITOA
-wchar_t* itow(int val, wchar_t* out, int radix)
+char* itoa(int val, char* out, int radix)
{
int mod;
- wchar_t temp;
- wchar_t* end = out;
- wchar_t* beg = out;
+ char temp;
+ char* end = out;
+ char* beg = out;
if(val != 0)
{
/* If negative and decimal*/
if(radix == 10 && val < 0)
- *beg++ = L'-';
+ *beg++ = '-';
/* Convert in reverse order */
while(val != 0)
@@ -363,7 +389,7 @@ wchar_t* itow(int val, wchar_t* out, int radix)
mod = val % radix;
val = val / radix;
- *end++ = (mod < 10) ? L'0' + mod : L'a' + mod - 10;
+ *end++ = (mod < 10) ? '0' + mod : 'a' + mod - 10;
}
*end-- = 0;
@@ -380,10 +406,25 @@ wchar_t* itow(int val, wchar_t* out, int radix)
}
else
{
- beg[0] = L'0';
+ beg[0] = '0';
beg[1] = 0;
}
return out;
}
-#endif \ No newline at end of file
+#endif
+
+#if HAVE_MALLOC == 0
+#undef malloc
+
+/* Allocate an N-byte block of memory from the heap.
+ If N is zero, allocate a 1-byte block. */
+
+void* rpl_malloc (size_t n)
+{
+ if (n == 0)
+ n = 1;
+ return malloc (n);
+}
+
+#endif
diff --git a/src/compat.h b/src/compat.h
index 8f75ef2..0047991 100644
--- a/src/compat.h
+++ b/src/compat.h
@@ -29,6 +29,8 @@
#endif
#endif
+#include <sys/types.h>
+
#ifndef MAX_PATH
#ifdef _MAX_PATH
#define MAX_PATH _MAX_PATH
@@ -75,106 +77,82 @@ typedef unsigned char bool;
#ifndef HAVE_BYTE
typedef unsigned char byte;
-#define HAVE_BYTE
#endif
#ifndef HAVE_UINT
typedef unsigned int uint;
-#define HAVE_UINT
#endif
+#include <stdint.h>
+
#ifndef HAVE_UINT64
- #ifdef _WIN32
- typedef unsigned __int64 uint64;
+ #ifdef HAVE_UINT64_T
+ typedef uint64_t uint64;
#else
- #ifdef HAVE_64BITLONGLONG
- typedef unsigned long long uint64;
+ #ifdef _WIN32
+ typedef unsigned __int64 uint64;
#else
- #ifdef HAVE_64BITLONG
- typedef unsigned long uint64;
- #else
- #error ERROR: Must have a compiler that can handle 64 bit integers
- #endif
+ #error ERROR: Must have a compiler that can handle 64 bit integers
#endif
#endif
#endif
-#ifndef HAVE_UINT32
- #ifdef _WIN32
- typedef unsigned __int32 uint32;
+#ifndef HAVE_INT64
+ #ifdef HAVE_INT64_T
+ typedef int64_t int64;
#else
- #ifdef HAVE_32BITLONG
- typedef unsigned long uint32;
+ #ifdef _WIN32
+ typedef signed __int64 int64;
#else
- #ifdef HAVE_32BITINT
- typedef unsigned int uint32;
- #else
- #error ERROR: Couldn't find a 32 bit integer to use
- #endif
+ #error ERROR: Must have a compiler that can handle 64 bit integers
#endif
#endif
#endif
-#ifndef HAVE_UINT16
- #ifdef _WIN32
- typedef unsigned __int16 uint16;
+#ifndef HAVE_UINT32
+ #ifdef HAVE_UINT32_T
+ typedef uint32_t uint32;
#else
- #ifdef HAVE_16BITSHORT
- typedef unsigned short uint16;
+ #ifdef _WIN32
+ typedef unsigned __int32 uint32;
#else
- #ifdef HAVE_16BITINT
- typedef unsigned int uint16;
- #else
- #error ERROR: Couldn't find a 16 bit integer to use
- #endif
+ #error ERROR: Couldnt determine a 32 bit integer
#endif
#endif
#endif
-#ifndef HAVE_INT64
- #ifdef _WIN32
- typedef signed __int64 int64;
+#ifndef HAVE_INT32
+ #ifdef HAVE_INT32_T
+ typedef int32_t int32;
#else
- #ifdef HAVE_64BITLONGLONG
- typedef signed long long int64;
+ #ifdef _WIN32
+ typedef signed __int32 int32;
#else
- #ifdef HAVE_64BITLONG
- typedef signed long int64;
- #else
- #error ERROR: Must have a compiler that can handle 64 bit integers
- #endif
+ #error ERROR: Couldnt determine a 32 bit integer
#endif
#endif
#endif
-#ifndef HAVE_INT32
- #ifdef _WIN32
- typedef signed __int32 int32;
+#ifndef HAVE_UINT16
+ #ifdef HAVE_UINT16_T
+ typedef uint16_t uint16;
#else
- #ifdef HAVE_32BITLONG
- typedef signed long int32;
+ #ifdef _WIN32
+ typedef unsigned __int16 uint16;
#else
- #ifdef HAVE_32BITINT
- typedef signed int int32;
- #else
- #error ERROR: Couldn't find a 32 bit integer to use
- #endif
+ #error ERROR: Couldnt determine a 16 bit integer
#endif
#endif
#endif
#ifndef HAVE_INT16
- #ifdef _WIN32
- typedef signed __int16 int16;
+ #ifdef HAVE_INT16_T
+ typedef int16_t int16;
#else
- #ifdef HAVE_16BITSHORT
- typedef signed short int16;
+ #ifdef _WIN32
+ typedef signed __int16 int16;
#else
- #ifdef HAVE_16BITINT
- typedef signed int int16;
- #else
- #error ERROR: Couldn't find a 16 bit integer to use
- #endif
+ #error ERROR: Couldnt determine a 16 bit integer
#endif
#endif
#endif
@@ -196,7 +174,20 @@ void err_set_file(void *fp);
void err_set_exit(void (*ef)(int));
void err(int eval, const char *fmt, ...);
void verr(int eval, const char *fmt, va_list ap);
-void errc(int eval, int code, const char *fmt, ...);
+void errc(int eval, int c #include <sys/types.h>
+
+ void *malloc ();
+
+ /* Allocate an N-byte block of memory from the heap.
+ If N is zero, allocate a 1-byte block. */
+
+ void *
+ rpl_malloc (size_t n)
+ {
+ if (n == 0)
+ n = 1;
+ return malloc (n);
+ }ode, const char *fmt, ...);
void verrc(int eval, int code, const char *fmt, va_list ap);
void errx(int eval, const char *fmt, ...);
void verrx(int eval, const char *fmt, va_list ap);
@@ -213,13 +204,14 @@ void* reallocf(void* ptr, size_t size);
#endif
/* Some number conversion stuff */
+#include <wchar.h>
#ifndef HAVE_ITOW
#ifdef _WIN32
#define itow _itow
#define HAVE_ITOW 1
#else
- wchar_t itow(int v, wchar_t* s, int r);
+ wchar_t* itow(int v, wchar_t* s, int r);
#endif
#endif
@@ -228,7 +220,7 @@ void* reallocf(void* ptr, size_t size);
#define itoa _itoa
#define HAVE_ITOA 1
#else
- char itoa(int v, char* s, int r);
+ char* itoa(int v, char* s, int r);
#endif
#endif
@@ -316,6 +308,7 @@ void* reallocf(void* ptr, size_t size);
#define fc_getcwd getcwd
#define fcscpy strcpy
+ #define fcsncpy strncpy
#define fcscat strcat
#define fcslen strlen
#define fcscmp strcmp
@@ -333,7 +326,7 @@ void* reallocf(void* ptr, size_t size);
#ifdef _WIN32
#define lseek64 _lseeki64
#else
- #ifdef HAVE_64BITOFFT
+ #if ( SIZEOF_OFF_T == 8 )
#define lseek64 lseek
#else
#error ERROR: Must have a working 64 bit seek function
@@ -341,10 +334,28 @@ void* reallocf(void* ptr, size_t size);
#endif
#endif
-#include <fnctl.h>
+#include <fcntl.h>
#ifdef O_LARGEFILE
#define OPEN_LARGE_OPTS O_LARGEFILE
#else
#define OPEN_LARGE_OPTS 0
#endif
+#ifndef O_BINARY
+ #ifdef _O_BINARY
+ #define O_BINARY _O_BINARY
+ #else
+ #define O_BINARY 0
+ #endif
+#endif
+
+#ifndef O_RDONLY
+ #ifdef _O_RDONLY
+ #define O_RDONLY _O_RDONLY
+ #else
+ #error ERROR: No O_RDONLY flag found
+ #endif
+#endif
+
+
+#endif /* _COMPAT_H_ */
diff --git a/src/drive.h b/src/drive.h
index 22e7d2d..fdfc0eb 100644
--- a/src/drive.h
+++ b/src/drive.h
@@ -23,10 +23,10 @@
#include "usuals.h"
#define kSectorSize 0x200
-#define kInvalidSector 0xFFFFFFFFFFFFFFFF
+#define kInvalidSector 0xFFFFFFFFFFFFFFFFLL
-typedef struct _ntfsx_mftmap ntfsx_mftmap;
-typedef struct _drivelocks drivelocks;
+struct _ntfsx_mftmap;
+struct _drivelocks;
typedef struct _partitioninfo
{
@@ -37,12 +37,11 @@ typedef struct _partitioninfo
int device; /* A handle to an open device */
/* Some other context stuff about the drive */
- drivelocks* locks;
- ntfsx_mftmap* mftmap;
+ struct _drivelocks* locks;
+ struct _ntfsx_mftmap* mftmap;
}
partitioninfo;
-#pragma pack(push, drive)
#pragma pack(1)
#define kPartition_Invalid 0
@@ -77,7 +76,7 @@ typedef struct _drive_mbr
}
drive_mbr;
-#pragma pack(pop, drive)
+#pragma pack()
#define CLUSTER_TO_SECTOR(info, clus) (((clus) * (info).cluster) + (info).first)
#define SECTOR_TO_BYTES(sec) ((sec) * kSectorSize)
diff --git a/src/list.c b/src/list.c
index 97ac912..fc30ac0 100644
--- a/src/list.c
+++ b/src/list.c
@@ -99,7 +99,7 @@ int printPartitionInfo(int dd, uint64 tblSector)
return 0;
}
-
+#ifdef _WIN32
void scroungeList()
{
char driveName[MAX_PATH];
@@ -113,7 +113,7 @@ void scroungeList()
{
makeDriveName(driveName, i);
- dd = open(driveName, _O_BINARY | _O_RDONLY | OPEN_LARGE_OPTS);
+ dd = open(driveName, O_BINARY | O_RDONLY | OPEN_LARGE_OPTS);
if(dd != -1)
{
printf(kPrintDrive, i);
@@ -122,14 +122,15 @@ void scroungeList()
}
}
}
+#endif
void scroungeListDrive(char* drive)
{
- int dd = open(driveName, _O_BINARY | _O_RDONLY | OPEN_LARGE_OPTS;
+ int dd = open(drive, O_BINARY | O_RDONLY | OPEN_LARGE_OPTS);
if(dd == -1)
- err(1, "couldn't open drive: %s", driveName);
+ err(1, "couldn't open drive: %s", drive);
- printf(kPrintDrivePath, driveName);
+ printf(kPrintDrivePath, drive);
printPartitionInfo(dd, 0);
close(dd);
}
diff --git a/src/main.c b/src/main.c
index b6156d0..9f6e4a2 100644
--- a/src/main.c
+++ b/src/main.c
@@ -77,7 +77,7 @@ int main(int argc, char* argv[])
int mode = 0;
int raw = 0;
partitioninfo pi;
- char driveName[MAX_PATH];
+ char driveName[MAX_PATH + 1];
#ifdef _WIN32
int drive = 0;
#endif
@@ -191,7 +191,9 @@ int main(int argc, char* argv[])
if(argc < 1)
errx(2, "must specify drive name");
- driveName = argv[0];
+ strncpy(driveName, argv[0], MAX_PATH);
+ driveName[MAX_PATH] = 0;
+
argv++;
argc--;
#endif
@@ -220,7 +222,7 @@ int main(int argc, char* argv[])
pi.end = temp;
/* Open the device */
- pi.device = open(driveName, _O_BINARY | _O_RDONLY | OPEN_LARGE_OPTS);
+ pi.device = open(driveName, O_BINARY | O_RDONLY | OPEN_LARGE_OPTS);
if(pi.device == -1)
err(1, "couldn't open drive: %s", driveName);
diff --git a/src/ntfs.h b/src/ntfs.h
index e5eb1ec..06ca6bb 100644
--- a/src/ntfs.h
+++ b/src/ntfs.h
@@ -24,10 +24,8 @@
#include "stddef.h"
#include "drive.h"
-#pragma pack(push, ntfs)
#pragma pack(1)
-
/* WARNING Assumptions: */
#define kNTFS_RecordLen 0x0400
@@ -149,7 +147,11 @@ ntfs_attribnonresident;
#define kNTFS_NameSpaceDOS 0x02
#define kNTFS_NameSpaceWINDOS 0x03
+#ifdef FC_WIDE
#define kNTFS_MFTName L"$MFT"
+#else
+#define kNTFS_MFTName "$MFT"
+#endif
typedef struct _ntfs_attribfilename
{
@@ -181,7 +183,7 @@ typedef struct _ntfs_attriblistrecord
}
ntfs_attriblistrecord;
-#pragma pack(pop, ntfs)
+#pragma pack()
ntfs_attribheader* ntfs_findattribute(ntfs_recordheader* record, uint32 attrType, byte* end);
ntfs_attribheader* ntfs_nextattribute(ntfs_attribheader* attrib, uint32 attrType, byte* end);
@@ -193,6 +195,6 @@ bool ntfs_dofixups(byte* cluster, uint32 size);
/* TODO: Move these declarations elsewhere */
char* unicode_transcode16to8(const wchar_t* src, size_t len);
-wchar_t* unicode_transcode8to16(const char* src, const wchar_t* out, size_t len);
+wchar_t* unicode_transcode8to16(const char* src, wchar_t* out, size_t len);
#endif /* __NTFS_H__ */
diff --git a/src/ntfsx.c b/src/ntfsx.c
index 5d9a526..35f1c23 100644
--- a/src/ntfsx.c
+++ b/src/ntfsx.c
@@ -144,7 +144,7 @@ bool ntfsx_cluster_read(ntfsx_cluster* clus, partitioninfo* info, uint64 begSect
}
pos = SECTOR_TO_BYTES(begSector);
- if(_lseeki64(dd, pos, SEEK_SET) == -1)
+ if(lseek64(dd, pos, SEEK_SET) == -1)
return false;
sz = read(dd, clus->data, clus->size);
@@ -316,7 +316,7 @@ ntfsx_attribute* ntfsx_record_findattribute(ntfsx_record* record, uint32 attrTyp
if(atlr->type == attrType)
{
/* Read in appropriate cluster */
- mftRecord = ntfsx_mftmap_sectorforindex(record->info->mftmap, atlr->refAttrib & 0xFFFFFFFFFFFF);
+ mftRecord = ntfsx_mftmap_sectorforindex(record->info->mftmap, atlr->refAttrib & 0xFFFFFFFFFFFFLL);
r2 = ntfsx_record_alloc(record->info);
if(!r2)
diff --git a/src/posix.c b/src/posix.c
index 0586715..91bb43a 100644
--- a/src/posix.c
+++ b/src/posix.c
@@ -21,6 +21,20 @@
#include "usuals.h"
#include "ntfs.h"
+#include <sys/stat.h>
+#include <unistd.h>
+
+#if TIME_WITH_SYS_TIME
+# include <sys/time.h>
+# include <time.h>
+#else
+# if HAVE_SYS_TIME_H
+# include <sys/time.h>
+# else
+# include <time.h>
+# endif
+#endif
+
/* NOTE: This file assumes that FC_WIDE is off */
/* The NTFS file time is a a 64-bit value representing the
@@ -80,7 +94,7 @@ void setFileAttributes(fchar_t* filename, uint32 flags)
}
else
{
- if(chmod(filename, st.st_mode & ~(S_IWUSR | S_IWGRP | S_IOTH)) == -1)
+ if(chmod(filename, st.st_mode & ~(S_IWUSR | S_IWGRP | S_IWOTH)) == -1)
warn("couldn't set file attributes for: " FC_PRINTF, encoded);
}
}
diff --git a/src/scrounge.c b/src/scrounge.c
index 8baf688..4b4851c 100644
--- a/src/scrounge.c
+++ b/src/scrounge.c
@@ -90,7 +90,7 @@ void processRecordFileBasics(partitioninfo* pi, ntfsx_record* record, filebasics
#ifdef FC_WIDE
wcsncpy(basics->filename, name, len);
#else
- temp = unicode_transcode16to8(basics->filename, len);
+ temp = unicode_transcode16to8(name, len);
if(!temp)
errx(1, "out of memory");
@@ -109,7 +109,7 @@ void processRecordFileBasics(partitioninfo* pi, ntfsx_record* record, filebasics
/* Parent Directory */
- basics->parent = filename->refParent & 0xFFFFFFFFFFFF;
+ basics->parent = filename->refParent & 0xFFFFFFFFFFFFLL;
/* Namespace */
@@ -196,16 +196,16 @@ void processMFTRecord(partitioninfo* pi, uint64 sector, int level)
if(header->flags & kNTFS_RecFlagDir)
{
/* Try to change to the directory */
- if(wchdir(basics.filename) == -1)
+ if(fc_chdir(basics.filename) == -1)
{
- if(wmkdir(basics.filename) == -1)
+ if(fc_mkdir(basics.filename) == -1)
{
warnx("couldn't create directory '" FC_PRINTF "' putting files in parent directory", basics.filename);
}
else
{
setFileAttributes(basics.filename, basics.flags);
- wchdir(basics.filename);
+ fc_chdir(basics.filename);
}
}
@@ -214,7 +214,7 @@ void processMFTRecord(partitioninfo* pi, uint64 sector, int level)
/* Normal file handling: */
- outfile = wopen(basics.filename, _O_BINARY | _O_CREAT | _O_EXCL | _O_WRONLY);
+ outfile = fc_open(basics.filename, O_BINARY | O_CREAT | O_EXCL | O_WRONLY);
fcsncpy(filename2, basics.filename, MAX_PATH);
filename2[MAX_PATH] = 0;
@@ -228,12 +228,12 @@ void processMFTRecord(partitioninfo* pi, uint64 sector, int level)
}
fcscpy(basics.filename, filename2);
- fcscat(basics.filename, L".");
+ fcscat(basics.filename, FC_DOT);
itofc(rename, basics.filename + fcslen(basics.filename), 10);
rename++;
- outfile = fc_open(basics.filename, _O_BINARY | _O_CREAT | _O_EXCL | _O_WRONLY);
+ outfile = fc_open(basics.filename, O_BINARY | O_CREAT | O_EXCL | O_WRONLY);
}
if(outfile == -1)
@@ -462,7 +462,7 @@ void scroungeUsingMFT(partitioninfo* pi)
void scroungeUsingRaw(partitioninfo* pi)
{
byte buffSec[kSectorSize];
- fchar_t dir[_MAX_PATH + 1];
+ fchar_t dir[MAX_PATH + 1];
uint64 sec;
drivelocks locks;
int64 pos;
@@ -472,7 +472,7 @@ void scroungeUsingRaw(partitioninfo* pi)
fprintf(stderr, "[Scrounging raw records...]\n");
/* Save current directory away */
- fc_getcwd(dir, _MAX_PATH);
+ fc_getcwd(dir, MAX_PATH);
/* Get the locks ready */
memset(&locks, 0, sizeof(locks));
@@ -486,7 +486,7 @@ void scroungeUsingRaw(partitioninfo* pi)
/* Read the record */
pos = SECTOR_TO_BYTES(sec);
- if(_lseeki64(pi->device, pos, SEEK_SET) == -1)
+ if(lseek64(pi->device, pos, SEEK_SET) == -1)
errx(1, "can't seek device to sector");
sz = read(pi->device, buffSec, kSectorSize);
diff --git a/src/scrounge.h b/src/scrounge.h
index b1425fd..6ebed43 100644
--- a/src/scrounge.h
+++ b/src/scrounge.h
@@ -23,7 +23,9 @@
#include "drive.h"
void scroungeSearch(partitioninfo* pi);
+#ifdef _WIN32
void scroungeList();
+#endif
void scroungeListDrive(char* drive);
void scroungeUsingMFT(partitioninfo* pi);
void scroungeUsingRaw(partitioninfo* pi);
diff --git a/src/unicode.c b/src/unicode.c
index ea64574..f6197b6 100644
--- a/src/unicode.c
+++ b/src/unicode.c
@@ -57,7 +57,7 @@ char* unicode_transcode16to8(const wchar_t* src, size_t len)
}
}
- ret[pos] = NULL;
+ ret[pos] = 0;
return ret;
}
@@ -67,7 +67,7 @@ char* unicode_transcode16to8(const wchar_t* src, size_t len)
* Since a semi predictable length of the resulting data is
* known, the caller should allocate the memory for this conversion.
*/
-wchar_t* unicode_transcode8to16(const char* src, const wchar_t* out, size_t len)
+wchar_t* unicode_transcode8to16(const char* src, wchar_t* out, size_t len)
{
/* Note: out should always be at least as long as src in chars */
@@ -133,7 +133,7 @@ wchar_t* unicode_transcode8to16(const char* src, const wchar_t* out, size_t len)
}
}
- out[pos] = NULL;
+ out[pos] = 0;
return out;
}
diff --git a/src/usuals.h b/src/usuals.h
index 699adb3..038e8f9 100644
--- a/src/usuals.h
+++ b/src/usuals.h
@@ -64,4 +64,12 @@
#define INTERSECTS(b1, e1, b2, e2) \
((b1) < (e2) && (e1) > (b2))
+#ifndef max
+#define max(a,b) (((a) > (b)) ? (a) : (b))
+#endif
+
+#ifndef min
+#define min(a,b) (((a) < (b)) ? (a) : (b))
+#endif
+
#endif /* __USUALS_H__ */