* freeramdisk implementation for busybox
*
* Copyright (C) 2000 and written by Emanuele Caratti <wiz@iol.it>
- * Adjusted a bit by Erik Andersen <andersee@debian.org>
+ * Adjusted a bit by Erik Andersen <andersen@codepoet.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
#include <stdio.h>
#include <string.h>
-#include <sys/mount.h>
#include <sys/types.h>
-#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
-#include <errno.h>
-#include "internal.h"
+#include <stdlib.h>
+#include <unistd.h>
+#include "busybox.h"
-static const char freeramdisk_usage[] =
- "freeramdisk DEVICE\n\n"
- "Frees all memory used by the specified ramdisk.\n";
+/* From linux/fs.h */
+#define BLKFLSBUF _IO(0x12,97) /* flush buffer cache */
extern int
freeramdisk_main(int argc, char **argv)
{
- char rname[256] = "/dev/ram";
- int f;
+ int result;
+ int fd;
- if (argc > 2 || ( argv[1] && *argv[1] == '-')) {
- usage(freeramdisk_usage);
+ if (argc != 2) {
+ bb_show_usage();
}
- if (argc >1)
- strcpy(rname, argv[1]);
+ fd = bb_xopen(argv[1], O_RDWR);
- if ((f = open(rname, O_RDWR)) == -1) {
- fatalError( "freeramdisk: cannot open %s: %s", rname, strerror(errno));
+ result = ioctl(fd, BLKFLSBUF);
+#ifdef CONFIG_FEATURE_CLEAN_UP
+ close(fd);
+#endif
+ if (result < 0) {
+ bb_perror_msg_and_die("failed ioctl on %s", argv[1]);
}
- if (ioctl(f, BLKFLSBUF) < 0) {
- fatalError( "freeramdisk: failed ioctl on %s: %s", rname, strerror(errno));
- }
- close(f);
- exit(TRUE);
+
+ /* Don't bother closing. Exit does
+ * that, so we can save a few bytes */
+ return EXIT_SUCCESS;
}
/*