📄 util.c
字号:
}
return result;
#endif /* HAVE_MKTIME */
} /* UnMDTMDate */
int
GetSockBufSize(int sockfd, size_t *rsize, size_t *ssize)
{
#ifdef SO_SNDBUF
int rc = -1;
int opt;
int optsize;
if (ssize != NULL) {
opt = 0;
optsize = sizeof(opt);
rc = getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, (char *) &opt, &optsize);
if (rc == 0)
*ssize = (size_t) opt;
else
*ssize = 0;
}
if (rsize != NULL) {
opt = 0;
optsize = sizeof(opt);
rc = getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (char *) &opt, &optsize);
if (rc == 0)
*rsize = (size_t) opt;
else
*rsize = 0;
}
return (rc);
#else
if (ssize != NULL)
*ssize = 0;
if (rsize != NULL)
*rsize = 0;
return (-1);
#endif
} /* GetSockBufSize */
int
SetSockBufSize(int sockfd, size_t rsize, size_t ssize)
{
#ifdef SO_SNDBUF
int rc = -1;
int opt;
int optsize;
#ifdef TCP_RFC1323
/* This is an AIX-specific socket option to do RFC1323 large windows */
if (ssize > 0 || rsize > 0) {
opt = 1;
optsize = sizeof(opt);
rc = setsockopt(sockfd, IPPROTO_TCP, TCP_RFC1323, &opt, optsize);
}
#endif
if (ssize > 0) {
opt = (int) ssize;
optsize = sizeof(opt);
rc = setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, (char *) &opt, optsize);
}
if (rsize > 0) {
opt = (int) rsize;
optsize = sizeof(opt);
rc = setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (char *) &opt, optsize);
}
return (rc);
#else
return (-1);
#endif
} /* SetSockBufSize */
void
Scramble(unsigned char *dst, size_t dsize, unsigned char *src, char *key)
{
int i;
unsigned int ch;
unsigned char *k2;
size_t keyLen;
keyLen = strlen(key);
k2 = (unsigned char *) key;
for (i=0; i < (int) dsize - 1; i++) {
ch = src[i];
if (ch == 0)
break;
dst[i] = (unsigned char) (ch ^ (int) (k2[i % (int) keyLen]));
}
dst[i] = '\0';
} /* Scramble */
#if defined(WIN32) || defined(_WINDOWS)
void WinSleep(unsigned int seconds)
{
DWORD now, deadline;
DWORD milliseconds = seconds * 1000;
if (milliseconds > 0) {
now = GetTickCount();
deadline = now + milliseconds;
if (now < deadline) {
/* Typical case */
do {
milliseconds = deadline - now;
Sleep(milliseconds);
now = GetTickCount();
} while (now < deadline);
} else {
/* Overflow case */
deadline = now - 1;
milliseconds -= (0xFFFFFFFF - now);
do {
Sleep(0xFFFFFFFF - now);
now = GetTickCount();
} while (now > deadline);
/* Counter has now wrapped around */
deadline = now + milliseconds;
do {
milliseconds = deadline - now;
Sleep(milliseconds);
now = GetTickCount();
} while (now < deadline);
}
}
} /* WinSleep */
char *
StrFindLocalPathDelim(const char *src) /* TODO: optimize */
{
const char *first;
int c;
first = NULL;
for (;;) {
c = *src++;
if (c == '\0')
break;
if (IsLocalPathDelim(c)) {
first = src - 1;
break;
}
}
return ((char *) first);
} /* StrFindLocalPathDelim */
char *
StrRFindLocalPathDelim(const char *src) /* TODO: optimize */
{
const char *last;
int c;
last = NULL;
for (;;) {
c = *src++;
if (c == '\0')
break;
if (IsLocalPathDelim(c))
last = src - 1;
}
return ((char *) last);
} /* StrRFindLocalPathDelim */
void
StrRemoveTrailingLocalPathDelim(char *dst)
{
char *cp;
cp = StrRFindLocalPathDelim(dst);
if ((cp == NULL) || (cp[1] != '\0'))
return;
/* Note: Do not destroy a path of "/" */
while ((cp > dst) && (IsLocalPathDelim(*cp)))
*cp-- = '\0';
} /* StrRemoveTrailingLocalPathDelim */
void
TVFSPathToLocalPath(char *dst)
{
int c;
/* Note: Technically we don't need to do this,
* since Win32 accepts a / as equivalent to a \
* in a pathname.
*/
if (dst != NULL) {
for (;;) {
c = *dst++;
if (c == '\0')
break;
if (c == '/')
dst[-1] = LOCAL_PATH_DELIM;
}
}
} /* TVFSPathToLocalPath */
void
LocalPathToTVFSPath(char *dst)
{
int c;
if (dst != NULL) {
for (;;) {
c = *dst++;
if (c == '\0')
break;
if (c == LOCAL_PATH_DELIM)
dst[-1] = '/';
}
}
} /* LocalPathToTVFSPath */
#endif /* WINDOWS */
void
StrRemoveTrailingSlashes(char *dst)
{
char *cp;
cp = strrchr(dst, '/');
if ((cp == NULL) || (cp[1] != '\0'))
return;
/* Note: Do not destroy a path of "/" */
while ((cp > dst) && (*cp == '/'))
*cp-- = '\0';
} /* StrRemoveTrailingSlashes */
int
MkDirs(const char *const newdir, int mode1)
{
char s[512];
int rc;
char *cp, *sl;
#if defined(WIN32) || defined(_WINDOWS)
struct _stat st;
char *share;
#else
struct Stat st;
mode_t mode = (mode_t) mode1;
#endif
#if defined(WIN32) || defined(_WINDOWS)
if ((isalpha(newdir[0])) && (newdir[1] == ':')) {
if (! IsLocalPathDelim(newdir[2])) {
/* Special case "c:blah", and errout.
* "c:\blah" must be used or _access GPFs.
*/
errno = EINVAL;
return (-1);
} else if (newdir[3] == '\0') {
/* Special case root directory, which cannot be made. */
return (0);
}
} else if (IsUNCPrefixed(newdir)) {
share = StrFindLocalPathDelim(newdir + 2);
if ((share == NULL) || (StrFindLocalPathDelim(share + 1) == NULL))
return (-1);
}
if (_access(newdir, 00) == 0) {
if (_stat(newdir, &st) < 0)
return (-1);
if (! S_ISDIR(st.st_mode)) {
errno = ENOTDIR;
return (-1);
}
return 0;
}
#else
if (access(newdir, F_OK) == 0) {
if (Stat(newdir, &st) < 0)
return (-1);
if (! S_ISDIR(st.st_mode)) {
errno = ENOTDIR;
return (-1);
}
return 0;
}
#endif
(void) strncpy(s, newdir, sizeof(s));
if (s[sizeof(s) - 1] != '\0') {
#ifdef ENAMETOOLONG
errno = ENAMETOOLONG;
#else
errno = EINVAL;
return (-1);
#endif
}
cp = StrRFindLocalPathDelim(s);
if (cp == NULL) {
#if defined(WIN32) || defined(_WINDOWS)
if (! CreateDirectory(newdir, (LPSECURITY_ATTRIBUTES) 0))
return (-1);
return (0);
#else
rc = mkdir(newdir, mode);
return (rc);
#endif
} else if (cp[1] == '\0') {
/* Remove trailing slashes from path. */
--cp;
while (cp > s) {
if (! IsLocalPathDelim(*cp))
break;
--cp;
}
cp[1] = '\0';
cp = StrRFindLocalPathDelim(s);
if (cp == NULL) {
#if defined(WIN32) || defined(_WINDOWS)
if (! CreateDirectory(s, (LPSECURITY_ATTRIBUTES) 0))
return (-1);
#else
rc = mkdir(s, mode);
return (rc);
#endif
}
}
/* Find the deepest directory in this
* path that already exists. When
* we do, we want to have the 's'
* string as it was originally, but
* with 'cp' pointing to the first
* slash in the path that starts the
* part that does not exist.
*/
sl = NULL;
for (;;) {
*cp = '\0';
#if defined(WIN32) || defined(_WINDOWS)
rc = _access(s, 00);
#else
rc = access(s, F_OK);
#endif
if (sl != NULL)
*sl = LOCAL_PATH_DELIM;
if (rc == 0) {
*cp = LOCAL_PATH_DELIM;
break;
}
sl = cp;
cp = StrRFindLocalPathDelim(s);
if (cp == NULL) {
/* We do not have any more
* slashes, so none of the
* new directory's components
* existed before, so we will
* have to make everything
* starting at the first node.
*/
if (sl != NULL)
*sl = LOCAL_PATH_DELIM;
/* We refer to cp + 1 below,
* so this is why we can
* set "cp" to point to the
* byte before the array starts.
*/
cp = s - 1;
break;
}
}
for (;;) {
/* Extend the path we have to
* include the next component
* to make.
*/
sl = StrFindLocalPathDelim(cp + 1);
if (sl == s) {
/* If the next slash is pointing
* to the start of the string, then
* the path is an absolute path and
* we don't need to make the root node,
* and besides the next mkdir would
* try an empty string.
*/
++cp;
sl = StrFindLocalPathDelim(cp + 1);
}
if (sl != NULL) {
*sl = '\0';
}
#if defined(WIN32) || defined(_WINDOWS)
if (! CreateDirectory(s, (LPSECURITY_ATTRIBUTES) 0))
return (-1);
#else
rc = mkdir(s, mode);
if (rc < 0)
return rc;
#endif
if (sl == NULL)
break;
*sl = LOCAL_PATH_DELIM;
cp = sl;
}
return (0);
} /* MkDirs */
int
FilenameExtensionIndicatesASCII(const char *const pathName, const char *const extnList)
{
const char *extn;
char *cp;
int c;
char extnPattern[16];
extn = pathName + strlen(pathName) - 1;
forever {
if (extn <= pathName)
return (0); /* End of pathname, no extension. */
c = (int) *--extn;
if (IsLocalPathDelim(c))
return (0); /* End of filename, no extension. */
if (c == '.') {
extn += 1;
break;
}
}
if (strlen(extn) > (sizeof(extnPattern) - 2 - 1 - 1)) {
return (0);
}
#ifdef HAVE_SNPRINTF
snprintf(extnPattern, sizeof(extnPattern),
#else
sprintf(extnPattern,
#endif
"|.%s|",
extn
);
cp = extnPattern;
forever {
c = *cp;
if (c == '\0')
break;
if (isupper(c)) {
c = tolower(c);
*cp++ = (char) c;
} else {
cp++;
}
}
/* Extension list is specially formatted, like this:
*
* |ext1|ext2|ext3|...|extN|
*
* I.e, each filename extension is delimited with
* a pipe, and we always begin and end the string
* with a pipe.
*/
if (strstr(extnList, extnPattern) != NULL) {
return (1);
}
return (0);
} /* FilenameExtensionIndicatesASCII */
#ifdef HAVE_SIGACTION
void (*NcSignal(int signum, void (*handler)(int)))(int)
{
struct sigaction sa, osa;
(void) sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = handler;
if (signum == SIGALRM) {
#ifdef SA_INTERRUPT
sa.sa_flags |= SA_INTERRUPT;
#endif
} else {
#ifdef SA_RESTART
sa.sa_flags |= SA_RESTART;
#endif
}
if (sigaction(signum, &sa, &osa) < 0)
return ((FTPSigProc) SIG_ERR);
return (osa.sa_handler);
}
#endif /* HAVE_SIGACTION */
/* eof */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -