Skip to content

Instantly share code, notes, and snippets.

@sourtin
Last active October 28, 2021 09:27
Show Gist options
  • Save sourtin/3e2f07d46a4fabf9eac98d61b0713198 to your computer and use it in GitHub Desktop.
Save sourtin/3e2f07d46a4fabf9eac98d61b0713198 to your computer and use it in GitHub Desktop.
Mirror of Night Productions' rmtrash

rmtrash

rmtrash is a small utility that will move the file to OS X's Trash rather than obliterating the file (as rm does).

Last updated: January 18, 2005

#Makefile for rmtrash
CFLAGS=-O3 -g -Wall -Wno-four-char-constants -Wno-unknown-pragmas
LDFLAGS=-framework Foundation -arch ppc -prebind
CC=gcc
DESTDIR=/usr/local
all: rmtrash
install:
install -s -o root -g wheel -m 755 rmtrash ${DESTDIR}/bin
install -o root -g wheel -m 644 rmtrash.1 ${DESTDIR}/man/man1
uninstall:
/bin/rm ${DESTDIR}/bin/rmtrash
/bin/rm ${DESTDIR}/share/man/man1/rmtrash.1
clean:
/bin/rm rmtrash
rmtrash:
${CC} ${CFLAGS} ${LDFLAGS} rmtrash.m -o rmtrash
.TH RMTRASH 1 "20 August 2003" "rmtrash 0.3.1" "BSD General Commands Manual"
.SH NAME
rmtrash \- move files to the trash
.SH SYNOPSIS
.B rmtrash
[\fI-u USERNAME\fR] [\fI-h\fR] [\fI-v\fR] file-name [\fIfile-name\fR]
.SH DESCRIPTION
.PP
This command moves files to the trash rather than removing them totally from the file system. Very useful if you decide you want that file after all...
.TP
\fB\-u\fR \fIUSERNAME\fR
an optional argument. This will move the file to the specified user's trash. Note that you need sufficient privileges to accomplish this.
.TP
\fB\-h\fR
displays a small help screen displaying these options.
.TP
\fB\-v\fR
prints out version info on \fBrmtrash\fR.
.SH BUGS
Not directly related to \fBrmtrash\fR; the trash can icon will not display that there is trash in it if it was empty and you used this command to place something there. It will stay empty until you throw something away in the Finder.
.SH AUTHOR
Written by Darkshadow <darkshadow02@mac.com>
.SH COPYRIGHT
Copyright \(co 2003 Night Productions.
.br
This is free software. There is NO warranty; not even for merchantability or fitness for a particular purpose.
.SH "SEE ALSO"
.BR rm (1),
.BR rmdir (1)
#import <Foundation/Foundation.h>
#import <unistd.h>
#define manager [NSFileManager defaultManager]
NSString *copyNumber(NSString *fileName,int copyNum)
{
NSString *ext=[[fileName lastPathComponent] pathExtension];
NSString *returnString;
if ([ext length] > 0) {
NSString *tempName;
if (copyNum > 1) {
tempName=[fileName substringWithRange:NSMakeRange(0,([fileName length] - ([ext length] + 2)))];
} else {
tempName=[fileName substringWithRange:NSMakeRange(0,([fileName length] - ([ext length] + 1)))];
}
returnString=[[NSString alloc]initWithFormat:@"%@%i.%@",tempName,copyNum,ext];
} else {
NSString *tempName=fileName;
if (copyNum > 1) {
tempName=[fileName substringWithRange:NSMakeRange(0,([fileName length] - 1))];
}
returnString=[[NSString alloc]initWithFormat:@"%@%i",tempName,copyNum];
}
if ([manager fileExistsAtPath:returnString]) {
NSString *tempString=[NSString stringWithString:copyNumber(returnString,(copyNum + 1))];
[returnString release];
returnString=[[NSString alloc]initWithString:tempString];
}
return [returnString autorelease];
}
NSString *trashFileName(NSString *fileName)
{
NSString *ext=[[fileName lastPathComponent] pathExtension];
NSString *copyName;
if ([ext length] > 0) {
NSString *tempName=[fileName substringWithRange:NSMakeRange(0,([fileName length] - ([ext length] + 1)))];
copyName=[[NSString alloc]initWithFormat:@"%@ Copy.%@",tempName,ext];
} else {
copyName=[[NSString alloc]initWithFormat:@"%@ Copy",fileName];
}
if ([manager fileExistsAtPath:copyName]) {
NSString *tempString=[NSString stringWithString:copyNumber(copyName,1)];
[copyName release];
copyName=[[NSString alloc]initWithString:tempString];
}
return [copyName autorelease];
}
void move_to_trash(BOOL user, NSString *userString, NSString *thefile)
{
NSString *fileString=thefile;
unichar firstChar=[fileString characterAtIndex:0];
NSString *trashPath=nil;
if (firstChar == '~') {
fileString=[thefile stringByExpandingTildeInPath];
}
if (![manager fileExistsAtPath:fileString]) {
printf("%s: File or directory does not exist.\n",[fileString UTF8String]);
return;
}
trashPath=[[NSString stringWithFormat:@"~%@/.Trash/%@",userString,[fileString lastPathComponent]]stringByExpandingTildeInPath];
if (![manager fileExistsAtPath:[[NSString stringWithFormat:@"~%@/.Trash",userString]stringByExpandingTildeInPath]]) {
printf("%s: Unknown user!\n",[userString UTF8String]);
return;
}
if ([manager fileExistsAtPath:trashPath]) {
//file exists in trash with this name...
NSString *copyString=[NSString stringWithString:trashFileName(trashPath)];
if (![manager movePath:fileString toPath:copyString handler:nil]) {
printf("Could not move \"%s\" to the trash!\n\t(Perhaps you don't have sufficient privileges?)\n\n",[fileString UTF8String]);
if ([manager fileExistsAtPath:copyString]) {
[manager removeFileAtPath:copyString handler:nil];
}
return;
}
} else if (![manager movePath:fileString toPath:trashPath handler:nil]) {
printf("Could not move \"%s\" to the trash!\n\t(Perhaps you don't have sufficient privileges?)\n\n",[fileString UTF8String]);
if ([manager fileExistsAtPath:trashPath]) {
[manager removeFileAtPath:trashPath handler:nil];
}
return;
}
}
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
NSString *file, *userStr=@"";
BOOL user=NO;
int c,i;
if (argc == 1) {
printf("USAGE: %s [-h] [-v] [-u USERNAME] FILENAME\n\n",argv[0]);
[pool release];
return 1;
}
while ((c=getopt(argc, argv, "uhv")) != EOF) {
switch (c) {
case 'u':
user=YES;
break;
case 'h':
printf("rmtrash options:\n\n");
printf("\t-u USERNAME\tmove the file to some other user's trash.\n");
printf("\t\t\t(note that you need sufficient privileges to do this.)\n");
printf("\t-h\t\tthis screen\n");
printf("\t-v\t\tprint out version info\n\n");
[pool release];
return 0;
break;
case 'v':
printf("rmtrash version 0.3.3\n\tCopyright 2003 Night Productions\n\n");
[pool release];
return 0;
break;
default:
printf("USAGE: %s [-h] [-v] [-u USERNAME] FILENAME\n\n",argv[0]);
return 0;
break;
}
}
if (user)
userStr=[NSString stringWithUTF8String:argv[2]];
for (i= optind; i < argc; i++) {
if (user && (i == 2)) {
//skip it
} else {
file=[NSString stringWithUTF8String:argv[i]];
move_to_trash(user, userStr, file);
file=nil;
}
}
[pool release];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment