【问题标题】:how to change color of xeyes using a path in c?如何使用 c 中的路径更改 xeyes 的颜色?
【发布时间】:2015-04-01 20:32:23
【问题描述】:

所有这些都在 Linux 而非 Windows 中

你好,我想知道如何像在终端中那样改变 xeyes 的颜色

xeyes -fg 蓝色 现在我想使用路径在 c 程序中执行此操作

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

#include <string.h>
#include <malloc.h>

//#inlcude <windows.h>

#define LB_SIZE 1024

int main(int argc, char *argv[])
{
  char fullPathName[] = "/usr/bin/X11/xeyes";
  char *myArgv[LB_SIZE];  // an array of pointers

  myArgv[0] = (char *) malloc(strlen(fullPathName) + 1);
  strcpy(myArgv[0], fullPathName);

  myArgv[1] = NULL;  // last element should be a NULL pointer

  execvp(fullPathName, myArgv);
  exit(0);  // should not be reached
}

如果我只是调用 /usr/bin/X11/xeyes 它只会显示眼睛

现在我正在尝试添加 /usr/bin/X11/xeyes-fg 之类的命令,但它不起作用

有什么建议吗?

【问题讨论】:

    标签: c linux terminal x11


    【解决方案1】:

    您可以添加到参数向量上,如下所示:

    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    
    #include <string.h>
    #include <malloc.h>
    
    #define LB_SIZE 1024
    
    int main(int argc, char *argv[])
    {
      char fullPathName[] = "/usr/bin/X11/xeyes";
      char *myArgv[LB_SIZE];  // an array of pointers
      int n = 0;
    
      myArgv[0] = (char *) malloc(strlen(fullPathName) + 1);
      strcpy(myArgv[n++], fullPathName);
      myArgv[n++] = "-fg";
      myArgv[n++] = "blue";
    
      myArgv[n] = NULL;  // last element should be a NULL pointer
    
      execvp(fullPathName, myArgv);
      exit(0);  // should not be reached
    }
    

    这是结果的图片:

    顺便说一句,我本来希望strace 显示正在打开的文件 rgb.txt,但使用-f 选项看不到这个(假设它发生在服务器中)。 “蓝色”确实出现在跟踪中,但仅出现在 exec 调用中,例如,

    execve("/usr/bin/X11/xeyes", ["/usr/bin/X11/xeyes", "-fg", "blue"], [/* 62 vars */]) = 0
    

    【讨论】:

    • 当我运行该示例时它起作用了 - 我可以附上屏幕截图,但这可能无济于事。顺便说一下,我能看到的唯一问题是在您的机器上搜索 rgb.txt 是否有问题(这会使命名颜色不起作用)。您可以通过使用strace 运行您的应用程序来检查它是否成功打开了该文件。
    猜你喜欢
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    相关资源
    最近更新 更多