training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

ourhdr.h (4107B)


      1 /* Our own header, to be included *after* all standard system headers */
      2 
      3 #ifndef	__ourhdr_h
      4 #define	__ourhdr_h
      5 
      6 #include	<sys/types.h>	/* required for some of our prototypes */
      7 #include	<stdio.h>		/* for convenience */
      8 #include	<stdlib.h>		/* for convenience */
      9 #include	<string.h>		/* for convenience */
     10 #include	<unistd.h>		/* for convenience */
     11 
     12 #define	MAXLINE	4096			/* max line length */
     13 
     14 #define	FILE_MODE	(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
     15 					/* default file access permissions for new files */
     16 #define	DIR_MODE	(FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH)
     17 					/* default permissions for new directories */
     18 
     19 typedef	void	Sigfunc(int);	/* for signal handlers */
     20 
     21 					/* 4.3BSD Reno <signal.h> doesn't define SIG_ERR */
     22 #if	defined(SIG_IGN) && !defined(SIG_ERR)
     23 #define	SIG_ERR	((Sigfunc *)-1)
     24 #endif
     25 
     26 #define	min(a,b)	((a) < (b) ? (a) : (b))
     27 #define	max(a,b)	((a) > (b) ? (a) : (b))
     28 
     29 					/* prototypes for our own functions */
     30 char	*path_alloc(int *);			/* {Prog pathalloc} */
     31 int		 open_max(void);			/* {Prog openmax} */
     32 void	 clr_fl(int, int);			/* {Prog setfl} */
     33 void	 set_fl(int, int);			/* {Prog setfl} */
     34 void	 pr_exit(int);				/* {Prog prexit} */
     35 void	 pr_mask(const char *);		/* {Prog prmask} */
     36 Sigfunc	*signal_intr(int, Sigfunc *);/* {Prog signal_intr_function} */
     37 
     38 int		 tty_cbreak(int);			/* {Prog raw} */
     39 int		 tty_raw(int);				/* {Prog raw} */
     40 int		 tty_reset(int);			/* {Prog raw} */
     41 void	 tty_atexit(void);			/* {Prog raw} */
     42 #ifdef	ECHO	/* only if <termios.h> has been included */
     43 struct termios	*tty_termios(void);	/* {Prog raw} */
     44 #endif
     45 
     46 void	 sleep_us(unsigned int);	/* {Ex sleepus} */
     47 ssize_t	 readn(int, void *, size_t);/* {Prog readn} */
     48 ssize_t	 writen(int, const void *, size_t);/* {Prog writen} */
     49 int		 daemon_init(void);			/* {Prog daemoninit} */
     50 
     51 int		 s_pipe(int *);				/* {Progs svr4_spipe bsd_spipe} */
     52 int		 recv_fd(int, ssize_t (*func)(int, const void *, size_t));
     53 									/* {Progs recvfd_svr4 recvfd_43bsd} */
     54 int		 send_fd(int, int);			/* {Progs sendfd_svr4 sendfd_43bsd} */
     55 int		 send_err(int, int, const char *);/* {Prog senderr} */
     56 int		 serv_listen(const char *);	/* {Progs servlisten_svr4 servlisten_44bsd} */
     57 int		 serv_accept(int, uid_t *);	/* {Progs servaccept_svr4 servaccept_44bsd} */
     58 int		 cli_conn(const char *);	/* {Progs cliconn_svr4 cliconn_44bsd} */
     59 int		 buf_args(char *, int (*func)(int, char **));
     60 									/* {Prog bufargs} */
     61 
     62 int		 ptym_open(char *);			/* {Progs ptyopen_svr4 ptyopen_44bsd} */
     63 int		 ptys_open(int, char *);	/* {Progs ptyopen_svr4 ptyopen_44bsd} */
     64 #ifdef	TIOCGWINSZ
     65 pid_t	 pty_fork(int *, char *, const struct termios *,
     66 				  const struct winsize *);	/* {Prog ptyfork} */
     67 #endif
     68 
     69 int		lock_reg(int, int, int, off_t, int, off_t);
     70 									/* {Prog lockreg} */
     71 #define	read_lock(fd, offset, whence, len) \
     72 			lock_reg(fd, F_SETLK, F_RDLCK, offset, whence, len)
     73 #define	readw_lock(fd, offset, whence, len) \
     74 			lock_reg(fd, F_SETLKW, F_RDLCK, offset, whence, len)
     75 #define	write_lock(fd, offset, whence, len) \
     76 			lock_reg(fd, F_SETLK, F_WRLCK, offset, whence, len)
     77 #define	writew_lock(fd, offset, whence, len) \
     78 			lock_reg(fd, F_SETLKW, F_WRLCK, offset, whence, len)
     79 #define	un_lock(fd, offset, whence, len) \
     80 			lock_reg(fd, F_SETLK, F_UNLCK, offset, whence, len)
     81 
     82 pid_t	lock_test(int, int, off_t, int, off_t);
     83 									/* {Prog locktest} */
     84 
     85 #define	is_readlock(fd, offset, whence, len) \
     86 			lock_test(fd, F_RDLCK, offset, whence, len)
     87 #define	is_writelock(fd, offset, whence, len) \
     88 			lock_test(fd, F_WRLCK, offset, whence, len)
     89 
     90 void	err_dump(const char *, ...);	/* {App misc_source} */
     91 void	err_msg(const char *, ...);
     92 void	err_quit(const char *, ...);
     93 void	err_ret(const char *, ...);
     94 void	err_sys(const char *, ...);
     95 
     96 void	log_msg(const char *, ...);		/* {App misc_source} */
     97 void	log_open(const char *, int, int);
     98 void	log_quit(const char *, ...);
     99 void	log_ret(const char *, ...);
    100 void	log_sys(const char *, ...);
    101 
    102 void	TELL_WAIT(void);		/* parent/child from {Sec race_conditions} */
    103 void	TELL_PARENT(pid_t);
    104 void	TELL_CHILD(pid_t);
    105 void	WAIT_PARENT(void);
    106 void	WAIT_CHILD(void);
    107 
    108 #endif	/* __ourhdr_h */