检测Linux的C应用程序中是否安装了USB磁盘

开源爱好者

我想检查USB磁盘是否安装在C应用程序中。我知道在脚本中我可以通过mount | grep / mnt(udev挂载USB驱动器的挂载点),但是我需要在C应用程序中执行此操作。之前我曾经使用system(“ sh script.sh”)来完成此操作,但是这样做会导致一些严重的问题,因为此代码在时间紧迫的线程中运行。

电信

如果需要检查安装点的完整列表,请使用getmntent(3)或其线程安全的GNU扩展getmntent_r(3)

如果只想快速检查给定目录上是否装有文件系统,请使用该stat(2)系列中的功能之一例如,如果要检查是否/mnt已安装文件系统,则可以执行以下操作:

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

struct stat mountpoint;
struct stat parent;

/* Get the stat structure of the directory...*/
if stat("/mnt", &mountpoint) == -1) {
    perror("failed to stat mountpoint:");
    exit(EXIT_FAILURE);
}

/* ... and its parent. */
if stat("/mnt/..", &parent) == -1) {
    perror("failed to stat parent:");
    exit(EXIT_FAILURE);
}

/* Compare the st_dev fields in the results: if they are
   equal, then both the directory and its parent belong 
   to the same filesystem, and so the directory is not 
   currently a mount point.
*/
if (mountpoint.st_dev == parent.st_dev) {
    printf("No, there is nothing mounted in that directory.\n");
} else {
    printf("Yes, there is currently a filesystem mounted.\n");
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

iMessage应用程序可检测接收方是否安装了相同的应用程序

如何检测 C# 中安装了 UWP 应用程序 (Skype)?

如何检测Windows Phone 8中是否更新了应用程序或是否全新安装了该应用程序

Android:检测附近的设备是否安装了某些应用程序

检查是否从Google Play安装了应用程序

检查Windows中是否安装了Java(或任何应用程序)

如何检查用户是否在网页中安装了移动应用程序?

如何以编程方式检查Android中是否安装了应用程序?

Xamarin:检查IOS设备中是否安装了任何特定的应用程序

从Deeplink重定向时,如何检查用户设备中是否安装了应用程序

Android:如何知道是否已使用ADB在Android设备中安装了任何应用程序?

我们如何知道Linux中安装了哪些应用程序?

如何找出物理磁盘上安装了应用程序的位置?

检测应用程序是否安装,不返回false

我如何知道是否从Apple App Store或OSX中的其他商店安装了应用程序

从 Ubuntu 软件安装应用程序时是否安装了依赖项?

是否可以在运行时可靠地检测哪个商店安装了Android应用程序(Google Play或Amazon Market)?

如何检测是否已从应用商店中安装了当前正在运行的应用?

安装程序在哪里安装了WinForm应用程序?

如何确认是否已从Deeplink / referlink安装了应用程序?

检查是否在用户设备上安装了“健康”应用程序。(迅速)

Windows是否按应用程序或系统范围安装了视频编码器?

检查 Windows 上是否安装了Wireguard 应用程序的可靠方法?

我如何知道Safari是否已在iPhone上安装了应用程序

在运行Add-AppxPackage之前检查是否安装了应用程序版本

检查是否安装了第三方应用程序?

如何检查是否通过iPhone上的网页安装了应用程序?

Bash脚本自动化检查是否安装了特定版本的应用程序

popen 可以找出是否安装了某个应用程序吗?