我有3个(我知道)问题:
and heres the output from the inbuilt command
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/param.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <locale.h>
#include <langinfo.h>
#include <stdint.h>
#include <fcntl.h>
#define KNRM "\x1B[0m"
#define KRED "\x1B[31m"
#define KGRN "\x1B[32m"
#define KYEL "\x1B[33m"
#define KBLU "\x1B[34m"
#define KMAG "\x1B[35m"
#define KCYN "\x1B[36m"
#define KWHT "\x1B[37m"
#define RESET "\033[0m"
static char perms_buff[30];
const char *get_perms(mode_t mode)
{
char ftype = '?';
if (S_ISREG(mode)) ftype = '-';
if (S_ISLNK(mode)) ftype = 'l';
if (S_ISDIR(mode)) ftype = 'd';
if (S_ISBLK(mode)) ftype = 'b';
if (S_ISCHR(mode)) ftype = 'c';
if (S_ISFIFO(mode)) ftype = '|';
sprintf(perms_buff, "%c%c%c%c%c%c%c%c%c%c %c%c%c", ftype,
mode & S_IRUSR ? 'r' : '-',
mode & S_IWUSR ? 'w' : '-',
mode & S_IXUSR ? 'x' : '-',
mode & S_IRGRP ? 'r' : '-',
mode & S_IWGRP ? 'w' : '-',
mode & S_IXGRP ? 'x' : '-',
mode & S_IROTH ? 'r' : '-',
mode & S_IWOTH ? 'w' : '-',
mode & S_IXOTH ? 'x' : '-',
mode & S_ISUID ? 'U' : '-',
mode & S_ISGID ? 'G' : '-',
mode & S_ISVTX ? 'S' : '-');
return (const char *)perms_buff;
}
char pathname[MAXPATHLEN];
void die(char *msg)
{
perror(msg);
exit(0);
}
static int
one (const struct dirent *unused)
{
return 1;
}
int main()
{
int count,i;
struct direct **files;
struct stat statbuf;
char datestring[256];
struct passwd pwent;
struct passwd *pwentp;
struct group grp;
struct group *grpt;
struct tm time;
char buf[1024];
if(!getcwd(pathname, sizeof(pathname)))
die("Error getting pathnamen");
count = scandir(pathname, &files, one, alphasort);
if(count > 0)
{
printf("total %dn",count);
for (i=0; i<count; ++i)
{
if (stat(files[i]->d_name, &statbuf) == 0)
{
/* Print out type, permissions, and number of links. */
printf("%10.10s", get_perms(statbuf.st_mode));
printf(" %d", (int)statbuf.st_nlink);
if (S_ISDIR(statbuf.st_mode)){
printf("%s" KGRN "%s RESET \n",
files[i-1]->d_name,datestring);
}
if (S_ISLNK(statbuf.st_mode)){
printf("%s" KMAG "%s RESET \n",
files[i-1]->d_name,datestring);
}
else
printf(" %s %s\n", datestring, files[i]->d_name);
}
free (files[i]);
}
free(files);
}
}
米/ IenKP.jpg
答案 0 :(得分:0)
以下是打印颜色的代码示例:
#include <stdio.h>
#define KMAG "\033[35m"
#define RESET "\033[0m"
int main(int argc, char *argv[]) {
printf("%smagenta text%s\n", KMAG, RESET);
return 0;
}
以上代码是否适合您?如果没有,关于颜色的termainal / shell设置可能会有所不同。
这里有一个问题,这是一些相当奇怪的C字符串文字串联:
printf("%s" KMAG "%s RESET \n", "name", "datestring");
在这种情况下,您的RESET
宏未被替换,因为它是字符串文字的一部分。
此外,要进行调试,您可能会将输出传递给hd
,这会显示您管道输入的十六进制转储。(例如,./a.out | hd
)。然后,您可以逐字节检查程序实际输出的内容,以便进行调试。