From 9da729e4141640e6996c0f0032655a0bf68087e5 Mon Sep 17 00:00:00 2001
From: Per Cederqvist <ceder@lysator.liu.se>
Date: Thu, 5 Aug 1993 00:21:49 +0000
Subject: [PATCH] (do_unmark_text): New function: delete a mark.
 (do_mark_mark_text): Never delete a mark. All callers (mark_text,
 unmark_text): New functions. (mark_text_old): Compatibility function that
 calls mark_text or unmark_text depending on mark_type. This will now return
 an error if you unmark at text that you had not marked.

---
 src/server/person.c | 147 +++++++++++++++++++++++++++++---------------
 1 file changed, 99 insertions(+), 48 deletions(-)

diff --git a/src/server/person.c b/src/server/person.c
index 79b014c0c..a54f9466c 100644
--- a/src/server/person.c
+++ b/src/server/person.c
@@ -1,5 +1,5 @@
 /*
- * $Id: person.c,v 0.18 1993/01/16 23:17:19 ceder Exp $
+ * $Id: person.c,v 0.19 1993/08/05 00:21:49 ceder Exp $
  * Copyright (C) 1991  Lysator Academic Computer Association.
  *
  * This file is part of the LysKOM server.
@@ -28,7 +28,7 @@
  * All atomic calls that deals with persons.
  */
 
-static char *rcsid = "$Id: person.c,v 0.18 1993/01/16 23:17:19 ceder Exp $";
+static char *rcsid = "$Id: person.c,v 0.19 1993/08/05 00:21:49 ceder Exp $";
 #include "rcs.h"
 USE(rcsid);
 
@@ -36,7 +36,6 @@ USE(rcsid);
 #include <time.h>
 #include <string.h>
 #include <stdlib.h>
-#include <crypt.h>
 
 #include "lyskomd.h"
 #include <kom-types.h>
