简体   繁体   中英

mount of SD card in C program

My environment: Petalinux on Xilinx/Zynq Soc

I am trying to mount microSD card. I confirmed that following code works to mount SD under root login.

#include <stdio.h>

int main(void)
{
    int ret;
    ret = system("mkdir /media/card");
    printf("%d\n", ret);

    ret = system("mount /dev/mmcblk0p1 /media/card");
    if (ret == 0) {
        printf("sd mounted to /media/card\n");
    } else {
        printf("sd mount : fail\n");
    }
}

Is this a normal way to mount SD card in linux C program? Or are there any special systemcall/API used in C program to mount SD?

Mounting filesystems is done with the mount(2) system call. That's what the mount program (that you're calling via system ) actually does.

The normal way to mount a filesystem from a C program is to use the system call. Forking off a separate process to run a shell command (ie using the system function) is inefficient and prone to bugs (such as shell-injection security vulnerabilities), and gives you less control. The same goes for calling the mkdir program; just use the mkdir(2) system call instead. (Your program above is essentially a shell script written in C, which is silly.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM