C program to eject external devices from command line.

For discussions about programming, and for programming questions and advice


Moderator: Forum moderators

Post Reply
User avatar
user1234
Posts: 413
Joined: Sat Feb 26, 2022 5:48 am
Location: Somewhere on earth
Has thanked: 154 times
Been thanked: 88 times

C program to eject external devices from command line.

Post by user1234 »

I have made an easy c program to eject external devices from command line. It needs command eject and udisksctl.

Github link:
https://github.com/lakshayrohila/ehdd

(can be compiled to any name, still no changes will be needed.)

when compiling use

gcc path/to/source/code/code.c -DVERSION=\"version\" -o the_name_you_want

(Tip: Since version is a string literal, any string like- foo, haha, lolololol, etc. can be used. If version will not be given, the program will automatically use unknown as the version.)

(NOTE: Please post bugs and ask for features replying to either this post or posting it to the Issues section of the github repo)

Last edited by user1234 on Sun May 01, 2022 4:35 pm, edited 3 times in total.

PuppyLinux 🐾 gives new life to old computers ✨

User avatar
user1234
Posts: 413
Joined: Sat Feb 26, 2022 5:48 am
Location: Somewhere on earth
Has thanked: 154 times
Been thanked: 88 times

Re: C program to eject external devices from command line.

Post by user1234 »

Change of line

Code: Select all

fprintf(stderr, "Unable to eject device. \033[1meject\033[0m returned with code %d. Try running \033[1m%s\033[0m.\n"
            , cmd_status, cmd);

to

Code: Select all

fprintf(stderr, "Unable to eject device. \033[1meject\033[0m returned with code %d. Try running \033[1meject %s%s\033[0m.\n"
            , cmd_status, device_path, argv[device_name]);

(same for the power off line as well.)

Previously when eject or udisksctl would have returned error, the program would've printed >/dev/device 2>/dev/device as well, which is to suppress the output of the commands. We don't want the user to suppress the output as it is usefull for manually checking the reason behind eject or udisksctl not working.

PuppyLinux 🐾 gives new life to old computers ✨

User avatar
user1234
Posts: 413
Joined: Sat Feb 26, 2022 5:48 am
Location: Somewhere on earth
Has thanked: 154 times
Been thanked: 88 times

Re: C program to eject external devices from command line.

Post by user1234 »

I have updated my code. This adds support for --ejectonly and --shutdownonly options and corrects few more things. I soon will upload this program to github.

PuppyLinux 🐾 gives new life to old computers ✨

User avatar
user1234
Posts: 413
Joined: Sat Feb 26, 2022 5:48 am
Location: Somewhere on earth
Has thanked: 154 times
Been thanked: 88 times

Re: C program to eject external devices from command line.

Post by user1234 »

Here is the link of the program on github.

PuppyLinux 🐾 gives new life to old computers ✨

User avatar
user1234
Posts: 413
Joined: Sat Feb 26, 2022 5:48 am
Location: Somewhere on earth
Has thanked: 154 times
Been thanked: 88 times

Re: C program to eject external devices from command line.

Post by user1234 »

ehdd updated to be not just a sample C code, but be a complete program! Read this for more info.

PuppyLinux 🐾 gives new life to old computers ✨

Post Reply

Return to “Programming”