sfm

Simple file manager
git clone git://git.christosmarg.xyz/sfm.git
Log | Files | Refs | README | LICENSE

commit 0ab1e6598cac6ca3b6f28b9eac58bbc8039bb7f6
Author: Christos Margiolis <christos@margiolis.net>
Date:   Tue, 27 Oct 2020 14:10:11 +0200

initial commit

Diffstat:
AMakefile | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aconfig.mk | 32++++++++++++++++++++++++++++++++
Asfm.c | 96+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 182 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -0,0 +1,54 @@ +# See LICENSE file for copyright and license details. +# sfm - simple file manager +.POSIX: + +include config.mk + +BIN = sfm +DIST = ${BIN}-${VERSION} +MAN1 = ${BIN}.1 + +EXT = c +SRC = sfm.c +OBJ = ${SRC:.${EXT}=.o} + +all: options ${BIN} + +options: + @echo ${BIN} build options: + @echo "CFLAGS = ${CFLAGS}" + @echo "LDFLAGS = ${LDFLAGS}" + @echo "CC = ${CC}" + +${BIN}: ${OBJ} + ${CC} ${LDFLAGS} ${OBJ} -o $@ + +.${EXT}.o: + ${CC} ${CFLAGS} -c $< + +dist: clean + ${MKDIR} ${DIST} + ${CP} -R config.mk Makefile ${SRC} ${DIST} + ${TAR} ${DIST}.tar ${DIST} + ${GZIP} ${DIST}.tar + ${RM_DIR} ${DIST} + +run: + ./${BIN} + +install: all + ${MKDIR} ${DESTDIR}${BIN_DIR} ${DESTDIR}${MAN_DIR} + ${CP} ${BIN} ${BIN_DIR} + ${CP} ${MAN1} ${DESTDIR}${MAN_DIR} + sed "s/VERSION/${VERSION}/g" < ${MAN1} > ${DESTDIR}${MAN_DIR}/${MAN1} + chmod 755 ${DESTDIR}${BIN_DIR}/${BIN} + chmod 644 ${DESTDIR}${MAN_DIR}/${MAN1} + +uninstall: + ${RM} ${DESTDIR}${BIN_DIR}/${BIN} + ${RM} ${DESTDIR}${MAN_DIR}/${MAN1} + +clean: + ${RM} ${BIN} ${OBJ} ${DIST}.tar.gz + +.PHONY: all options clean dist install uninstall run diff --git a/config.mk b/config.mk @@ -0,0 +1,32 @@ +# See LICENSE file for copyright and license details. +# sfm version +VERSION = 0 + +# paths +PREFIX = /usr/local +MAN_DIR = ${PREFIX}/man/man1 +BIN_DIR = ${PREFIX}/bin + +# includes and libs +INCS = -Iinclude +LIBS = -Llib -lncurses # add ncursesw + +# flags +CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_POSIX_C_SOURCE=200809L \ + -DVERSION=\"${VERSION}\" +CFLAGS = -std=c99 -pedantic -Wall -Wno-deprecated-declarations \ + -O3 ${INCS} ${CPPFLAGS} +LDFLAGS = ${LIBS} + +# utils +CP = cp -f +RM = rm -f +RM_DIR = rm -rf +MV = mv +MKDIR = mkdir -p +RM_DIR = rm -rf +TAR = tar -cf +GZIP = gzip + +# compiler +CC = gcc diff --git a/sfm.c b/sfm.c @@ -0,0 +1,96 @@ +#include <sys/types.h> + +#include <dirent.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <ncurses.h> + +/* globals */ +static const char *envs[] = { + "HOME", + "USER", + "EDITOR", + "BROWSER", + "PAGER" +}; + +atatic unsigned int maxcols = 3; +static int nfiles = 0; + +/* function declarations */ +static void curses_init(void); +static void files_list(const char *); + +/* function implementations */ +void +curses_init(void) +{ + initscr(); + noecho(); + cbreak(); + curs_set(0); + keypad(stdscr, 1); +} + +void +files_list(const char *dirname) +{ + DIR *dir; + struct dirent *dp; + char *filename; + + dir = opendir(dirname); + // get hidden files flag + while ((dp = readdir(dir)) != NULL) { + if (strcmp(dp->d_name, dirname) && strcmp(dp->d_name, ".") + && strcmp(dp->d_name, "..")) { + filename = dp->d_name; + printw("%s\n", filename); + nfiles++; + } + } + refresh(); + closedir(dir); +} + +int +main(int argc, char *argv[]) +{ + char cwd[256], *curdir; + int c, i; + + if ((curdir = getcwd(cwd, sizeof(cwd))) == NULL) { + return -1; + } + curses_init(); + + while ((c = getch()) != 'q') { + erase(); + + attron(A_BOLD); + mvprintw(0, 0, "%s\n", curdir); + attroff(A_BOLD); + + files_list(curdir); + + switch (c) { + case 'h': /* FALLTHROUGH */ + case KEY_LEFT: + chdir(".."); + break; + case 'l': /* FALLTHROUGH */ + case KEY_RIGHT: + break; + } + curdir = getcwd(cwd, sizeof(cwd)); + + refresh(); + } + + endwin(); + + return 0; +}