@@ -117,7 +116,8 @@ do_set_passwd( Password        pwd,
 
 /*
  * Mark a text. No check is done if pers_p is really allowed to mark it.
- * Use mark_type==0 to unmark the text.
+ * This function can mark a text with mark_type == 0.  If a previous mark
+ * existed it is changed to the given mark_type.
  */
 
 static Success
@@ -145,7 +145,7 @@ do_mark_text(Pers_no    pers_no,
 	    old_mark = markp;
     }
 
-    if (old_mark != NULL && mark_type != 0)
+    if (old_mark != NULL)
     {
 	/* Change marktype of an already marked text. (No need to check
 	   that the text exists). */
@@ -154,10 +154,65 @@ do_mark_text(Pers_no    pers_no,
 	old_mark->mark_type = mark_type;
 	mark_person_as_changed(pers_no);
     }
-    else if (old_mark != NULL)
+    else
+    {
+	/* A new mark. Check that the text exists. */
+	BUG(("do_mark_text(): new mark.\n"));
+
+	if ( text_s == NULL )
+	    GET_T_STAT(text_s, text_no, FAILURE);
+
+	if (text_s->no_of_marks >= MAX_MARKS_TEXT
+	    || pers_p->marks.no_of_marks >= MAX_MARKS_PERSON)
+	{
+	    kom_errno = KOM_MARK_LIMIT;
+	    return FAILURE;
+	}
+
+	text_s->no_of_marks++;
+	mark_text_as_changed(text_no);
+	
+	pers_p->marks.marks = srealloc(pers_p->marks.marks,
+			       ++pers_p->marks.no_of_marks * sizeof(Mark));
+	pers_p->marks.marks[pers_p->marks.no_of_marks - 1].text_no = text_no;
+	pers_p->marks.marks[pers_p->marks.no_of_marks - 1].mark_type
+	    = mark_type;
+	mark_person_as_changed(pers_no);
+    }
+    
+    return OK;
+}
+
+/* Unmark a text. Returns a failure if the text was not marked. */
+
+static Success
+do_unmark_text(Pers_no    pers_no,
+	       Person    *pers_p, /* May be NULL */
+	       Text_no    text_no,
+	       Text_stat *text_s) /* May be NULL */
+{
+    Mark      *markp;
+    Mark      *old_mark = NULL;
+    int        i;
+    BUGDECL;
+
+    if ( pers_p == NULL )
+	GET_P_STAT(pers_p, pers_no, FAILURE);
+	
+    /* Locate the mark. */
+    
+    for ( i = pers_p->marks.no_of_marks, markp = pers_p->marks.marks;
+	 i > 0 && old_mark == NULL;
+	 i--, markp++ )
+    {
+	if ( markp->text_no == text_no )
+	    old_mark = markp;
+    }
+
+    if (old_mark != NULL)
     {
 	/* Delete this mark. */
-	BUG(("do_mark_text(): Delete mark.\n"));
+	BUG(("do_unmark_text(): Delete mark.\n"));
 	
 	while ( old_mark < ( pers_p->marks.marks
 			    + pers_p->marks.no_of_marks - 1 ))
@@ -175,7 +230,7 @@ do_mark_text(Pers_no    pers_no,
 	{
 	    if ( text_s->no_of_marks == 0 )
 	    {
-		log("WNG: do_mark_text(): Text %d has no_of_marks==0,\
+		log("WNG: do_unmark_text(): Text %d has no_of_marks==0,\
  		     but person %d had marked the text.",
 		    text_no, pers_no);
 	    }
@@ -187,35 +242,12 @@ do_mark_text(Pers_no    pers_no,
 	}
 	/* ...but it is not an error if someone has deleted the text. */
     }
-    else if ( mark_type != 0 )
-    {
-	/* A new mark. Check that the text exists. */
-	BUG(("do_mark_text(): new mark.\n"));
-
-	if ( text_s == NULL )
-	    GET_T_STAT(text_s, text_no, FAILURE);
-
-	if (text_s->no_of_marks >= MAX_MARKS_TEXT
-	    || pers_p->marks.no_of_marks >= MAX_MARKS_PERSON)
-	{
-	    kom_errno = KOM_MARK_LIMIT;
-	    return FAILURE;
-	}
-
-	text_s->no_of_marks++;
-	mark_text_as_changed(text_no);
-	
-	pers_p->marks.marks = srealloc(pers_p->marks.marks,
-			       ++pers_p->marks.no_of_marks * sizeof(Mark));
-	pers_p->marks.marks[pers_p->marks.no_of_marks - 1].text_no = text_no;
-	pers_p->marks.marks[pers_p->marks.no_of_marks - 1].mark_type
-	    = mark_type;
-	mark_person_as_changed(pers_no);
-    }
     else
     {
-	/* An attempt to delete a non-existent mark. This is a noop. */
-	BUG(("do_mark_text(): delete non-existent mark.\n"));
+	/* An attempt to delete a non-existent mark. */
+	BUG(("do_unmark_text(): delete non-existent mark.\n"));
+	kom_errno = KOM_NOT_MARKED;
+	return FAILURE;
     }
     
     return OK;
@@ -319,8 +351,8 @@ do_delete_pers (Pers_no pers_no)
 
     for ( i = pers_p->marks.no_of_marks; i > 0; i-- )
     {
-	if ( do_mark_text(pers_no, pers_p,
-			  pers_p->marks.marks[0].text_no, NULL, 0) != OK )
+	if ( do_unmark_text(pers_no, pers_p,
+			    pers_p->marks.marks[0].text_no, NULL) != OK )
 	{
 	    log("WNG: do_delete_pers(): can't unmark text %d (i == %d)\n",
 		pers_p->marks.marks[0].text_no, i);
@@ -398,23 +430,30 @@ chk_passwd (Password      pwd,
  */
 
 extern Success
-mark_text( Text_no	text_no,	/* Will fail if the user is not */
-	   u_char	mark_type )	/* allowed to read the text.	*/
+mark_text_old(Text_no text_no,
+	      u_char mark_type)
+{
+    if (mark_type == 0)
+	return unmark_text(text_no);
+    else
+	return mark_text(text_no, mark_type);
+}
+
+extern Success
+mark_text(Text_no text_no,   /* Will fail if the user is not */
+	  u_char mark_type)	/* allowed to read the text.	*/
 {
     Text_stat *text_s = NULL;
     BUGDECL;
         
     CHK_LOGIN(FAILURE);
 
-    if ( mark_type != 0 )
+    /* Check that the user is allowed to read the text. */
+    GET_T_STAT(text_s, text_no, FAILURE);
+    if ( !text_read_access(text_no, text_s) )
     {
-	/* Check that the user is allowed to read the text. */
-	GET_T_STAT(text_s, text_no, FAILURE);
-	if ( !text_read_access(text_no, text_s) )
-	{
-	    kom_errno = KOM_PERM;
-	    return FAILURE;
-	}
+	kom_errno = KOM_PERM;
+	return FAILURE;
     }
 
     BUG(("Person %d markerar text %d med typ %d\n",
@@ -423,6 +462,18 @@ mark_text( Text_no	text_no,	/* Will fail if the user is not */
     return do_mark_text(ACTPERS, ACT_P, text_no, text_s, mark_type);
 }
 
+extern Success
+unmark_text(Text_no text_no)
+{
+    Text_stat *text_s = NULL;
+    BUGDECL;
+        
+    CHK_LOGIN(FAILURE);
+    BUG(("Person %d avmarkerar text %d\n", ACTPERS, text_no));
+    
+    return do_unmark_text(ACTPERS, ACT_P, text_no, text_s);
+}
+
 /*
  * Create a new person.
  *
@@ -469,7 +520,7 @@ create_person (String         name,
 
     if ( !legal_name( name ) )
     {
-	kom_errno = KOM_BAD_NAME;
+	/* kom_errno will be set by legal_name(). */
 	return 0;
     }
 
-- 
GitLab