📄 lscdlib.c
字号:
/* lscdLib.c - ls(), ll(), cd(), and pwd() from usrLib.c */
#include "vxWorks.h"
#include "fioLib.h"
#include "dirent.h"
#include "stat.h"
#include "private/funcBindP.h"
#include "string.h"
#include "stdio.h"
#include "pathLib.h"
#include "time.h"
#include "ioLib.h"
#include "netDrv.h"
#include "errno.h"
#define MAX_DATE_STRING 21
/*******************************************************************************
*
* ls - list the contents of a directory
*
* This command is similar to UNIX ls. It lists the contents of a directory
* in one of two formats. If <doLong> is FALSE, only the names of the files
* (or subdirectories) in the specified directory are displayed. If <doLong>
* is TRUE, then the file name, size, date, and time are displayed. For
* a long listing, any entries that describe subdirectories are also flagged
* with the label "DIR".
*
* The <dirName> parameter specifies which directory to list. If
* <dirName> is omitted or NULL, the current working directory is listed.
*
* Empty directory entries and dosFs volume label entries are not reported.
*
* NOTE: When used with netDrv devices (FTP or RSH), <doLong> has no effect.
*
* RETURNS: OK or ERROR.
*
* SEE ALSO: ll(), lsOld(), stat(),
* .pG "Target Shell"
*/
STATUS ls
(
FAST char *dirName, /* name of dir to list */
BOOL doLong /* if TRUE, do long listing */
)
{
FAST STATUS status; /* return status */
FAST DIR *pDir; /* ptr to directory descriptor */
FAST struct dirent *pDirEnt; /* ptr to dirent */
struct stat fileStat; /* file status info (long listing) */
STATUS statResult; /* return value of stat () call */
char *pDirComment; /* dir comment (long listing) */
BOOL firstFile; /* first file flag (long listing) */
struct tm fileDate; /* file date in time.h format */
char dateString [MAX_DATE_STRING];
char fileName [MAX_FILENAME_LENGTH];
/* buffer for building file name */
if (dirName == NULL)
dirName = ".";
/* try to do a netDrv listing first */
if (_func_netLsByName != NULL)
{
if ((*_func_netLsByName) (dirName) == OK)
return (OK);
else if (errno != S_netDrv_UNKNOWN_REQUEST)
return (ERROR);
}
if ((pDir = opendir (dirName)) == NULL)
{
printf ("Can't open \"%s\".\n", dirName);
return (ERROR);
}
status = OK;
firstFile = TRUE;
while (TRUE)
{
errno = OK;
pDirComment = "";
pDirEnt = readdir (pDir);
if (pDirEnt == NULL)
{
if (errno != OK)
{
printf ("error reading entry (errno=%#x)\n", errno);
status = ERROR;
}
break;
}
if (!doLong)
printf ("%s\n", pDirEnt->d_name);
else
{
if (firstFile)
{
printf (" size date time name\n");
printf ("-------- ------ ------ --------\n");
firstFile = FALSE;
}
/* Construct path/filename for stat */
(void) pathCat (dirName, pDirEnt->d_name, fileName);
/* Get and print file status info */
if ((statResult = (stat (fileName, &fileStat))) != OK)
{
/*
* Since we got this file from a readdir call, we know
* something is there. A failed stat() is probably
* a symlink that can't be resolved. Continue with the
* listing.
*/
memset (&fileStat, 0, sizeof (fileStat));
perror ("IO Error - can\'t stat file");
pDirComment = "<IO Error>";
}
else if (S_ISDIR (fileStat.st_mode))
pDirComment = "<DIR>";
/* zero out the fileDate struct */
memset (&fileDate, 0, sizeof (fileDate));
/* use the results of the stat() call to fill in the fileDate */
localtime_r (&fileStat.st_mtime, &fileDate);
if (statResult == OK)
strftime (dateString, MAX_DATE_STRING, "%b-%d-%Y %H:%M:%S",
&fileDate);
else
{
memset (dateString, ' ', MAX_DATE_STRING);
dateString [MAX_DATE_STRING] = EOS;
}
printf ("%8d %s %-16s %s\n",
fileStat.st_size,
dateString,
pDirEnt->d_name, pDirComment);
}
}
status |= closedir (pDir);
return (status);
}
/*******************************************************************************
*
* ll - do a long listing of directory contents
*
* This command causes a long listing of a directory's contents to be
* displayed. It is equivalent to:
* .CS
* -> ls dirName, TRUE
* .CE
*
* NOTE: When used with netDrv devices (FTP or RSH), ll() does not give
* directory information. It is equivalent to an ls() call with no
* long-listing option.
*
* RETURNS: OK or ERROR.
*
* SEE ALSO: ls(), stat(),
* .pG "Target Shell"
*/
STATUS ll
(
char *dirName /* name of directory to list */
)
{
return (ls (dirName, TRUE));
}
/*******************************************************************************
*
* cd - change the default directory
*
* This command sets the default directory to <name>. The default directory
* is a device name, optionally followed by a directory local to that
* device.
*
* To change to a different directory, specify one of the following:
* .iP "" 4
* an entire path name with a device name, possibly followed by a directory
* name. The entire path name will be changed.
* .iP
* a directory name starting with a `~' or `/' or `$'. The directory part
* of the path, immediately after the device name, will be replaced with the new
* directory name.
* .iP
* a directory name to be appended to the current default directory.
* The directory name will be appended to the current default directory.
* .LP
*
* An instance of ".." indicates one level up in the directory tree.
*
* Note that when accessing a remote file system via RSH or FTP, the
* VxWorks network device must already have been created using
* netDevCreate().
*
* WARNING
* The cd() command does very little checking that <name> represents a valid
* path. If the path is invalid, cd() may return OK, but subsequent
* calls that depend on the default path will fail.
*
* EXAMPLES
* The following example changes the directory to device `/fd0/':
* .CS
* -> cd "/fd0/"
* .CE
* This example changes the directory to device `wrs:' with the local
* directory `~leslie/target':
* .CS
* -> cd "wrs:~leslie/target"
* .CE
* After the previous command, the following changes the directory to
* `wrs:~leslie/target/config':
* .CS
* -> cd "config"
* .CE
* After the previous command, the following changes the directory to
* `wrs:~leslie/target/demo':
* .CS
* -> cd "../demo"
* .CE
* After the previous command, the following changes the directory to
* `wrs:/etc'.
* .CS
* -> cd "/etc"
* .CE
* Note that `~' can be used only on network devices (RSH or FTP).
*
* RETURNS: OK or ERROR.
*
* SEE ALSO: pwd(),
* .pG "Target Shell"
*/
STATUS cd
(
char *name /* new directory name */
)
{
if (ioDefPathCat (name) != OK)
{
printf ("cd: error = %#x.\n", errno);
return (ERROR);
}
return (OK);
}
/*******************************************************************************
*
* pwd - print the current default directory
*
* This command displays the current working device/directory.
*
* RETURNS: N/A
*
* SEE ALSO: cd(),
* .pG "Target Shell,"
* windsh,
* .tG "Shell"
*/
void pwd (void)
{
char name [MAX_FILENAME_LENGTH];
ioDefPathGet (name);
printf ("%s\n", name);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -