From 4f49b16079b27d7ce633619e4cddfdf2bffbc952 Mon Sep 17 00:00:00 2001 From: cashmere Date: Fri, 4 Sep 2026 09:05:24 +0200 Subject: initial commit --- src/xsystem.c | 614 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 614 insertions(+) create mode 100644 src/xsystem.c (limited to 'src/xsystem.c') diff --git a/src/xsystem.c b/src/xsystem.c new file mode 100644 index 0000000..4c7590c --- /dev/null +++ b/src/xsystem.c @@ -0,0 +1,614 @@ +/* +** Copyright (c) 2025 D. Richard Hipp +** +** This program is free software; you can redistribute it and/or +** modify it under the terms of the Simplified BSD License (also +** known as the "2-Clause License" or "FreeBSD License".) + +** This program is distributed in the hope that it will be useful, +** but without any warranty; without even the implied warranty of +** merchantability or fitness for a particular purpose. +** +** Author contact information: +** drh@sqlite.org +** +******************************************************************************* +** +** This file contains code used to implement "fossil system ..." command. +** +** Fossil is frequently used by people familiar with Unix but who must +** sometimes also work on Windows systems. The "fossil sys ..." command +** provides a few work-arounds for command unix command-line utilities to +** help make development on Windows more habitable for long-time unix +** users. The commands provided here are normally cheap substitutes to +** their more feature-reach unix counterparts. But they are sufficient to +** get the job done. +** +** This source code file is called "xsystem.c" with the 'x' up front because +** if it were called "system.c", then makeheaders would generate a "system.h" +** header file, and that might be confused with an actual system header +** file. +*/ +#include "config.h" +#include "xsystem.h" +#include "qrf.h" +#include +#ifdef _WIN32 +# include +#endif + + +/* Date and time */ +void xsystem_date(int argc, char **argv){ + (void)argc; + (void)argv; + fossil_print("%z = ", cgi_iso8601_datestamp()); + fossil_print("%z\n", cgi_rfc822_datestamp(time(0))); +} + +/* Present working directory */ +void xsystem_pwd(int argc, char **argv){ + char *zPwd = file_getcwd(0, 0); + fossil_print("%z\n", zPwd); +} + +/* Implement "stty size" */ +void xsystem_stty(int argc, char **argv){ + TerminalSize ts; + if( argc!=2 || strcmp(argv[1],"size")!=0 ){ + fossil_print("ERROR: only \"stty size\" is supported\n"); + }else{ + terminal_get_size(&ts); + fossil_print("%d %d\n", ts.nLines, ts.nColumns); + } +} + +/* Show where an executable is located on PATH */ +void xsystem_which(int argc, char **argv){ + int ePrint = 1; + int i; + for(i=1; i= mx ){ + fossil_print("\n%s", z); + sumW = w; + }else{ + fossil_print(", %s", z); + sumW += w+2; + } + } + fossil_free(zSql); + sqlite3_finalize(pStmt); + if( sumW>0 ) fossil_print("\n"); + }else{ + /* Column mode with just filenames */ + sqlite3_qrf_spec spec; + char *zSql; + memset(&spec, 0, sizeof(spec)); + spec.iVersion = 1; + spec.xWrite = xsystem_write; + spec.eStyle = QRF_STYLE_Column; + spec.bTitles = QRF_No; + spec.eEsc = QRF_No; + if( mFlags & LS_COLUMNS ){ + spec.nScreenWidth = terminal_get_width(80); + spec.bSplitColumn = QRF_Yes; + } + zSql = mprintf("SELECT color(fn,mode) FROM ls ORDER BY %s", + xsystem_ls_orderby(mFlags)); + sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); + fossil_free(zSql); + sqlite3_format_query_result(pStmt, &spec, 0); + sqlite3_finalize(pStmt); + } + sqlite3_exec(db, "DELETE FROM ls;", 0, 0, 0); +} + +/* List files "ls" +** Options: +** +** -a Show files that begin with "." +** -C List by columns +** --color=WHEN Colorize output? +** -d Show just directory names, not content +** -l Long listing +** -m Comma-separated list +** -r Reverse sort +** -S Sort by size, largest first +** -t Sort by mtime, newest first +*/ +void xsystem_ls(int argc, char **argv){ + int i, rc; + sqlite3 *db; + sqlite3_stmt *pStmt = 0; + int mFlags = 0; + int nFile = 0; + int nDir = 0; + int bAutoColor = 1; + int needBlankLine = 0; + rc = sqlite3_open(":memory:", &db); + if( rc || db==0 ){ + fossil_fatal("Cannot open in-memory database"); + } + sqlite3_exec(db, "CREATE TABLE ls(fn,mtime,size,mode,dlen);", 0,0,0); + rc = sqlite3_prepare_v2(db, "INSERT INTO ls VALUES(?1,?2,?3,?4,?5)", + -1, &pStmt, 0); + if( rc || db==0 ){ + fossil_fatal("Cannot prepare INSERT statement"); + } + for(i=1; i0 ){ + xsystem_ls_render(db, mFlags); + needBlankLine = 1; + }else if( nDir==0 ){ + xsystem_ls_insert(pStmt, ".", mFlags); + xsystem_ls_render(db, mFlags); + } + if( nDir>0 ){ + for(i=1; i>12)&15)=10,name||' -> '||data,name) AS Name" + " FROM zip;"; + n = 4; + }else{ + a[2] = ".mode list"; + a[3] = "SELECT if(((mode>>12)&15)==10,'symlink-ignored: '||name," + "writefile(name,data,mode,mtime) IS NULL," + "'error: '||name,'extracting: '||name) FROM zip"; + n = 4; + } + a[n] = 0; + sqlite3_shell(n,a); +} + +/* +** zip [OPTIONS] ZIPFILE FILE ... +*/ +void xsystem_zip(int argc, char **argv){ + int i; + for(i=0; i