I'm trying to remove a file in a C program, but when I use the remove() function it completely removes the file and not sending it to the trash. How can I remove a file to trash?
Thanks in advance!
On Mac OS, you should use the recycleURLs:completionHandler: method in NSWorkspace.
Almost every operating system provides an API to perform a soft delete by moving files to the trash. Unfortunately there is no unified specification on how a trash / recycling bin should work. On Windows you can use IFileOperation, macOS has the NSFileManager and on Linux and BSD you either use a tool that implements the FreeDesktop.org trash specification such as gio trash (GNOME), kioclient5 move (KDE), trash-cli or implement your own.
Another solution is using a cross-platform library that allows to move files and directories to the trash / recycling bin like libtrashcan. This simplifies development because you only use a single function call without worrying about platform specific details.
For Windows See SHFileOperation with FOF_ALLOWUNDO.
http://msdn.microsoft.com/en-us/library/bb762164.aspx
On Mac OS X, you have to call FSMoveObjectToTrashSync (or its async friend) from CoreServices.
In linux i will do...
system("mv your_file.txt /home/username/.local/share/Trash/");
If you are on Windows try this.
Open a Command Prompt (DOS windows) and type
cd \$RECYCLE.BIN
dir \a
and you will get a list of all the trashes of all the users in your computer.
In your program move the file to the proper trash.
Related
I wants to get the names of execute files in some directory in Linux.
How can I do it?
I tried to use opendir like this:
dir = opendir(directoryName);
I need to get only the names of the execute files.
I programming in C.
thanks :)
You should define what you mean by executable files.
That could be any file with its execute bit (it is the owner, group, or other) set. Then test with access(2) & X_OK and/or use stat(2).
That could also be only ELF executables. See elf(5); then the issue might be to check that a file could indeed be executed, which might be difficult (what about missing library dependencies? or ill-formed ELF files?). Maybe use libelf (and/or libmagic to do the equivalent of file(1) command).
To scan recursively a file tree, use nftw(3); to scan just a directory use opendir(3) & readdir(3) (don't forget the closedir!), then you'll probably need to build the complete file path from each directory entry (perhaps using snprintf(3) or asprintf(3))
See also Advanced Linux Programming
Is there any function to check file(s) exist with specific extension or pattern in a directory(s) in Windows and Linux?
For example, to check for files with bbram extension in nvmdir directory.
file_exists(nvmdir .. "\\*.bbram")
A somewhat less elegant solution would be to use popen() or even lesser elegant system() to issue an OS specific "shell" command.
If you want linux/windows compatibility I guess you'll need to do it yourself (look at #ifdef etc to define witch os you're using).
Then you'll need opendir (http://man7.org/linux/man-pages/man3/opendir.3.html), readdir (http://man7.org/linux/man-pages/man3/readdir.3.html) functions.
I let you dig into it.
I want to know how can I easily click (or maybe use some easy shortcuts) on a function name and find all its callee or open where it has been defined. Most of the web manuals in web are really hard to follow or don't happen to work out. Say I want to click on allocuvm and see where it has been defined?
uint newstk=allocuvm(pgdir, USERTOP-PGSIZE, USERTOP);
cscope minimal example
Ingo mentioned it, here is an example.
First you should set on your .vimrc:
set cscopequickfix=s-,c-,d-,i-,t-,e-
Then to the base directory of your project and run:
cscope -Rb
This generates a cscope.out file which contains the parsed information. Generation is reasonably fast, even for huge projects like the Linux kernel.
Open vim and run:
:cs add cscope.out
:cs find c my_func
c is a mnemonic for callers. The other cscope provided queries are also possible, mnemonics are listed under:
help cscope
This adds a list of the callers to the quickfix list, which you can open with:
:copen
Go to the line that interests you and hit enter to jump there.
To find callers of the function name currently under the cursor, add to your .vimrc:
function! Csc()
cscope find c <cword>
copen
endfunction
command! Csc call Csc()
and enter :Csc<enter> when the cursor is on top of the function.
TODO:
do it for the current function under cursor with a single command. Related: Show function name in status line
automatically add the nearest database (parent directories) when you enter a file: how to auto load cscope.out in vim
interactively open the call graph like Eclipse. Related: Generate Call-Tree from cscope database
A word of advice: I love vim, but it is too complicated for me to setup this kind of thing. And it does not take into account classes e.g. in C++. If a project matters enough to you, try to get the project working on some "IDE". It may involve some overhead if the project does not track the IDE configuration files (which are auto-changing blobs that pollute the repo...), but it is worth it to me. For C / C++, my favorite so far was KDevelop 4.
For that, Vim integrates with the cscope tool; see :help cscope for more information.
vi / . --- / is the search function in vi, and . will repeat the same command.
you could also use sed ( stream editor ) if it is a large file
sed
grep can get you the line numbers
read the man page
What is the easiest way to get the UUID of a hard drive partition programmically in Linux? (Using C)
Use libblkid from the util-linux-ng distribution. It includes some sample code.
(Your system probably already has a "blkid" utility linked against some version of libblkid.)
What Nemo said.
Or... You could open and read the contents of /proc/mounts, and ignore all lines that don't start with /dev/disk/by-uuid/. Slightly more work, but no dependency.
Say I have a command line C program which is currently executing, and I want to read a file or execute another binary in the same directory - how can I find out what directory that is?
Note that I'm not looking for the current working directory. The user may have invoked my original program in any of the following ways (and possibly others I don't know about).
../../program
/home/matt/program
PATH=$PATH:/home/matt program
Ideally I'm looking for something which will work on a unix system and windows via MinGW.
http://c-faq.com/osdep/exepath.html
According to the C FAQ it can't be done reliably
Finding current executable's path without /proc/self/exe
Concat getcwd() and dirname(argv[0])