-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.c
115 lines (100 loc) · 2.88 KB
/
io.c
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
/***************************************************************************
* Title: Input/Output
* -------------------------------------------------------------------------
* Purpose: Handles the input and output
* Author: Stefan Birrer
* Version: $Revision: 1.1 $
* Last Modification: $Date: 2005/10/13 05:24:59 $
* File: $RCSfile: io.c,v $
* Copyright: (C) 2002 by Stefan Birrer
***************************************************************************/
/***************************************************************************
* ChangeLog:
* -------------------------------------------------------------------------
* $Log: io.c,v $
* Revision 1.1 2005/10/13 05:24:59 sbirrer
* - added the skeleton files
*
* Revision 1.4 2002/10/24 21:32:47 sempi
* final release
*
* Revision 1.3 2002/10/23 21:54:27 sempi
* beta release
*
* Revision 1.2 2002/10/15 20:37:26 sempi
* Comments updated
*
* Revision 1.1 2002/10/15 20:20:56 sempi
* Milestone 1
*
***************************************************************************/
#define __IO_IMPL__
/************System include***********************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <unistd.h>
#include <termios.h>
#include <assert.h>
/************Private include**********************************************/
#include "io.h"
#include "runtime.h"
/************Defines and Typedefs*****************************************/
/* #defines and typedefs should have their names in all caps.
* Global variables begin with g. Global constants with k. Local
* variables should be in all lower case. When initializing
* structures and arrays, line everything up in neat columns.
*/
/************Global Variables*********************************************/
/* indicates that the standard input stream is currently read */
bool isReading = FALSE;
/************Function Prototypes******************************************/
/************External Declaration*****************************************/
/**************Implementation***********************************************/
void PrintNewline()
{
putchar('\n');
}
void Print(char* msg)
{
assert(msg != NULL);
puts(msg);
}
void PrintPError(char* msg)
{
char* format = "%s: %s";
char str[MAXLINE];
if (msg == NULL)
{
perror(SHELLNAME);
return;
}
snprintf(str, MAXLINE-1, format, SHELLNAME, msg);
perror(str);
}
bool IsReading()
{
return isReading;
}
void getCommandLine(char** buf, int size)
{
char ch;
size_t used=0;
char* cmd = *buf;
cmd[0] = '\0';
isReading = TRUE;
while (((ch = getc(stdin)) != EOF) &&
(ch != '\n'))
{
if (used == size)
{
size *= 2;
cmd = realloc(cmd, sizeof(char)*(size+1));
}
cmd[used] = ch;
used++;
cmd[used] = '\0';
}
isReading = FALSE;
}