summaryrefslogtreecommitdiff
path: root/examples/loadables/head.c
blob: 90526895534f1ee6b3b398ec863110bf836a0212 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* head - copy first part of files. */

/* See Makefile for compilation details. */

#include "config.h"

#include "bashtypes.h"
#include "posixstat.h"
#include "filecntl.h"

#if defined (HAVE_UNISTD_H)
#  include <unistd.h>
#endif

#include "bashansi.h"

#include <stdio.h>
#include <errno.h>
#include "chartypes.h"

#include "builtins.h"
#include "shell.h"
#include "bashgetopt.h"

#if !defined (errno)
extern int errno;
#endif

static void
munge_list (list)
     WORD_LIST *list;
{
  WORD_LIST *l, *nl;
  WORD_DESC *wd;
  char *arg;

  for (l = list; l; l = l->next)
    {
      arg = l->word->word;
      if (arg[0] != '-' || arg[1] == '-' || (DIGIT(arg[1]) == 0))
        return;
      /* We have -[0-9]* */
      wd = make_bare_word (arg+1);
      nl = make_word_list (wd, l->next);
      l->word->word[1] = 'n';
      l->word->word[2] = '\0';
      l->next = nl;
      l = nl;	/* skip over new argument */
    }
}

static int
file_head (fp, cnt)
     FILE *fp;
     int cnt;
{
  int ch;

  while (cnt--)
    {
      while ((ch = getc (fp)) != EOF)
	{
	  if (putchar (ch) == EOF)
	    {
	      builtin_error ("write error: %s", strerror (errno));
	      return EXECUTION_FAILURE;
	    }
	  if (ch == '\n')
	    break;
	}
    }
}

head_builtin (list)
     WORD_LIST *list;
{
  int nline, opt, rval;
  WORD_LIST *l;
  FILE *fp;

  char *t;

  munge_list (list);	/* change -num into -n num */

  reset_internal_getopt ();
  nline = 10;
  while ((opt = internal_getopt (list, "n:")) != -1)
    {
      switch (opt)
	{
	case 'n':
	  nline = atoi (list_optarg);
	  if (nline <= 0)
	    {
	      builtin_error ("bad line count: %s", list_optarg);
	      return (EX_USAGE);
	    }
	  break;
	default:
	  builtin_usage ();
	  return (EX_USAGE);
	}
    }
  list = loptend;

  if (list == 0)
    return (file_head (stdin, nline));

  for (rval = EXECUTION_SUCCESS, opt = 1, l = list; l; l = l->next)
    {
      fp = fopen (l->word->word, "r");
      if (fp == NULL)
	{
	  builtin_error ("%s: %s", l->word->word, strerror (errno));
	  continue;
	}
      if (list->next)	/* more than one file */
	{
	  printf ("%s==> %s <==\n", opt ? "" : "\n", l->word->word);
	  opt = 0;
	}
      rval = file_head (fp, nline);
      fclose (fp);
    }
   
  return (rval);
}

char *head_doc[] = {
	"Copy the first N lines from the input files to the standard output.",
	"N is supplied as an argument to the `-n' option.  If N is not given,",
	"the first ten lines are copied.",
	(char *)NULL
};

struct builtin head_struct = {
	"head",			/* builtin name */
	head_builtin,		/* function implementing the builtin */
	BUILTIN_ENABLED,	/* initial flags for builtin */
	head_doc,		/* array of long documentation strings. */
	"head [-n num] [file ...]", /* usage synopsis; becomes short_doc */
	0			/* reserved for internal use */
};