Get instant Hello World example scripts, batch files and source code for the Windows CMD.EXE shell, Linux bash, HTML code, or a simple Java/C++/PHP program.

Create a starting point for your own editing, with the free sfk samp command for Windows, Mac OS X and Linux.
sfk sample java|cpp|... [|.cpp|...]

print a short example code in a programming language.

supported parameters
   sfk          create a simple sfk script
   sfkbat       sfk script embedded in a .bat or .cmd
   sfkbash      sfk script embedded in a bash script
   http         automated http access example script
   http -bat    ... as a windows .bat file
   http -bash   ... as a linux .sh file
   httpdata     create a .zip file with webdemo files
                for local use with sfk httpserv

   cmd          create generic windows .cmd or .bat file
   bash         create generic linux bash script
   html         simple html page with css and javascript
   java         create a java class doing text  file I/O
   javaimg      create a java class doing image file I/O
   javahex      create a hexdump of binary data in java
   javagui      create a simple java gui application
   c            create a simple c source code template
   cpp          same as the c source code template
   cppnetlog    how to send UDP network text in C++
   javanetlog   how to send UDP network text in Java
   php          create command line php code for text I/O
   phpimg       create php example for image processing

   -force    if output file exists already, overwrite it.

command shortcut
   sfk batch myscript.bat
      does the same as "sfk samp sfkbat myscript.bat"

web reference

more in the SFK Book
   the SFK Book contains a 60 page tutorial, including
   long sfk script examples with input, output and
   detail explanations. type "sfk book" for more.

   sfk batch foo.bat
      creates batch file foo.bat with embedded sfk script.
      type "foo.bat" to run the created script. note that
      foo.bat must be created in a directory of your PATH,
      or in the current directory.

   sfk batch foo.bat -force
      the same as above, and overwrites an existing file.

   sfk batch
      create a bash script file, with lf only line endings.
      can also be used under windows for mingw environments.

   sfk samp java
      create a java class foo. if the java JDK is available,
      type "javac" and then "java foo" to run it.

   sfk samp phpimg doimg.php
      create image processing script that can be run by:
      php doimg.php
      if php.exe is in your PATH (read remarks in the script).

   sfk samp javahex +toclip
      copy example for java hexdump creation to the clipboard.

   sfk samp http tmp.bat
      create an example script for automated web/http access.

   sfk samp http -bash
      the same, but using bash in a Windows Cygwin environment.

   sfk samp c tmp.c
      create an instant c program with some helper functions
      for quick and easy writing of a small c project.
      compile by: gcc tmp.c or g++ tmp.c or cl tmp.c

   sfk samp cpp tmp.cpp
      same as the 'c' code example.


the samples produced are:

