C 从来都不是我的强大力量,但我决定试一试。以下是我的代码,但是在运行时,它给出了分段错误(核心转储)。
基本上我想要的是检查文件夹是否为空(mtp设备安装在其中),如果它是空的,运行mount命令,如果不是,运行另一个命令。
#include <sys/types.h>
#include <dirent.h>
#include <libgen.h>
#include <libnotify/notify.h>
#include <stdio.h>
#include <unistd.h>
void main ()
{
int n = 0;
struct dirent *d;
const char *dir_path="~/Nexus";
DIR *dir = opendir(dir_path);
while ((d = readdir(dir)) != NULL) {
if(++n > 2)
break;
}
closedir(dir);
if (n <= 2) //Directory Empty
{
notify_init ("Galaxy Nexus mounter");
NotifyNotification * Mount = notify_notification_new ("Galaxy Nexus", "Mounted at ~/Nexus", "/home/tristan202/bin/test/android_on.png");
system("jmtpfs ~/Nexus");
notify_notification_show (Mount, NULL);
}
else
{
notify_init ("Galaxy Nexus mounter");
NotifyNotification * uMount = notify_notification_new ("Galaxy Nexus", "Unmounted", "/home/tristan202/bin/test/android_off.png");
system("fusermount -u ~/Nexus");
notify_notification_show (uMount, NULL);
}
}
欢迎任何建议。
编辑
#include <sys/types.h>
#include <dirent.h>
#include <libgen.h>
#include <libnotify/notify.h>
#include <stdio.h>
#include <unistd.h>
int main ()
{
int n = 0;
struct dirent *d;
const char *dir_path="/home/tristan202/Nexus";
DIR *dir = opendir(dir_path);
while ((d = readdir(dir)) != NULL) {
if(++n > 2)
break;
}
closedir(dir);
if (n <= 2) //Directory Empty
{
notify_init ("Galaxy Nexus mounter");
NotifyNotification * Mount = notify_notification_new ("Galaxy Nexus", "Mounted at ~/Nexus", "/home/tristan202/bin/test/android_on.png");
system("jmtpfs ~/Nexus");
notify_notification_show (Mount, NULL);
}
else
{
notify_init ("Galaxy Nexus mounter");
NotifyNotification * uMount = notify_notification_new ("Galaxy Nexus", "Unmounted", "/home/tristan202/bin/test/android_off.png");
system("fusermount -u ~/Nexus");
notify_notification_show (uMount, NULL);
}
}