source: grub-pc/trunk/fuentes/grub-core/lib/getline.c @ 22

Last change on this file since 22 was 22, checked in by mabarracus, 4 years ago

updated version and apply net.ifnames=0 into debian/rules

File size: 2.1 KB
Line 
1/* main.c - the normal mode main routine */
2/*
3 *  GRUB  --  GRand Unified Bootloader
4 *  Copyright (C) 2000,2001,2002,2003,2005,2006,2007,2008,2009,2013  Free Software Foundation, Inc.
5 *
6 *  GRUB is free software: you can redistribute it and/or modify
7 *  it under the terms of the GNU General Public License as published by
8 *  the Free Software Foundation, either version 3 of the License, or
9 *  (at your option) any later version.
10 *
11 *  GRUB is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *  GNU General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <grub/kernel.h>
21#include <grub/normal.h>
22#include <grub/dl.h>
23#include <grub/misc.h>
24#include <grub/file.h>
25#include <grub/mm.h>
26#include <grub/term.h>
27#include <grub/env.h>
28#include <grub/parser.h>
29#include <grub/reader.h>
30#include <grub/menu_viewer.h>
31#include <grub/auth.h>
32#include <grub/i18n.h>
33#include <grub/charset.h>
34#include <grub/script_sh.h>
35
36/* Read a line from the file FILE.  */
37char *
38grub_file_getline (grub_file_t file)
39{
40  char c;
41  grub_size_t pos = 0;
42  char *cmdline;
43  int have_newline = 0;
44  grub_size_t max_len = 64;
45
46  /* Initially locate some space.  */
47  cmdline = grub_malloc (max_len);
48  if (! cmdline)
49    return 0;
50
51  while (1)
52    {
53      if (grub_file_read (file, &c, 1) != 1)
54        break;
55
56      /* Skip all carriage returns.  */
57      if (c == '\r')
58        continue;
59
60
61      if (pos + 1 >= max_len)
62        {
63          char *old_cmdline = cmdline;
64          max_len = max_len * 2;
65          cmdline = grub_realloc (cmdline, max_len);
66          if (! cmdline)
67            {
68              grub_free (old_cmdline);
69              return 0;
70            }
71        }
72
73      if (c == '\n')
74        {
75          have_newline = 1;
76          break;
77        }
78
79      cmdline[pos++] = c;
80    }
81
82  cmdline[pos] = '\0';
83
84  /* If the buffer is empty, don't return anything at all.  */
85  if (pos == 0 && !have_newline)
86    {
87      grub_free (cmdline);
88      cmdline = 0;
89    }
90
91  return cmdline;
92}
Note: See TracBrowser for help on using the repository browser.