1STRCMP(P) POSIX Programmer's Manual STRCMP(P)
2
3
4
6 strcmp - compare two strings
7
9 #include <string.h>
10
11 int strcmp(const char *s1, const char *s2);
12
13
15 The strcmp() function shall compare the string pointed to by s1 to the
16 string pointed to by s2.
17
18 The sign of a non-zero return value shall be determined by the sign of
19 the difference between the values of the first pair of bytes (both
20 interpreted as type unsigned char) that differ in the strings being
21 compared.
22
24 Upon completion, strcmp() shall return an integer greater than, equal
25 to, or less than 0, if the string pointed to by s1 is greater than,
26 equal to, or less than the string pointed to by s2, respectively.
27
29 No errors are defined.
30
31 The following sections are informative.
32
34 Checking a Password Entry
35 The following example compares the information read from standard input
36 to the value of the name of the user entry. If the strcmp() function
37 returns 0 (indicating a match), a further check will be made to see if
38 the user entered the proper old password. The crypt() function shall
39 encrypt the old password entered by the user, using the value of the
40 encrypted password in the passwd structure as the salt. If this value
41 matches the value of the encrypted passwd in the structure, the entered
42 password oldpasswd is the correct user's password. Finally, the program
43 encrypts the new password so that it can store the information in the
44 passwd structure.
45
46
47 #include <string.h>
48 #include <unistd.h>
49 #include <stdio.h>
50 ...
51 int valid_change;
52 struct passwd *p;
53 char user[100];
54 char oldpasswd[100];
55 char newpasswd[100];
56 char savepasswd[100];
57 ...
58 if (strcmp(p->pw_name, user) == 0) {
59 if (strcmp(p->pw_passwd, crypt(oldpasswd, p->pw_passwd)) == 0) {
60 strcpy(savepasswd, crypt(newpasswd, user));
61 p->pw_passwd = savepasswd;
62 valid_change = 1;
63 }
64 else {
65 fprintf(stderr, "Old password is not valid\n");
66 }
67 }
68 ...
69
71 None.
72
74 None.
75
77 None.
78
80 strncmp() , the Base Definitions volume of IEEE Std 1003.1-2001,
81 <string.h>
82
84 Portions of this text are reprinted and reproduced in electronic form
85 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
86 -- Portable Operating System Interface (POSIX), The Open Group Base
87 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
88 Electrical and Electronics Engineers, Inc and The Open Group. In the
89 event of any discrepancy between this version and the original IEEE and
90 The Open Group Standard, the original IEEE and The Open Group Standard
91 is the referee document. The original Standard can be obtained online
92 at http://www.opengroup.org/unix/online.html .
93
94
95
96IEEE/The Open Group 2003 STRCMP(P)