📄 cmds.c
字号:
/* Does directory listing on the local host. */
void
LocalListCmd(const int argc, const char **const argv, const CommandPtr cmdp, const ArgvInfoPtr aip)
{
#if defined(WIN32) || defined(_WINDOWS)
volatile int i;
int j;
char options[32];
char option[2];
volatile int listmode;
FILE *volatile stream;
volatile int paging;
ARGSUSED(gUnusedArg);
stream = stdout;
paging = 0;
if (argv[0][1] == 'd') {
/* dir */
listmode = 'l';
} else if (argv[0][1] == 'n') {
/* nlist */
listmode = '1';
} else {
/* ls */
listmode = 'C';
}
options[0] = '\0';
option[1] = '\0';
for (i=1; i<argc; i++) {
if (argv[i][0] != '-')
break;
if (argv[i][1] == '-') {
if (argv[i][2] == '\0') {
/* end of options. */
++i;
break;
} else {
/* GNU-esque long --option? */
PrintCmdUsage(cmdp);
}
} else {
for (j=1; ; j++) {
option[0] = argv[i][j];
if (argv[i][j] == '\0')
break;
switch (argv[i][j]) {
case 'l':
listmode = 'l';
break;
case '1':
listmode = '1';
break;
case 'C':
listmode = 'C';
break;
default:
(void) STRNCAT(options, option);
break;
}
}
}
}
if (argv[i] == NULL) {
/* No directory specified, use cwd. */
LLs(NULL, listmode, options, stream);
} else {
/* List each item. */
for ( ; i<argc; i++) {
LLs(argv[i], listmode, options, stream);
}
}
#else
FILE *volatile outfp;
FILE *volatile infp;
int i;
int sj;
int dashopts;
char incmd[256];
char line[256];
vsigproc_t osigpipe, osigint;
ARGSUSED(gUnusedArg);
(void) fflush(stdin);
outfp = OpenPager();
(void) STRNCPY(incmd, "/bin/ls");
for (i=1, dashopts=0; i<argc; i++) {
(void) STRNCAT(incmd, " ");
if (argv[i][0] == '-')
dashopts++;
(void) STRNCAT(incmd, argv[i]);
}
if (dashopts == 0) {
(void) STRNCPY(incmd, "/bin/ls -CF");
for (i=1; i<argc; i++) {
(void) STRNCAT(incmd, " ");
(void) STRNCAT(incmd, argv[i]);
}
}
infp = popen(incmd, "r");
if (infp == NULL) {
ClosePager(outfp);
return;
}
#ifdef HAVE_SIGSETJMP
sj = sigsetjmp(gCancelJmp, 1);
#else /* HAVE_SIGSETJMP */
sj = setjmp(gCancelJmp);
#endif /* HAVE_SIGSETJMP */
if (sj != 0) {
/* Caught a signal. */
(void) NcSignal(SIGPIPE, (FTPSigProc) SIG_IGN);
ClosePager(outfp);
if (infp != NULL)
(void) pclose(infp);
(void) NcSignal(SIGPIPE, osigpipe);
(void) NcSignal(SIGINT, osigint);
(void) fprintf(stderr, "Canceled.\n");
Trace(0, "Canceled because of signal %d.\n", gGotSig);
gMayCancelJmp = 0;
return;
} else {
osigpipe = NcSignal(SIGPIPE, Cancel);
osigint = NcSignal(SIGINT, Cancel);
gMayCancelJmp = 1;
}
while (fgets(line, sizeof(line) - 1, infp) != NULL)
(void) fputs(line, outfp);
(void) fflush(outfp);
(void) pclose(infp);
infp = NULL;
ClosePager(outfp);
outfp = NULL;
(void) NcSignal(SIGPIPE, osigpipe);
(void) NcSignal(SIGINT, osigint);
gMayCancelJmp = 0;
#endif
} /* LocalListCmd */
static void
Sys(const int argc, const char **const argv, const ArgvInfoPtr aip, const char *syscmd, int noDQuote)
{
char cmd[256];
int i;
(void) STRNCPY(cmd, syscmd);
for (i = 1; i < argc; i++) {
if (aip->noglobargv[i] != 0) {
(void) STRNCAT(cmd, " '");
(void) STRNCAT(cmd, argv[i]);
(void) STRNCAT(cmd, "'");
} else if (noDQuote != 0) {
(void) STRNCAT(cmd, " ");
(void) STRNCAT(cmd, argv[i]);
} else {
(void) STRNCAT(cmd, " \"");
(void) STRNCAT(cmd, argv[i]);
(void) STRNCAT(cmd, "\" ");
}
}
#if defined(WIN32) || defined(_WINDOWS)
fprintf(stderr, "Cannot run command: %s\n", cmd);
#else
Trace(0, "Sys: %s\n", cmd);
(void) system(cmd);
#endif
} /* Sys */
#if defined(WIN32) || defined(_WINDOWS)
#else
void
LocalChmodCmd(const int argc, const char **const argv, const CommandPtr cmdp, const ArgvInfoPtr aip)
{
ARGSUSED(gUnusedArg);
Sys(argc, argv, aip, "/bin/chmod", 1);
} /* LocalChmodCmd */
#endif
void
LocalMkdirCmd(const int argc, const char **const argv, const CommandPtr cmdp, const ArgvInfoPtr aip)
{
#if defined(WIN32) || defined(_WINDOWS)
const char *arg;
int i;
for (i = 1; i < argc; i++) {
arg = argv[i];
if (MkDirs(arg, 00755) < 0) {
perror(arg);
}
}
#else
ARGSUSED(gUnusedArg);
Sys(argc, argv, aip, "/bin/mkdir", 0);
#endif
} /* LocalMkdirCmd */
#if defined(WIN32) || defined(_WINDOWS)
#else
void
LocalPageCmd(const int argc, const char **const argv, const CommandPtr cmdp, const ArgvInfoPtr aip)
{
ARGSUSED(gUnusedArg);
Sys(argc, argv, aip, gPager, 0);
} /* LocalPageCmd */
#endif
void
LocalRenameCmd(const int argc, const char **const argv, const CommandPtr cmdp, const ArgvInfoPtr aip)
{
#if defined(WIN32) || defined(_WINDOWS)
if (rename(argv[1], argv[2]) < 0) {
perror("rename");
}
#else
ARGSUSED(gUnusedArg);
Sys(argc, argv, aip, "/bin/mv", 1);
#endif
} /* LocalRenameCmd */
void
LocalRmCmd(const int argc, const char **const argv, const CommandPtr cmdp, const ArgvInfoPtr aip)
{
#if defined(WIN32) || defined(_WINDOWS)
int i;
int result;
LineList ll;
LinePtr lp;
ARGSUSED(gUnusedArg);
for (i=1; i<argc; i++) {
InitLineList(&ll);
result = FTPLocalGlob(&gConn, &ll, argv[i], (aip->noglobargv[i] != 0) ? kGlobNo: kGlobYes);
if (result < 0) {
FTPPerror(&gConn, result, kErrGlobFailed, "local glob", argv[i]);
} else {
for (lp = ll.first; lp != NULL; lp = lp->next) {
if (lp->line != NULL) {
if (remove(lp->line) < 0)
perror(lp->line);
}
}
}
DisposeLineListContents(&ll);
}
#else
ARGSUSED(gUnusedArg);
Sys(argc, argv, aip, "/bin/rm", 1);
#endif
} /* LocalRmCmd */
void
LocalRmdirCmd(const int argc, const char **const argv, const CommandPtr cmdp, const ArgvInfoPtr aip)
{
#if defined(WIN32) || defined(_WINDOWS)
int i;
int result;
LineList ll;
LinePtr lp;
ARGSUSED(gUnusedArg);
for (i=1; i<argc; i++) {
InitLineList(&ll);
result = FTPLocalGlob(&gConn, &ll, argv[i], (aip->noglobargv[i] != 0) ? kGlobNo: kGlobYes);
if (result < 0) {
FTPPerror(&gConn, result, kErrGlobFailed, "local glob", argv[i]);
} else {
for (lp = ll.first; lp != NULL; lp = lp->next) {
if (lp->line != NULL) {
if (rmdir(lp->line) < 0)
perror(lp->line);
}
}
}
DisposeLineListContents(&ll);
}
#else
ARGSUSED(gUnusedArg);
Sys(argc, argv, aip, "/bin/rmdir", 1);
#endif
} /* LocalRmdirCmd */
/* Displays the current local working directory. */
void
LocalPwdCmd(const int argc, const char **const argv, const CommandPtr cmdp, const ArgvInfoPtr aip)
{
ARGSUSED(gUnusedArg);
if (FTPGetLocalCWD(gLocalCWD, sizeof(gLocalCWD)) != NULL) {
Trace(-1, "%s\n", gLocalCWD);
}
} /* LocalPwdCmd */
/* This is a simple interface to name service. I prefer using this instead
* of nslookup.
*/
void
LookupCmd(const int argc, const char **const argv, const CommandPtr cmdp, const ArgvInfoPtr aip)
{
int i, j;
struct hostent *hp;
const char *host;
char **cpp;
struct in_addr ip_address;
int shortMode, opt;
char ipStr[16];
ARGSUSED(gUnusedArg);
shortMode = 1;
GetoptReset();
while ((opt = Getopt(argc, argv, "v")) >= 0) {
if (opt == 'v')
shortMode = 0;
else {
PrintCmdUsage(cmdp);
return;
}
}
for (i=gOptInd; i<argc; i++) {
hp = GetHostEntry((host = argv[i]), &ip_address);
if ((i > gOptInd) && (shortMode == 0))
Trace(-1, "\n");
if (hp == NULL) {
Trace(-1, "Unable to get information about site %s.\n", host);
} else if (shortMode) {
MyInetAddr(ipStr, sizeof(ipStr), hp->h_addr_list, 0);
Trace(-1, "%-40s %s\n", hp->h_name, ipStr);
} else {
Trace(-1, "%s:\n", host);
Trace(-1, " Name: %s\n", hp->h_name);
for (cpp = hp->h_aliases; *cpp != NULL; cpp++)
Trace(-1, " Alias: %s\n", *cpp);
for (j = 0, cpp = hp->h_addr_list; *cpp != NULL; cpp++, ++j) {
MyInetAddr(ipStr, sizeof(ipStr), hp->h_addr_list, j);
Trace(-1, " Address: %s\n", ipStr);
}
}
}
} /* LookupCmd */
/* Directory listing in a machine-readable format;
* Mostly for debugging, since NcFTP uses MLSD automatically when it needs to.
*/
void
MlsCmd(const int argc, const char **const argv, const CommandPtr cmdp, const ArgvInfoPtr aip)
{
int i;
int opt;
LinePtr linePtr, nextLinePtr;
int result;
LineList dirContents;
int mlsd = 1, x;
const char *item;
ARGSUSED(gUnusedArg);
GetoptReset();
while ((opt = Getopt(argc, argv, "dt")) >= 0) {
if ((opt == 'd') || (opt == 't')) {
/* Use MLST instead of MLSD,
* which is similar to using "ls -d" instead of "ls".
*/
mlsd = 0;
} else {
PrintCmdUsage(cmdp);
return;
}
}
i = gOptInd;
if (i == argc) {
/* No args, do current directory. */
x = 1;
item = "";
if ((result = FTPListToMemory2(&gConn, item, &dirContents, (mlsd == 0) ? "-d" : "", 1, &x)) < 0) {
if (mlsd != 0) {
FTPPerror(&gConn, result, 0, "Could not MLSD", item);
} else {
FTPPerror(&gConn, result, 0, "Could not MLST", item);
}
} else {
for (linePtr = dirContents.first;
linePtr != NULL;
linePtr = nextLinePtr)
{
nextLinePtr = linePtr->next;
(void) fprintf(stdout, "%s\n", linePtr->line);
Trace(0, "%s\n", linePtr->line);
}
}
}
for ( ; i<argc; i++) {
x = 1;
item = argv[i];
if ((result = FTPListToMemory2(&gConn, item, &dirContents, (mlsd == 0) ? "-d" : "", 1, &x)) < 0) {
if (mlsd != 0) {
FTPPerror(&gConn, result, 0, "Could not MLSD", item);
} else {
FTPPerror(&gConn, result, 0, "Could not MLST", item);
}
} else {
for (linePtr = dirContents.first;
linePtr != NULL;
linePtr = nextLinePtr)
{
nextLinePtr = linePtr->next;
(void) fprintf(stdout, "%s\n", linePtr->line);
Trace(0, "%s\n", linePtr->line);
}
}
}
} /* MlsCmd */
/* Create directories on the remote system. */
void
MkdirCmd(const int argc, const char **const argv, const CommandPtr cmdp, const ArgvInfoPtr aip)
{
int i;
int opt;
int result;
int recurseFlag = kRecursiveNo;
ARGSUSED(gUnusedArg);
GetoptReset();
while ((opt = Getopt(argc, argv, "p")) >= 0) {
if (opt == 'p') {
/* Try creating intermediate directories if they
* don't exist.
*
* For example if only /pub/stuff existed, and you
* do a "mkdir -p /pub/stuff/a/b/c", the "a" and "b"
* directories would also be created.
*/
recurseFlag = kRecursiveYes;
} else {
PrintCmdUsage(cmdp);
return;
}
}
for (i=gOptInd; i<argc; i++) {
result = FTPMkdir(&gConn, argv[i], recurseFlag);
if (result < 0)
FTPPerror(&gConn, result, kErrMKDFailed, "Could not mkdir", argv[i]);
}
/* Really should just flush only the modified directories... */
FlushLsCache();
} /* MkdirCmd */
/*VARARGS*/
static void
OpenMsg(const char *const fmt, ...)
{
va_list ap;
char buf[512];
size_t len, padlim;
padlim = (size_t) gScreenColumns;
if ((size_t) gScreenColumns > (sizeof(buf) - 1))
padlim = sizeof(buf) - 1;
va_start(ap, fmt);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -