3

I am new to MAC OS X. I want to get Mount Point of newly attached disk (Pen Drive). I already tried some code and got mount path from command

diskutil info disk1S2 | grep \" Mount Point: \" | awk '{print $3}'

where disk1S2 is Identifier of newly attached disk.

But I want to get mount point from C code without depending upon command.

So tried this code

#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);
}

and I run this code by this command

gcc DAD.c -o DAD -framework DiskArbitration -framework Foundation

but I am getting this error

Segmentation fault: 11

  • Why I am getting this error ?
  • How can I get mount point in MAC OS ?
Omkar
  • 343
  • 3
  • 12

0 Answers0