xref: /illumos-gate/usr/src/cmd/lp/cmd/lpsched/rstatus.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50a44ef6dSjacobs  * Common Development and Distribution License (the "License").
60a44ef6dSjacobs  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
210a44ef6dSjacobs 
227c478bd9Sstevel@tonic-gate /*
230a44ef6dSjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include "lpsched.h"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /**
347c478bd9Sstevel@tonic-gate  ** insertr()
357c478bd9Sstevel@tonic-gate  **/
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate void
insertr(RSTATUS * r)387c478bd9Sstevel@tonic-gate insertr(RSTATUS *r)
397c478bd9Sstevel@tonic-gate {
407c478bd9Sstevel@tonic-gate 	RSTATUS			*prs;
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate 	if (!Request_List) {
447c478bd9Sstevel@tonic-gate 		Request_List = r;
457c478bd9Sstevel@tonic-gate 		return;
467c478bd9Sstevel@tonic-gate 	}
47*2a8bcb4eSToomas Soome 
487c478bd9Sstevel@tonic-gate 	for (prs = Request_List; prs; prs = prs->next) {
497c478bd9Sstevel@tonic-gate 		if (rsort(&r, &prs) < 0) {
507c478bd9Sstevel@tonic-gate 			r->prev = prs->prev;
517c478bd9Sstevel@tonic-gate 			if (r->prev)
527c478bd9Sstevel@tonic-gate 				r->prev->next = r;
537c478bd9Sstevel@tonic-gate 			r->next = prs;
547c478bd9Sstevel@tonic-gate 			prs->prev = r;
557c478bd9Sstevel@tonic-gate 			if (prs == Request_List)
567c478bd9Sstevel@tonic-gate 				Request_List = r;
577c478bd9Sstevel@tonic-gate 			return;
587c478bd9Sstevel@tonic-gate 		}
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 		if (prs->next)
617c478bd9Sstevel@tonic-gate 			continue;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 		r->prev = prs;
647c478bd9Sstevel@tonic-gate 		prs->next = r;
657c478bd9Sstevel@tonic-gate 		return;
667c478bd9Sstevel@tonic-gate 	}
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /**
707c478bd9Sstevel@tonic-gate  ** remover()
717c478bd9Sstevel@tonic-gate  **/
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate void
remover(RSTATUS * r)747c478bd9Sstevel@tonic-gate remover(RSTATUS *r)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	if (r == Request_List)		/* on the request chain */
777c478bd9Sstevel@tonic-gate 		Request_List = r->next;
78*2a8bcb4eSToomas Soome 
797c478bd9Sstevel@tonic-gate 	if (r->next)
807c478bd9Sstevel@tonic-gate 		r->next->prev = r->prev;
81*2a8bcb4eSToomas Soome 
827c478bd9Sstevel@tonic-gate 	if (r->prev)
837c478bd9Sstevel@tonic-gate 		r->prev->next = r->next;
84*2a8bcb4eSToomas Soome 
857c478bd9Sstevel@tonic-gate 	r->next = 0;
867c478bd9Sstevel@tonic-gate 	r->prev = 0;
877c478bd9Sstevel@tonic-gate 	return;
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /**
917c478bd9Sstevel@tonic-gate  ** request_by_id()
927c478bd9Sstevel@tonic-gate  **/
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate RSTATUS *
request_by_id(char * id)957c478bd9Sstevel@tonic-gate request_by_id(char *id)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	register RSTATUS	*prs;
98*2a8bcb4eSToomas Soome 
997c478bd9Sstevel@tonic-gate 	for (prs = Request_List; prs; prs = prs->next)
1007c478bd9Sstevel@tonic-gate 		if (STREQU(id, prs->secure->req_id))
1017c478bd9Sstevel@tonic-gate 			return (prs);
1027c478bd9Sstevel@tonic-gate 	return (0);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate RSTATUS *
request_by_id_num(long num)1067c478bd9Sstevel@tonic-gate request_by_id_num( long num )
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	register RSTATUS        *prs;
1097c478bd9Sstevel@tonic-gate 
1100a44ef6dSjacobs 	for (prs = Request_List; prs; prs = prs->next) {
1110a44ef6dSjacobs 		char *tmp = strrchr(prs->secure->req_id, '-');
1127c478bd9Sstevel@tonic-gate 
1130a44ef6dSjacobs 		if (tmp && (num == atol(++tmp)))
1140a44ef6dSjacobs 			return (prs);
1150a44ef6dSjacobs 	}
1167c478bd9Sstevel@tonic-gate 	return(0);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /**
1217c478bd9Sstevel@tonic-gate  ** rsort()
1227c478bd9Sstevel@tonic-gate  **/
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate static int		later ( RSTATUS * , RSTATUS * );
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate int
rsort(RSTATUS ** p1,RSTATUS ** p2)1277c478bd9Sstevel@tonic-gate rsort (RSTATUS **p1, RSTATUS **p2)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	/*
1307c478bd9Sstevel@tonic-gate 	 * Of two requests needing immediate handling, the first
1317c478bd9Sstevel@tonic-gate 	 * will be the request with the LATER date. In case of a tie,
1327c478bd9Sstevel@tonic-gate 	 * the first is the one with the larger request ID (i.e. the
1337c478bd9Sstevel@tonic-gate 	 * one that came in last).
1347c478bd9Sstevel@tonic-gate 	 */
1357c478bd9Sstevel@tonic-gate 	if ((*p1)->request->outcome & RS_IMMEDIATE)
1367c478bd9Sstevel@tonic-gate 		if ((*p2)->request->outcome & RS_IMMEDIATE)
1377c478bd9Sstevel@tonic-gate 			if (later(*p1, *p2))
1387c478bd9Sstevel@tonic-gate 				return (-1);
1397c478bd9Sstevel@tonic-gate 			else
1407c478bd9Sstevel@tonic-gate 				return (1);
1417c478bd9Sstevel@tonic-gate 		else
1427c478bd9Sstevel@tonic-gate 			return (-1);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	else if ((*p2)->request->outcome & RS_IMMEDIATE)
1457c478bd9Sstevel@tonic-gate 		return (1);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	/*
1487c478bd9Sstevel@tonic-gate 	 * Of two requests not needing immediate handling, the first
1497c478bd9Sstevel@tonic-gate 	 * will be the request with the highest priority. If both have
1507c478bd9Sstevel@tonic-gate 	 * the same priority, the first is the one with the EARLIER date.
1517c478bd9Sstevel@tonic-gate 	 * In case of a tie, the first is the one with the smaller ID
1527c478bd9Sstevel@tonic-gate 	 * (i.e. the one that came in first).
1537c478bd9Sstevel@tonic-gate 	 */
1547c478bd9Sstevel@tonic-gate 	else if ((*p1)->request->priority == (*p2)->request->priority)
1557c478bd9Sstevel@tonic-gate 		if (!later(*p1, *p2))
1567c478bd9Sstevel@tonic-gate 			return (-1);
1577c478bd9Sstevel@tonic-gate 		else
1587c478bd9Sstevel@tonic-gate 			return (1);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	else
1617c478bd9Sstevel@tonic-gate 		return ((*p1)->request->priority - (*p2)->request->priority);
1627c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate static int
later(RSTATUS * prs1,RSTATUS * prs2)1667c478bd9Sstevel@tonic-gate later(RSTATUS *prs1, RSTATUS *prs2)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	if (prs1->secure->date > prs2->secure->date)
1697c478bd9Sstevel@tonic-gate 		return (1);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	else if (prs1->secure->date < prs2->secure->date)
1727c478bd9Sstevel@tonic-gate 		return (0);
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	/*
1757c478bd9Sstevel@tonic-gate 	 * The dates are the same, so compare the request IDs.
1767c478bd9Sstevel@tonic-gate 	 * One problem with comparing request IDs is that the order
1777c478bd9Sstevel@tonic-gate 	 * of two IDs may be reversed if the IDs wrapped around. This
1787c478bd9Sstevel@tonic-gate 	 * is a very unlikely problem, because the cycle should take
1797c478bd9Sstevel@tonic-gate 	 * more than one second to wrap!
1807c478bd9Sstevel@tonic-gate 	 */
1817c478bd9Sstevel@tonic-gate 	else {
1827c478bd9Sstevel@tonic-gate 		register int		len1 = strlen(prs1->req_file),
1837c478bd9Sstevel@tonic-gate 					len2 = strlen(prs2->req_file);
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 		/*
1867c478bd9Sstevel@tonic-gate 		 * Use the request file name (ID-0) for comparison,
1877c478bd9Sstevel@tonic-gate 		 * because the real request ID (DEST-ID) won't compare
1887c478bd9Sstevel@tonic-gate 		 * properly because of the destination prefix.
1897c478bd9Sstevel@tonic-gate 		 * The strlen() comparison is necessary, otherwise
1907c478bd9Sstevel@tonic-gate 		 * IDs like "99-0" and "100-0" will compare wrong.
1917c478bd9Sstevel@tonic-gate 		 */
1927c478bd9Sstevel@tonic-gate 		if (len1 > len2)
1937c478bd9Sstevel@tonic-gate 			return (1);
1947c478bd9Sstevel@tonic-gate 		else if (len1 < len2)
1957c478bd9Sstevel@tonic-gate 			return (0);
1967c478bd9Sstevel@tonic-gate 		else
1977c478bd9Sstevel@tonic-gate 			return (strcmp(prs1->req_file, prs2->req_file) > 0);
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
2007c478bd9Sstevel@tonic-gate }
201