📄 nonport.h
字号:
// nonport.h// collection of nonportable routines (the interfaces// are portable, but the implementations are not)// copyright SafeTP Development Group, Inc., 2000 Terms of use are as specified in license.txt#ifndef __NONPORT_H#define __NONPORT_H#include "typ.h" // bool// I'm attempting to improve error handling in this module; this fn will be// called when a syscall fails, *in addition* to whatever error behavior// is documented here (e.g., a fn might call this, and then return false).// The default behavior is to do nothing. In sftpc and sftpd, I plan to// point this at xSysError::xsyserror (see syserr.h).typedef void (*NonportFailFunc)(char const *syscallName, char const *context); // syscallName - name of failing system call // context - current activity (maybe just calling fn's name) or NULLextern NonportFailFunc nonportFail;// this is default handlervoid defaultNonportFail(char const *syscallName, char const *context);// put terminal into 'raw' or 'cooked' modevoid setRawMode(bool raw);// get the next character typed without buffering or echoing; needs the// console to be in 'raw' modechar getConsoleChar();// get a millisecond count, where 0 is an unspecified eventlong getMilliseconds();// remove all priviledges to a file, except for read/write// access by the file's owner; returns false on errorbool limitFileAccess(char const *fname);// create a new directory; returns false on error;// precise naming semantics, such as use// of 'current working directory', etc., are specified by the// underlying OS's mkdir (or equivalent) command (it is hoped// this underspecification will not be a problem in practice)bool createDirectory(char const *dirname);// change to a directory; returns false on failure// again, current-directory semantics are unspecifiedbool changeDirectory(char const *dirname);// retrieve the name of the current working directory// (more best effort crap, I guess)bool getCurrentDirectory(char *dirname, int dirnameLen);// get and process the names of files *and directories* in the current directorytypedef bool (*PerFileFunc)(char const *name, void *extra); // name - file/dir being processed (contains no slashes) // extra - 2nd parameter to applyToCwdContents // return - true to continue, false to stop iteratingvoid applyToCwdContents(PerFileFunc func, void *extra=NULL);// same as above, but in an explicitly named directoryvoid applyToDirContents(char const *dirName, PerFileFunc func, void *extra=NULL);// return true if the given string names a directorybool isDirectory(char const *path);// delete a file; returns false on failurebool removeFile(char const *fname);// retrieve the current datevoid getCurrentDate(int &month, int &day, int &year); // month: 1 = January ... 12 = December // day: 1 = first day of month, ... // year: 1999 is when this being coded // e.g., February 8, 1999 is month=2, day=8, year=1999// retrieve current timevoid getCurrentTime(int &hour, int &minute, int &second); // hour: 0 = midnight (12am), 1 = 1am, .. 11 = 11am, // 12 = noon (12pm), 13 = 1pm, .. 23 = 11pm // minute: 0 .. 59 // second: 0 .. 59// sleep for a bit (low resolution)void portableSleep(unsigned seconds);// determine usable name of current user, and write it into 'buffer'void getCurrentUsername(char *buffer, int buflen);// read a string from the console, with no echovoid readNonechoString(char *buffer, int buflen, char const *prompt);// return true if a file or directory existsbool fileOrDirectoryExists(char const *name);// ensure that the pathname part of a file name exists;// it creates missing directories as necessary, with only// user rwx permission; if 'isDirectory' is true, the whole// name is also verified as a directory; returns false on// errorbool ensurePath(char const *filename, bool isDirectory);// returns true if the system has a cryptographically-// secure random number generatorbool hasSystemCryptoRandom();// if the above fn returns true, this will retrieve a// random 32-bit integer; may block until the bits// become availableunsigned getSystemCryptoRandom();// get process id; meaning is somewhat system-dependent, but the goal// is to return something that can be used to correlate log output// from (say) sftpd with log output from some other source (syslog,// or NT event viewer, etc.)int getProcessId();// enter a critical section - blocks until section is free; used for// synchronizing across threads in a single process; note there is// only one critical section object here; a single thread may call// this multiple times without being blocked but must call// portableUnlock() the same number of timesvoid portableLock();// leave a critical section; this thread must have previously// successfully executed a portableLock() callvoid portableUnlock();// C++ provides a very easy way to ensure proper pairing of locks and// unlocks: put the former into a ctor and the latter into a dtor. this// of course relies on the C++ compiler not sucking, which is not in// general true. however, it really is the right way from a maintenance// point of view, and there's only one platform right now that might be// multithreaded (nt, with msvc, which sucks but maybe not in this// particular way (fingers crossed)).class Locker {public: Locker() { portableLock(); } ~Locker() { portableUnlock(); }};// convenience#define LOCKER Locker locker;#endif // __NONPORT_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -