From 400c80e68042ad48d7e825bab35b78f96cb7a1d7 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Fri, 25 Mar 2011 10:19:30 -0400 Subject: user_del is now capable of removing text from the temp files. still does not modify original files --- duser.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/duser.c b/duser.c index 07e8144..f31e57b 100644 --- a/duser.c +++ b/duser.c @@ -82,8 +82,12 @@ int user_del(record_t* rec) { memset(buf, 0, sizeof(buf)); fgets(buf, REGEX_MAX, fp); - if((verify = find_in_file_ex(rec))) - write(fd, buf, sizeof(buf)); + buf[strlen(buf) - 1] = '\0'; + if((strcmp(buf, rec->name)) != 0 ) + { + buf[strlen(buf)] = '\n'; + write(fd, buf, strlen(buf)); + } } fclose(fp); @@ -95,30 +99,29 @@ int user_del(record_t* rec) // VERIFY that the record is proper (useful for deletion of users) int find_in_file_ex(record_t* rec) { - int index_temp = 0; - int index_local = -1; + int match = -1; char buf[REGEX_MAX]; FILE *fp; if((fp = fopen(rec->file, "r")) == NULL) { perror("find_in_file_ex"); - return index_local; + return -1; } while(!feof(fp)) { fgets(buf, REGEX_MAX, fp); buf[strlen(buf) - 1] = '\0'; - index_temp++; - if((strcmp(buf, rec->name)) != 0) - index_local = index_temp; - else + if((strcmp(buf, rec->name)) == 0) + { + match = 0; break; + } } fclose(fp); - return index_local; + return match; } record_t* find_in_file(const char* filename, const char* needle) -- cgit