sfk samp sfk
sfk select testfiles .txt .hpp .cpp // find words supplied by user. // note that %1 is the same as $1. +find %1 %2 %3 $4 $5 $6 // process files containing hits +run -quiet "sfk echo \"Found hit in: [green]$file[def]\"" -yes // run the script by: // sfk script "thisfile" pattern1 [pattern2 ...]
sfk samp sfkbat
@echo off sfk script "%~f0" -from begin %* rem %~f0 is the absolute batch file name. GOTO xend sfk label begin -var // default batch, with some variable support. // for a full file backup example use: // sfk batch -full myfile.bat +if "%1 = " begin +tell "[green]usage:[def]" +tell " #( copy [-yes]" +stop -all +endif +setvar cmd="%1" +setvar yes="%2" +if "#(cmd) = copy" begin +call docopy +stop +endif +tell "unknown command: #(cmd)" +end sfk label docopy +copy -checkdirs mydir mydir2 -dir core doc db -subdir !\tmp !\save -file !.tmp #(yes) +end :xend
sfk samp sfkbash
#!/bin/bash sfk script "$0" -from begin $@ exit function skip_block { sfk label begin -var -upat2 // default batch, with unified windows/linux syntax (-upat2). // for a full file backup example use: // sfk batch -full +if "%1 = " begin +tell "[green]usage:[def]" +tell " #( copy [-yes]" +stop -all +endif +setvar cmd="%1" +setvar yes="%2" +if "#(cmd) = copy" begin +call docopy +stop +endif +tell "unknown command: #(cmd)" +end sfk label docopy +copy -checkdirs mydir mydir2 -dir core doc db -subdir :\tmp :\save -file :.tmp #(yes) +end }
sfk samp http
@echo off sfk script "%~f0" -from begin %* rem "%~f0" is the absolute batch file name. GOTO xend sfk label begin -var // the -var above enables variables everywhere. +setvar "baseurl=" +web "#(baseurl)/contents.xml" +xex "_<category>**<id>*</id>**<name>*< _[part4] [part8]\n_" +perline "call listCategory #text" -yes +end sfk label listCategory +echo "[green]=== List of %2: ===[def]" +echo -spat "[yellow]Name Price[def]" +then web "#(baseurl)/product_list_%1.xml" // +xmlform +stop -all +xex "=<row>**<name>*<**<price>*< =[part4]\t[part8] \x24\n=" +filter -upat -stabform "#(-12col1) #col2" +end rem a longer example with input, output and detail rem explanations is available in the SFK Book. :xend
sfk samp java
import*; public class fileio { static void log(String s) { System.out.println("main: "+s); } public static void main(String args[]) throws Throwable { if (args.length < 2) { log("supply in- and output filename."); return; } // copy or convert text file BufferedReader rin = new BufferedReader( new InputStreamReader( new FileInputStream(args[0]), "ISO-8859-1" // or US-ASCII,UTF-8,UTF-16BE,UTF-16LE,UTF-16 )); PrintWriter pout = new PrintWriter( new OutputStreamWriter( new FileOutputStream(args[1]), "ISO-8859-1" )); while (true) { String sline = rin.readLine(); if (sline == null) break; // EOD log("copying line: "+sline); pout.println(sline); } pout.close(); rin.close(); } };
sfk samp cpp
// simple c source template 1.0 for instant coding, free for any use. // beware that the simplified semantics may not fit into larger projects. #ifdef _WIN32 #include <windows.h> #endif #include <stdio.h> #include <string.h> #include <stdarg.h> #include <stdlib.h> #include <ctype.h> #include <stdint.h> #include <sys/stat.h> #ifndef _WIN32 #include <unistd.h> #include <sys/time.h> #endif // sfk quick types typedef unsigned char uchar; typedef unsigned short ushort; typedef unsigned int uint; typedef const char cchar; #ifndef __cplusplus typedef unsigned char bool; #endif #ifdef _WIN32 typedef __int64 num; typedef unsigned __int64 unum; #else typedef long long num; typedef unsigned long long unum; #endif // print error message with variable parameters. int perr(const char *pszFormat, ...) { va_list argList; va_start(argList, pszFormat); char szBuf[1024]; vsprintf(szBuf, pszFormat, argList); fprintf(stderr, "error: %s", szBuf); return 0; } int merr(const char *pszWhere) { fprintf(stderr, "out of memory at %s", pszWhere); return 0; } char *myvtext(const char *pszFormat, ...) { static char szBuf[4096]; va_list argList; va_start(argList, pszFormat); vsnprintf(szBuf, sizeof(szBuf)-10, pszFormat, argList); szBuf[sizeof(szBuf)-10] = '\0'; return szBuf; } char *numtoa(num n) { static char szBuf[100]; #ifdef _WIN32 sprintf(szBuf, "%I64d", n); #else sprintf(szBuf, "%lld", n); #endif return szBuf; } num atonum(char *psz) { #ifdef _WIN32 return _atoi64(psz); #else return atoll(psz); #endif } num currentTime() { #ifdef _WIN32 return (num)GetTickCount(); #else struct timeval tv; gettimeofday(&tv, NULL); return ((num)tv.tv_sec) * 1000 + ((num)tv.tv_usec) / 1000; #endif } void doSleep(int nmsec) { #ifdef _WIN32 Sleep(nmsec); #else // sleep(1); // const timespec ts = { nmsec / 1000, nmsec % 1000 * 1000000 }; // nanosleep(&ts, NULL); usleep(nmsec*1000); #endif } int mystricmp(char *psz1, cchar *psz2) { while (*psz1 && *psz2 && tolower(*psz1) == tolower(*psz2)) { psz1++; psz2++; } return tolower(*psz1) - tolower(*psz2); } int mystrnicmp(char *psz1, cchar *psz2, int nLen) { int i=0; for (i=0; i<nLen && psz1[i] && psz2[i]; i++) if (tolower(psz1[i]) != tolower(psz2[i])) return tolower(psz1[i]) - tolower(psz2[i]); if (i >= nLen) return 0; return tolower(psz1[i]) - tolower(psz2[i]); } bool strBegins(char *pszStr, cchar *pszPat) { return strncmp(pszStr, pszPat, strlen(pszPat)) ? 0 : 1; } bool striBegins(char *pszStr, cchar *pszPat) { return mystrnicmp(pszStr, pszPat, strlen(pszPat)) ? 0 : 1; } bool strEnds(char *pszStr, cchar *pszPat) { int nhay = strlen(pszStr); int npat = strlen(pszPat); if (nhay < npat) return 0; return strcmp(pszStr+nhay-npat, pszPat) ? 0 : 1; } bool striEnds(char *pszStr, cchar *pszPat) { int nhay = strlen(pszStr); int npat = strlen(pszPat); if (nhay < npat) return 0; return mystricmp(pszStr+nhay-npat, pszPat) ? 0 : 1; } char *mystrstri(char *phay, cchar *ppat) { int i=0,j=0,ncmp=0,ndiff=0; int llen = (int)strlen(phay); int slen = (int)strlen(ppat); ncmp = (llen-slen)+1; if (ncmp < 0) return 0; for (j=0; j<ncmp; ++j) { ndiff = 0; for (i=0; (i<slen) && !ndiff; ++i) ndiff = tolower(phay[j+i]) - tolower(ppat[i]); if (!ndiff) return (char *)phay+j; } return 0; } uchar *memFind(uchar *pNeedle, num nNeedleSize, uchar *pHayStack, num nHaySize) { uchar *pCur = pHayStack; uchar *pMax = pHayStack + nHaySize - nNeedleSize; // inclusive uchar c1 = *pNeedle; while (pCur <= pMax) { uchar *p1 = pCur; p1 = (uchar*)memchr(p1, c1, pMax - p1 + 1); // +1: pMax is inclusive if (!p1 || (p1 > pMax)) return 0; if ((p1 <= pMax) && !memcmp(p1, pNeedle, nNeedleSize)) return p1; // hit pCur = p1+1; } return 0; } // may fail with file sizes > 2 gb in 32 bit builds num getFileSize(char *pszName) { struct stat buf; if (stat(pszName, &buf)) return -1; return buf.st_size; } // loads text and binary char *loadFile(char *pszFile) { num nFileSize=0, nTolerance=10; char *pOut; FILE *fin; if ((nFileSize = getFileSize(pszFile)) < 0) { perr("file not found: %s", pszFile); return 0; } if (!(pOut = (char*)malloc(nFileSize+nTolerance+4))) { perr("out of memory: %s\n", pszFile); return 0; } memset(pOut+nFileSize, 0, nTolerance); // added safety if (!(fin = fopen(pszFile, "rb"))) { perr("cannot read: %s\n", pszFile); free(pOut); return 0; } int nRead = fread(pOut, 1, nFileSize, fin); fclose(fin); if (nRead != nFileSize) { perr("cannot read: %s (%d %d)\n", pszFile, nRead, nFileSize); free(pOut); return 0; } // loadFile guarantees zero termination after buffer: pOut[nFileSize] = '\0'; return pOut; } int saveFile(char *pszName, uchar *pData, int iSize, const char *pszMode) { FILE *fout; if (!(fout = fopen(pszName, pszMode))) return 9+perr("cannot write: %s\n", pszName); if (fwrite(pData, 1, iSize, fout) != iSize) { fclose(fout); return 10+perr("cannot fully write (disk full?): %s\n", pszName); } fclose(fout); return 0; } int existsFile(char *pszName, bool bOrDir) { struct stat buf; if (stat(pszName, &buf)) return 0; if (!bOrDir && (buf.st_mode & 0040000)) // _S_IFDIR return 0; // is a dir, not a file return 1; } char *dataAsHex(void *pAnyData, int iDataSize) { static char szBuf[300]; uchar *pSrcCur = (uchar *)pAnyData; uchar *pSrcMax = pSrcCur + iDataSize; char *pszDstCur = szBuf; char *pszDstMax = szBuf+sizeof(szBuf)-30; while (pSrcCur < pSrcMax && pszDstCur < pszDstMax) { uchar uc = *pSrcCur++; sprintf(pszDstCur, "%02X", uc); pszDstCur += 2; } *pszDstCur = '\0'; return szBuf; } char *dataAsTrace(void *pAnyData, int iDataSize) { static char szBuf[300]; if (iDataSize == -1) iDataSize = strlen((char*)pAnyData); uchar *pSrcCur = (uchar *)pAnyData; uchar *pSrcMax = pSrcCur + iDataSize; char *pszDstCur = szBuf; char *pszDstMax = szBuf+sizeof(szBuf)-30; while (pSrcCur < pSrcMax && pszDstCur < pszDstMax) { uchar uc = *pSrcCur++; if (isprint((char)uc)) { *pszDstCur++ = (char)uc; continue; } sprintf(pszDstCur, "{%02X}", uc); pszDstCur += 4; } *pszDstCur = '\0'; return szBuf; } // copy text lines from one file into another. int main(int argc, char *argv[]) { char *pszInFile=0,*pszOutFile=0; int iarg = 1; for (;iarg<argc;iarg++) { char *parg = argv[iarg]; if (!strcmp(parg,"-?") || !strcmp(parg,"-h") || !strcmp(parg,"-help")) { printf("usage: %s infile outfile\n", argv[0]); return 0; } if (!pszInFile) { pszInFile = parg; continue; } if (!pszOutFile) { pszOutFile = parg; continue; } return 9+perr("unexpected: %s\n", parg); } if (!pszOutFile) return 9+perr("specify input and output filename.\n"); FILE *fin = fopen(pszInFile , "rb"); if (!fin ) return 9+perr("cannot read %s\n" , pszInFile); FILE *fout = fopen(pszOutFile, "wb"); if (!fout) return 9+perr("cannot write %s\n", pszOutFile); char szBuf[1024]; memset(szBuf, 0, sizeof(szBuf)); while (fgets(szBuf, sizeof(szBuf)-10, fin)) { char *psz = strchr(szBuf, '\r'); if (psz) *psz = '\0'; // strip cr psz = strchr(szBuf, '\n'); if (psz) *psz = '\0'; // strip lf printf("line: \"%s\"\n", szBuf); strcat(szBuf, "\n"); int nlen = strlen(szBuf); if (fwrite(szBuf, 1, nlen, fout) != nlen) return 9+perr("failed to fully write %s\n", pszOutFile); } fclose(fout); fclose(fin); return 0; }
sfk samp cmd
@rem windows command shell batch example @echo off IF "%1"=="" GOTO xerr01 echo "parameter is %1" GOTO xdone :xerr01 echo "please supply a parameter." echo "example: mybat parm123" GOTO xdone :xdone
sfk samp bash
#!/bin/bash function pmsg { # uses a local variable mystr local mystr="info: $1" echo $mystr } myparm1="$1 and $2" # no blanks around "=" if [ "$2" = "" ]; then # requires all blanks pmsg "please supply two parameters." else pmsg "you supplied \"$myparm1\"." # < -lt > -gt <= -le >= -ge == -eq != -ne i=1 while [ $i -le 5 ]; do # not "$i < 5" echo counting: $i # quotes are optional let i+=1 # not "i += 1" or "$i+=1" done fi