⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 head.c

📁 使用于OS/2下的小工具的C源程序,使用于OS/2下的小工具的C源程序
💻 C
字号:
/*
* HEAD.C - Output the first 10 lines from a text file.
*
* PROGRAMMER:	    Martti Ylikoski
* CREATED:	    5.12.1990
*/
static char *VERSION = "Version  1.1, Copyright(c) Martti Ylikoski, 1990-1992" ;
/*
*/

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <param.h>
#include <paramstd.h>
#define INCL_VIO
#include <os2.h>

static char *progname ;

extern unsigned long pflags ;

ParamEntry pentry[12] = {
     "P", &ParamSetPause, 0,
     "F", &ParamSetFold, 0,
     "V", &ParamSetVerbose, 0,
     "R", &ParamSetReport, 0,
     "S", &ParamSetSubDirs, 0,
     "?", &ParamSetHelp, 1,
     "H", &ParamSetHelp, 1,
     "NOD", &ParamSetNoDefault, 0,
     "TEST", &ParamSetTest, 0,
     "Y", &ParamSetYes, 0,
     "N", &ParamSetTest, 0,
     "\0", NULL, 0
} ;

ParamBlock params = {
    "/-",   IGNORECASE | NOPRIORITY | NOTRIGGERSALLOWED ,
    pentry
} ;

static int fhnd, writecnt = 1 ;
static int cols, count, stdinassumed ;
static char buf[512] ;
/* local prototypes */
int head (char *fname) ;

int main(int argc, char *argv[])
{
FILE *fptr ;
int i, nargc ;
VIOMODEINFO vioinfo ;
USHORT ret ;

   progname = argv[0] ;
   count = 10 ; 	    /* default for lines to output */
   stdinassumed = FALSE ;

   ParamHandle(&params, &argc, argv) ;

   if (pflags & PA_HELP)
   {
      puts("HEAD - display the beginning lines from a text file.") ;
      puts(VERSION) ;
      puts("Usage: head [-count | - | /H | /?] file") ;
      return( 0 ) ;
   }

   nargc = argc ;
   for (i = 1 ; i < argc ; i++)
      if (argv[i][0] == '-' || argv[i][0] == '/')
       {
	 if (strlen(argv[i]) == 1)
	 {
	    stdinassumed = TRUE ;
	    argv[i] = NULL ;
	    nargc -- ;
	    continue ;
	 }
	 count = atoi(&argv[i][1]) ;
	 argv[i] = NULL ;
	 nargc -- ;
      }

   vioinfo.cb = sizeof(vioinfo) ;
   if (( ret = VioGetMode(&vioinfo, 0 )) != 0)
   {
       fprintf(stderr, "%s: error in VioGetMode...\nExiting ...\n", progname) ;
       return( 1 ) ;
   }

   cols = vioinfo.col ;     /* the number of colunms in current video mode */
   if (cols > sizeof(buf))  /* very unlikely that cols > 512 */
      cols = sizeof(buf) ;

   if (nargc == 1)
      head("-") ;
   else
   {
      for (i = 1 ; i < argc ; i++)
	 if (argv[i] != NULL)
	   head(argv[i]) ;
      if (stdinassumed == TRUE)
	 head("-") ;
   }

   return( 0 ) ;
}

int head (char *fname)
{
FILE *fptr ;
int i ;

   fprintf(stderr,"---- FILE : %s\n", fname) ;
   /* open file */
   if (strcmp(fname, "-") == 0)
      fptr = stdin ;
   else
      if ((fptr = fopen(fname, "r")) == NULL)
      {
	 printf("%s: unable to open file %s for reading...\n", progname, fname) ;
	 printf("Exiting...\n") ;
	 return( 1 ) ;
      }

   for (i = 0 ; i < count && feof(fptr) == 0 ; i++ )
   {
      fgets(buf, cols - 1, fptr) ;
      if (feof(fptr) == 0)
      {
	 buf[cols - 1] = '\0' ;
	 fputs(buf, stdout) ;
	 if (buf[strlen(buf)-1] != '\n') /* scan to the next line */
	 {
	    int c ;
	    fputs("\n", stdout) ;
	    while ( (c = fgetc(fptr)) != '\n' && c != EOF)
	       ;
	 }
      }
   }

   /* close file */
   fclose ( fptr ) ;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -