CS360: Exam 1: 10/22/97. Cheat Sheet
System calls for I/O
int open(char *path, int flags [ , int mode ] );
int close(int fd);
int read(int fd, char *buf, int size);
int write(int fd, char *buf, int size);
off_t lseek(int fd, off_t offset, int whence);
flags: O_RDONLY, O_WRONLY, O_APPEND, O_SYNC, O_CREAT, O_TRUNC
Standard I/O library calls
FILE *fopen(char *filename, char *type);
int fclose(FILE *stream);
int fread (char *ptr, int size, int nitems, FILE *stream);
int fwrite (char *ptr, int size, int nitems, FILE *stream);
char *fgets(char *s, int n, FILE *stream);
int fputs(char *s, FILE *stream);
int fprintf(FILE *stream, char *format [ , arg... ] );
int fscanf(FILE *stream, char *format [ , pointer... ] );
int fgetc(FILE *stream);
int fseek(FILE *stream, long offset, int ptrname);
char *sprintf(char *s, char *format [ , arg... ] );
Strings
char *strcpy(char *s1, char *s2); /* Copies s2 to s1 */
int strcmp(char *s1, char *s2); /* Compares s1 and s2 */
char *strdup(char *s); /* Mallocs and copies s */
int strlen(char *s); /* Returns length of s*/
Red-black trees
Rb_node make_rb(); /* Creates a new rb-tree */
Rb_node rb_insert(Rb_node tree, char *key, char *val);
#define rb_first(n) (n->c.list.flink)
#define rb_last(n) (n->c.list.blink)
#define rb_next(n) (n->c.list.flink)
#define rb_prev(n) (n->c.list.blink)
#define rb_nil(t) (t)
#define rb_traverse(ptr, lst) \
for(ptr = rb_first(lst); ptr != rb_nil(lst); ptr = rb_next(ptr))
Dlists
typedef struct dlist {
struct dlist *flink;
struct dlist *blink;
void *val;
} *Dlist;
Dlist make_dl(); /* Creates a new dlist*/
dl_insert_b(Dlist node, char *val); /* Insert before the node */
dl_insert_a(Dlist node, char *val); /* Insert after the node */
#define nil(l) (l)
#define first(l) (l->flink)
#define last(l) (l->blink)
#define next(n) (n->flink)
#define prev(n) (n->blink)
#define dl_traverse(ptr, list) \
for (ptr = first(list); ptr != nil(list); ptr = next(ptr))
Fields
#define MAXLEN 1001
#define MAXFIELDS 1000
typedef struct inputstruct {
char *name; /* File name */
FILE *f; /* File descriptor */
int line; /* Line number */
char text1[MAXLEN]; /* The line */
char text2[MAXLEN]; /* Working -- contains fields */
int NF; /* Number of fields */
char *fields[MAXFIELDS]; /* Pointers to fields */
int file; /* 1 for file, 0 for popen */
} *IS;
extern IS new_inputstruct(/* FILENAME -- NULL for stdin */);
extern int get_line(/* IS */); /* returns NF, or -1 on EOF */