我是 MAC OS X 的新手。我想获取新连接磁盘(笔式驱动器)的挂载点。我已经尝试了一些代码并从命令中获取了挂载路径
diskutil 信息 disk1S2 | grep \" 挂载点:\" | awk '{打印 $3}'
其中disk1S2是新附加磁盘的标识符。
但是我想从 C 代码中获取挂载点而不依赖于 command。
所以尝试了这段代码
#include <stdio.h>
#include <DiskArbitration/DiskArbitration.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <DiskArbitration/DASession.h>
#include <stdlib.h>
#include <strings.h>
#define MAXPATHLEN 500
void hello_disk(DADiskRef disk, void *context){
CFDictionaryRef diskinfo,dict;
diskinfo = DADiskCopyDescription(disk);
CFURLRef fspath = CFDictionaryGetValue(dict,&kDADiskDescriptionVolumePathKey);
char buf[MAXPATHLEN];
if (CFURLGetFileSystemRepresentation(fspath, false, (UInt8 *)buf, sizeof(buf))) {
printf("Disk %s mounted at %s\n",DADiskGetBSDName(disk),buf);
// Print the complete dictionary for debugging.
CFShow(diskinfo);
}else {
// Something is *really* wrong. /
}
}
void goodbye_disk(DADiskRef disk, void *context){
printf("disk %s disappeared\n", DADiskGetBSDName(disk));
}
int main(){
DASessionRef session;
session = DASessionCreate(kCFAllocatorDefault);
DARegisterDiskAppearedCallback(session, NULL, hello_disk, NULL);
DARegisterDiskDisappearedCallback(session, NULL, goodbye_disk, NULL);
DASessionScheduleWithRunLoop(session,CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
CFRunLoopRun();
CFRelease(session);
exit(0);
}
我通过这个命令运行这段代码
gcc DAD.c -o DAD -framework DiskArbitration -framework Foundation
但我收到了这个错误
分段错误:11
- 为什么我收到此错误?
- 如何在 MAC OS 中获取挂载点?