4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
37 #include <sys/types.h>
48 static int nflg
= 0; /* -n seen */
49 static int iflg
= 0; /* -i seen */
50 static int fflg
= 0; /* -f seen */
51 static int lflg
= 0; /* -l seen */
53 struct Gdef Gdef
[MAXDEFS
]; /* array to hold entries in /etc/ttydefs */
54 int Ndefs
= 0; /* highest index to Gdef that was used */
55 static struct Gdef DEFAULT
= { /* default terminal settings */
61 * next label is set to 4800 so we can start searching ttydefs.
62 * if 4800 is not in ttydefs, we will loop back to use DEFAULT
69 static void check_ref();
70 static void add_entry();
71 static void remove_entry();
72 static int copy_file();
74 static FILE *open_temp();
75 extern void read_ttydefs();
76 extern int check_version();
77 extern int find_label();
80 * sttydefs - add, remove or check entries in /etc/ttydefs
82 * Usage: sttydefs -a ttylabel [-n nextlabel] [-i initail-flags]
83 * [-f final-flags] [-b]
84 * sttydefs -r ttylabel
85 * sttydefs -l [ttylabel]
90 main(int argc
, char *argv
[])
92 int c
; /* option letter */
93 int errflg
= 0; /* error indicator */
94 int aflg
= 0; /* -a seen */
95 int bflg
= 0; /* -b seen */
102 struct Gdef ttydef
, *ptr
;
110 /* Initialize ttydef structure */
111 memset (&ttydef
, 0, sizeof(ttydef
));
114 while ((c
= getopt(argc
, argv
, "a:n:i:f:br:l")) != -1) {
122 ptr
->g_nextid
= optarg
;
126 ptr
->g_iflags
= optarg
;
130 ptr
->g_fflags
= optarg
;
134 ptr
->g_autobaud
|= A_FLAG
;
137 if ((argc
> 3) || (optind
< argc
))
139 remove_entry(optarg
);
145 if ((ret
= check_version(TTYDEFS_VERS
, TTYDEFS
)) != 0) {
147 (void)fprintf(stderr
, "%s version number is incorrect or missing.\n",TTYDEFS
);
150 (void)fprintf(stderr
, "sttydefs: can't open %s.\n",TTYDEFS
);
153 if (argv
[optind
] == NULL
) {
154 read_ttydefs(NULL
,TRUE
);
159 if (argc
== 3) { /* -l ttylabel */
160 if (verify(argv
[optind
],0) != 0) {
164 argtmp
= argv
[optind
];
166 else { /* -lttylabel */
167 argtmp
= argv
[optind
]+2;
169 read_ttydefs(argtmp
,TRUE
);
171 (void)fprintf(stderr
,
172 "ttylabel <%s> not found.\n", argtmp
);
175 nextlabel
= Gdef
[--Ndefs
].g_nextid
;
177 read_ttydefs(nextlabel
,FALSE
);
179 (void)printf("\nWarning -- nextlabel <%s> of <%s> does not reference any existing ttylabel.\n",
184 break; /*NOTREACHED*/
196 add_entry(ptr
); /* never return */
198 if ((iflg
) || (fflg
) || (bflg
) || (nflg
))
204 * verify - to check if arg is valid
205 * - i.e. arg cannot start with '-' and
206 * arg must not longer than maxarglen
207 * - return 0 if ok. Otherwise return -1
210 verify(arg
,maxarglen
)
215 (void)fprintf(stderr
, "Invalid argument -- %s.\n", arg
);
218 if ((maxarglen
) && ((int)strlen(arg
) > maxarglen
)) {
219 arg
[maxarglen
] = '\0';
220 (void)fprintf(stderr
,"string too long, truncated to %s.\n",arg
);
227 * usage - print out a usage message
233 (void)fprintf(stderr
, "Usage:\tsttydefs -a ttylabel [-n nextlabel] [-i initial-flags]\n\t\t [-f final-flags] [-b]\n");
234 (void)fprintf(stderr
, "\tsttydefs -r ttylabel\n");
235 (void)fprintf(stderr
, "\tsttydefs -l [ttylabel]\n");
240 * add_entry - add an entry to /etc/ttydefs
249 char tbuf
[BUFSIZ
], *tp
;
250 int add_version
= FALSE
;
251 extern int check_flags();
254 (void)fprintf(stderr
, "User not privileged for operation.\n");
259 if ((fp
= fopen(TTYDEFS
, "r")) != NULL
) {
260 if (check_version(TTYDEFS_VERS
, TTYDEFS
) != 0) {
261 (void)fprintf(stderr
,
262 "%s version number is incorrect or missing.\n",TTYDEFS
);
265 if (find_label(fp
,ttydef
->g_id
)) {
267 (void)fprintf(stderr
,
268 "Invalid request -- ttylabel <%s> already exists.\n",
277 if ((fp
= fopen(TTYDEFS
, "a+")) == NULL
) {
278 (void) fprintf(stderr
, "Could not open \"%s\": %s", TTYDEFS
,
284 (void)fprintf(fp
,"# VERSION=%d\n", TTYDEFS_VERS
);
288 /* if optional fields are not provided, set to default */
290 ttydef
->g_iflags
= DEFAULT
.g_iflags
;
292 if (check_flags(ttydef
->g_iflags
) != 0 )
295 ttydef
->g_fflags
= DEFAULT
.g_fflags
;
297 if (check_flags(ttydef
->g_fflags
) != 0 )
303 ttydef
->g_nextid
= ttydef
->g_id
;
305 if (ttydef
->g_autobaud
& A_FLAG
) {
306 (void)fprintf(fp
,"%s:%s:%s:A:%s\n", ttydef
->g_id
, ttydef
->g_iflags
,
307 ttydef
->g_fflags
, ttydef
->g_nextid
);
310 (void)fprintf(fp
,"%s:%s:%s::%s\n", ttydef
->g_id
, ttydef
->g_iflags
,
311 ttydef
->g_fflags
, ttydef
->g_nextid
);
318 remove_entry(ttylabel
)
321 FILE *tfp
; /* file pointer for temp file */
322 int line
; /* line number entry is on */
323 FILE *fp
; /* scratch file pointer */
324 char *tname
= "/etc/.ttydefs";
327 (void)fprintf(stderr
, "User not privileged for operation.\n");
330 fp
= fopen(TTYDEFS
, "r");
332 (void) fprintf(stderr
, "Could not open \"%s\": %s", TTYDEFS
,
336 if (check_version(TTYDEFS_VERS
, TTYDEFS
) != 0) {
337 (void)fprintf(stderr
,
338 "%s version number is incorrect or missing.\n",TTYDEFS
);
341 if ((line
= find_label(fp
, ttylabel
)) == 0) {
342 (void)fprintf(stderr
,
343 "Invalid request, ttylabel <%s> does not exist.\n", ttylabel
);
346 tfp
= open_temp(tname
);
348 if (copy_file(fp
, tfp
, 1, line
- 1)) {
349 (void)fprintf(stderr
,"Error accessing temp file.\n");
352 if (copy_file(fp
, tfp
, line
+ 1, -1)) {
353 (void)fprintf(stderr
,"Error accessing temp file.\n");
357 if (fclose(tfp
) == EOF
) {
359 (void)fprintf(stderr
,"Error closing temp file.\n");
362 (void)unlink(TTYDEFS
);
363 if (rename(tname
, TTYDEFS
) != 0 ) {
364 perror("Rename failed");
372 * open_temp - open up a temp file
374 * args: tname - temp file name
381 FILE *fp
; /* fp associated with tname */
382 struct sigaction sigact
; /* for signal handling */
385 sigact
.sa_handler
= SIG_IGN
;
386 (void) sigemptyset(&sigact
.sa_mask
);
387 (void) sigaddset(&sigact
.sa_mask
, SIGHUP
);
388 (void) sigaddset(&sigact
.sa_mask
, SIGINT
);
389 (void) sigaddset(&sigact
.sa_mask
, SIGQUIT
);
390 (void) sigaction(SIGHUP
, &sigact
, NULL
);
391 (void) sigaction(SIGINT
, &sigact
, NULL
);
392 (void) sigaction(SIGQUIT
, &sigact
, NULL
);
394 if (access(tname
, 0) != -1) {
395 (void)fprintf(stderr
,"tempfile busy; try again later.\n");
398 fp
= fopen(tname
, "w");
400 perror("Cannot create tempfile");
407 * copy_file - copy information from one file to another, return 0 on
408 * success, -1 on failure
410 * args: fp - source file's file pointer
411 * tfp - destination file's file pointer
412 * start - starting line number
413 * finish - ending line number (-1 indicates entire file)
418 copy_file(fp
, tfp
, start
, finish
)
424 register int i
; /* loop variable */
425 char dummy
[BUFSIZ
]; /* scratch buffer */
428 * always start from the beginning because line numbers are absolute
434 * get to the starting point of interest
438 for (i
= 1; i
< start
; i
++)
439 if (!fgets(dummy
, BUFSIZ
, fp
))
444 * copy as much as was requested
448 for (i
= start
; i
<= finish
; i
++) {
449 if (!fgets(dummy
, BUFSIZ
, fp
))
451 if (fputs(dummy
, tfp
) == EOF
)
457 if (fgets(dummy
, BUFSIZ
, fp
) == NULL
) {
463 if (fputs(dummy
, tfp
) == EOF
)
471 * check_ref - to check if nextlabel are referencing
479 extern struct Gdef
*find_def();
481 for (i
= 0; i
< Ndefs
; i
++, np
++) {
482 if (find_def(np
->g_nextid
) == NULL
) {
483 (void)printf("Warning -- nextlabel <%s> of <%s> does not reference any existing ttylabel.\n",
484 np
->g_nextid
, np
->g_id
);
491 * log - print a message to stdout
495 log(const char *msg
, ...)
500 (void) vprintf(msg
, ap
);
505 (void) vfprintf(stderr
, msg
, ap
);
507 (void) fprintf(stderr
,"\n");