diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/bulk_ln.c (renamed from src/bulk_ln/bulk_ln.c) | 199 | ||||
| -rw-r--r-- | src/bulk_ln/bulk_ln.h | 10 | ||||
| -rw-r--r-- | src/bulk_ln/bulk_ln_main.c | 10 | ||||
| -rw-r--r-- | src/common/commonbase.h | 23 |
4 files changed, 142 insertions, 100 deletions
diff --git a/src/bulk_ln/bulk_ln.c b/src/bulk_ln.c index ffd360a..f3cda22 100644 --- a/src/bulk_ln/bulk_ln.c +++ b/src/bulk_ln.c @@ -1,8 +1,59 @@ +#if 0 +--[===========================================================================[ -/* This */ -#include "bulk_ln.h" + How to build: + + lua src/bulk_ln.c + + How to use: + + ./bulk-ln --help + + ]===========================================================================] +function shEsc(s)return "'".. s:gsub("'", "'\"'\"'") .."'" end +do + local thisFile = debug.getinfo(1,"S").source:sub(2) + local sh = io.popen("sh", "w") + assert(sh, "Failed to launch a shell") + sh:write([=[set -e \ + \ + && CC="${CC:-gcc}" \ + && LD="${LD:-gcc}" \ + && OBJDUMP="${OBJDUMP:-objdump}" \ + && RIMRAF="${RIMRAF:-rm -rf}" \ + && BINEXT="${BINEXT:-}" \ + && CFLAGS="${CFLAGS:- -std=c99 -Wall -Werror -pedantic -nostdlib -nodefaultlibs}" \ + && LDFLAGS="${LDFLAGS:-}" \ + && TMPDIR="${TMPDIR:-/tmp}" \ + && OUTFILE="bulk-ln${BINEXT?}" \ + \ + && if test -z "$PROJECT_VERSION" ;then true \ + && tag="$(git tag|grep -E '^v'|sed -E 's,^v,,'|sort -V|tail -n1)" \ + && cnt="$(git rev-list --count "v${tag:?}"..HEAD)" \ + && hsh="$(git rev-parse --short HEAD)" \ + && if test -n "$tag" -a -n "$cnt" -a -n "$hsh" ;then true \ + && PROJECT_VERSION="${tag:?}-${cnt:?}-g${hsh:?}" \ + ;else true \ + && PROJECT_VERSION="$(date -u +0.0.0-%Y%m%d.%H%M%S)" \ + ;fi \ + ;fi \ + \ + && ${CC:?} -c -o "${TMPDIR:?}"/YC71NcBIJMwpwUfL ]=].. shEsc(thisFile) ..[=[ ${CFLAGS?} -DPROJECT_VERSION="${PROJECT_VERSION:?}" -DEXPOSE_MAIN=1 \ + && ${LD:?} -o ./"${OUTFILE:?}" "${TMPDIR:?}"/YC71NcBIJMwpwUfL ${LDFLAGS?} \ + && ${RIMRAF:?} "${TMPDIR:?}"/YC71NcBIJMwpwUfL \ + && bullshit=$(${OBJDUMP?} -p "${OUTFILE:?}"|grep DLL\ Name|egrep -v ' (KERNEL32.dll|SHELL32.dll|WS2_32.dll|ADVAPI32.dll|msvcrt.dll|GDI32.dll|USER32.dll)$'||true) \ + && if test -n "$bullshit"; then printf '\n ERROR: Bullshit has sneaked in:\n\n%s\n\n' "$bullshit"; rm "${OUTFILE:?}"; false; fi \ +]=]) + -- Shitty systems maybe need adaptions like: + -- && LDFLAGS="-Wl,-lws2_32,-l:libwinpthread.a" \ + local ok, how, code = sh:close() + if not ok then os.exit(code) end +end +--[===========================================================================[ +#endif + +#define _POSIX_C_SOURCE 200809L -/* System */ #include <assert.h> #include <errno.h> #include <stdio.h> @@ -11,53 +62,69 @@ #include <sys/stat.h> #include <unistd.h> +#define STR_QUOT_IAHGEWIH(s) #s +#define STR_QUOT(s) STR_QUOT_IAHGEWIH(s) -#define DATA_FILE_FIELD_SEP_CHR '\t' +#ifndef PRIVATE +# define PRIVATE(T) static T +#endif -typedef int bool; -typedef struct BulkLn BulkLn; +#ifndef DATA_FILE_FIELD_SEP_CHR +# define DATA_FILE_FIELD_SEP_CHR '\t' +#endif -/* Root context */ -struct BulkLn { +typedef struct BulkLn { + /* Root context + */ - /** File (path) where we read the paths-to-link from. The special - * value - (dash) means to use the stdin stream. */ char *dataFilePath; + /* File (path) where we read the paths-to-link from. The special + * value - (dash) means to use the stdin stream. + */ FILE *dataFd; - /** Count of links we created */ int createdLinksCount; + /* Count of links we created + */ - /** Count of direcrories we created */ int createdDirsCount; + /* Count of direcrories we created + */ - /** If a dry-run got requested */ - bool dryRun; + int dryRun; + /* If a dry-run got requested + */ - /** If true, we will print to stdout for every link we create (like we do - * in dry run) */ - bool isPrintEachCreateLink; + int isPrintEachCreateLink; + /* If true, we will print to stdout for every link we create (like we do + * in dry run) + 0*/ - /** print every call we make to mkdir to stdout. Usually only useful for - * debugging */ - bool isPrintEachMkdir; + int isPrintEachMkdir; + /* print every call we make to mkdir to stdout. Usually only useful for + * debugging + */ - /** if true, we periodically print some kind of progress to stderr */ - bool isPrintStatus; + int isPrintStatus; + /* if true, we periodically print some kind of progress to stderr + */ - /** if true, we print a summary what we did at the end of the run to stderr */ - bool isPrintSummary; + int isPrintSummary; + /* if true, we print a summary what we did at the end of the run to stderr + */ - /** if true, we will override existing files. This will be enabled for - * example if --force got specified. */ - bool isRelinkExistingFiles; -}; + int isRelinkExistingFiles; + /* if true, we will override existing files. This will be enabled for + * example if --force got specified. + */ +} BulkLn; -static void printHelp(){ +PRIVATE(void) +printHelp(){ printf(" \n %s @ " STR_QUOT(PROJECT_VERSION) "\n" " \n" " Takes paths (pairwise) from stdin (see --stdin for details) and\n" @@ -109,7 +176,8 @@ static void printHelp(){ /** returns non-zero on errors. Error messages will already be printed * internally. */ -static int parseArgs( int argc, char**argv, BulkLn*bulkLn ){ +PRIVATE(int) +parseArgs( int argc, char**argv, BulkLn*bulkLn ){ /* init (aka set defaults) */ bulkLn->dataFilePath = NULL; bulkLn->dryRun = 0; @@ -119,7 +187,7 @@ static int parseArgs( int argc, char**argv, BulkLn*bulkLn ){ bulkLn->isPrintSummary = !0; bulkLn->isRelinkExistingFiles = 0; - // Parse args + /* Parse args */ for( int i = 1 ; i < argc ; ++i ){ char *arg = argv[i]; if( !strcmp(arg, "--help") ){ @@ -170,7 +238,8 @@ static int parseArgs( int argc, char**argv, BulkLn*bulkLn ){ * WARN: Passed 'path' might be temporarily changed during execution. * Nevertheless will be in original state after return. */ -static int mkdirs( char*path, BulkLn*bulkLn ){ +PRIVATE(int) +mkdirs( char*path, BulkLn*bulkLn ){ int err; char *tmpEnd = path; /* Backup original length so we later still can recognize the original end @@ -211,7 +280,7 @@ static int mkdirs( char*path, BulkLn*bulkLn ){ /* Fine :) So just continue with the next one. */ }else{ fprintf(stderr, "mkdir(\"%s\"): %s\n", path, strerror(errno)); - err = -1; goto finally; + err = -1; goto resolveWithErr; } }else{ /* Only increment the directory counter if we really created @@ -238,13 +307,14 @@ static int mkdirs( char*path, BulkLn*bulkLn ){ } err = 0; -finally: +resolveWithErr: return err; } /** Like: ln srcPath dstPath */ -static int createHardlink( char*srcPath, char*dstPath, BulkLn*bulkLn ){ +PRIVATE(int) +createHardlink( char*srcPath, char*dstPath, BulkLn*bulkLn ){ int err; if( bulkLn->dryRun || bulkLn->isPrintEachCreateLink ){ @@ -264,26 +334,27 @@ static int createHardlink( char*srcPath, char*dstPath, BulkLn*bulkLn ){ }else{ /* Some other (unepxected) error */ fprintf(stderr, "unlink(%s): %s\n", dstPath, strerror(errno)); - err = -1; goto finally; + err = -1; goto resolveWithErr; } } } err = link(srcPath, dstPath); if( err ){ fprintf(stderr, "link(\"%s\", \"%s\"): %s\n", srcPath, dstPath, strerror(errno)); - err = -1; goto finally; + err = -1; goto resolveWithErr; } } bulkLn->createdLinksCount += 1; err = 0; -finally: +resolveWithErr: return err; } -static int onPathPair( char*srcPath, char*dstPath, BulkLn*bulkLn ){ +PRIVATE(int) +onPathPair( char*srcPath, char*dstPath, BulkLn*bulkLn ){ assert(srcPath != NULL); assert(dstPath != NULL); assert(bulkLn != NULL); @@ -303,19 +374,20 @@ static int onPathPair( char*srcPath, char*dstPath, BulkLn*bulkLn ){ err = mkdirs(dstPath, bulkLn); /* Restore path */ tmpEnd[0] = '/'; - if( err ){ err = -1; goto finally; } + if( err ){ err = -1; goto resolveWithErr; } } err = createHardlink(srcPath, dstPath, bulkLn); - if( err ){ err = -1; goto finally; } + if( err ){ err = -1; goto resolveWithErr; } err = 0; -finally: +resolveWithErr: return err; } -static int parseDataFileAsPairPerLine( BulkLn*bulkLn ){ +PRIVATE(int) +parseDataFileAsPairPerLine( BulkLn*bulkLn ){ int err; size_t buf_cap = 0; size_t buf_len = 0; @@ -328,13 +400,13 @@ static int parseDataFileAsPairPerLine( BulkLn*bulkLn ){ /* Read input line-by-line. Not the most elegant way to parse stuff, * but should suffice for our use-case */ err = getline(&buf, &buf_cap, bulkLn->dataFd); - if(unlikely( err < 0 )){ + if( err < 0 ){ /* Error handling */ if( feof(bulkLn->dataFd) ){ break; /* End-Of-File. Just break off the loop */ }else if( ferror(bulkLn->dataFd) ){ fprintf(stderr, "getline(%s): %s\n", bulkLn->dataFilePath, strerror(errno)); - err = -1; goto finally; + err = -1; goto resolveWithErr; }else{ abort(); /* I don't know how this could happen */ } @@ -347,20 +419,20 @@ static int parseDataFileAsPairPerLine( BulkLn*bulkLn ){ if( tab == NULL ){ fprintf(stderr, "Too few field separators (tab) in '%s' @ %lu\n", bulkLn->dataFilePath, lineNum); - err = -1; goto finally; + err = -1; goto resolveWithErr; } char *unwantedTab = memchr(tab + 1, DATA_FILE_FIELD_SEP_CHR, buf_len - (tab + 1 - buf)); if( unwantedTab != NULL ){ fprintf(stderr, "Too many field separators (tab) in '%s' line %lu\n", bulkLn->dataFilePath, lineNum); - err = -1; goto finally; + err = -1; goto resolveWithErr; } char *dstPath = tab + 1; /* <- path starts one char after the separator */ char *dstPath_end = buf + buf_len; for(;; --dstPath_end ){ - if(unlikely( dstPath_end < buf )){ + if( dstPath_end < buf ){ fprintf(stderr, "IMHO cannot happen %s:%d\n", __FILE__, __LINE__); - err = -1; goto finally; + err = -1; goto resolveWithErr; } if( dstPath_end[0]=='\n' || dstPath_end[0]=='\0' || dstPath_end[0]=='\r' ){ continue; /* last char not found yet */ @@ -377,7 +449,7 @@ static int parseDataFileAsPairPerLine( BulkLn*bulkLn ){ /* Publish this pair for processing */ err = onPathPair(srcPath, dstPath, bulkLn); - if( err ){ err = -1; goto finally; } + if( err ){ err = -1; goto resolveWithErr; } } if( bulkLn->isPrintStatus ){ @@ -385,19 +457,20 @@ static int parseDataFileAsPairPerLine( BulkLn*bulkLn ){ } err = 0; -finally: +resolveWithErr: free(buf); return err; } -int bulk_ln_main( int argc, char**argv ){ +int +bulk_ln_main( int argc, char**argv ){ int err; BulkLn bulkLn = {0}; #define bulkLn (&bulkLn) err = parseArgs(argc, argv, bulkLn); - if( err ){ err = -1; goto finally; } + if( err ){ err = -1; goto resolveWithErr; } /* Open data source */ if( !strcmp(bulkLn->dataFilePath, "-") ){ @@ -406,12 +479,12 @@ int bulk_ln_main( int argc, char**argv ){ bulkLn->dataFd = fopen(bulkLn->dataFilePath, "rb"); if( bulkLn->dataFd == NULL ){ fprintf(stderr, "fopen(%s): %s\n", bulkLn->dataFilePath, strerror(errno)); - err = -1; goto finally; + err = -1; goto resolveWithErr; } } err = parseDataFileAsPairPerLine(bulkLn); - if( err ){ err = -1; goto finally; } + if( err ){ err = -1; goto resolveWithErr; } if( bulkLn->isPrintSummary ){ fprintf(stderr, "Created %d directories and linked %d files.\n", @@ -419,11 +492,23 @@ int bulk_ln_main( int argc, char**argv ){ } err = 0; -finally: +resolveWithErr: if( bulkLn->dataFd != NULL && bulkLn->dataFd != stdin ){ fclose(bulkLn->dataFd); } - return -err; + return err; #undef bulkLn } + +#if EXPOSE_MAIN +int +main( int argc, char**argv ){ + argc = bulk_ln_main(argc, argv); + if( argc < 0 ) argc = -argc; + return (argc >= 0 && argc <= 0x7F) ? argc : 1; +} +#endif + + +/*]===========================================================================]--*/ diff --git a/src/bulk_ln/bulk_ln.h b/src/bulk_ln/bulk_ln.h deleted file mode 100644 index 6054386..0000000 --- a/src/bulk_ln/bulk_ln.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef INCGUARD_87768d11a9f7d831f2a8b9d0ab7efa9c -#define INCGUARD_87768d11a9f7d831f2a8b9d0ab7efa9c - -#include "commonbase.h" - - -int bulk_ln_main( int argc, char**argv ); - - -#endif /* INCGUARD_87768d11a9f7d831f2a8b9d0ab7efa9c */ diff --git a/src/bulk_ln/bulk_ln_main.c b/src/bulk_ln/bulk_ln_main.c deleted file mode 100644 index a3ac228..0000000 --- a/src/bulk_ln/bulk_ln_main.c +++ /dev/null @@ -1,10 +0,0 @@ - -#include "bulk_ln.h" - -int main( int argc, char**argv ){ - int err = bulk_ln_main(argc, argv); - /* Ensure to return 7-bit value only (See POSIX) */ - if( err < 0 ) err = -err; - return err > 127 ? 1 : err; -} - diff --git a/src/common/commonbase.h b/src/common/commonbase.h deleted file mode 100644 index 497d3fa..0000000 --- a/src/common/commonbase.h +++ /dev/null @@ -1,23 +0,0 @@ - -/* - * common config for project. Here goes stuff like feature-test-macros etc. - * - * Every header file MUST include this file AS THE 1ST include. - */ - - -#define _POSIX_C_SOURCE 200809L - - -#define STR_QUOT_IAHGEWIH(s) #s -#define STR_QUOT(s) STR_QUOT_IAHGEWIH(s) - -#define STR_CAT(a, b) a ## b - -#ifndef likely -# define likely(a) (a) -#endif -#ifndef unlikely -# define unlikely(a) (a) -#endif - |
